diff --git a/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs b/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs index ce900bf..047f9cb 100644 --- a/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs +++ b/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -32,7 +33,7 @@ namespace ZKLT.Hadoop.Interface /// 数据表 /// 数据 /// 是否成功 - public bool Insert(HDP_Source source, HDP_Table table, Dictionary row); + public bool Insert(HDP_Source source, HDP_Table table, JContainer? row); /// /// 更新 @@ -42,7 +43,7 @@ namespace ZKLT.Hadoop.Interface /// 条件 /// 数据 /// 是否成功 - public bool Update(HDP_Source source, HDP_Table table, Dictionary where, Dictionary row); + public bool Update(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row); /// /// 删除 @@ -52,7 +53,7 @@ namespace ZKLT.Hadoop.Interface /// 条件 /// 数据 /// 是否成功 - public bool Delete(HDP_Source source, HDP_Table table, Dictionary where, Dictionary row); + public bool Delete(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row); /// /// 查询单个 @@ -62,7 +63,7 @@ namespace ZKLT.Hadoop.Interface /// 条件 /// 数据 /// 结果 - public T? QuerySingle(HDP_Source source, HDP_Table table, Dictionary where, Dictionary row, string[]? col); + public T? QuerySingle(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row, string[]? col); /// /// 查询列表 @@ -73,8 +74,8 @@ namespace ZKLT.Hadoop.Interface /// 条件 /// 数据 /// 结果集 - public T[] Query(HDP_Source source, HDP_Table table, Dictionary? where, Dictionary? row, - Dictionary? order, string[]? col); + public T[] Query(HDP_Source source, HDP_Table table, JContainer? where, JContainer? row, + JContainer? order, string[]? col); /// /// 查询列表 @@ -85,7 +86,7 @@ namespace ZKLT.Hadoop.Interface /// 条件 /// 数据 /// 结果集 - public HDP_Page QueryPage(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary? where, Dictionary? row, Dictionary? order, string[]? col); + public HDP_Page QueryPage(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, JContainer? where, JContainer? row, JContainer? order, string[]? col); /// diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs index afb092c..bf6f4b1 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -23,11 +24,11 @@ namespace ZKLT.Hadoop.Model private string[]? _Col; - private Dictionary? _Where; + private JContainer? _Where; - private Dictionary? _Data; + private JContainer? _Data; - private Dictionary? _Order; + private JContainer? _Order; /// /// 源 @@ -42,12 +43,12 @@ namespace ZKLT.Hadoop.Model /// /// 条件 /// - public Dictionary? Where { get => _Where; set => _Where = value; } + public JContainer? Where { get => _Where; set => _Where = value; } /// /// 数据 /// - public Dictionary? Data { get => _Data; set => _Data = value; } + public JContainer? Data { get => _Data; set => _Data = value; } /// /// 分页下标 @@ -62,7 +63,7 @@ namespace ZKLT.Hadoop.Model /// /// 排序 /// - public Dictionary? Order { get => _Order; set => _Order = value; } + public JContainer? Order { get => _Order; set => _Order = value; } /// /// 命令类型 diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs index 7480205..c20d37c 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -27,7 +28,7 @@ namespace ZKLT.Hadoop.Model /// 函数 /// 参数 /// 命令 - public static object Convert(string action, Dictionary param) + public static object Convert(string action, JContainer param) { if (action == DATENOW) { diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs index 4847fb3..91d3b03 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; @@ -136,6 +137,29 @@ namespace ZKLT.Hadoop.Model } } + return _result; + } + + /// + /// 类转JObject + /// + /// 数据 + /// + public static JObject Class2JObject(object data) { + var _result = new JObject(); + + Type _type = data.GetType(); + + var _properties = _type.GetProperties(); + + foreach (var _property in _properties) + { + if (_property.GetValue(data) != null) + { + _result.Add(new JProperty(_property.Name, _property.GetValue(data)!)); + } + } + return _result; } } diff --git a/Hadoop/ZKLT.Hadoop.Model/ZKLT.Hadoop.Model.csproj b/Hadoop/ZKLT.Hadoop.Model/ZKLT.Hadoop.Model.csproj index daeed6e..4f722d2 100644 --- a/Hadoop/ZKLT.Hadoop.Model/ZKLT.Hadoop.Model.csproj +++ b/Hadoop/ZKLT.Hadoop.Model/ZKLT.Hadoop.Model.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/Hadoop/ZKLT.Hadoop/HadoopService.cs b/Hadoop/ZKLT.Hadoop/HadoopService.cs index 0872e8e..44291c3 100644 --- a/Hadoop/ZKLT.Hadoop/HadoopService.cs +++ b/Hadoop/ZKLT.Hadoop/HadoopService.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using MySqlX.XDevAPI.Relational; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; @@ -140,10 +141,11 @@ namespace ZKLT.Hadoop { return _Source; } - var _result = _TableService.QuerySingle(_Source, GetTable("HDP_Source")!, new Dictionary + + var _result = _TableService.QuerySingle(_Source, GetTable("HDP_Source")!, new JObject { { "Id","=" } - }, new Dictionary { + }, new JObject { { "Id",sourceid} }, null); return _result; @@ -186,7 +188,7 @@ namespace ZKLT.Hadoop throw new ArgumentException("编号已存在"); } - return _TableService.Insert(_Source, GetTable("HDP_Source")!, HDP_Table.Class2Dictionary(source)); + return _TableService.Insert(_Source, GetTable("HDP_Source")!, HDP_Table.Class2JObject(source)); } /// @@ -226,9 +228,9 @@ namespace ZKLT.Hadoop throw new ArgumentException("编号不存在"); } - return _TableService.Update(_Source, GetTable("HDP_Source")!, new Dictionary { + return _TableService.Update(_Source, GetTable("HDP_Source")!, new JObject { {"Id","=" } - }, HDP_Table.Class2Dictionary(source)); + }, HDP_Table.Class2JObject(source)); } /// @@ -248,9 +250,9 @@ namespace ZKLT.Hadoop throw new ArgumentException("编号不存在"); } - return _TableService.Delete(_Source, GetTable("HDP_Source")!, new Dictionary { + return _TableService.Delete(_Source, GetTable("HDP_Source")!, new JObject{ {"Id","=" } - }, new Dictionary { + }, new JObject{ {"Id",sourceid } }); } @@ -282,18 +284,18 @@ namespace ZKLT.Hadoop return _Tables.First(x => x.Id == tableid); } - var _result = _TableService.QuerySingle(_Source, GetTable("HDP_Table")!, new Dictionary + var _result = _TableService.QuerySingle(_Source, GetTable("HDP_Table")!, new JObject { { "Id","=" } - }, new Dictionary { + }, new JObject{ { "Id",tableid} }, null); if (_result != null) { - _result.Columns = _TableService.Query(_Source, GetTable("HDP_Column")!, new Dictionary { + _result.Columns = _TableService.Query(_Source, GetTable("HDP_Column")!, new JObject { { "TableId","="} - }, new Dictionary { + }, new JObject{ {"TableId",_result.Id! } }, null, null); } @@ -320,7 +322,7 @@ namespace ZKLT.Hadoop { using (TransactionScope _scope = new TransactionScope()) { - if (!_TableService.Insert(_Source, GetTable("HDP_Table")!, HDP_Table.Class2Dictionary(table))) + if (!_TableService.Insert(_Source, GetTable("HDP_Table")!, HDP_Table.Class2JObject(table))) { return false; } @@ -328,7 +330,7 @@ namespace ZKLT.Hadoop { var _column = table.Columns![i]; _column.TableId = table.Id; - if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2Dictionary(_column))) + if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2JObject(_column))) { return false; } @@ -362,9 +364,9 @@ namespace ZKLT.Hadoop { using (TransactionScope _scope = new TransactionScope()) { - if (!_TableService.Update(_Source, GetTable("HDP_Table")!, new Dictionary { + if (!_TableService.Update(_Source, GetTable("HDP_Table")!, new JObject{ { "Id","="} - }, HDP_Table.Class2Dictionary(table))) + }, HDP_Table.Class2JObject(table))) { return false; } @@ -373,21 +375,21 @@ namespace ZKLT.Hadoop { var _column = table.Columns![i]; _column.TableId = table.Id; - if (_TableService.QuerySingle(_Source, GetTable("HDP_Column")!, new Dictionary + if (_TableService.QuerySingle(_Source, GetTable("HDP_Column")!, new JObject { {"Id","=" } - }, HDP_Table.Class2Dictionary(_column), null) == null) + }, HDP_Table.Class2JObject(_column), null) == null) { - if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2Dictionary(_column))) + if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2JObject(_column))) { return false; } } else { - if (!_TableService.Update(_Source, GetTable("HDP_Column")!, new Dictionary { + if (!_TableService.Update(_Source, GetTable("HDP_Column")!, new JObject { {"Id","=" } - }, HDP_Table.Class2Dictionary(_column))) + }, HDP_Table.Class2JObject(_column))) { return false; } @@ -423,17 +425,17 @@ namespace ZKLT.Hadoop using (TransactionScope _scope = new TransactionScope()) { - if (!_TableService.Delete(_Source, GetTable("HDP_Table")!, new Dictionary { + if (!_TableService.Delete(_Source, GetTable("HDP_Table")!, new JObject{ {"Id","=" } - }, new Dictionary { + }, new JObject { {"Id",tableId } })) { return false; } - if (!_TableService.Delete(_Source, GetTable("HDP_Column")!, new Dictionary { + if (!_TableService.Delete(_Source, GetTable("HDP_Column")!, new JObject{ { "TableId","="} - }, new Dictionary { + }, new JObject { {"TableId",tableId } })) { diff --git a/Hadoop/ZKLT.Hadoop/TableService.cs b/Hadoop/ZKLT.Hadoop/TableService.cs index 7f74144..767819a 100644 --- a/Hadoop/ZKLT.Hadoop/TableService.cs +++ b/Hadoop/ZKLT.Hadoop/TableService.cs @@ -12,6 +12,8 @@ using MySqlX.XDevAPI.Relational; using Mysqlx.Crud; using Newtonsoft.Json; using Mysqlx.Resultset; +using Newtonsoft.Json.Linq; +using System.Transactions; namespace ZKLT.Hadoop { @@ -22,60 +24,70 @@ namespace ZKLT.Hadoop /// /// 表 /// 条件 - /// 数据 - /// 命令 + /// 数据 /// 参数 - private void MergeWhere(HDP_Table table, Dictionary? where, Dictionary? row, StringBuilder command, Dictionary param) + private string MergeWhere(HDP_Table table, JContainer? where, JContainer? data, Dictionary param) { - string _guid = ""; - //执行条件 StringBuilder _wherestr = new StringBuilder(); - _wherestr.Append("WHERE 1 = 1"); - if (where != null && where.Count > 0) + string _guid = ""; + if (where != null && data != null) { - for (var i = 0; i < table.Columns!.Length; i++) + _wherestr.AppendLine("WHERE"); + var _wheres = new List(); + var _datas = new List(); + if (where.Type == JTokenType.Object) + { + _wheres.Add((JObject)where); + _datas.Add((JObject)data); + } + else if (where.Type == JTokenType.Array) { - var _column = table.Columns[i]; - if (where.ContainsKey(_column.Key!)) + for(var i = 0;i < where.Count;i++) { - switch (where[_column.Key!]) + _wheres.Add((JObject)where.Children().ToArray()[i]); + _datas.Add((JObject)data.Children().ToArray()[i]); + } + } + for (var j = 0; j < _wheres.Count; j++) { + if (j > 0) { + _wherestr.AppendLine("OR"); + } + _wherestr.AppendLine("("); + var _where = _wheres[j]; + var _data = _datas[j]; + if (_where.Count > 0) + { + _wherestr.AppendLine("1 = 1"); + var _fileds = _where.Children().ToArray(); + for (var i = 0; i < _fileds.Length; i++) { - case HDP_WhereType.LIKE: - _guid = Guid.NewGuid().ToString("N"); - _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} CONCAT('%',@{_guid},'%')"); - param.Add(_guid, row![_column.Key!]); - break; - case HDP_WhereType.BETWEEN: - if (row![_column.Key!] != null) + var _item = (JProperty)_fileds[i]; + if (table.Columns!.Any(x => x.Key == _item.Name) && _data.ContainsKey(_item.Name)) + { + if (_item.Value.Type == JTokenType.String) + { + _guid = Guid.NewGuid().ToString("N"); + _wherestr.AppendLine(@$"AND `{_item.Name}` {_item.Value.ToString()} @{_guid}"); + param.Add(_guid, ((JValue)_data[_item.Name]!).Value!); + } + else if (_item.Value.Type == JTokenType.Array) { - var _betweendata = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(row![_column.Key!])); - if (_betweendata != null) + string[] _itemv = _item.Value.ToObject()!; + object[] _colv = _data[_item.Name]!.ToObject()!; + for (var k = 0; k < _itemv.Length; k++) { - if (_betweendata[0] != null && _betweendata[0].ToString() != "") - { - _guid = Guid.NewGuid().ToString("N"); - _wherestr.Append($@" AND `{_column.Key!}` >= @{_guid}"); - param.Add(_guid, _betweendata[0]); - } - if (_betweendata[1] != null && _betweendata[1].ToString() != "") - { - _guid = Guid.NewGuid().ToString("N"); - _wherestr.Append($@" AND `{_column.Key!}` <= @{_guid}"); - param.Add(_guid, _betweendata[1]); - } + _guid = Guid.NewGuid().ToString("N"); + _wherestr.AppendLine(@$"AND `{_item.Name}` {_itemv[k]} @{_guid}"); + param.Add(_guid, _colv.Length > k ? _colv[k] : _colv[_colv.Length - 1]); } } - break; - default: - _guid = Guid.NewGuid().ToString("N"); - _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_guid}"); - param.Add(_guid, row![_column.Key!]); - break; + } } } + _wherestr.AppendLine(")"); } - command.AppendLine(_wherestr.ToString()); } + return _wherestr.ToString(); } /// @@ -111,21 +123,23 @@ namespace ZKLT.Hadoop /// 表 /// 排序 /// - private string MergeOrder(HDP_Table table, Dictionary? order, Dictionary param) + private string MergeOrder(HDP_Table table, JContainer? order, Dictionary param) { string _guid = ""; StringBuilder _orderstr = new StringBuilder(); if (order != null && order.Count > 0) { _orderstr.Append("ORDER BY "); - foreach (var key in order.Keys) + var _fields = order.Children().ToArray(); + foreach (var field in _fields) { - if (table.Columns!.Any(x => x.Key == key)) + var _field = (JProperty)field; + if (table.Columns!.Any(x => x.Key == _field.Name)) { - var _column = table.Columns!.First(x => x.Key == key); - if (order[key] is string) + var _column = table.Columns!.First(x => x.Key == _field.Name); + if (_field.Value.Type == JTokenType.String) { - switch (order[key]) + switch (_field.Value.ToString()) { case "DESC": _orderstr.Append($@"`{_column.Key!}` DESC,"); @@ -135,9 +149,9 @@ namespace ZKLT.Hadoop break; } } - else if (order[key] is Newtonsoft.Json.Linq.JArray) + else if (_field.Value.Type == JTokenType.Array) { - var _orderTemp = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(order[key])); + var _orderTemp = _field.Value.ToObject(); _orderstr.Append(@$"CASE `{_column.Key!}`"); for (var i = 0; i < _orderTemp!.Length; i++) { @@ -373,9 +387,9 @@ namespace ZKLT.Hadoop /// /// 数据源 /// 数据表 - /// 数据 + /// 数据 /// 是否成功 - public bool Insert(HDP_Source source, HDP_Table table, Dictionary row) + public bool Insert(HDP_Source source, HDP_Table table, JContainer? data) { //数据校验 if (string.IsNullOrEmpty(table.Key)) @@ -386,80 +400,99 @@ namespace ZKLT.Hadoop { throw new ArgumentNullException("列无效"); } - if (row == null || row.Count == 0) + if (data == null || data.Count == 0) { throw new ArgumentNullException("数据无效"); } - using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) + List _data = new List(); + if (data.Type == JTokenType.Object) { - try - { - _connection.Open(); - } - catch - { - throw new ArgumentException("数据源连接失败"); - } - StringBuilder _command = new StringBuilder(); - - //主键检查 - var _primarys = table.Columns.Where(x => x.IsPrimary == true).ToArray(); - for (var i = 0; i < _primarys.Length; i++) + _data.Add((JObject)data); + } + else if (data.Type == JTokenType.Array) + { + _data = data.ToObject>()!; + } + var _result = 0; + using (TransactionScope _scope = new TransactionScope()) + { + using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) { - var _primary = _primarys[i]; - if (!string.IsNullOrEmpty(_primary.InsertDefault) || (row.ContainsKey(_primary.Key!) && row[_primary.Key!] != null)) + try { - continue; + _connection.Open(); } - else + catch { - throw new ArgumentException($@"主键{_primary.Key}值无效"); + throw new ArgumentException("数据源连接失败"); } - } + for (var j = 0; j < _data.Count; j++) + { + var _row = _data[j]; - //插入命令 - _command.AppendLine($@"INSERT INTO `{table.Key}` ("); - StringBuilder _colstr = new StringBuilder(); - StringBuilder _parmstr = new StringBuilder(); - Dictionary _params = new Dictionary(); - for (var i = 0; i < table.Columns.Length; i++) - { - var _column = table.Columns[i]; - if (row.ContainsKey(_column.Key!) && row[_column.Key!] != null) + StringBuilder _command = new StringBuilder(); + + //主键检查 + var _primarys = table.Columns.Where(x => x.IsPrimary == true).ToArray(); + for (var i = 0; i < _primarys.Length; i++) + { + var _primary = _primarys[i]; + if (!string.IsNullOrEmpty(_primary.InsertDefault) || _row.ContainsKey(_primary.Key!)) + { + continue; + } + else + { + throw new ArgumentException($@"主键{_primary.Key}值无效"); + } + } + + //插入命令 + _command.AppendLine($@"INSERT INTO `{table.Key}` ("); + StringBuilder _colstr = new StringBuilder(); + StringBuilder _parmstr = new StringBuilder(); + Dictionary _params = new Dictionary(); + for (var i = 0; i < table.Columns.Length; i++) + { + var _column = table.Columns[i]; + if (_row.ContainsKey(_column.Key!)) + { + _colstr.Append($@"`{_column.Key!}`,"); + _parmstr.Append($@"@{_column.Key},"); + _params.Add(_column.Key!, ((JValue)_row[_column.Key!]!).Value!); + } + else if (!string.IsNullOrEmpty(_column.InsertDefault)) + { + _colstr.Append($@"`{_column.Key!}`,"); + _parmstr.Append($@"@{_column.Key},"); + _params.Add(_column.Key!, HDP_CommandAction.Convert(_column.InsertDefault, _row)); + } + } + if (_colstr[_colstr.Length - 1] == ',') + { + _colstr.Remove(_colstr.Length - 1, 1); + } + if (_parmstr[_parmstr.Length - 1] == ',') + { + _parmstr.Remove(_parmstr.Length - 1, 1); + } + _command.AppendLine(_colstr.ToString()); + _command.AppendLine(") VALUES ("); + _command.AppendLine(_parmstr.ToString()); + _command.AppendLine(")"); + _result += _connection.Execute(_command.ToString(), _params); + } + _connection.Close(); + if (_result == _data.Count) { - _colstr.Append($@"`{_column.Key!}`,"); - _parmstr.Append($@"@{_column.Key},"); - _params.Add(_column.Key!, row[_column.Key!]); + _scope.Complete(); + return true; } - else if (!string.IsNullOrEmpty(_column.InsertDefault)) + else { - _colstr.Append($@"`{_column.Key!}`,"); - _parmstr.Append($@"@{_column.Key},"); - _params.Add(_column.Key!, HDP_CommandAction.Convert(_column.InsertDefault, row)); + return false; } } - if (_colstr[_colstr.Length - 1] == ',') - { - _colstr.Remove(_colstr.Length - 1, 1); - } - if (_parmstr[_parmstr.Length - 1] == ',') - { - _parmstr.Remove(_parmstr.Length - 1, 1); - } - _command.AppendLine(_colstr.ToString()); - _command.AppendLine(") VALUES ("); - _command.AppendLine(_parmstr.ToString()); - _command.AppendLine(")"); - var _result = _connection.Execute(_command.ToString(), _params); - _connection.Close(); - if (_result > 0) - { - return true; - } - else - { - return false; - } } } @@ -471,7 +504,7 @@ namespace ZKLT.Hadoop /// 条件 /// 数据 /// 是否成功 - public bool Update(HDP_Source source, HDP_Table table, Dictionary where, Dictionary row) + public bool Update(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data) { //数据校验 if (string.IsNullOrEmpty(table.Key)) @@ -482,64 +515,81 @@ namespace ZKLT.Hadoop { throw new ArgumentNullException("列无效"); } - if (where == null || where.Count == 0) + if (where == null) { throw new ArgumentNullException("条件无效"); } - if (row == null || row.Count == 0) + if (data == null || data.Count == 0) { throw new ArgumentNullException("数据无效"); } - using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) + List _data = new List(); + if (data.Type == JTokenType.Object) { - try - { - _connection.Open(); - } - catch - { - throw new ArgumentException("数据源连接失败"); - } - - //更新命令 - StringBuilder _command = new StringBuilder(); - Dictionary _params = new Dictionary(); - _command.AppendLine(@$"UPDATE `{table.Key}` SET "); - - //更新列 - StringBuilder _colstr = new StringBuilder(); - for (var i = 0; i < table.Columns.Length; i++) + _data.Add((JObject)data); + } + else if (data.Type == JTokenType.Array) + { + _data = data.ToObject>()!; + } + var _result = 0; + using (TransactionScope _scope = new TransactionScope()) + { + using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) { - var _column = table.Columns[i]; - if (row.ContainsKey(_column.Key!) && !where.ContainsKey(_column.Key!) && row[_column.Key!] != null) + try { - _colstr.Append($@"`{_column.Key!}`=@{_column.Key!},"); - _params.Add(_column.Key!, row[_column.Key!]); + _connection.Open(); } - else if (!string.IsNullOrEmpty(_column.UpdateDefault)) + catch { - _colstr.Append($@"`{_column.Key!}`=@{_column.Key!},"); - _params.Add(_column.Key!, HDP_CommandAction.Convert(_column.UpdateDefault, row)); + throw new ArgumentException("数据源连接失败"); } - } - if (_colstr[_colstr.Length - 1] == ',') - { - _colstr.Remove(_colstr.Length - 1, 1); - } - _command.AppendLine(_colstr.ToString()); + for (var j = 0; j < _data.Count; j++) + { + var _row = _data[j]; + //更新命令 + StringBuilder _command = new StringBuilder(); + Dictionary _params = new Dictionary(); + _command.AppendLine(@$"UPDATE `{table.Key}` SET "); - //执行条件 - MergeWhere(table, where, row, _command, _params); + //更新列 + StringBuilder _colstr = new StringBuilder(); + for (var i = 0; i < table.Columns.Length; i++) + { + var _column = table.Columns[i]; + if (_row.ContainsKey(_column.Key!)) + { + _colstr.Append($@"`{_column.Key!}`=@{_column.Key!},"); + _params.Add(_column.Key!, ((JValue)_row[_column.Key!]!).Value!); + } + else if (!string.IsNullOrEmpty(_column.UpdateDefault)) + { + _colstr.Append($@"`{_column.Key!}`=@{_column.Key!},"); + _params.Add(_column.Key!, HDP_CommandAction.Convert(_column.UpdateDefault, _row)); + } + } + if (_colstr[_colstr.Length - 1] == ',') + { + _colstr.Remove(_colstr.Length - 1, 1); + } + _command.AppendLine(_colstr.ToString()); - var _result = _connection.Execute(_command.ToString(), _params); - _connection.Close(); - if (_result > 0) - { - return true; - } - else - { - return false; + //执行条件 + _command.AppendLine(MergeWhere(table, where, _row, _params)); + + _result += _connection.Execute(_command.ToString(), _params); + } + _connection.Close(); + if (_result == _data.Count) + { + _scope.Complete(); + return true; + } + else + { + return false; + } } } } @@ -552,7 +602,7 @@ namespace ZKLT.Hadoop /// 条件 /// 数据 /// 是否成功 - public bool Delete(HDP_Source source, HDP_Table table, Dictionary where, Dictionary row) + public bool Delete(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data) { //数据校验 if (string.IsNullOrEmpty(table.Key)) @@ -563,41 +613,59 @@ namespace ZKLT.Hadoop { throw new ArgumentNullException("列无效"); } - if (where == null || where.Count == 0) + if (where == null) { throw new ArgumentNullException("条件无效"); } - if (row == null || row.Count == 0) + if (data == null || data.Count == 0) { throw new ArgumentNullException("数据无效"); } - using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) + List _data = new List(); + if (data.Type == JTokenType.Object) { - try - { - _connection.Open(); - } - catch + _data.Add((JObject)data); + } + else if (data.Type == JTokenType.Array) + { + _data = data.ToObject>()!; + } + var _result = 0; + using (TransactionScope _scope = new TransactionScope()) + { + using (MySqlConnection _connection = new MySqlConnection(source.GetConnectString())) { - throw new ArgumentException("数据源连接失败"); - } + try + { + _connection.Open(); + } + catch + { + throw new ArgumentException("数据源连接失败"); + } - //更新命令 - StringBuilder _command = new StringBuilder(); - Dictionary _params = new Dictionary(); - _command.AppendLine(@$"DELETE FROM `{table.Key}`"); + for (var j = 0; j < _data.Count; j++) + { + var _row = _data[j]; + //删除命令 + StringBuilder _command = new StringBuilder(); + Dictionary _params = new Dictionary(); + _command.AppendLine(@$"DELETE FROM `{table.Key}`"); - MergeWhere(table, where, row, _command, _params); + _command.AppendLine(MergeWhere(table, where, _row, _params)); - var _result = _connection.Execute(_command.ToString(), _params); - _connection.Close(); - if (_result > 0) - { - return true; - } - else - { - return false; + _result += _connection.Execute(_command.ToString(), _params); + } + _connection.Close(); + if (_result == _data.Count) + { + _scope.Complete(); + return true; + } + else + { + return false; + } } } } @@ -608,9 +676,9 @@ namespace ZKLT.Hadoop /// 数据源 /// 数据表 /// 条件 - /// 数据 + /// 数据 /// 结果 - public T? QuerySingle(HDP_Source source, HDP_Table table, Dictionary where, Dictionary row, + public T? QuerySingle(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data, string[]? col) { //数据校验 @@ -622,11 +690,11 @@ namespace ZKLT.Hadoop { throw new ArgumentNullException("列无效"); } - if (where == null || where.Count == 0) + if (where == null) { throw new ArgumentNullException("条件无效"); } - if (row == null || row.Count == 0) + if (data == null || data.Count == 0) { throw new ArgumentNullException("数据无效"); } @@ -656,7 +724,7 @@ namespace ZKLT.Hadoop } //执行条件 - MergeWhere(table, where, row, _command, _params); + _command.AppendLine(MergeWhere(table, where, data, _params)); var _result = _connection.Query(_command.ToString(), _params).ToArray(); _connection.Close(); @@ -678,10 +746,10 @@ namespace ZKLT.Hadoop /// 数据源 /// 数据表 /// 条件 - /// 数据 + /// 数据 /// 结果集 - public T[] Query(HDP_Source source, HDP_Table table, Dictionary? where, Dictionary? row, - Dictionary? order, string[]? col) + public T[] Query(HDP_Source source, HDP_Table table, JContainer? where, JContainer? data, + JContainer? order, string[]? col) { //数据校验 if (string.IsNullOrEmpty(table.Key)) @@ -718,7 +786,7 @@ namespace ZKLT.Hadoop } //执行条件 - MergeWhere(table, where, row, _command, _params); + _command.AppendLine(MergeWhere(table, where, data, _params)); //执行排序 _command.AppendLine(MergeOrder(table, order, _params)); @@ -738,8 +806,8 @@ namespace ZKLT.Hadoop /// 条件 /// 数据 /// 结果集 - public HDP_Page QueryPage(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary? where, - Dictionary? row, Dictionary? order, string[]? col) + public HDP_Page QueryPage(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, JContainer? where, + JContainer? data, JContainer? order, string[]? col) { //数据校验 if (string.IsNullOrEmpty(table.Key)) @@ -776,35 +844,10 @@ namespace ZKLT.Hadoop } //执行条件 - MergeWhere(table, where, row, _command, _params); + _command.AppendLine(MergeWhere(table, where, data, _params)); //执行排序 - StringBuilder _orderstr = new StringBuilder(); - _orderstr.Append("ORDER BY "); - if (order != null && order.Count > 0) - { - for (var i = 0; i < table.Columns.Length; i++) - { - var _column = table.Columns[i]; - if (order.ContainsKey(_column.Key!)) - { - switch (order[_column.Key!]) - { - case "DESC": - _orderstr.Append($@"`{_column.Key!}` DESC,"); - break; - default: - _orderstr.Append($@"`{_column.Key!}` ASC,"); - break; - } - } - } - if (_orderstr[_orderstr.Length - 1] == ',') - { - _orderstr.Remove(_orderstr.Length - 1, 1); - } - _command.AppendLine(_orderstr.ToString()); - } + _command.AppendLine(MergeOrder(table, order, _params)); var _result = new HDP_Page(); _result.PageIndex = pageIndex; _result.PageSize = pageSize;