LJExprParser.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using CSUST.Data.Expr;
  2. using JLHHJSvr.LJException;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace JLHHJSvr.LJFramework.Tools
  10. {
  11. public class LJExprParser
  12. {
  13. public static TExprParserEx Parse(string expr)
  14. {
  15. if (string.IsNullOrEmpty(expr)) return new TExprParserChina("");
  16. var treatedExpr = expr;
  17. treatedExpr = treatedExpr.Replace("不包含", " not like ");
  18. treatedExpr = treatedExpr.Replace("包含", " like ");
  19. treatedExpr = treatedExpr.Replace("不等于", " != ");
  20. treatedExpr = treatedExpr.Replace("等于", " == ");
  21. treatedExpr = treatedExpr.Replace("大于", " > ");
  22. treatedExpr = treatedExpr.Replace("小于", " < ");
  23. treatedExpr = treatedExpr.Replace("<>", " != ");
  24. treatedExpr = treatedExpr.Replace("'%", "'");
  25. treatedExpr = treatedExpr.Replace("%'", "'");
  26. //treatedExpr = treatedExpr.Replace(" and ", " && ");
  27. //treatedExpr = treatedExpr.Replace(" or ", " || ");
  28. //treatedExpr = treatedExpr.Replace(" = ", " == ");
  29. treatedExpr = Regex.Replace(treatedExpr, "([ ]|[\\w])([=])([^=])", "$1==$3");
  30. treatedExpr = Regex.Replace(treatedExpr, "if(\\s*)[(]", "iif(");
  31. treatedExpr = ConvertNotLikeExpression(treatedExpr);
  32. treatedExpr = ConvertLikeExpression(treatedExpr);
  33. treatedExpr = ConvertPosExpression(treatedExpr);
  34. treatedExpr = ConvertInExpression(treatedExpr);
  35. var rslt = new TExprParserChina(treatedExpr);
  36. if (rslt.HasError)
  37. {
  38. throw new LJCommonException($"解析表达式{expr}失败:" + rslt.ErrorMessage);
  39. }
  40. return rslt;
  41. }
  42. public static string ConvertInExpression(string expression)
  43. {
  44. string pattern = "";
  45. string replacement = "";
  46. string convertedExpression = expression;
  47. // 字符串in,单引号用占位符代替,数组转为字符串对比,移除所有空格
  48. pattern = @"('[^']*')\s+not\s+in\s+\(([^)]*)\)";
  49. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  50. {
  51. var group1 = match.Groups[1].Value.Replace(" ", "").Replace("'", "quote");
  52. var group2 = match.Groups[2].Value.Replace(" ", "").Replace("'", "quote");
  53. return $"!HasStr(',{group2},',',{group1},')";
  54. });
  55. pattern = @"('[^']*')\s+in\s+\(([^)]*)\)";
  56. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  57. {
  58. var group1 = match.Groups[1].Value.Replace(" ", "").Replace("'", "quote");
  59. var group2 = match.Groups[2].Value.Replace(" ", "").Replace("'", "quote");
  60. return $"HasStr(',{group2},',',{group1},')";
  61. });
  62. // 数字in,数组转为字符串对比,移除所有空格
  63. pattern = @"(\d*)\s+not\s+in\s+\(([^)]*)\)";
  64. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  65. {
  66. var group1 = match.Groups[1].Value.Replace(" ", "");
  67. var group2 = match.Groups[2].Value.Replace(" ", "");
  68. return $"!HasStr(',{group2},',',{group1},')";
  69. });
  70. pattern = @"(\d*)\s+in\s+\(([^)]*)\)";
  71. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  72. {
  73. var group1 = match.Groups[1].Value.Replace(" ", "");
  74. var group2 = match.Groups[2].Value.Replace(" ", "");
  75. return $"HasStr(',{group2},',',{group1},')";
  76. });
  77. return convertedExpression;
  78. }
  79. public static string ConvertNotLikeExpression(string expression)
  80. {
  81. string pattern = @"'([^']*)'\s+not\s+like\s+'([^']*)'";
  82. string replacement = "!HasStr('$1', '$2')";
  83. string convertedExpression = Regex.Replace(expression, pattern, replacement);
  84. return convertedExpression;
  85. }
  86. public static string ConvertLikeExpression(string expression)
  87. {
  88. string pattern = @"'([^']*)'\s+like\s+'([^']*)'";
  89. string replacement = "HasStr('$1', '$2')";
  90. string convertedExpression = Regex.Replace(expression, pattern, replacement);
  91. return convertedExpression;
  92. }
  93. public static string ConvertPosExpression(string expression)
  94. {
  95. var reg = new Regex(@"pos[(](\s*)'([^']*)'(\s*),(\s*)'([^']*)'(\s*)[)]");
  96. var rslt = expression;
  97. while (true)
  98. {
  99. var match = reg.Match(rslt);
  100. if (!match.Success)
  101. {
  102. break;
  103. }
  104. var p1 = match.Groups[2].Value;
  105. var p2 = match.Groups[5].Value;
  106. var calVal = p1.IndexOf(p2) + 1;
  107. rslt = rslt.Replace(match.Value, calVal.ToString());
  108. }
  109. return rslt;
  110. }
  111. }
  112. }