sql_builder.js 6.0 KB

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