You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.2 KiB

2 years ago
  1. using EasyNet.DBUtility;
  2. using System;
  3. using System.Text;
  4. namespace EasyNet.Common
  5. {
  6. public class DbCondition : Map
  7. {
  8. private static string WHERE = " WHERE ";
  9. private static string EQUAL = " {0} = {1} ";
  10. private static string AND_EQ = " AND {0} = {1} ";
  11. private static string OR_EQ = " OR {0} = {1} ";
  12. private static string GT = " {0} > {1} ";
  13. private static string GT_EQ = " {0} >= {1} ";
  14. private static string AND_GT = " AND {0} > {1} ";
  15. private static string AND_GT_EQ = " AND {0} >= {1} ";
  16. private static string OR_GT = " OR {0} > {1} ";
  17. private static string OR_GT_EQ = " OR {0} >= {1} ";
  18. private static string LT = " {0} < {1} ";
  19. private static string LT_EQ = " {0} <= {1} ";
  20. private static string AND_LT = " AND {0} < {1} ";
  21. private static string AND_LT_EQ = " AND {0} <= {1} ";
  22. private static string OR_LT = " OR {0} < {1} ";
  23. private static string OR_LT_EQ = " OR {0} <= {1} ";
  24. private static string ORDER_BY_ASC = " ORDER BY {0} ASC ";
  25. private static string ORDER_BY_DESC = " ORDER BY {0} DESC ";
  26. private static string paramChar = DbFactory.CreateDbParmCharacter();
  27. private StringBuilder sbSQL = new StringBuilder();
  28. public string queryString = String.Empty;
  29. public ColumnInfo Columns = new ColumnInfo();
  30. public DbCondition()
  31. {
  32. }
  33. public DbCondition(string query)
  34. {
  35. this.queryString = query;
  36. sbSQL.Append(query);
  37. }
  38. public DbCondition Query(string query)
  39. {
  40. this.queryString = query;
  41. sbSQL.Append(query);
  42. return this;
  43. }
  44. public DbCondition Where()
  45. {
  46. sbSQL.Append(WHERE);
  47. return this;
  48. }
  49. public DbCondition Where(string fieldName, object fieldValue)
  50. {
  51. var formatName = FormatKey(fieldName);
  52. sbSQL.AppendFormat(WHERE + EQUAL, fieldName, paramChar + formatName);
  53. Columns[formatName] = fieldValue;
  54. return this;
  55. }
  56. public DbCondition Equal(string fieldName, object fieldValue)
  57. {
  58. var formatName = FormatKey(fieldName);
  59. sbSQL.AppendFormat(EQUAL, fieldName, paramChar + formatName);
  60. Columns[formatName] = fieldValue;
  61. return this;
  62. }
  63. public DbCondition AndEqual(string fieldName, object fieldValue)
  64. {
  65. var formatName = FormatKey(fieldName);
  66. sbSQL.AppendFormat(AND_EQ, fieldName, paramChar + formatName);
  67. Columns[formatName] = fieldValue;
  68. return this;
  69. }
  70. public DbCondition OrEqual(string fieldName, object fieldValue)
  71. {
  72. var formatName = FormatKey(fieldName);
  73. sbSQL.AppendFormat(OR_EQ, fieldName, paramChar + formatName);
  74. Columns[formatName] = fieldValue;
  75. return this;
  76. }
  77. public DbCondition GreaterThan(string fieldName, object fieldValue)
  78. {
  79. var formatName = FormatKey(fieldName);
  80. sbSQL.AppendFormat(GT, fieldName, paramChar + formatName);
  81. Columns[formatName] = fieldValue;
  82. return this;
  83. }
  84. public DbCondition GreaterThanEqual(string fieldName, object fieldValue)
  85. {
  86. var formatName = FormatKey(fieldName);
  87. sbSQL.AppendFormat(GT_EQ, fieldName, paramChar + formatName);
  88. Columns[formatName] = fieldValue;
  89. return this;
  90. }
  91. public DbCondition AndGreaterThan(string fieldName, object fieldValue)
  92. {
  93. var formatName = FormatKey(fieldName);
  94. sbSQL.AppendFormat(AND_GT, fieldName, paramChar + formatName);
  95. Columns[formatName] = fieldValue;
  96. return this;
  97. }
  98. public DbCondition AndGreaterThanEqual(string fieldName, object fieldValue)
  99. {
  100. var formatName = FormatKey(fieldName);
  101. sbSQL.AppendFormat(AND_GT_EQ, fieldName, paramChar + formatName);
  102. Columns[formatName] = fieldValue;
  103. return this;
  104. }
  105. public DbCondition OrGreaterThan(string fieldName, object fieldValue)
  106. {
  107. var formatName = FormatKey(fieldName);
  108. sbSQL.AppendFormat(OR_GT, fieldName, paramChar + formatName);
  109. Columns[formatName] = fieldValue;
  110. return this;
  111. }
  112. public DbCondition OrGreaterThanEqual(string fieldName, object fieldValue)
  113. {
  114. var formatName = FormatKey(fieldName);
  115. sbSQL.AppendFormat(OR_GT_EQ, fieldName, paramChar + formatName);
  116. Columns[formatName] = fieldValue;
  117. return this;
  118. }
  119. public DbCondition LessThan(string fieldName, object fieldValue)
  120. {
  121. var formatName = FormatKey(fieldName);
  122. sbSQL.AppendFormat(LT, fieldName, paramChar + formatName);
  123. Columns[formatName] = fieldValue;
  124. return this;
  125. }
  126. public DbCondition LessThanEqual(string fieldName, object fieldValue)
  127. {
  128. var formatName = FormatKey(fieldName);
  129. sbSQL.AppendFormat(LT_EQ, fieldName, paramChar + formatName);
  130. Columns[formatName] = fieldValue;
  131. return this;
  132. }
  133. public DbCondition AndLessThan(string fieldName, object fieldValue)
  134. {
  135. var formatName = FormatKey(fieldName);
  136. sbSQL.AppendFormat(AND_LT, fieldName, paramChar + formatName);
  137. Columns[formatName] = fieldValue;
  138. return this;
  139. }
  140. public DbCondition AndLessThanEqual(string fieldName, object fieldValue)
  141. {
  142. var formatName = FormatKey(fieldName);
  143. sbSQL.AppendFormat(AND_LT_EQ, fieldName, paramChar + formatName);
  144. Columns[formatName] = fieldValue;
  145. return this;
  146. }
  147. public DbCondition OrLessThan(string fieldName, object fieldValue)
  148. {
  149. var formatName = FormatKey(fieldName);
  150. sbSQL.AppendFormat(OR_LT, fieldName, paramChar + formatName);
  151. Columns[formatName] = fieldValue;
  152. return this;
  153. }
  154. public DbCondition OrLessThanEqual(string fieldName, object fieldValue)
  155. {
  156. var formatName = FormatKey(fieldName);
  157. sbSQL.AppendFormat(OR_LT_EQ, fieldName, paramChar + formatName);
  158. Columns[formatName] = fieldValue;
  159. return this;
  160. }
  161. public DbCondition And(string fieldName, object fieldValue)
  162. {
  163. return this.AndEqual(fieldName, fieldValue);
  164. }
  165. public DbCondition Or(string fieldName, object fieldValue)
  166. {
  167. return this.OrEqual(fieldName, fieldValue);
  168. }
  169. public DbCondition OrderByASC(string fieldName)
  170. {
  171. sbSQL.AppendFormat(ORDER_BY_ASC, fieldName);
  172. return this;
  173. }
  174. public DbCondition OrderByDESC(string fieldName)
  175. {
  176. sbSQL.AppendFormat(ORDER_BY_DESC, fieldName);
  177. return this;
  178. }
  179. public DbCondition Like(string fieldName, object fieldValue)
  180. {
  181. sbSQL.AppendFormat(" {0} LIKE '%{1}%' ", fieldName, fieldValue);
  182. return this;
  183. }
  184. public DbCondition AndLike(string fieldName, object fieldValue)
  185. {
  186. sbSQL.AppendFormat(" AND {0} LIKE '%{1}%' ", fieldName, fieldValue);
  187. return this;
  188. }
  189. public DbCondition OrLike(string fieldName, object fieldValue)
  190. {
  191. sbSQL.AppendFormat(" OR {0} LIKE '%{1}%' ", fieldName, fieldValue);
  192. return this;
  193. }
  194. public DbCondition LeftLike(string fieldName, object fieldValue)
  195. {
  196. sbSQL.AppendFormat(" {0} LIKE '%{1}' ", fieldName, fieldValue);
  197. return this;
  198. }
  199. public DbCondition AndLeftLike(string fieldName, object fieldValue)
  200. {
  201. sbSQL.AppendFormat(" AND {0} LIKE '%{1}' ", fieldName, fieldValue);
  202. return this;
  203. }
  204. public DbCondition OrLeftLike(string fieldName, object fieldValue)
  205. {
  206. sbSQL.AppendFormat(" OR {0} LIKE '%{1}' ", fieldName, fieldValue);
  207. return this;
  208. }
  209. public DbCondition RightLike(string fieldName, object fieldValue)
  210. {
  211. sbSQL.AppendFormat(" {0} LIKE '{1}%' ", fieldName, fieldValue);
  212. return this;
  213. }
  214. public DbCondition AndRightLike(string fieldName, object fieldValue)
  215. {
  216. sbSQL.AppendFormat(" AND {0} LIKE '{1}%' ", fieldName, fieldValue);
  217. return this;
  218. }
  219. public DbCondition OrRightLike(string fieldName, object fieldValue)
  220. {
  221. sbSQL.AppendFormat(" OR {0} LIKE '{1}%' ", fieldName, fieldValue);
  222. return this;
  223. }
  224. public override string ToString()
  225. {
  226. return sbSQL.ToString();
  227. }
  228. private static string FormatKey(string key)
  229. {
  230. var index = key.IndexOf('.');
  231. if (index >= 0)
  232. {
  233. key = key.Substring(index + 1, key.Length - (index + 1));
  234. }
  235. return key;
  236. }
  237. }
  238. }