From 97d01f98a747cb347afbcfffadd7d710ab5de055 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=BD=98=E5=BB=BA=E4=B8=9C?= <617601767@qq.com>
Date: Thu, 7 Mar 2024 13:40:21 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9Like=E9=94=99=E8=AF=AF?=
=?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E5=B0=81=E8=A3=85where=E6=9D=A1?=
=?UTF-8?q?=E4=BB=B6=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Hadoop/ZKLT.Hadoop/TableService.cs | 149 ++++++++---------------------
1 file changed, 42 insertions(+), 107 deletions(-)
diff --git a/Hadoop/ZKLT.Hadoop/TableService.cs b/Hadoop/ZKLT.Hadoop/TableService.cs
index 9adae70..e1f7afd 100644
--- a/Hadoop/ZKLT.Hadoop/TableService.cs
+++ b/Hadoop/ZKLT.Hadoop/TableService.cs
@@ -15,6 +15,42 @@ namespace ZKLT.Hadoop
{
public class TableService : ITableService
{
+ ///
+ /// 合并条件
+ ///
+ /// 表
+ /// 条件
+ /// 数据
+ /// 命令
+ /// 参数
+ private void MergeWhere(HDP_Table table, Dictionary? where, Dictionary? row, StringBuilder command, Dictionary param)
+ {
+ //执行条件
+ StringBuilder _wherestr = new StringBuilder();
+ _wherestr.Append("WHERE 1 = 1");
+ if (where != null && where.Count > 0)
+ {
+ for (var i = 0; i < table.Columns!.Length; i++)
+ {
+ var _column = table.Columns[i];
+ if (where.ContainsKey(_column.Key!))
+ {
+ switch (where[_column.Key!])
+ {
+ case HDP_WhereType.LIKE:
+ _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} CONCAT('%',@{_column.Key!},'%')");
+ break;
+ default:
+ _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
+ break;
+ }
+ param.Add(_column.Key!, row![_column.Key!]);
+ }
+ }
+ command.AppendLine(_wherestr.ToString());
+ }
+ }
+
///
/// 同步结构
///
@@ -398,26 +434,7 @@ namespace ZKLT.Hadoop
_command.AppendLine(_colstr.ToString());
//执行条件
- StringBuilder _wherestr = new StringBuilder();
- _wherestr.Append("WHERE 1 = 1");
- for (var i = 0; i < table.Columns.Length; i++)
- {
- var _column = table.Columns[i];
- if (where.ContainsKey(_column.Key!))
- {
- switch (where[_column.Key!])
- {
- case HDP_WhereType.LIKE:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
- break;
- default:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
- break;
- }
- _params.Add(_column.Key!, row[_column.Key!]);
- }
- }
- _command.AppendLine(_wherestr.ToString());
+ MergeWhere(table, where, row, _command, _params);
var _result = _connection.Execute(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params);
_connection.Close();
@@ -475,27 +492,7 @@ namespace ZKLT.Hadoop
Dictionary _params = new Dictionary();
_command.AppendLine(@$"DELETE FROM `{table.Key}`");
- //执行条件
- StringBuilder _wherestr = new StringBuilder();
- _wherestr.Append("WHERE 1 = 1");
- for (var i = 0; i < table.Columns.Length; i++)
- {
- var _column = table.Columns[i];
- if (where.ContainsKey(_column.Key!))
- {
- switch (where[_column.Key!])
- {
- case HDP_WhereType.LIKE:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
- break;
- default:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
- break;
- }
- _params.Add(_column.Key!, row[_column.Key!]);
- }
- }
- _command.AppendLine(_wherestr.ToString());
+ MergeWhere(table, where, row, _command, _params);
var _result = _connection.Execute(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params);
_connection.Close();
@@ -554,26 +551,7 @@ namespace ZKLT.Hadoop
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
//执行条件
- StringBuilder _wherestr = new StringBuilder();
- _wherestr.Append("WHERE 1 = 1");
- for (var i = 0; i < table.Columns.Length; i++)
- {
- var _column = table.Columns[i];
- if (where.ContainsKey(_column.Key!))
- {
- switch (where[_column.Key!])
- {
- case HDP_WhereType.LIKE:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
- break;
- default:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
- break;
- }
- _params.Add(_column.Key!, row[_column.Key!]);
- }
- }
- _command.AppendLine(_wherestr.ToString());
+ MergeWhere(table, where, row, _command, _params);
var _result = _connection.Query(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params).ToArray();
_connection.Close();
@@ -625,29 +603,7 @@ namespace ZKLT.Hadoop
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
//执行条件
- StringBuilder _wherestr = new StringBuilder();
- _wherestr.Append("WHERE 1 = 1");
- if (where != null && where.Count > 0)
- {
- for (var i = 0; i < table.Columns.Length; i++)
- {
- var _column = table.Columns[i];
- if (where.ContainsKey(_column.Key!))
- {
- switch (where[_column.Key!])
- {
- case HDP_WhereType.LIKE:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} CONCAT('%',@{_column.Key!},'%')");
- break;
- default:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
- break;
- }
- _params.Add(_column.Key!, row[_column.Key!]);
- }
- }
- _command.AppendLine(_wherestr.ToString());
- }
+ MergeWhere(table, where, row, _command, _params);
//执行排序
StringBuilder _orderstr = new StringBuilder();
@@ -719,29 +675,8 @@ namespace ZKLT.Hadoop
_command.AppendLine(@$"SELECT * FROM `{table.Key}`");
//执行条件
- StringBuilder _wherestr = new StringBuilder();
- _wherestr.Append("WHERE 1 = 1");
- if (where != null && where.Count > 0)
- {
- for (var i = 0; i < table.Columns.Length; i++)
- {
- var _column = table.Columns[i];
- if (where.ContainsKey(_column.Key!))
- {
- switch (where[_column.Key!])
- {
- case HDP_WhereType.LIKE:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} '%' + @{_column.Key!} + '%'");
- break;
- default:
- _wherestr.Append($@" AND `{_column.Key!}` {where[_column.Key!]} @{_column.Key!}");
- break;
- }
- _params.Add(_column.Key!, row[_column.Key!]);
- }
- }
- _command.AppendLine(_wherestr.ToString());
- }
+ MergeWhere(table, where, row, _command, _params);
+
//执行排序
StringBuilder _orderstr = new StringBuilder();
_orderstr.Append("ORDER BY ");