From 0e3091f7d21c939d59103c4303a2828700f6da49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sun, 28 Jul 2024 14:14:18 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=8B=BC?= =?UTF-8?q?=E6=8E=A5selects=E5=AD=90=E5=8F=A5=E6=97=B6=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E8=A6=81=E6=A3=80=E6=B5=8B=E5=88=B0Or=E5=B0=B1=E5=8A=A0?= =?UTF-8?q?=E4=B8=8A=E5=9C=86=E6=8B=AC=E5=8F=B7=EF=BC=8C=E5=90=A6=E5=88=99?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCode/Model/ConcatExpression.cs | 6 +- XCode/Model/Expression.cs | 5 +- XCode/Model/WhereExpression.cs | 301 ++++++++++++++++---------------- 3 files changed, 156 insertions(+), 156 deletions(-) diff --git a/XCode/Model/ConcatExpression.cs b/XCode/Model/ConcatExpression.cs index 87c2aa25b..ce9c6194b 100644 --- a/XCode/Model/ConcatExpression.cs +++ b/XCode/Model/ConcatExpression.cs @@ -9,7 +9,7 @@ public class ConcatExpression : Expression { #region 属性 /// 内置表达式集合 - public IList Expressions { get; set; } = new List(); + public IList Expressions { get; set; } = []; /// 是否为空 public override Boolean IsEmpty => Expressions.Count == 0; @@ -21,7 +21,7 @@ public ConcatExpression() { } /// 实例化 /// - public ConcatExpression(String exp) => Expressions.Add(new Expression(exp)); + public ConcatExpression(String exp) => Expressions.Add(new Expression(exp) { DetectOr = false }); #endregion #region 方法 @@ -32,7 +32,7 @@ public ConcatExpression And(String exp) { if (String.IsNullOrEmpty(exp)) return this; - Expressions.Add(new Expression(exp)); + Expressions.Add(new Expression(exp) { DetectOr = false }); return this; } diff --git a/XCode/Model/Expression.cs b/XCode/Model/Expression.cs index 6af2f3fbd..727de713a 100644 --- a/XCode/Model/Expression.cs +++ b/XCode/Model/Expression.cs @@ -14,6 +14,9 @@ public class Expression /// 文本表达式 public String? Text { get; private set; } + /// 是否检测Or关键字并加上括号。默认true + public Boolean DetectOr { get; set; } = true; + /// 是否为空 public virtual Boolean IsEmpty => Text.IsNullOrEmpty(); @@ -54,7 +57,7 @@ public virtual void GetString(IDatabase? db, StringBuilder builder, IDictionary< var txt = Text; if (txt.IsNullOrEmpty()) return; - if (_regOr.IsMatch(txt)) + if (DetectOr && _regOr.IsMatch(txt)) builder.AppendFormat("({0})", txt); else builder.Append(txt); diff --git a/XCode/Model/WhereExpression.cs b/XCode/Model/WhereExpression.cs index b1e5fe7de..d27242611 100644 --- a/XCode/Model/WhereExpression.cs +++ b/XCode/Model/WhereExpression.cs @@ -1,197 +1,194 @@ -using System; -using System.Collections; -using System.Collections.Generic; +using System.Collections; using System.Text; using NewLife.Collections; using XCode.DataAccessLayer; -namespace XCode +namespace XCode; + +/// 操作符 +public enum Operator { - /// 操作符 - public enum Operator - { - /// 与,交集 - And, + /// 与,交集 + And, - /// 或,并集 - Or, + /// 或,并集 + Or, - /// 空格 - Space - }; + /// 空格 + Space +}; - /// 条件表达式 - public class WhereExpression : Expression, IEnumerable - { - #region 属性 - /// 左节点 - public Expression Left { get; set; } +/// 条件表达式 +public class WhereExpression : Expression, IEnumerable +{ + #region 属性 + /// 左节点 + public Expression Left { get; set; } = null!; - /// 右节点 - public Expression Right { get; set; } + /// 右节点 + public Expression Right { get; set; } = null!; - /// 是否And - public Operator Operator { get; set; } + /// 是否And + public Operator Operator { get; set; } - /// 是否为空 - public override Boolean IsEmpty => (Left == null || Left.IsEmpty) && (Right == null || Right.IsEmpty); - #endregion + /// 是否为空 + public override Boolean IsEmpty => (Left == null || Left.IsEmpty) && (Right == null || Right.IsEmpty); + #endregion - #region 构造 - /// 实例化 - public WhereExpression() { } + #region 构造 + /// 实例化 + public WhereExpression() { } - /// 实例化 - /// - /// - /// - public WhereExpression(Expression left, Operator op, Expression right) - { - Left = Flatten(left); - Operator = op; - Right = Flatten(right); - } - #endregion - - #region 方法 - /// 输出条件表达式的字符串表示,遍历表达式集合并拼接起来 - /// 数据库 - /// - /// 参数字典 - /// - public override void GetString(IDatabase? db, StringBuilder builder, IDictionary? ps) - { - if (IsEmpty) return; + /// 实例化 + /// + /// + /// + public WhereExpression(Expression left, Operator op, Expression right) + { + Left = Flatten(left); + Operator = op; + Right = Flatten(right); + } + #endregion + + #region 方法 + /// 输出条件表达式的字符串表示,遍历表达式集合并拼接起来 + /// 数据库 + /// + /// 参数字典 + /// + public override void GetString(IDatabase? db, StringBuilder builder, IDictionary? ps) + { + if (IsEmpty) return; - // 递归构建,下级运算符优先级较低时加括号 + // 递归构建,下级运算符优先级较低时加括号 - var len = builder.Length; + var len = builder.Length; - // 左侧表达式 - GetString(db, builder, ps, Left); + // 左侧表达式 + GetString(db, builder, ps, Left); - // 右侧表达式 - var sb = Pool.StringBuilder.Get(); - GetString(db, sb, ps, Right); + // 右侧表达式 + var sb = Pool.StringBuilder.Get(); + GetString(db, sb, ps, Right); - // 中间运算符 - if (builder.Length > len && sb.Length > 0) + // 中间运算符 + if (builder.Length > len && sb.Length > 0) + { + switch (Operator) { - switch (Operator) - { - case Operator.And: builder.Append(" And "); break; - case Operator.Or: builder.Append(" Or "); break; - case Operator.Space: builder.Append(' '); break; - default: break; - } + case Operator.And: builder.Append(" And "); break; + case Operator.Or: builder.Append(" Or "); break; + case Operator.Space: builder.Append(' '); break; + default: break; } - - builder.Append(sb.Put(true)); } - private void GetString(IDatabase? db, StringBuilder builder, IDictionary ps, Expression exp) - { - exp = Flatten(exp); - if (exp == null || exp.IsEmpty) return; + builder.Append(sb.Put(true)); + } - // 递归构建,下级运算符优先级较低时加括号 - var bracket = false; - if (exp is WhereExpression where) - { - //if (where.IsEmpty) return; + private void GetString(IDatabase? db, StringBuilder builder, IDictionary ps, Expression exp) + { + exp = Flatten(exp); + if (exp == null || exp.IsEmpty) return; - if (where.Operator > Operator) bracket = true; - } + // 递归构建,下级运算符优先级较低时加括号 + var bracket = false; + if (exp is WhereExpression where) + { + //if (where.IsEmpty) return; - if (bracket) builder.Append('('); - exp.GetString(db, builder, ps); - if (bracket) builder.Append(')'); + if (where.Operator > Operator) bracket = true; } - /// 拉平表达式,避免空子项 - /// - /// - private Expression Flatten(Expression exp) - { - if (exp == null) return null; + if (bracket) builder.Append('('); + exp.GetString(db, builder, ps); + if (bracket) builder.Append(')'); + } - if (exp is WhereExpression where) - { - // 左右为空,返回空 - if (where.Left == null && where.Right == null) return null; + /// 拉平表达式,避免空子项 + /// + /// + private Expression Flatten(Expression exp) + { + if (exp == null) return null; - // 其中一边为空,递归拉平另一边 - if (where.Left == null) return Flatten(where.Right); - if (where.Right == null) return Flatten(where.Left); - } + if (exp is WhereExpression where) + { + // 左右为空,返回空 + if (where.Left == null && where.Right == null) return null; - return exp; + // 其中一边为空,递归拉平另一边 + if (where.Left == null) return Flatten(where.Right); + if (where.Right == null) return Flatten(where.Left); } - ///// 访问表达式 - ///// - ///// - //public Expression Visit(Func visitor) - //{ - // if (Left != null) - // { - // var rs = visitor(Left); - // if (rs) return Left; - - // if (Left is WhereExpression where) - // { - // var exp = where.Visit(visitor); - // if (exp != null) return exp; - // } - // } - - // if (Right != null) - // { - // var rs = visitor(Right); - // if (rs) return Right; - - // if (Right is WhereExpression where) - // { - // var exp = where.Visit(visitor); - // if (exp != null) return exp; - // } - // } - - // return null; - //} - - /// 枚举 - /// - public IEnumerator GetEnumerator() + return exp; + } + + ///// 访问表达式 + ///// + ///// + //public Expression Visit(Func visitor) + //{ + // if (Left != null) + // { + // var rs = visitor(Left); + // if (rs) return Left; + + // if (Left is WhereExpression where) + // { + // var exp = where.Visit(visitor); + // if (exp != null) return exp; + // } + // } + + // if (Right != null) + // { + // var rs = visitor(Right); + // if (rs) return Right; + + // if (Right is WhereExpression where) + // { + // var exp = where.Visit(visitor); + // if (exp != null) return exp; + // } + // } + + // return null; + //} + + /// 枚举 + /// + public IEnumerator GetEnumerator() + { + if (Left != null) { - if (Left != null) - { - yield return Left; + yield return Left; - if (Left is WhereExpression where) + if (Left is WhereExpression where) + { + foreach (var item in where) { - foreach (var item in where) - { - yield return item; - } + yield return item; } } + } - if (Right != null) - { - yield return Right; + if (Right != null) + { + yield return Right; - if (Right is WhereExpression where) + if (Right is WhereExpression where) + { + foreach (var item in where) { - foreach (var item in where) - { - yield return item; - } + yield return item; } } } - - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - #endregion } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + #endregion } \ No newline at end of file