• 前端sql条件拼接js工具


    因为项目原因,需要前端写sql,所以弄了一套sql条件拼接的js工具

    1. /*常量
    2. LT : " < ",
    3. LE : " <= ",
    4. GT : " > ",
    5. GE : " >= ",
    6. NE : " != ",
    7. EQ : " = ",
    8. LIKE : " like ",
    9. OR : " or ",
    10. IN : " in ",
    11. */
    12. let sqlUtil = {
    13. // 拼接sql
    14. buildSql : '',
    15. /*函数*/
    16. lt: lessThan,
    17. lte: lessThanOrEqual,
    18. gt: greaterThan,
    19. gte: greaterThanOrEqual,
    20. ne: notEqual,
    21. eq: equal,
    22. toLike: sqlLike,
    23. leftLike:leftLike,
    24. rightLike:rightLike,
    25. in: sqlIn,
    26. notIn: notIn,
    27. isNull: isNull,
    28. isNotNull: isNotNull,
    29. isEmpty: isEmpty,
    30. isEmptyOrNull: isEmptyOrNull,
    31. isNotEmpty:isNotEmpty,
    32. isNotEmptyAndNotNull:isNotEmptyAndNotNull,
    33. sor: simpleOr,
    34. toBetween: between,
    35. notBetween : notBetween,
    36. sqlOr: sqlOr,
    37. orderByDesc : orderByDesc,
    38. orderByAsc: orderByAsc,
    39. orderBy: orderBy,
    40. notDelete : notDelete,
    41. userDefined:userDefined,
    42. toSql: toSql
    43. };
    44. /**
    45. * 小于 <
    46. * sqlUtil.lt("field_name",fieldValue)
    47. * @param fieldName 字段名
    48. * @param fieldValue 字段值
    49. * @param condition 判断条件是否成立
    50. * @returns {{sqlUtil}} 拼接后的sqlUtil
    51. */
    52. function lessThan(fieldName,fieldValue,condition = true){
    53. if(!condition){
    54. return this;
    55. }
    56. if(fieldValue && fieldValue.trim() !== ''){
    57. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" < '"+fieldValue+"' ";
    58. }
    59. return this;
    60. }
    61. /**
    62. * 小于等于 <=
    63. * sqlUtil.le("field_name",fieldValue)
    64. * @param fieldName 字段名
    65. * @param fieldValue 字段值
    66. * @param condition 判断条件是否成立
    67. * @returns {{sqlUtil}} 拼接后的sqlUtil
    68. */
    69. function lessThanOrEqual(fieldName,fieldValue,condition = true){
    70. if(!condition){
    71. return this;
    72. }
    73. if(fieldValue && fieldValue.trim() !== ''){
    74. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" <= '"+fieldValue+"' ";
    75. }
    76. return this;
    77. }
    78. /**
    79. * 大于 >
    80. * sqlUtil.gt("field_name",fieldValue)
    81. * @param fieldName 字段名
    82. * @param fieldValue 字段值
    83. * @param condition 判断条件是否成立
    84. * @returns {{sqlUtil}} 拼接后的sqlUtil
    85. */
    86. function greaterThan(fieldName,fieldValue,condition = true){
    87. if(!condition){
    88. return this;
    89. }
    90. if(fieldValue && fieldValue.trim() !== ''){
    91. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" > '"+fieldValue+"' ";
    92. }
    93. return this;
    94. }
    95. /**
    96. * 大于等于 >=
    97. * sqlUtil.ge("field_name",fieldValue)
    98. * @param fieldName 字段名
    99. * @param fieldValue 字段值
    100. * @param condition 判断条件是否成立
    101. * @returns {{sqlUtil}} 拼接后的sqlUtil
    102. */
    103. function greaterThanOrEqual(fieldName,fieldValue,condition = true){
    104. if(!condition){
    105. return this;
    106. }
    107. if(fieldValue && fieldValue.trim() !== ''){
    108. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" >= '"+fieldValue+"' ";
    109. }
    110. return this;
    111. }
    112. /**
    113. * 不等于 !=
    114. * sqlUtil.ne("field_name",fieldValue)
    115. * @param fieldName 字段名
    116. * @param fieldValue 字段值
    117. * @param condition 判断条件是否成立
    118. * @returns {{sqlUtil}} 拼接后的sqlUtil
    119. */
    120. function notEqual(fieldName,fieldValue,condition = true){
    121. if(!condition){
    122. return this;
    123. }
    124. if(fieldValue && fieldValue.trim() !== ''){
    125. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" != '"+fieldValue+"' ";
    126. }
    127. return this;
    128. }
    129. /**
    130. * 等于 =
    131. * sqlUtil.eq("field_name",fieldValue)
    132. * @param fieldName 字段名
    133. * @param fieldValue 字段值
    134. * @param condition 判断条件是否成立
    135. * @returns {{sqlUtil}} 拼接后的sqlUtil
    136. */
    137. function equal(fieldName,fieldValue,condition = true){
    138. if(!condition){
    139. return this;
    140. }
    141. if(!fieldValue){
    142. return this;
    143. }
    144. if(typeof fieldValue === 'string' && fieldValue.trim() === ''){
    145. return this;
    146. }
    147. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" = '"+fieldValue+"' ";
    148. return this;
    149. }
    150. /**
    151. * 模糊搜索 like
    152. * sqlUtil.like("field_name",fieldValue)
    153. * @param fieldName 字段名
    154. * @param fieldValue 字段值
    155. * @param condition 判断条件是否成立
    156. * @returns {{sqlUtil}} 拼接后的sqlUtil
    157. */
    158. function sqlLike(fieldName,fieldValue,condition = true){
    159. if(!condition){
    160. return this;
    161. }
    162. if(fieldValue && fieldValue.trim() !== ''){
    163. fieldValue = fieldValue.replaceAll('%','\\%');
    164. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" like '%"+fieldValue+"%' ";
    165. }
    166. return this;
    167. }
    168. /**
    169. * 模糊搜索 左like
    170. * sqlUtil.leftLike("field_name",fieldValue)
    171. * @param fieldName 字段名
    172. * @param fieldValue 字段值
    173. * @param condition 判断条件是否成立
    174. * @returns {{sqlUtil}} 拼接后的sqlUtil
    175. */
    176. function leftLike(fieldName,fieldValue,condition = true){
    177. if(!condition){
    178. return this;
    179. }
    180. if(fieldValue && fieldValue.trim() !== ''){
    181. fieldValue = fieldValue.replaceAll('%','\\%');
    182. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" like '%"+fieldValue+"' ";
    183. }
    184. return this;
    185. }
    186. /**
    187. * 模糊搜索 右like
    188. * sqlUtil.rightLike("field_name",fieldValue)
    189. * @param fieldName 字段名
    190. * @param fieldValue 字段值
    191. * @param condition 判断条件是否成立
    192. * @returns {{sqlUtil}} 拼接后的sqlUtil
    193. */
    194. function rightLike(fieldName,fieldValue,condition = true){
    195. if(!condition){
    196. return this;
    197. }
    198. if(fieldValue && fieldValue.trim() !== ''){
    199. fieldValue = fieldValue.replaceAll('%','\\%');
    200. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" like '"+fieldValue+"%' ";
    201. }
    202. return this;
    203. }
    204. /**
    205. * in查询
    206. * sqlUtil.in("field_name",fieldValueArray)
    207. * @param fieldName 字段名
    208. * @param fieldValueArray 字段值数组
    209. * @param condition 判断条件是否成立
    210. * @returns {{sqlUtil}} 拼接后的sqlUtil
    211. */
    212. function sqlIn(fieldName,fieldValueArray = [],condition = true){
    213. if(!condition){
    214. return this;
    215. }
    216. if (fieldValueArray.length === 0){
    217. return this;
    218. }
    219. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" in ( ";
    220. for (const fieldValue of fieldValueArray) {
    221. if(typeof fieldValue === "string"){
    222. sqlUtil.buildSql = sqlUtil.buildSql + "'" + fieldValue + "',";
    223. }else{
    224. sqlUtil.buildSql = sqlUtil.buildSql + fieldValue + ",";
    225. }
    226. }
    227. sqlUtil.buildSql = sqlUtil.buildSql.substring(0,sqlUtil.buildSql.length-1) + ") ";
    228. return this;
    229. }
    230. /**
    231. * not in查询
    232. * sqlUtil.notIn("field_name",fieldValueArray)
    233. * @param fieldName 字段名
    234. * @param fieldValueArray 字段值数组
    235. * @param condition 判断条件是否成立
    236. * @returns {{sqlUtil}} 拼接后的sqlUtil
    237. */
    238. function notIn(fieldName,fieldValueArray = [],condition = true){
    239. if(!condition){
    240. return this;
    241. }
    242. if (fieldValueArray.length === 0){
    243. return this;
    244. }
    245. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" not in ( ";
    246. for (const fieldValue of fieldValueArray) {
    247. sqlUtil.buildSql = sqlUtil.buildSql + fieldValue + " ,";
    248. }
    249. sqlUtil.buildSql = sqlUtil.buildSql.substring(0,sqlUtil.buildSql.length-1) + " ) ";
    250. return this;
    251. }
    252. /**
    253. * is null
    254. * sqlUtil.isNull("field_name",fieldValue)
    255. * @param fieldName 字段名
    256. * @param fieldValue 字段值
    257. * @param condition 判断条件是否成立
    258. * @returns {{sqlUtil}} 拼接后的sqlUtil
    259. */
    260. function isNull(fieldName,fieldValue,condition = true){
    261. if(!condition){
    262. return this;
    263. }
    264. if(fieldValue && fieldValue.trim() !== ''){
    265. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" is null '"+fieldValue+"%' ";
    266. }
    267. return this;
    268. }
    269. /**
    270. * is not null
    271. * sqlUtil.isNotNull("field_name",fieldValue)
    272. * @param fieldName 字段名
    273. * @param fieldValue 字段值
    274. * @param condition 判断条件是否成立
    275. * @returns {{sqlUtil}} 拼接后的sqlUtil
    276. */
    277. function isNotNull(fieldName,fieldValue,condition = true){
    278. if(!condition){
    279. return this;
    280. }
    281. if(fieldValue && fieldValue.trim() !== ''){
    282. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" is not null '"+fieldValue+"%' ";
    283. }
    284. return this;
    285. }
    286. /**
    287. * 是否为空字符
    288. * sqlUtil.isEmpty("field_name",fieldValue)
    289. * @param fieldName 字段名
    290. * @param condition 判断条件是否成立
    291. * @returns {{sqlUtil}} 拼接后的sqlUtil
    292. */
    293. function isEmpty(fieldName,condition = true){
    294. if(!condition){
    295. return this;
    296. }
    297. if(fieldName){
    298. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" = '' ";
    299. }
    300. return this;
    301. }
    302. /**
    303. * 是否为 空字符或null
    304. * sqlUtil.isEmpty("field_name",fieldValue)
    305. * @param fieldName 字段名
    306. * @param condition 判断条件是否成立
    307. * @returns {{sqlUtil}} 拼接后的sqlUtil
    308. */
    309. function isEmptyOrNull(fieldName,condition = true){
    310. if(!condition){
    311. return this;
    312. }
    313. if(fieldName){
    314. sqlUtil.buildSql = sqlUtil.buildSql + "and ("+fieldName+" = '' or "+fieldName+" is null)";
    315. }
    316. return this;
    317. }
    318. /**
    319. * 是否为空字符
    320. * sqlUtil.isNotEmpty("field_name",fieldValue)
    321. * @param fieldName 字段名
    322. * @param condition 判断条件是否成立
    323. * @returns {{sqlUtil}} 拼接后的sqlUtil
    324. */
    325. function isNotEmpty(fieldName,condition = true){
    326. if(!condition){
    327. return this;
    328. }
    329. if(fieldName){
    330. sqlUtil.buildSql = sqlUtil.buildSql + "and "+fieldName+" != '' ";
    331. }
    332. return this;
    333. }
    334. /**
    335. * 是否为空字符
    336. * sqlUtil.isNotEmpty("field_name",fieldValue)
    337. * @param fieldName 字段名
    338. * @param condition 判断条件是否成立
    339. * @returns {{sqlUtil}} 拼接后的sqlUtil
    340. */
    341. function isNotEmptyAndNotNull(fieldName,condition = true){
    342. if(!condition){
    343. return this;
    344. }
    345. if(fieldName){
    346. sqlUtil.buildSql = sqlUtil.buildSql + "and ("+fieldName+" != '' or "+fieldName+" is not null)";
    347. }
    348. return this;
    349. }
    350. /**
    351. * between("age", 20, 22);
    352. * @param fieldName 字段名
    353. * @param fieldValue1 第一个值
    354. * @param fieldValue2 第二个值
    355. * @param condition 判断条件是否成立
    356. * @returns {{sqlUtil}} 拼接后的sqlUtil
    357. */
    358. function between(fieldName,fieldValue1,fieldValue2,condition = true){
    359. if(!condition){
    360. return this;
    361. }
    362. if(!fieldValue1 || !fieldValue2){
    363. return this;
    364. }
    365. if(typeof fieldValue1 === 'string' && typeof fieldValue2 === 'string'){
    366. // 有可能是日期
    367. sqlUtil.buildSql = sqlUtil.buildSql + "and '"+fieldName+"' between '"+fieldValue1+"' and '"+fieldValue2+"' ";
    368. }else{
    369. sqlUtil.buildSql = sqlUtil.buildSql + "and '"+fieldName+"' between "+fieldValue1+" and "+fieldValue2+" ";
    370. }
    371. return this;
    372. }
    373. /**
    374. * sqlUtil.notBetween("field_name",20,22)
    375. * notBetween("age", 20, 22);
    376. * @param fieldName 字段名
    377. * @param fieldValue1 第一个值
    378. * @param fieldValue2 第二个值
    379. * @param condition 判断条件是否成立
    380. * @returns {{sqlUtil}} 拼接后的sqlUtil
    381. */
    382. function notBetween(fieldName,fieldValue1,fieldValue2,condition = true){
    383. if(!condition){
    384. return this;
    385. }
    386. if(fieldName){
    387. sqlUtil.buildSql = sqlUtil.buildSql + "and '"+fieldName+"' not between "+fieldValue1+" and "+fieldValue2+" ";
    388. }
    389. return this;
    390. }
    391. /**
    392. * sqlUtil.orderByAsc("field_name") || sqlUtil.orderByAsc(["field_name1","field_name2"])
    393. * "order by id asc"
    394. * @param fieldName 字段名 || 数组
    395. * @param condition 判断条件是否成立
    396. * @returns {{sqlUtil}} 拼接后的sqlUtil
    397. */
    398. function orderByAsc(fieldName,condition = true){
    399. if(!condition){
    400. return this;
    401. }
    402. if(typeof fieldName === 'string'){
    403. sqlUtil.buildSql = sqlUtil.buildSql +" order by "+fieldName+" asc"
    404. }else{
    405. sqlUtil.buildSql = sqlUtil.buildSql +" order by ";
    406. for (const itemName of fieldName) {
    407. sqlUtil.buildSql = sqlUtil.buildSql + itemName + " asc,";
    408. }
    409. sqlUtil.buildSql = sqlUtil.buildSql.substring(0,sqlUtil.buildSql.length-1) + " ";
    410. }
    411. return this;
    412. }
    413. /**
    414. * sqlUtil.orderByDesc("field_name") || sqlUtil.orderByDesc(["field_name1","field_name2"])
    415. * "order by id desc"
    416. * @param fieldName 字段名 || 数组
    417. * @param condition 判断条件是否成立
    418. * @returns {{sqlUtil}} 拼接后的sqlUtil
    419. */
    420. function orderByDesc(fieldName,condition = true){
    421. if(!condition){
    422. return this;
    423. }
    424. if(typeof fieldName === 'string'){
    425. sqlUtil.buildSql = sqlUtil.buildSql +" order by "+fieldName+" desc"
    426. }else{
    427. sqlUtil.buildSql = sqlUtil.buildSql +" order by ";
    428. for (const itemName of fieldName) {
    429. sqlUtil.buildSql = sqlUtil.buildSql + itemName + " desc,";
    430. }
    431. sqlUtil.buildSql = sqlUtil.buildSql.substring(0,sqlUtil.buildSql.length-1) + " ";
    432. }
    433. return this;
    434. }
    435. /**
    436. * 自定义排序
    437. * @param fieldList 字段名或数组[{fileName:'',order:''}],order的值为 desc|asc
    438. * @param condition
    439. * @returns {orderBy}
    440. */
    441. function orderBy(fieldList,condition = true){
    442. if(!condition){
    443. return this;
    444. }
    445. sqlUtil.buildSql = sqlUtil.buildSql +" order by ";
    446. for (const item of fieldList) {
    447. sqlUtil.buildSql = sqlUtil.buildSql + item.fileName +" "+item.order +",";
    448. }
    449. sqlUtil.buildSql = sqlUtil.buildSql.substring(0,sqlUtil.buildSql.length-1) + " ";
    450. return this;
    451. }
    452. /**
    453. * 简单的或条件 or
    454. * const paramMap = new Map([
    455. * ["field_name1",fieldValue1],
    456. * ["field_name2",fieldValue2],
    457. * ["field_name3",fieldValue3],
    458. * ])
    459. *
    460. * sqlUtil.sor(paramMap)
    461. * @param paramMap 参数集
    462. *
    463. * @param condition
    464. * @returns {{sqlUtil}} 拼接后的sqlUtil
    465. */
    466. function simpleOr(paramMap,condition = true){
    467. if(!condition){
    468. return this;
    469. }
    470. if(! paramMap){
    471. return this;
    472. }
    473. sqlUtil.buildSql += "and ( ";
    474. for(let [index,field] of paramMap.entries()){
    475. sqlUtil.buildSql = sqlUtil.buildSql + field[0]+" = '"+field[1]+"' or ";
    476. }
    477. sqlUtil.buildSql = sqlUtil.buildSql.substring(0, sqlUtil.buildSql.length - 4) + ") ";
    478. return this;
    479. }
    480. /**
    481. * 或条件 or
    482. *
    483. * sqlUtil.OR(paramMap)
    484. * @param paramSqlList sql集
    485. * @param condition
    486. * @returns {{sqlUtil}} 拼接后的sqlUtil
    487. */
    488. function sqlOr(paramSqlList= [],condition = true){
    489. if(!condition){
    490. return this;
    491. }
    492. if(paramSqlList.length === 0){
    493. return this;
    494. }
    495. sqlUtil.buildSql = sqlUtil.buildSql +" and (";
    496. for (const paramSql of paramSqlList) {
    497. sqlUtil.buildSql = sqlUtil.buildSql + " or "
    498. }
    499. sqlUtil.buildSql = sqlUtil.buildSql.substring(0,sqlUtil.buildSql.length - 3) + ") "
    500. return this;
    501. }
    502. /**
    503. * 查询没有被逻辑删除的
    504. * @returns {{sqlUtil}} 拼接后的sqlUtil
    505. */
    506. function notDelete(){
    507. sqlUtil.buildSql += "and is_delete = '0' "
    508. return this;
    509. }
    510. /**
    511. * 自定义sql,
    512. * @param paramSql 'and {condition}'
    513. * @param condition 条件
    514. */
    515. function userDefined(paramSql,condition = true){
    516. if(!condition){
    517. return this;
    518. }
    519. sqlUtil.buildSql += paramSql;
    520. return this;
    521. }
    522. /**
    523. * 获取拼装后的sql
    524. * @returns {string}
    525. */
    526. function toSql(){
    527. let sql = '';
    528. //判断有没有orderBy
    529. if(sqlUtil.buildSql.indexOf("order by") != -1){
    530. let arr = sqlUtil.buildSql.split("order by");
    531. sql = arr[0] + " and is_delete = '0' "+"order by"+arr[1];
    532. }else{
    533. sql = sqlUtil.notDelete()["buildSql"];
    534. }
    535. // sqlUtil.notDelete().buildSql
    536. sqlUtil.buildSql = '';
    537. console.log("sql:"+sql);
    538. return sql;
    539. }
    540. export default sqlUtil;

    或许某些场景,例如用户自定义条件的场景才用得到吧?这篇文章如能提供到帮助,方便的话请在评论下留言,我也想知道还能用到什么地方。

    使用方式参考如下(使用userDefined方法可以利用sql函数,并进行条件拼接):

    1. return sqlUtil
    2. .toLike("discussion_title",searchParam.discussionTitle)
    3. .eq("division_code",searchParam.divisionCode)
    4. .eq("party_grid_deliberation_id",searchParam.partyGridDeliberationId)
    5. .userDefined(" and (DATE_FORMAT(discussion_time_start, '%Y-%m') = '"+this.searchParam.discussionYearMonth+"' or DATE_FORMAT(discussion_time_end, '%Y-%m') = '"+this.searchParam.discussionYearMonth+"') ",this.searchParam.discussionYearMonth)
    6. .orderByDesc("discussion_time_start")
    7. .toSql();

    另外进阶版,可以结合jq的extend,进一步封装,像后端开发那样,把业务代码写到前端来。如图:

    fwptPost(表名,提交的json对象)。

    这样做当然是有缺陷,需要解决事务一致性的问题,要考虑如何联表,要考虑性能。

  • 相关阅读:
    l8-d15 IO多路复用select函数
    前端算法:链表,逆置,递归的写法
    《创业者必学的搞流量营销课》负责百万到年入千万,500W+粉丝操盘经验
    EMQX +计算巢:构建云上物联网平台,轻松实现百万级设备连接
    如何定位el-tree中的树节点当父元素滚动时如何定位子元素
    微服务链路追踪-SkyWalking
    【简单dp】舔狗舔到最后一无所有
    【小程序】使用WXSS编写样式介绍以及与CSS的区别
    Java集合01:
    Maven依赖仲裁
  • 原文地址:https://blog.csdn.net/hunwanjie/article/details/136306232