@@ -309,4 +309,196 @@ describe('builder test case', () => {
309
309
} ;
310
310
expect ( ( new Builder ( options ) ) . sql ) . to . be . equal ( 'SELECT * FROM `table1` AS `t1` LEFT JOIN `table2` AS `t2` ON `t1`.`id` = `t2`.`t1_id`' ) ;
311
311
} ) ;
312
+
313
+ it ( 'build with join using subquery' , ( ) => {
314
+ const subQuery = new Query ( 'select' ) ;
315
+ subQuery . table ( 'table3' , 't3' ) . where ( 't3.status' , 1 ) ;
316
+
317
+ const options = {
318
+ sql : '' ,
319
+ values : [ ] ,
320
+ conditions : [ ] ,
321
+ orders : [ ] ,
322
+ tables : [ { table : 'table1' , alias : 't1' } ] ,
323
+ operator : 'select' ,
324
+ data : null ,
325
+ groupField : [ ] ,
326
+ having : [ ] ,
327
+ joins : [
328
+ {
329
+ table : subQuery ,
330
+ alias : 't2' ,
331
+ self_column : 't1.id' ,
332
+ foreign_column : 't2.t1_id' ,
333
+ join_type : 'left'
334
+ }
335
+ ]
336
+ } ;
337
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
338
+ 'SELECT * FROM `table1` AS `t1` LEFT JOIN (SELECT * FROM `table3` AS `t3` WHERE `t3`.`status` = ?) AS `t2` ON `t1`.`id` = `t2`.`t1_id`'
339
+ ) ;
340
+ } ) ;
341
+
342
+ it ( 'test insert operation' , ( ) => {
343
+ const options = {
344
+ sql : '' ,
345
+ values : [ ] ,
346
+ conditions : [ ] ,
347
+ tables : [ { table : 'table1' } ] ,
348
+ operator : 'insert' ,
349
+ data : { name : 'test' , age : 18 }
350
+ } ;
351
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
352
+ 'INSERT INTO `table1`(`name`,`age`) VALUES (?,?)'
353
+ ) ;
354
+ } ) ;
355
+
356
+ it ( 'test batch insert operation' , ( ) => {
357
+ const options = {
358
+ sql : '' ,
359
+ values : [ ] ,
360
+ conditions : [ ] ,
361
+ tables : [ { table : 'table1' } ] ,
362
+ operator : 'insert' ,
363
+ data : [
364
+ { name : 'test1' , age : 18 } ,
365
+ { name : 'test2' , age : 20 }
366
+ ]
367
+ } ;
368
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
369
+ 'INSERT INTO `table1`(`name`,`age`) VALUES (?,?),(?,?)'
370
+ ) ;
371
+ } ) ;
372
+
373
+ it ( 'test insert with on duplicate key update' , ( ) => {
374
+ const options = {
375
+ sql : '' ,
376
+ values : [ ] ,
377
+ conditions : [ ] ,
378
+ tables : [ { table : 'table1' } ] ,
379
+ operator : 'insert' ,
380
+ data : { id : 1 , name : 'test' , age : 18 } ,
381
+ keys : [ 'id' ]
382
+ } ;
383
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
384
+ 'INSERT INTO `table1`(`id`,`name`,`age`) VALUES (?,?,?) ON DUPLICATE KEY UPDATE `name` = VALUES(`name`),`age` = VALUES(`age`)'
385
+ ) ;
386
+ } ) ;
387
+
388
+ it ( 'test incrBy operation' , ( ) => {
389
+ const options = {
390
+ sql : '' ,
391
+ values : [ ] ,
392
+ conditions : [ { key : 'id' , opt : '=' , value : 1 } ] ,
393
+ tables : [ { table : 'table1' } ] ,
394
+ operator : 'incrBy' ,
395
+ attrs : [ 'count' ] ,
396
+ increment : 1
397
+ } ;
398
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
399
+ 'UPDATE `table1` SET `count` = `count` + ? WHERE `id` = ?'
400
+ ) ;
401
+ } ) ;
402
+
403
+ it ( 'test condition with JSON field' , ( ) => {
404
+ const options = {
405
+ sql : '' ,
406
+ values : [ ] ,
407
+ conditions : [ {
408
+ key : 'data->$.name' ,
409
+ opt : '=' ,
410
+ value : 'test'
411
+ } ] ,
412
+ tables : [ { table : 'table1' } ] ,
413
+ operator : 'select'
414
+ } ;
415
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
416
+ 'SELECT * FROM `table1` WHERE JSON_EXTRACT(`data`, \'$.name\') = ?'
417
+ ) ;
418
+ } ) ;
419
+
420
+ it ( 'test condition with IN operator' , ( ) => {
421
+ const options = {
422
+ sql : '' ,
423
+ values : [ ] ,
424
+ conditions : [ {
425
+ key : 'id' ,
426
+ opt : 'in' ,
427
+ value : [ 1 , 2 , 3 ]
428
+ } ] ,
429
+ tables : [ { table : 'table1' } ] ,
430
+ operator : 'select'
431
+ } ;
432
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
433
+ 'SELECT * FROM `table1` WHERE `id` IN (?)'
434
+ ) ;
435
+ } ) ;
436
+
437
+ it ( 'test condition with BETWEEN operator' , ( ) => {
438
+ const options = {
439
+ sql : '' ,
440
+ values : [ ] ,
441
+ conditions : [ {
442
+ key : 'age' ,
443
+ opt : 'between' ,
444
+ value : [ 18 , 30 ]
445
+ } ] ,
446
+ tables : [ { table : 'table1' } ] ,
447
+ operator : 'select'
448
+ } ;
449
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
450
+ 'SELECT * FROM `table1` WHERE `age` BETWEEN ? AND ?'
451
+ ) ;
452
+ } ) ;
453
+
454
+ it ( 'test condition with CONTAIN operator' , ( ) => {
455
+ const options = {
456
+ sql : '' ,
457
+ values : [ ] ,
458
+ conditions : [ {
459
+ key : 'name' ,
460
+ opt : 'contain' ,
461
+ value : 'test'
462
+ } ] ,
463
+ tables : [ { table : 'table1' } ] ,
464
+ operator : 'select'
465
+ } ;
466
+ expect ( ( new Builder ( options ) ) . sql ) . to . be . equal (
467
+ 'SELECT * FROM `table1` WHERE `name` LIKE CONCAT(\'%\', ?, \'%\')'
468
+ ) ;
469
+ } ) ;
470
+
471
+ it ( 'test ManageSQLBuilder create table' , ( ) => {
472
+ const options = {
473
+ operator : 'create' ,
474
+ target : 'table' ,
475
+ name : 'test_table' ,
476
+ columns : {
477
+ id : {
478
+ type : 'int' ,
479
+ primaryKey : true ,
480
+ autoIncrement : true ,
481
+ comment : 'Primary Key'
482
+ } ,
483
+ name : {
484
+ type : 'varchar' ,
485
+ length : 255 ,
486
+ allowNull : false ,
487
+ comment : 'User Name'
488
+ } ,
489
+ status : {
490
+ type : 'tinyint' ,
491
+ default : 1 ,
492
+ comment : 'Status'
493
+ }
494
+ }
495
+ } ;
496
+
497
+ const sql = ( new ManageSQLBuilder ( options ) ) . sql ;
498
+ expect ( sql ) . to . include ( 'CREATE TABLE `test_table`' ) ;
499
+ expect ( sql ) . to . include ( '`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT \'Primary Key\'' ) ;
500
+ expect ( sql ) . to . include ( '`name` VARCHAR(255) NOT NULL COMMENT \'User Name\'' ) ;
501
+ expect ( sql ) . to . include ( '`status` TINYINT(4) DEFAULT 1 COMMENT \'Status\'' ) ;
502
+ expect ( sql ) . to . include ( 'PRIMARY KEY (`id`)' ) ;
503
+ } ) ;
312
504
} ) ;
0 commit comments