LJExprParser.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 bool CheckFormula(string expr,out string arg_msg)
  14. {
  15. var parser = new TExprParserChina(expr);
  16. //
  17. arg_msg = parser.ErrorMessage;
  18. return parser.HasError;
  19. }
  20. public static TExprParserEx Parse(string expr)
  21. {
  22. if (string.IsNullOrEmpty(expr)) return new TExprParserChina("");
  23. var treatedExpr = expr;
  24. treatedExpr = treatedExpr.Replace("不包含", " not like ");
  25. treatedExpr = treatedExpr.Replace("包含", " like ");
  26. treatedExpr = treatedExpr.Replace("不等于", " != ");
  27. treatedExpr = treatedExpr.Replace("等于", " == ");
  28. treatedExpr = treatedExpr.Replace("大于", " > ");
  29. treatedExpr = treatedExpr.Replace("小于", " < ");
  30. treatedExpr = treatedExpr.Replace("<>", " != ");
  31. treatedExpr = treatedExpr.Replace("'%", "'");
  32. treatedExpr = treatedExpr.Replace("%'", "'");
  33. //treatedExpr = treatedExpr.Replace(" and ", " && ");
  34. //treatedExpr = treatedExpr.Replace(" or ", " || ");
  35. //treatedExpr = treatedExpr.Replace(" = ", " == ");
  36. treatedExpr = Regex.Replace(treatedExpr, "([ ]|[\\w])([=])([^=])", "$1==$3");
  37. treatedExpr = Regex.Replace(treatedExpr, "if(\\s*)[(]", "iif(");
  38. treatedExpr = Regex.Replace(treatedExpr, "int(\\s*)[(]", "floor(");
  39. treatedExpr = ConvertNotLikeExpression(treatedExpr);
  40. treatedExpr = ConvertLikeExpression(treatedExpr);
  41. treatedExpr = ConvertPosExpression(treatedExpr);
  42. treatedExpr = ConvertInExpression(treatedExpr);
  43. var rslt = new TExprParserChina(treatedExpr);
  44. if (rslt.HasError)
  45. {
  46. throw new LJCommonException($"解析表达式{expr}失败:" + rslt.ErrorMessage);
  47. }
  48. return rslt;
  49. }
  50. public static string ConvertInExpression(string expression)
  51. {
  52. string pattern = "";
  53. string replacement = "";
  54. string convertedExpression = expression;
  55. // 字符串in,单引号用占位符代替,数组转为字符串对比,移除所有空格
  56. pattern = @"('[^']*')\s+not\s+in\s+\(([^)]*)\)";
  57. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  58. {
  59. var group1 = match.Groups[1].Value.Replace(" ", "").Replace("'", "quote");
  60. var group2 = match.Groups[2].Value.Replace(" ", "").Replace("'", "quote");
  61. return $"!HasStr(',{group2},',',{group1},')";
  62. });
  63. pattern = @"('[^']*')\s+in\s+\(([^)]*)\)";
  64. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  65. {
  66. var group1 = match.Groups[1].Value.Replace(" ", "").Replace("'", "quote");
  67. var group2 = match.Groups[2].Value.Replace(" ", "").Replace("'", "quote");
  68. return $"HasStr(',{group2},',',{group1},')";
  69. });
  70. // 数字in,数组转为字符串对比,移除所有空格
  71. pattern = @"(\d*)\s+not\s+in\s+\(([^)]*)\)";
  72. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  73. {
  74. var group1 = match.Groups[1].Value.Replace(" ", "");
  75. var group2 = match.Groups[2].Value.Replace(" ", "");
  76. return $"!HasStr(',{group2},',',{group1},')";
  77. });
  78. pattern = @"(\d*)\s+in\s+\(([^)]*)\)";
  79. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  80. {
  81. var group1 = match.Groups[1].Value.Replace(" ", "");
  82. var group2 = match.Groups[2].Value.Replace(" ", "");
  83. return $"HasStr(',{group2},',',{group1},')";
  84. });
  85. return convertedExpression;
  86. }
  87. public static string ConvertNotLikeExpression(string expression)
  88. {
  89. string pattern = @"'([^']*)'\s+not\s+like\s+'([^']*)'";
  90. string replacement = "!HasStr('$1', '$2')";
  91. string convertedExpression = Regex.Replace(expression, pattern, replacement);
  92. return convertedExpression;
  93. }
  94. public static string ConvertLikeExpression(string expression)
  95. {
  96. string pattern = @"'([^']*)'\s+like\s+'([^']*)'";
  97. string replacement = "HasStr('$1', '$2')";
  98. string convertedExpression = Regex.Replace(expression, pattern, replacement);
  99. return convertedExpression;
  100. }
  101. public static string ConvertPosExpression(string expression)
  102. {
  103. var reg = new Regex(@"pos[(](\s*)'([^']*)'(\s*),(\s*)'([^']*)'(\s*)[)]");
  104. var rslt = expression;
  105. while (true)
  106. {
  107. var match = reg.Match(rslt);
  108. if (!match.Success)
  109. {
  110. break;
  111. }
  112. var p1 = match.Groups[2].Value;
  113. var p2 = match.Groups[5].Value;
  114. var calVal = p1.IndexOf(p2) + 1;
  115. rslt = rslt.Replace(match.Value, calVal.ToString());
  116. }
  117. reg = new Regex("pos[(](\\s*)'([^']*)'(\\s*),(\\s*)'([^']*)'(\\s*),(\\s*)(\\d+)(\\s*)[)]");
  118. while (true)
  119. {
  120. var match = reg.Match(rslt);
  121. if (!match.Success)
  122. {
  123. break;
  124. }
  125. var p1 = match.Groups[2].Value;
  126. var p2 = match.Groups[5].Value;
  127. var p3 = Convert.ToInt32(match.Groups[8].Value);
  128. var calVal = p1.IndexOf(p2, p3 - 1) + 1;
  129. rslt = rslt.Replace(match.Value, calVal.ToString());
  130. }
  131. reg = new Regex("pos[(](\\s*)\"([^\"]*)\"(\\s*),(\\s*)\"([^\"]*)\"(\\s*)[)]");
  132. while (true)
  133. {
  134. var match = reg.Match(rslt);
  135. if (!match.Success)
  136. {
  137. break;
  138. }
  139. var p1 = match.Groups[2].Value;
  140. var p2 = match.Groups[5].Value;
  141. var calVal = p1.IndexOf(p2) + 1;
  142. rslt = rslt.Replace(match.Value, calVal.ToString());
  143. }
  144. reg = new Regex("pos[(](\\s*)\"([^\"]*)\"(\\s*),(\\s*)\"([^\"]*)\"(\\s*),(\\s*)(\\d+)(\\s*)[)]");
  145. while (true)
  146. {
  147. var match = reg.Match(rslt);
  148. if (!match.Success)
  149. {
  150. break;
  151. }
  152. var p1 = match.Groups[2].Value;
  153. var p2 = match.Groups[5].Value;
  154. var p3 = Convert.ToInt32(match.Groups[8].Value);
  155. var calVal = p1.IndexOf(p2, p3 - 1) + 1;
  156. rslt = rslt.Replace(match.Value, calVal.ToString());
  157. }
  158. return rslt;
  159. }
  160. }
  161. }