@@ -402,140 +402,17 @@ func buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName string, uni
402
402
return query , nil
403
403
}
404
404
405
- func BuildDMLDeleteQuery (databaseName , tableName string , tableColumns , uniqueKeyColumns * ColumnList , args []interface {}) (result string , uniqueKeyArgs []interface {}, err error ) {
406
- if len (args ) != tableColumns .Len () {
407
- return result , uniqueKeyArgs , fmt .Errorf ("args count differs from table column count in BuildDMLDeleteQuery" )
408
- }
409
- if uniqueKeyColumns .Len () == 0 {
410
- return result , uniqueKeyArgs , fmt .Errorf ("No unique key columns found in BuildDMLDeleteQuery" )
411
- }
412
- for _ , column := range uniqueKeyColumns .Columns () {
413
- tableOrdinal := tableColumns .Ordinals [column .Name ]
414
- arg := column .convertArg (args [tableOrdinal ], true )
415
- uniqueKeyArgs = append (uniqueKeyArgs , arg )
416
- }
417
- databaseName = EscapeName (databaseName )
418
- tableName = EscapeName (tableName )
419
- equalsComparison , err := BuildEqualsPreparedComparison (uniqueKeyColumns .Names ())
420
- if err != nil {
421
- return result , uniqueKeyArgs , err
422
- }
423
- result = fmt .Sprintf (`
424
- delete /* gh-ost %s.%s */
425
- from
426
- %s.%s
427
- where
428
- %s` ,
429
- databaseName , tableName ,
430
- databaseName , tableName ,
431
- equalsComparison ,
432
- )
433
- return result , uniqueKeyArgs , nil
434
- }
435
-
436
- func BuildDMLInsertQuery (databaseName , tableName string , tableColumns , sharedColumns , mappedSharedColumns * ColumnList , args []interface {}) (result string , sharedArgs []interface {}, err error ) {
437
- if len (args ) != tableColumns .Len () {
438
- return result , args , fmt .Errorf ("args count differs from table column count in BuildDMLInsertQuery" )
439
- }
440
- if ! sharedColumns .IsSubsetOf (tableColumns ) {
441
- return result , args , fmt .Errorf ("shared columns is not a subset of table columns in BuildDMLInsertQuery" )
442
- }
443
- if sharedColumns .Len () == 0 {
444
- return result , args , fmt .Errorf ("No shared columns found in BuildDMLInsertQuery" )
445
- }
446
- databaseName = EscapeName (databaseName )
447
- tableName = EscapeName (tableName )
448
-
449
- for _ , column := range sharedColumns .Columns () {
450
- tableOrdinal := tableColumns .Ordinals [column .Name ]
451
- arg := column .convertArg (args [tableOrdinal ], false )
452
- sharedArgs = append (sharedArgs , arg )
453
- }
454
-
455
- mappedSharedColumnNames := duplicateNames (mappedSharedColumns .Names ())
456
- for i := range mappedSharedColumnNames {
457
- mappedSharedColumnNames [i ] = EscapeName (mappedSharedColumnNames [i ])
458
- }
459
- preparedValues := buildColumnsPreparedValues (mappedSharedColumns )
460
-
461
- result = fmt .Sprintf (`
462
- replace /* gh-ost %s.%s */
463
- into
464
- %s.%s
465
- (%s)
466
- values
467
- (%s)` ,
468
- databaseName , tableName ,
469
- databaseName , tableName ,
470
- strings .Join (mappedSharedColumnNames , ", " ),
471
- strings .Join (preparedValues , ", " ),
472
- )
473
- return result , sharedArgs , nil
474
- }
475
-
476
- func BuildDMLUpdateQuery (databaseName , tableName string , tableColumns , sharedColumns , mappedSharedColumns , uniqueKeyColumns * ColumnList , valueArgs , whereArgs []interface {}) (result string , sharedArgs , uniqueKeyArgs []interface {}, err error ) {
477
- if len (valueArgs ) != tableColumns .Len () {
478
- return result , sharedArgs , uniqueKeyArgs , fmt .Errorf ("value args count differs from table column count in BuildDMLUpdateQuery" )
479
- }
480
- if len (whereArgs ) != tableColumns .Len () {
481
- return result , sharedArgs , uniqueKeyArgs , fmt .Errorf ("where args count differs from table column count in BuildDMLUpdateQuery" )
482
- }
483
- if ! sharedColumns .IsSubsetOf (tableColumns ) {
484
- return result , sharedArgs , uniqueKeyArgs , fmt .Errorf ("shared columns is not a subset of table columns in BuildDMLUpdateQuery" )
485
- }
486
- if ! uniqueKeyColumns .IsSubsetOf (sharedColumns ) {
487
- return result , sharedArgs , uniqueKeyArgs , fmt .Errorf ("unique key columns is not a subset of shared columns in BuildDMLUpdateQuery" )
488
- }
489
- if sharedColumns .Len () == 0 {
490
- return result , sharedArgs , uniqueKeyArgs , fmt .Errorf ("No shared columns found in BuildDMLUpdateQuery" )
491
- }
492
- if uniqueKeyColumns .Len () == 0 {
493
- return result , sharedArgs , uniqueKeyArgs , fmt .Errorf ("No unique key columns found in BuildDMLUpdateQuery" )
494
- }
495
- databaseName = EscapeName (databaseName )
496
- tableName = EscapeName (tableName )
497
-
498
- for _ , column := range sharedColumns .Columns () {
499
- tableOrdinal := tableColumns .Ordinals [column .Name ]
500
- arg := column .convertArg (valueArgs [tableOrdinal ], false )
501
- sharedArgs = append (sharedArgs , arg )
502
- }
503
-
504
- for _ , column := range uniqueKeyColumns .Columns () {
505
- tableOrdinal := tableColumns .Ordinals [column .Name ]
506
- arg := column .convertArg (whereArgs [tableOrdinal ], true )
507
- uniqueKeyArgs = append (uniqueKeyArgs , arg )
508
- }
509
-
510
- setClause , err := BuildSetPreparedClause (mappedSharedColumns )
511
- if err != nil {
512
- return "" , sharedArgs , uniqueKeyArgs , err
513
- }
514
-
515
- equalsComparison , err := BuildEqualsPreparedComparison (uniqueKeyColumns .Names ())
516
- if err != nil {
517
- return "" , sharedArgs , uniqueKeyArgs , err
518
- }
519
- result = fmt .Sprintf (`
520
- update /* gh-ost %s.%s */
521
- %s.%s
522
- set
523
- %s
524
- where
525
- %s` ,
526
- databaseName , tableName ,
527
- databaseName , tableName ,
528
- setClause ,
529
- equalsComparison ,
530
- )
531
- return result , sharedArgs , uniqueKeyArgs , nil
532
- }
533
-
405
+ // DMLDeleteQueryBuilder can build DELETE queries for DML events.
406
+ // It holds the prepared query statement so it doesn't need to be recreated every time.
534
407
type DMLDeleteQueryBuilder struct {
535
408
tableColumns , uniqueKeyColumns * ColumnList
536
409
preparedStatement string
537
410
}
538
411
412
+ // NewDMLDeleteQueryBuilder creates a new DMLDeleteQueryBuilder.
413
+ // It prepares the DELETE query statement.
414
+ // Returns an error if no unique key columns are given
415
+ // or the prepared statement cannot be built.
539
416
func NewDMLDeleteQueryBuilder (databaseName , tableName string , tableColumns , uniqueKeyColumns * ColumnList ) (* DMLDeleteQueryBuilder , error ) {
540
417
if uniqueKeyColumns .Len () == 0 {
541
418
return nil , fmt .Errorf ("no unique key columns found in NewDMLDeleteQueryBuilder" )
@@ -566,6 +443,9 @@ func NewDMLDeleteQueryBuilder(databaseName, tableName string, tableColumns, uniq
566
443
return b , nil
567
444
}
568
445
446
+ // BuildQuery builds the arguments array for a DML event DELETE query.
447
+ // It returns the query string and the unique key arguments array.
448
+ // Returns an error if the number of arguments is not equal to the number of table columns.
569
449
func (b * DMLDeleteQueryBuilder ) BuildQuery (args []interface {}) (string , []interface {}, error ) {
570
450
if len (args ) != b .tableColumns .Len () {
571
451
return "" , nil , fmt .Errorf ("args count differs from table column count in BuildDMLDeleteQuery" )
@@ -579,11 +459,17 @@ func (b *DMLDeleteQueryBuilder) BuildQuery(args []interface{}) (string, []interf
579
459
return b .preparedStatement , uniqueKeyArgs , nil
580
460
}
581
461
462
+ // DMLInsertQueryBuilder can build INSERT queries for DML events.
463
+ // It holds the prepared query statement so it doesn't need to be recreated every time.
582
464
type DMLInsertQueryBuilder struct {
583
465
tableColumns , sharedColumns * ColumnList
584
466
preparedStatement string
585
467
}
586
468
469
+ // NewDMLInsertQueryBuilder creates a new DMLInsertQueryBuilder.
470
+ // It prepares the INSERT query statement.
471
+ // Returns an error if no shared columns are given, the shared columns are not a subset of the table columns,
472
+ // or the prepared statement cannot be built.
587
473
func NewDMLInsertQueryBuilder (databaseName , tableName string , tableColumns , sharedColumns , mappedSharedColumns * ColumnList ) (* DMLInsertQueryBuilder , error ) {
588
474
if ! sharedColumns .IsSubsetOf (tableColumns ) {
589
475
return nil , fmt .Errorf ("shared columns is not a subset of table columns in NewDMLInsertQueryBuilder" )
@@ -619,6 +505,9 @@ func NewDMLInsertQueryBuilder(databaseName, tableName string, tableColumns, shar
619
505
}, nil
620
506
}
621
507
508
+ // BuildQuery builds the arguments array for a DML event INSERT query.
509
+ // It returns the query string and the shared arguments array.
510
+ // Returns an error if the number of arguments differs from the number of table columns.
622
511
func (b * DMLInsertQueryBuilder ) BuildQuery (args []interface {}) (string , []interface {}, error ) {
623
512
if len (args ) != b .tableColumns .Len () {
624
513
return "" , nil , fmt .Errorf ("args count differs from table column count in BuildDMLInsertQuery" )
@@ -632,11 +521,17 @@ func (b *DMLInsertQueryBuilder) BuildQuery(args []interface{}) (string, []interf
632
521
return b .preparedStatement , sharedArgs , nil
633
522
}
634
523
524
+ // DMLUpdateQueryBuilder can build UPDATE queries for DML events.
525
+ // It holds the prepared query statement so it doesn't need to be recreated every time.
635
526
type DMLUpdateQueryBuilder struct {
636
527
tableColumns , sharedColumns , uniqueKeyColumns * ColumnList
637
528
preparedStatement string
638
529
}
639
530
531
+ // NewDMLUpdateQueryBuilder creates a new DMLUpdateQueryBuilder.
532
+ // It prepares the UPDATE query statement.
533
+ // Returns an error if no shared columns are given, the shared columns are not a subset of the table columns,
534
+ // no unique key columns are given or the prepared statement cannot be built.
640
535
func NewDMLUpdateQueryBuilder (databaseName , tableName string , tableColumns , sharedColumns , mappedSharedColumns , uniqueKeyColumns * ColumnList ) (* DMLUpdateQueryBuilder , error ) {
641
536
if ! sharedColumns .IsSubsetOf (tableColumns ) {
642
537
return nil , fmt .Errorf ("shared columns is not a subset of table columns in NewDMLUpdateQueryBuilder" )
@@ -678,6 +573,8 @@ func NewDMLUpdateQueryBuilder(databaseName, tableName string, tableColumns, shar
678
573
}, nil
679
574
}
680
575
576
+ // BuildQuery builds the arguments array for a DML event UPDATE query.
577
+ // It returns the query string, the shared arguments array, and the unique key arguments array.
681
578
func (b * DMLUpdateQueryBuilder ) BuildQuery (valueArgs , whereArgs []interface {}) (string , []interface {}, []interface {}, error ) {
682
579
// TODO: move this check back to `NewDMLUpdateQueryBuilder()`, needs fix on generated columns.
683
580
if ! b .uniqueKeyColumns .IsSubsetOf (b .sharedColumns ) {
0 commit comments