From 49a99615034dbfa44169840cfc070e03ef13606f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=E5=8D=8E=E5=85=B5?= <455982533@qq.com> Date: Thu, 21 Nov 2024 15:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=98=E5=82=A8=E8=BF=87=E7=A8=8B=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HadoopController.cs | 13 +++++ .../ZKLT.Hadoop.Interface/IHadoopService.cs | 8 +++ Hadoop/ZKLT.Hadoop.Interface/ITableService.cs | 9 +++ Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs | 7 +++ Hadoop/ZKLT.Hadoop.Model/HDP_StoredParams.cs | 31 +++++++++++ Hadoop/ZKLT.Hadoop/HadoopService.cs | 8 ++- Hadoop/ZKLT.Hadoop/TableService.cs | 55 +++++++++++++++++++ 7 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 Hadoop/ZKLT.Hadoop.Model/HDP_StoredParams.cs diff --git a/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs b/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs index a8a6c80..a65953a 100644 --- a/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs +++ b/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs @@ -307,5 +307,18 @@ namespace ZKLT.Hadoop.API.Controllers return BadRequest(e.Message); } } + + [HttpPost("storedProcedure")] + public ActionResult StoredProcedure(HDP_Command command) + { + try + { + return Ok(_HadoopService.StoredProcedure(command)); + } + catch (Exception e) + { + return BadRequest(e.Message); + } + } } } diff --git a/Hadoop/ZKLT.Hadoop.Interface/IHadoopService.cs b/Hadoop/ZKLT.Hadoop.Interface/IHadoopService.cs index c5c394b..287866b 100644 --- a/Hadoop/ZKLT.Hadoop.Interface/IHadoopService.cs +++ b/Hadoop/ZKLT.Hadoop.Interface/IHadoopService.cs @@ -120,5 +120,13 @@ namespace ZKLT.Hadoop.Interface /// 指令 /// public object?[] PatchCommand(HDP_Command[] command); + + /// + /// 存储过程 + /// + /// + /// + /// + public T? StoredProcedure(HDP_Command command); } } diff --git a/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs b/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs index 4925ef4..43310d6 100644 --- a/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs +++ b/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs @@ -136,5 +136,14 @@ namespace ZKLT.Hadoop.Interface /// 表 /// public HDP_Column[] DbGetColumns(string sourceId, string tableId); + + /// + /// 执行存储过程 + /// + /// + /// + /// + /// + public T? StoredProcedure(string sourceId, string name, JToken? data); } } diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs index a5c00e5..5b4ba9b 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs @@ -26,6 +26,8 @@ namespace ZKLT.Hadoop.Model private string[]? _Col; + private string? _StoredProcedureName; + private JToken? _Where; private JToken? _Data; @@ -42,6 +44,11 @@ namespace ZKLT.Hadoop.Model /// public string? TableId { get => _TableId; set => _TableId = value; } + /// + /// 存储过程名 + /// + public string? StoredProcedureName { get => _StoredProcedureName; set => _StoredProcedureName = value; } + /// /// 条件 /// diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_StoredParams.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_StoredParams.cs new file mode 100644 index 0000000..c9163bc --- /dev/null +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_StoredParams.cs @@ -0,0 +1,31 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZKLT.Hadoop; + +namespace ZKLT.Hadoop.Model +{ + /// + /// 存储过程 + /// + public class HDP_StoredParams + { + + private string _Name; + + private JToken? _Params; + + /// + /// 存储过程名称 + /// + public string Name { get => _Name; set => _Name = value; } + + /// + /// 存储过程参数 + /// + public JToken? Params { get => _Params; set => _Params = value; } + } +} diff --git a/Hadoop/ZKLT.Hadoop/HadoopService.cs b/Hadoop/ZKLT.Hadoop/HadoopService.cs index c2ff30c..f95e89b 100644 --- a/Hadoop/ZKLT.Hadoop/HadoopService.cs +++ b/Hadoop/ZKLT.Hadoop/HadoopService.cs @@ -188,7 +188,7 @@ namespace ZKLT.Hadoop throw; } } - + if (_TableService.InitStruct("", table)) { using (TransactionScope _scope = new TransactionScope()) @@ -485,6 +485,10 @@ namespace ZKLT.Hadoop } return _result.ToArray(); } - + + public T? StoredProcedure(HDP_Command command) + { + return _TableService.StoredProcedure(command.SourceId!, command.StoredProcedureName!, command.Data!); + } } } diff --git a/Hadoop/ZKLT.Hadoop/TableService.cs b/Hadoop/ZKLT.Hadoop/TableService.cs index 3dff2c2..0dca58b 100644 --- a/Hadoop/ZKLT.Hadoop/TableService.cs +++ b/Hadoop/ZKLT.Hadoop/TableService.cs @@ -14,6 +14,9 @@ using Newtonsoft.Json; using Mysqlx.Resultset; using Newtonsoft.Json.Linq; using System.Transactions; +using Org.BouncyCastle.Asn1.X509; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Data; namespace ZKLT.Hadoop { @@ -1142,5 +1145,57 @@ namespace ZKLT.Hadoop return _result.ToArray(); } } + + /// + /// 执行存储过程 + /// + /// + /// + /// + /// + public T? StoredProcedure(string sourceId,string name, JToken? data) + { + var source = GetSource(sourceId); + using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) + { + try + { + _connection.Open(); + } + catch + { + throw new ArgumentException("数据源连接失败"); + } + using (MySqlCommand _command = new MySqlCommand(name, _connection)) + { + _command.CommandType = CommandType.StoredProcedure; + if (data != null) + { + var _datas = data.ToObject>()!; + foreach (var item in _datas) + { + _command.Parameters.AddWithValue(item.Key, item.Value); + } + } + using (MySqlDataReader _reader = _command.ExecuteReader()) + { + var _result = new List>(); + while (_reader.Read()) + { + var row = new Dictionary(); + for (int i = 0; i < _reader.FieldCount; i++) + { + row[_reader.GetName(i)] = _reader.GetValue(i); + } + _result.Add(row); + } + _reader.Close(); + _connection.Close(); + return (T)(object)_result; + } + } + + } + } } }