sql_builder.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. const tmp = set.data.selfOperate !== undefined ?
  97. ' ?? = ?? ' + set.data.selfOperate + ' ' + set.data.value : ' ?? = ' + set.data.value;
  98. setDataArr.push(tmp);
  99. // 如果是自身操作则压多一次字段进数组
  100. if (set.data.selfOperate !== undefined) {
  101. this.sqlParam.push(set.field);
  102. }
  103. this.sqlParam.push(set.field);
  104. }
  105. const setString = setDataArr.join(',');
  106. this.sql += setString;
  107. }
  108. /**
  109. * andWhere设置
  110. *
  111. * @return {void}
  112. */
  113. _andWhereBuild() {
  114. if (this.andWhere.length <= 0) {
  115. return;
  116. }
  117. const whereArr = [];
  118. for (const where of this.andWhere) {
  119. if (where.data.operate === 'in') {
  120. // in操作
  121. const valueLength = where.data.value instanceof Array ? where.data.value.length : 1;
  122. // 生成参数集
  123. const inArr = [];
  124. for (let i = 0; i < valueLength; i++) {
  125. inArr.push('?');
  126. }
  127. const inData = inArr.join(',');
  128. whereArr.push(' ?? IN (' + inData + ')');
  129. this.sqlParam.push(where.field);
  130. // 赋值参数
  131. this.sqlParam.push.apply(this.sqlParam, where.data.value);
  132. } else {
  133. // 普通操作
  134. whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
  135. this.sqlParam.push(where.field);
  136. }
  137. }
  138. const whereString = whereArr.join(' AND ');
  139. this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;
  140. }
  141. /**
  142. * orWhere设置
  143. *
  144. * @return {void}
  145. */
  146. _orWhereBuild() {
  147. if (this.orWhere.length <= 0) {
  148. return;
  149. }
  150. const whereArr = [];
  151. for (const where in this.orWhere) {
  152. whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
  153. this.sqlParam.push(where.field);
  154. }
  155. const whereString = whereArr.join(' OR ');
  156. // 如果前面已经有设置过WHERE则不需要再重复添加
  157. this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;
  158. }
  159. /**
  160. * limit设置
  161. *
  162. * @return {void}
  163. */
  164. _limitBuild() {
  165. if (typeof this.limit !== 'number' || this.limit <= 0) {
  166. return;
  167. }
  168. this.offset = parseInt(this.offset);
  169. this.offset = isNaN(this.offset) || this.offset < 0 ? 0 : this.offset;
  170. const limitString = this.offset >= 0 ? this.offset + ',' + this.limit : this.limit;
  171. this.sql += ' LIMIT ' + limitString;
  172. }
  173. /**
  174. * orderby 设置
  175. *
  176. * @return {void}
  177. */
  178. _orderByBuild() {
  179. if (this.orderBy.length <= 0) {
  180. return;
  181. }
  182. const orderArr = [];
  183. for (const index in this.orderBy) {
  184. orderArr.push(' ?? ' + this.orderBy[index][1]);
  185. this.sqlParam.push(this.orderBy[index][0]);
  186. }
  187. const orderByString = orderArr.join(',');
  188. this.sql += ' ORDER BY ' + orderByString;
  189. }
  190. /**
  191. * 构建sql
  192. *
  193. * @param {String} tableName - 表名
  194. * @param {String} type - 类型
  195. * @return {Array} - 返回数组,第一个元素为sql语句,第二个元素为sql中问号部分的param
  196. */
  197. build(tableName, type = 'select') {
  198. this.type = type;
  199. this._typeBuild(tableName);
  200. if (this.sql === '') {
  201. throw '类型错误';
  202. }
  203. this._setDataBuild();
  204. this._andWhereBuild();
  205. this._orWhereBuild();
  206. this._limitBuild();
  207. this._orderByBuild();
  208. const sql = this.sql;
  209. const sqlParam = this.sqlParam;
  210. // 重置数据
  211. this.resetCondition();
  212. return [sql, sqlParam];
  213. }
  214. }
  215. module.exports = SqlBuilder;