sql_builder.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
  116. this.sqlParam.push(where.field);
  117. }
  118. const whereString = whereArr.join(' AND ');
  119. this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;
  120. }
  121. /**
  122. * orWhere设置
  123. *
  124. * @return {void}
  125. */
  126. _orWhereBuild() {
  127. if (this.orWhere.length <= 0) {
  128. return;
  129. }
  130. const whereArr = [];
  131. for (const where in this.orWhere) {
  132. whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
  133. this.sqlParam.push(where.field);
  134. }
  135. const whereString = whereArr.join(' OR ');
  136. // 如果前面已经有设置过WHERE则不需要再重复添加
  137. this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;
  138. }
  139. /**
  140. * limit设置
  141. *
  142. * @return {void}
  143. */
  144. _limitBuild() {
  145. if (typeof this.limit !== 'number' || this.limit <= 0) {
  146. return;
  147. }
  148. this.offset = parseInt(this.offset);
  149. this.offset = isNaN(this.offset) || this.offset < 0 ? 0 : this.offset;
  150. const limitString = this.offset >= 0 ? this.offset + ',' + this.limit : this.limit;
  151. this.sql += ' LIMIT ' + limitString;
  152. }
  153. /**
  154. * orderby 设置
  155. *
  156. * @return {void}
  157. */
  158. _orderByBuild() {
  159. if (this.orderBy.length <= 0) {
  160. return;
  161. }
  162. const orderArr = [];
  163. for (const index in this.orderBy) {
  164. orderArr.push(' ?? ' + this.orderBy[index][1]);
  165. this.sqlParam.push(this.orderBy[index][0]);
  166. }
  167. const orderByString = orderArr.join(',');
  168. this.sql += ' ORDER BY ' + orderByString;
  169. }
  170. /**
  171. * 构建sql
  172. *
  173. * @param {String} tableName - 表名
  174. * @param {String} type - 类型
  175. * @return {Array} - 返回数组,第一个元素为sql语句,第二个元素为sql中问号部分的param
  176. */
  177. build(tableName, type = 'select') {
  178. this.type = type;
  179. this._typeBuild(tableName);
  180. if (this.sql === '') {
  181. throw '类型错误';
  182. }
  183. this._setDataBuild();
  184. this._andWhereBuild();
  185. this._orWhereBuild();
  186. this._limitBuild();
  187. this._orderByBuild();
  188. const sql = this.sql;
  189. const sqlParam = this.sqlParam;
  190. // 重置数据
  191. this.resetCondition();
  192. return [sql, sqlParam];
  193. }
  194. }
  195. module.exports = SqlBuilder;