From 2733c0f7a72d24b8f3022f144f0619045a58675a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=98=E5=BB=BA=E4=B8=9C?= <617601767@qq.com> Date: Sat, 16 Mar 2024 12:52:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hadoop/ZKLT.Hadoop.Interface/ITableService.cs | 5 +- Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs | 4 +- Hadoop/ZKLT.Hadoop/TableService.cs | 95 ++++++++++++------- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs b/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs index 10b2a6c..ce900bf 100644 --- a/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs +++ b/Hadoop/ZKLT.Hadoop.Interface/ITableService.cs @@ -73,7 +73,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, Dictionary? where, Dictionary? row, + Dictionary? order, string[]? col); /// /// 查询列表 @@ -84,7 +85,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, Dictionary? where, Dictionary? row, Dictionary? order, string[]? col); /// diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs index aa0c339..afb092c 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_Command.cs @@ -27,7 +27,7 @@ namespace ZKLT.Hadoop.Model private Dictionary? _Data; - private Dictionary? _Order; + private Dictionary? _Order; /// /// 源 @@ -62,7 +62,7 @@ namespace ZKLT.Hadoop.Model /// /// 排序 /// - public Dictionary? Order { get => _Order; set => _Order = value; } + public Dictionary? Order { get => _Order; set => _Order = value; } /// /// 命令类型 diff --git a/Hadoop/ZKLT.Hadoop/TableService.cs b/Hadoop/ZKLT.Hadoop/TableService.cs index 676f11c..0428a5f 100644 --- a/Hadoop/ZKLT.Hadoop/TableService.cs +++ b/Hadoop/ZKLT.Hadoop/TableService.cs @@ -11,6 +11,7 @@ using ZKLT.Hadoop.Model; using MySqlX.XDevAPI.Relational; using Mysqlx.Crud; using Newtonsoft.Json; +using Mysqlx.Resultset; namespace ZKLT.Hadoop { @@ -43,9 +44,11 @@ namespace ZKLT.Hadoop param.Add(_column.Key!, row![_column.Key!]); break; case HDP_WhereType.BETWEEN: - if (row![_column.Key!] != null) { + if (row![_column.Key!] != null) + { var _betweendata = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(row![_column.Key!])); - if (_betweendata != null) { + if (_betweendata != null) + { if (_betweendata[0] != null && _betweendata[0].ToString() != "") { _wherestr.Append($@" AND `{_column.Key!}` >= @{_column.Key!}0"); @@ -97,6 +100,56 @@ namespace ZKLT.Hadoop command.AppendLine(_colstr.ToString()); } + /// + /// 合并排序 + /// + /// 表 + /// 排序 + /// + private string MergeOrder(HDP_Table table, Dictionary? order, Dictionary param) + { + StringBuilder _orderstr = new StringBuilder(); + if (order != null && order.Count > 0) + { + _orderstr.Append("ORDER BY "); + foreach (var key in order.Keys) + { + if (table.Columns!.Any(x => x.Key == key)) + { + var _column = table.Columns!.First(x => x.Key == key); + if (order[key] is string) + { + switch (order[key]) + { + case "DESC": + _orderstr.Append($@"`{_column.Key!}` DESC,"); + break; + default: + _orderstr.Append($@"`{_column.Key!}` ASC,"); + break; + } + } + else if (order[key] is Newtonsoft.Json.Linq.JArray) + { + var _orderTemp = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(order[key])); + _orderstr.Append(@$"CASE `{_column.Key!}`"); + for (var i = 0; i < _orderTemp!.Length; i++) + { + _orderstr.Append(@$" WHEN @OR{_column.Key}{i} THEN {i}"); + param.Add($@"@OR{_column.Key}{i}", _orderTemp[i]); + } + _orderstr.Append($@" ELSE {_orderTemp.Length} END,"); + } + } + } + if (_orderstr.Length > 0 && _orderstr[_orderstr.Length - 1] == ',') + { + _orderstr.Remove(_orderstr.Length - 1, 1); + } + } + return _orderstr.ToString(); + } + /// /// 同步结构 /// @@ -550,7 +603,7 @@ 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, Dictionary where, Dictionary row, string[]? col) { //数据校验 @@ -620,8 +673,8 @@ 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, Dictionary? where, Dictionary? row, + Dictionary? order, string[]? col) { //数据校验 if (string.IsNullOrEmpty(table.Key)) @@ -661,32 +714,8 @@ namespace ZKLT.Hadoop MergeWhere(table, where, row, _command, _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 = _connection.Query(_command.ToString(), _params); _connection.Close(); return _result.ToArray(); @@ -702,8 +731,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, Dictionary? where, + Dictionary? row, Dictionary? order, string[]? col) { //数据校验 if (string.IsNullOrEmpty(table.Key))