Compare commits

..

2 Commits

@ -307,5 +307,18 @@ namespace ZKLT.Hadoop.API.Controllers
return BadRequest(e.Message); return BadRequest(e.Message);
} }
} }
[HttpPost("storedProcedure")]
public ActionResult StoredProcedure(HDP_Command command)
{
try
{
return Ok(_HadoopService.StoredProcedure<dynamic>(command));
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
} }
} }

@ -120,5 +120,13 @@ namespace ZKLT.Hadoop.Interface
/// <param name="command">指令</param> /// <param name="command">指令</param>
/// <returns></returns> /// <returns></returns>
public object?[] PatchCommand(HDP_Command[] command); public object?[] PatchCommand(HDP_Command[] command);
/// <summary>
/// 存储过程
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="storedParams"></param>
/// <returns></returns>
public T? StoredProcedure<T>(HDP_Command command);
} }
} }

@ -136,5 +136,14 @@ namespace ZKLT.Hadoop.Interface
/// <param name="tableId">表</param> /// <param name="tableId">表</param>
/// <returns>列</returns> /// <returns>列</returns>
public HDP_Column[] DbGetColumns(string sourceId, string tableId); public HDP_Column[] DbGetColumns(string sourceId, string tableId);
/// <summary>
/// 执行存储过程
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="param"></param>
/// <returns></returns>
public T? StoredProcedure<T>(string sourceId, string name, JToken? data);
} }
} }

@ -26,6 +26,8 @@ namespace ZKLT.Hadoop.Model
private string[]? _Col; private string[]? _Col;
private string? _StoredProcedureName;
private JToken? _Where; private JToken? _Where;
private JToken? _Data; private JToken? _Data;
@ -42,6 +44,11 @@ namespace ZKLT.Hadoop.Model
/// </summary> /// </summary>
public string? TableId { get => _TableId; set => _TableId = value; } public string? TableId { get => _TableId; set => _TableId = value; }
/// <summary>
/// 存储过程名
/// </summary>
public string? StoredProcedureName { get => _StoredProcedureName; set => _StoredProcedureName = value; }
/// <summary> /// <summary>
/// 条件 /// 条件
/// </summary> /// </summary>

@ -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
{
/// <summary>
/// 存储过程
/// </summary>
public class HDP_StoredParams
{
private string _Name;
private JToken? _Params;
/// <summary>
/// 存储过程名称
/// </summary>
public string Name { get => _Name; set => _Name = value; }
/// <summary>
/// 存储过程参数
/// </summary>
public JToken? Params { get => _Params; set => _Params = value; }
}
}

@ -188,7 +188,7 @@ namespace ZKLT.Hadoop
throw; throw;
} }
} }
if (_TableService.InitStruct("", table)) if (_TableService.InitStruct("", table))
{ {
using (TransactionScope _scope = new TransactionScope()) using (TransactionScope _scope = new TransactionScope())
@ -485,6 +485,10 @@ namespace ZKLT.Hadoop
} }
return _result.ToArray(); return _result.ToArray();
} }
public T? StoredProcedure<T>(HDP_Command command)
{
return _TableService.StoredProcedure<T>(command.SourceId!, command.StoredProcedureName!, command.Data!);
}
} }
} }

@ -14,6 +14,9 @@ using Newtonsoft.Json;
using Mysqlx.Resultset; using Mysqlx.Resultset;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Transactions; using System.Transactions;
using Org.BouncyCastle.Asn1.X509;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Data;
namespace ZKLT.Hadoop namespace ZKLT.Hadoop
{ {
@ -1142,5 +1145,57 @@ namespace ZKLT.Hadoop
return _result.ToArray(); return _result.ToArray();
} }
} }
/// <summary>
/// 执行存储过程
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="param"></param>
/// <returns></returns>
public T? StoredProcedure<T>(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<Dictionary<string, object>>()!;
foreach (var item in _datas)
{
_command.Parameters.AddWithValue(item.Key, item.Value);
}
}
using (MySqlDataReader _reader = _command.ExecuteReader())
{
var _result = new List<Dictionary<string, object>>();
while (_reader.Read())
{
var row = new Dictionary<string, object>();
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;
}
}
}
}
} }
} }

Loading…
Cancel
Save