电子药箱通讯服务端
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.

SqlBuilder.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace RDH.Data.BLL
  6. {
  7. /// <summary>
  8. /// 表示SQL语句拼接的帮助类
  9. /// </summary>
  10. public class SqlBuilder
  11. {
  12. private BaseBLL _dalMain;
  13. private List<TableJoinInfo> _listJoinInfo;
  14. private StringBuilder _whereBuilder;
  15. private StringBuilder _selectionBuilder;
  16. private StringBuilder _orderBuilder;
  17. public SqlBuilder(BaseBLL dal)
  18. {
  19. _dalMain = dal;
  20. _listJoinInfo = new List<TableJoinInfo>();
  21. _whereBuilder = new StringBuilder();
  22. }
  23. public Boolean DistinctFlag { get; set; }
  24. public Boolean DonotMapMainTableFlag { get; set; }
  25. public void AppendWherePhrases(String value)
  26. {
  27. if (_whereBuilder.Length == 0)
  28. {
  29. if (!value.Trim().StartsWith("WHERE", StringComparison.CurrentCultureIgnoreCase))
  30. {
  31. _whereBuilder.Append(" WHERE ");
  32. }
  33. }
  34. else
  35. {
  36. if (!value.Trim().StartsWith("AND", StringComparison.CurrentCultureIgnoreCase)
  37. && !value.Trim().StartsWith("OR", StringComparison.CurrentCultureIgnoreCase))
  38. {
  39. _whereBuilder.Append(" AND ");
  40. }
  41. else if (!value.StartsWith(" "))
  42. {
  43. _whereBuilder.Append(" ");
  44. }
  45. }
  46. _whereBuilder.Append(value);
  47. }
  48. public void AppendWherePhrasesFormat(String value, params String[] paramInfo)
  49. {
  50. _whereBuilder.AppendFormat(value, paramInfo);
  51. }
  52. public void AppendOrderPhrases(String phrase)
  53. {
  54. if (_orderBuilder == null)
  55. {
  56. _orderBuilder = new StringBuilder();
  57. if (!phrase.Trim().ToLower().StartsWith("order"))
  58. {
  59. _orderBuilder.Append(" ORDER BY");
  60. }
  61. }
  62. if (_orderBuilder.Length > 9
  63. && !phrase.Trim().StartsWith(","))
  64. {
  65. _orderBuilder.Append(",");
  66. }
  67. else
  68. {
  69. _orderBuilder.Append(" ");
  70. }
  71. _orderBuilder.Append(phrase);
  72. }
  73. public void AppendSelectionTable(TableJoinInfo joinInfo)
  74. {
  75. if (joinInfo == null || joinInfo.LeftDal == null || joinInfo.RightDal == null)
  76. {
  77. return;
  78. }
  79. _listJoinInfo.Add(joinInfo);
  80. joinInfo.CurrentSpiltor = "spiltor" + _listJoinInfo.Count.ToString();
  81. }
  82. public void AppendSelectionTable(TableJoinTypes join, BaseBLL rightDal, String leftColumn, String rightColumn)
  83. {
  84. AppendSelectionTable(new TableJoinInfo()
  85. {
  86. LeftDal = _dalMain,
  87. RightDal = rightDal,
  88. LeftColumnName = leftColumn,
  89. RightColumnName = rightColumn,
  90. JoinType = join,
  91. });
  92. }
  93. public void AppendSelectionPhrases(String value)
  94. {
  95. value = value.Trim();
  96. if (_selectionBuilder == null)
  97. {
  98. _selectionBuilder = new StringBuilder();
  99. }
  100. if (!value.StartsWith(","))
  101. {
  102. _selectionBuilder.Append(",");
  103. }
  104. _selectionBuilder.Append(value);
  105. }
  106. public String GetSpiltors()
  107. {
  108. return _listJoinInfo.Count == 0 ? null
  109. : String.Join(",", _listJoinInfo.Where(x => !x.DonotMapFlag).Select(x => x.CurrentSpiltor));
  110. }
  111. public override string ToString()
  112. {
  113. Boolean beginSpiltor = false;
  114. StringBuilder builder = new StringBuilder();
  115. #region Build Select
  116. builder.Append("SELECT ");
  117. if (DistinctFlag)
  118. {
  119. builder.Append("DISTINCT ");
  120. }
  121. //main table selection
  122. String mainTableName = String.IsNullOrEmpty(_dalMain.AliasTableName) ? _dalMain.TABLE_NAME : _dalMain.AliasTableName;
  123. if (!DonotMapMainTableFlag)
  124. {
  125. foreach (KeyValuePair<String, String> pair in _dalMain.ColumnPropMaps)
  126. {
  127. builder.AppendFormat("{0}.{1} {2},", mainTableName, pair.Key, pair.Value);
  128. }
  129. beginSpiltor = true;
  130. }
  131. //slave table selection
  132. if (_listJoinInfo.Count > 0)
  133. {
  134. foreach (TableJoinInfo info in _listJoinInfo)
  135. {
  136. if (info.LeftTableAliasName == null)
  137. {
  138. info.LeftTableAliasName = String.IsNullOrEmpty(info.LeftDal.AliasTableName) ? info.LeftDal.TABLE_NAME : info.LeftDal.AliasTableName;
  139. }
  140. if (info.RightTableAliasName == null)
  141. {
  142. info.RightTableAliasName = String.IsNullOrEmpty(info.RightDal.AliasTableName) ? info.RightDal.TABLE_NAME : info.RightDal.AliasTableName;
  143. }
  144. if (info.DonotMapFlag)
  145. {
  146. continue;
  147. }
  148. if (beginSpiltor)
  149. {
  150. builder.AppendFormat("'' {0},", info.CurrentSpiltor);
  151. }
  152. foreach (KeyValuePair<String, String> pair in info.RightDal.ColumnPropMaps)
  153. {
  154. builder.AppendFormat("{0}.{1} {2},",
  155. info.RightTableAliasName,
  156. pair.Key, pair.Value);
  157. }
  158. if (info.CustomPropertiesMap != null)
  159. {
  160. foreach (KeyValuePair<String, String> pair in info.CustomPropertiesMap)
  161. {
  162. builder.AppendFormat("{0} {1},", pair.Value, pair.Key);
  163. }
  164. }
  165. if (!beginSpiltor)
  166. {
  167. beginSpiltor = true;
  168. }
  169. }
  170. }
  171. builder.Remove(builder.Length - 1, 1);
  172. if (_selectionBuilder != null)
  173. {
  174. builder.Append(_selectionBuilder.ToString());
  175. }
  176. #endregion
  177. //from
  178. builder.AppendFormat(" FROM {0} {1} ",
  179. _dalMain.TABLE_NAME,
  180. mainTableName);
  181. #region Build Join
  182. if (_listJoinInfo.Count > 0)
  183. {
  184. foreach (TableJoinInfo info in _listJoinInfo)
  185. {
  186. builder.AppendFormat(" {0} JOIN {1} {2} ",
  187. info.JoinType.ToString(),
  188. info.RightDal.TABLE_NAME,
  189. info.RightTableAliasName);
  190. if (!String.IsNullOrEmpty(info.CustomJoinCondition))
  191. {
  192. builder.AppendFormat(info.CustomJoinCondition);
  193. }
  194. else
  195. {
  196. builder.AppendFormat("ON {0}.{1}={2}.{3}",
  197. info.LeftTableAliasName, info.LeftColumnName,
  198. info.RightTableAliasName, info.RightColumnName);
  199. }
  200. }
  201. }
  202. #endregion
  203. if (_whereBuilder.Length > 0)
  204. {
  205. builder.Append(" ");
  206. builder.Append(_whereBuilder.ToString());
  207. }
  208. if (_orderBuilder != null)
  209. {
  210. builder.Append(_orderBuilder.ToString());
  211. }
  212. return builder.ToString();
  213. }
  214. }
  215. }