1. module.exports = {
    2. /**
    3. * wen
    4. * sql 拼接
    5. * @param {*} object 数据对象需要是map对象
    6. * @param {*} sqlMap map对象对于的sql对象
    7. * @param {*} sql sql查询语句用来判断是否加where可不传,不传默认需拼接的sql带where,所有条件都会加 and
    8. * 拼接时变量会用?进行站位 返回一个sql 字符串,和param数组
    9. * object 需要家$符,$前部分为key和sqlMapkey要一样,$后半部分为对比条件,比如
    10. * = :=
    11. * !=: !=
    12. * l : like
    13. * nl : not like
    14. * i : in
    15. * ni : not in
    16. * b : between
    17. * nb : not between
    18. * f : FIND_IN_SET
    19. * nf : not FIND_IN_SET
    20. * >:?
    21. * <:<
    22. * >=:>=
    23. * <=:<=
    24. */
    25. sqlAppend(object, sqlMap, sql) {
    26. const result = {
    27. sql: '',
    28. param: []
    29. };
    30. let check = true;
    31. if (!sql) {
    32. check = false;
    33. }
    34. let addSql = '';
    35. try {
    36. if (object && sqlMap && typeof object === 'object' && typeof sqlMap === 'object') {
    37. const keys = Object.keys(object);
    38. keys.forEach(item => {
    39. // 有包含$才进行处理
    40. if (item.includes('$')) {
    41. const mapKeys = item.split('$');
    42. // $拆分后有两个数据,并且sqlmap有对应的数据,boject的数据不为空
    43. if (mapKeys.length == 2 && object[item] && sqlMap[mapKeys[0]]) {
    44. switch (mapKeys[1]) {
    45. case '=':
    46. addSql += ` ${sqlMap[mapKeys[0]]} = ? `;
    47. result.param.push(object[item]);
    48. break;
    49. case '!=':
    50. addSql += ` ${sqlMap[mapKeys[0]]} != ? `;
    51. result.param.push(object[item]);
    52. break;
    53. case '<':
    54. addSql += ` ${sqlMap[mapKeys[0]]} < ? `;
    55. result.param.push(object[item]);
    56. break;
    57. case '>':
    58. addSql += ` ${sqlMap[mapKeys[0]]} > ? `;
    59. result.param.push(object[item]);
    60. break;
    61. case '<=':
    62. addSql += ` ${sqlMap[mapKeys[0]]} <= ? `;
    63. result.param.push(object[item]);
    64. break;
    65. case '>=':
    66. addSql += ` ${sqlMap[mapKeys[0]]} >= ? `;
    67. result.param.push(object[item]);
    68. break;
    69. case 'l':
    70. addSql += ` ${sqlMap[mapKeys[0]]} like ? `;
    71. result.param.push(`%${object[item]}%`);
    72. break;
    73. case 'nl':
    74. addSql += ` ${sqlMap[mapKeys[0]]} not like ? `;
    75. result.param.push(`%${object[item]}%`);
    76. break;
    77. case 'i':
    78. addSql += ` ${sqlMap[mapKeys[0]]} in (?) `;
    79. result.param.push(object[item]);
    80. break;
    81. case 'ni':
    82. addSql += ` ${sqlMap[mapKeys[0]]} not in (?) `;
    83. result.param.push(object[item]);
    84. break;
    85. case 'b':
    86. if (object[item].length > 1 && object[item][0] && object[item][1]) {
    87. addSql += ` ${sqlMap[mapKeys[0]]} between ? and ? `;
    88. result.param.push(object[item][0], object[item][1]);
    89. }
    90. break;
    91. case 'nb':
    92. if (object[item].length > 1 && object[item][0] && object[item][1]) {
    93. addSql += ` ${sqlMap[mapKeys[0]]} not between ? and ? `;
    94. result.param.push(object[item][0], object[item][1]);
    95. }
    96. break;
    97. case 'f':
    98. addSql += ` find_in_set(${sqlMap[mapKeys[0]]} ?) `;
    99. result.param.push(object[item]);
    100. break;
    101. case 'nf':
    102. addSql += ` not find_in_set(${sqlMap[mapKeys[0]]} ?) `;
    103. result.param.push(object[item]);
    104. break;
    105. default:
    106. break;
    107. }
    108. if (check && addSql && !sql.includes('where') && !sql.includes('WHERE')) {
    109. check = false;
    110. result.sql += ` where ${addSql}`;
    111. } else if (addSql) {
    112. result.sql += ` and ${addSql}`;
    113. }
    114. addSql = '';
    115. }
    116. }
    117. });
    118. }
    119. return result;
    120. } catch (error) {
    121. console.log('拼接sql产生错误', error);
    122. return {
    123. sql: '',
    124. param: []
    125. };
    126. }
    127. },
    128. /**
    129. * 分页
    130. * @param {*} page
    131. * @param {*} pageSize
    132. */
    133. sqlLimit(page, pageSize) {
    134. let sql = '';
    135. if (page && pageSize && !isNaN(page) && !isNaN(pageSize)) {
    136. sql += ` limit ${(Number(page) - 1) * pageSize},${pageSize}`;
    137. }
    138. return sql;
    139. }
    140. };

    调用例子

    1. let sql =
    2. ' SELECT t1.*,t1.start_time*1000 as start_time,t1.end_time*1000 as end_time FROM task t1 ';
    3. let object = {
    4. 'product_id$=': param.product_id,
    5. 'group_id$=': param.group_id,
    6. 'executors$=': param.executors,
    7. start_time$b: param.time,
    8. ' status$i': [1, 2]
    9. },
    10. sqlMap = {
    11. product_id: 't1.product_id',
    12. group_id: 't1.group_id',
    13. executors: 't1.executors',
    14. start_time: 't1.start_time',
    15. status: 't1.status'
    16. };
    17. const sqlResuld = sqlAppend(object, sqlMap, sql);
    18. sql += sqlResult.sql
    19. await models.sequelize.query(sql, {
    20. replacements: sqlResult.param,
    21. type: models.SELECT
    22. });