LJExprParser.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = Regex.Replace(treatedExpr, "int(\\s*)[(]", "(");
  32. treatedExpr = ConvertNotLikeExpression(treatedExpr);
  33. treatedExpr = ConvertLikeExpression(treatedExpr);
  34. treatedExpr = ConvertPosExpression(treatedExpr);
  35. treatedExpr = ConvertInExpression(treatedExpr);
  36. var rslt = new TExprParserChina(treatedExpr);
  37. if (rslt.HasError)
  38. {
  39. throw new LJCommonException($"解析表达式{expr}失败:" + rslt.ErrorMessage);
  40. }
  41. return rslt;
  42. }
  43. public static string ConvertInExpression(string expression)
  44. {
  45. string pattern = "";
  46. string replacement = "";
  47. string convertedExpression = expression;
  48. // 字符串in,单引号用占位符代替,数组转为字符串对比,移除所有空格
  49. pattern = @"('[^']*')\s+not\s+in\s+\(([^)]*)\)";
  50. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  51. {
  52. var group1 = match.Groups[1].Value.Replace(" ", "").Replace("'", "quote");
  53. var group2 = match.Groups[2].Value.Replace(" ", "").Replace("'", "quote");
  54. return $"!HasStr(',{group2},',',{group1},')";
  55. });
  56. pattern = @"('[^']*')\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. // 数字in,数组转为字符串对比,移除所有空格
  64. pattern = @"(\d*)\s+not\s+in\s+\(([^)]*)\)";
  65. convertedExpression = Regex.Replace(convertedExpression, pattern, match =>
  66. {
  67. var group1 = match.Groups[1].Value.Replace(" ", "");
  68. var group2 = match.Groups[2].Value.Replace(" ", "");
  69. return $"!HasStr(',{group2},',',{group1},')";
  70. });
  71. pattern = @"(\d*)\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. return convertedExpression;
  79. }
  80. public static string ConvertNotLikeExpression(string expression)
  81. {
  82. string pattern = @"'([^']*)'\s+not\s+like\s+'([^']*)'";
  83. string replacement = "!HasStr('$1', '$2')";
  84. string convertedExpression = Regex.Replace(expression, pattern, replacement);
  85. return convertedExpression;
  86. }
  87. public static string ConvertLikeExpression(string expression)
  88. {
  89. string pattern = @"'([^']*)'\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 ConvertPosExpression(string expression)
  95. {
  96. var reg = new Regex(@"pos[(](\s*)'([^']*)'(\s*),(\s*)'([^']*)'(\s*)[)]");
  97. var rslt = expression;
  98. while (true)
  99. {
  100. var match = reg.Match(rslt);
  101. if (!match.Success)
  102. {
  103. break;
  104. }
  105. var p1 = match.Groups[2].Value;
  106. var p2 = match.Groups[5].Value;
  107. var calVal = p1.IndexOf(p2) + 1;
  108. rslt = rslt.Replace(match.Value, calVal.ToString());
  109. }
  110. reg = new Regex("pos[(](\\s*)'([^']*)'(\\s*),(\\s*)'([^']*)'(\\s*),(\\s*)(\\d+)(\\s*)[)]");
  111. while (true)
  112. {
  113. var match = reg.Match(rslt);
  114. if (!match.Success)
  115. {
  116. break;
  117. }
  118. var p1 = match.Groups[2].Value;
  119. var p2 = match.Groups[5].Value;
  120. var p3 = Convert.ToInt32(match.Groups[8].Value);
  121. var calVal = p1.IndexOf(p2, p3 - 1) + 1;
  122. rslt = rslt.Replace(match.Value, calVal.ToString());
  123. }
  124. reg = new Regex("pos[(](\\s*)\"([^\"]*)\"(\\s*),(\\s*)\"([^\"]*)\"(\\s*)[)]");
  125. while (true)
  126. {
  127. var match = reg.Match(rslt);
  128. if (!match.Success)
  129. {
  130. break;
  131. }
  132. var p1 = match.Groups[2].Value;
  133. var p2 = match.Groups[5].Value;
  134. var calVal = p1.IndexOf(p2) + 1;
  135. rslt = rslt.Replace(match.Value, calVal.ToString());
  136. }
  137. reg = new Regex("pos[(](\\s*)\"([^\"]*)\"(\\s*),(\\s*)\"([^\"]*)\"(\\s*),(\\s*)(\\d+)(\\s*)[)]");
  138. while (true)
  139. {
  140. var match = reg.Match(rslt);
  141. if (!match.Success)
  142. {
  143. break;
  144. }
  145. var p1 = match.Groups[2].Value;
  146. var p2 = match.Groups[5].Value;
  147. var p3 = Convert.ToInt32(match.Groups[8].Value);
  148. var calVal = p1.IndexOf(p2, p3 - 1) + 1;
  149. rslt = rslt.Replace(match.Value, calVal.ToString());
  150. }
  151. return rslt;
  152. }
  153. }
  154. }