Condition.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections;
  3. using IwbZero.IwbDataQuery;
  4. using IwbZero.ToolCommon.StringModel;
  5. namespace IwbZero.IwbBase
  6. {
  7. public class Condition
  8. {
  9. private static ArrayList _supportedOps;
  10. public string Op;
  11. public string Source;
  12. public string Target;
  13. public Condition(string pcSource, string pcOp, string pcTarget)
  14. {
  15. Source = pcSource;
  16. Op = pcOp;
  17. Target = pcTarget;
  18. }
  19. public static Condition ConvertFrom(string pcString)
  20. {
  21. Condition condition = new Condition("", "", "");
  22. ArrayList supportedOps = GetSupportedOps();
  23. Array array = pcString.StrToArrayEx("=");
  24. if (array.Length == 2)
  25. {
  26. string str = array.GetValue(1).ToString();
  27. if (str.IndexOf('*') >= 0)
  28. {
  29. pcString = array.GetValue(0) + " like " + str;
  30. }
  31. else
  32. {
  33. char ch = ' ';
  34. if (str.Length > 0)
  35. {
  36. ch = str[0];
  37. }
  38. if (condition.ReservedChars.IndexOf(ch) < 0)
  39. {
  40. pcString = array.GetValue(0) + "=" + str;
  41. }
  42. else
  43. {
  44. pcString = array.GetValue(0) + array.GetValue(1).ToString();
  45. }
  46. }
  47. }
  48. foreach (IwbIdName name in supportedOps)
  49. {
  50. int index = pcString.IndexOf(name.Id, StringComparison.Ordinal);
  51. if (index > 0)
  52. {
  53. condition.Source = pcString.Substring(0, index).Trim();
  54. condition.Op = name.Id;
  55. if (pcString.Length > (index + name.Id.Length))
  56. {
  57. condition.Target = pcString.Substring(index + name.Id.Length).Trim();
  58. }
  59. return condition;
  60. }
  61. }
  62. return condition;
  63. }
  64. public static ArrayList GetSupportedOps()
  65. {
  66. return _supportedOps ?? (_supportedOps = new ArrayList
  67. {
  68. new IwbIdName(">=", "GreaterEqual"),
  69. new IwbIdName("<=", "SmallerEqual"),
  70. new IwbIdName("!=", "Not Equal"),
  71. new IwbIdName("=", "Equal"),
  72. new IwbIdName(">", "Greater"),
  73. new IwbIdName("<", "Smaller"),
  74. new IwbIdName(" like ", "Like")
  75. });
  76. }
  77. public string ToMappingString()
  78. {
  79. if ((Op == "like") && (Target.IndexOf('*') < 0))
  80. {
  81. return ("[" + Source + "]=[*" + Target + "*]");
  82. }
  83. if ((Op == "=") || ((Op == "like") && (Target.IndexOf('*') >= 0)))
  84. {
  85. return ("[" + Source + "]=[" + Target + "]");
  86. }
  87. return ("[" + Source + "]=[" + Op + Target + "]");
  88. }
  89. public override string ToString()
  90. {
  91. return (Source + Op + Target);
  92. }
  93. public string TranslateIntoSql(string pcTableId, int piConnectionId)
  94. {
  95. return "";
  96. }
  97. public string TranslateIntoSql(string pcTableId, string pcServerType)
  98. {
  99. return "";
  100. }
  101. public string ReservedChars { get; } = "<>=*";
  102. public static string ConditionMappingListToMappingString(ArrayList poMappings)
  103. {
  104. string pcSource = "";
  105. foreach (Condition condition in poMappings)
  106. {
  107. if (condition.Source != "")
  108. {
  109. pcSource = pcSource.AddStr(condition.ToMappingString());
  110. }
  111. }
  112. return pcSource;
  113. }
  114. public static ArrayList ConditionMappingToArrayList(string pcMapping)
  115. {
  116. ArrayList list = new ArrayList();
  117. Array array = pcMapping.IndexOf(" and ", StringComparison.Ordinal) > 0 ? pcMapping.StrToArrayEx(" and ") : pcMapping.StrToArray();
  118. foreach (string str in array)
  119. {
  120. Condition condition = ConvertFrom(str);
  121. if ((condition.Source != "") && (condition.Op != ""))
  122. {
  123. list.Add(condition);
  124. }
  125. }
  126. return list;
  127. }
  128. public static string ConditionMappingToConditionString(string pcMapping)
  129. {
  130. string str = "";
  131. foreach (Condition condition in ConditionMappingToArrayList(pcMapping))
  132. {
  133. if (condition.Source != "")
  134. {
  135. string str2 = condition.ToString();
  136. if (str2 != "")
  137. {
  138. str = str + ((str == "") ? "" : " and ") + $"({str2})";
  139. }
  140. }
  141. }
  142. return str;
  143. }
  144. }
  145. }