sql_builder.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. 'use strict';
  2. /**
  3. * sql语句构建器
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/11
  7. * @version
  8. */
  9. class SqlBuilder {
  10. /**
  11. * 构造函数
  12. *
  13. * @return {void}
  14. */
  15. constructor() {
  16. this.resetCondition();
  17. }
  18. /**
  19. * 设置andWhere数据
  20. *
  21. * @param {String} field - where中的字段名称
  22. * @param {Object} data - where中的value部分
  23. * @return {void}
  24. */
  25. setAndWhere(field, data) {
  26. if (Object.keys(data).length <= 0) {
  27. return;
  28. }
  29. this.andWhere.push({ field, data });
  30. }
  31. /**
  32. * 更新字段设置
  33. *
  34. * @param {String} field - set的字段
  35. * @param {Object} data - set中的字段
  36. * @return {void}
  37. */
  38. setUpdateData(field, data) {
  39. if (Object.keys(data).length <= 0) {
  40. return;
  41. }
  42. this.setData.push({ field, data });
  43. }
  44. /**
  45. * 重置条件
  46. *
  47. * @return {void}
  48. */
  49. resetCondition() {
  50. this.andWhere = [];
  51. this.orWhere = [];
  52. this.columns = [];
  53. this.limit = -1;
  54. this.offset = -1;
  55. this.orderBy = [];
  56. this.setData = [];
  57. this.sql = '';
  58. this.sqlParam = [];
  59. this.type = 'select';
  60. }
  61. /**
  62. * 类型设置
  63. *
  64. * @param {String} tableName - 表名
  65. * @return {void}
  66. */
  67. _typeBuild(tableName) {
  68. switch (this.type) {
  69. case 'select':
  70. this.sql = this.columns.length === 0 ? 'SELECT * FROM ??' : 'SELECT ?? FROM ??';
  71. this.sqlParam = this.columns.length === 0 ? [tableName] : [this.columns, tableName];
  72. break;
  73. case 'update':
  74. this.sql = 'UPDATE ?? SET ';
  75. this.sqlParam = [tableName];
  76. break;
  77. case 'delete':
  78. this.sql = 'DELETE FROM ??';
  79. this.sqlParam = [tableName];
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. /**
  86. * 设置update数据
  87. *
  88. * @return {void}
  89. */
  90. _setDataBuild() {
  91. if (this.setData.length <= 0) {
  92. return;
  93. }
  94. const setDataArr = [];
  95. for (const set of this.setData) {
  96. if (set.data.literal) {
  97. const values = set.data.value instanceof Array ? set.data.value : [set.data.value];
  98. setDataArr.push(' ?? = ' + set.data.literal + '(' + values.join(',') + ')');
  99. this.sqlParam.push(set.field);
  100. } else {
  101. const tmp = set.data.selfOperate !== undefined ?
  102. ' ?? = IF(IsNull(??), 0, ??) ' + set.data.selfOperate + ' ' + set.data.value : ' ?? = ' + set.data.value;
  103. setDataArr.push(tmp);
  104. // 如果是自身操作则压多一次字段进数组
  105. if (set.data.selfOperate !== undefined) {
  106. this.sqlParam.push(set.field);
  107. this.sqlParam.push(set.field);
  108. }
  109. this.sqlParam.push(set.field);
  110. }
  111. }
  112. const setString = setDataArr.join(',');
  113. this.sql += setString;
  114. }
  115. /**
  116. * andWhere设置
  117. *
  118. * @return {void}
  119. */
  120. _andWhereBuild() {
  121. if (this.andWhere.length <= 0) {
  122. return;
  123. }
  124. const whereArr = [];
  125. for (const where of this.andWhere) {
  126. if (where.data.operate === 'in') {
  127. // in操作
  128. const valueLength = where.data.value instanceof Array ? where.data.value.length : 1;
  129. // 生成参数集
  130. const inArr = [];
  131. for (let i = 0; i < valueLength; i++) {
  132. inArr.push('?');
  133. }
  134. const inData = inArr.join(',');
  135. whereArr.push(' ?? IN (' + inData + ')');
  136. this.sqlParam.push(where.field);
  137. // 赋值参数
  138. this.sqlParam.push.apply(this.sqlParam, where.data.value);
  139. } else {
  140. // 普通操作
  141. whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
  142. this.sqlParam.push(where.field);
  143. }
  144. }
  145. const whereString = whereArr.join(' AND ');
  146. this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;
  147. }
  148. /**
  149. * orWhere设置
  150. *
  151. * @return {void}
  152. */
  153. _orWhereBuild() {
  154. if (this.orWhere.length <= 0) {
  155. return;
  156. }
  157. const whereArr = [];
  158. for (const where in this.orWhere) {
  159. whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
  160. this.sqlParam.push(where.field);
  161. }
  162. const whereString = whereArr.join(' OR ');
  163. // 如果前面已经有设置过WHERE则不需要再重复添加
  164. this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;
  165. }
  166. /**
  167. * limit设置
  168. *
  169. * @return {void}
  170. */
  171. _limitBuild() {
  172. if (typeof this.limit !== 'number' || this.limit <= 0) {
  173. return;
  174. }
  175. this.offset = parseInt(this.offset);
  176. this.offset = isNaN(this.offset) || this.offset < 0 ? 0 : this.offset;
  177. const limitString = this.offset >= 0 ? this.offset + ',' + this.limit : this.limit;
  178. this.sql += ' LIMIT ' + limitString;
  179. }
  180. /**
  181. * orderby 设置
  182. *
  183. * @return {void}
  184. */
  185. _orderByBuild() {
  186. if (this.orderBy.length <= 0) {
  187. return;
  188. }
  189. const orderArr = [];
  190. for (const index in this.orderBy) {
  191. orderArr.push(' ?? ' + this.orderBy[index][1]);
  192. this.sqlParam.push(this.orderBy[index][0]);
  193. }
  194. const orderByString = orderArr.join(',');
  195. this.sql += ' ORDER BY ' + orderByString;
  196. }
  197. /**
  198. * 构建sql
  199. *
  200. * @param {String} tableName - 表名
  201. * @param {String} type - 类型
  202. * @return {Array} - 返回数组,第一个元素为sql语句,第二个元素为sql中问号部分的param
  203. */
  204. build(tableName, type = 'select') {
  205. this.type = type;
  206. this._typeBuild(tableName);
  207. if (this.sql === '') {
  208. throw '类型错误';
  209. }
  210. this._setDataBuild();
  211. this._andWhereBuild();
  212. this._orWhereBuild();
  213. this._orderByBuild();
  214. this._limitBuild();
  215. const sql = this.sql;
  216. const sqlParam = this.sqlParam;
  217. // 重置数据
  218. this.resetCondition();
  219. return [sql, sqlParam];
  220. }
  221. }
  222. module.exports = SqlBuilder;