Skip to content

Commit

Permalink
add support for table hint in multi-table MySQL DELETE queries
Browse files Browse the repository at this point in the history
  • Loading branch information
vlanse authored and doug-martin committed Mar 5, 2021
1 parent ee815b3 commit b65f65c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions dialect/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func DialectOptions() *goqu.SQLDialectOptions {
opts.SupportsWithCTERecursive = false
opts.SupportsDistinctOn = false
opts.SupportsWindowFunction = false
opts.SupportsDeleteTableHint = true

opts.UseFromClauseForMultipleUpdateTables = false

Expand Down
9 changes: 7 additions & 2 deletions sqlgen/delete_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (dsg *deleteSQLGenerator) Generate(b sb.SQLBuilder, clauses exp.DeleteClaus
case CommonTableSQLFragment:
dsg.esg.Generate(b, clauses.CommonTables())
case DeleteBeginSQLFragment:
dsg.DeleteBeginSQL(b)
dsg.DeleteBeginSQL(
b, exp.NewColumnListExpression(clauses.From()), !(clauses.HasLimit() || clauses.HasOrder()),
)
case FromSQLFragment:
dsg.FromSQL(b, exp.NewColumnListExpression(clauses.From()))
case WhereSQLFragment:
Expand All @@ -68,6 +70,9 @@ func (dsg *deleteSQLGenerator) Generate(b sb.SQLBuilder, clauses exp.DeleteClaus
}

// Adds the correct fragment to being an DELETE statement
func (dsg *deleteSQLGenerator) DeleteBeginSQL(b sb.SQLBuilder) {
func (dsg *deleteSQLGenerator) DeleteBeginSQL(b sb.SQLBuilder, from exp.ColumnListExpression, multiTable bool) {
b.Write(dsg.dialectOptions.DeleteClause)
if multiTable && dsg.dialectOptions.SupportsDeleteTableHint {
dsg.SourcesSQL(b, from)
}
}
3 changes: 3 additions & 0 deletions sqlgen/sql_dialect_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type (
SQLDialectOptions struct {
// Set to true if the dialect supports ORDER BY expressions in DELETE statements (DEFAULT=false)
SupportsOrderByOnDelete bool
// Set to true if the dialect supports table hint for DELETE statements (DELETE t FROM t ...), DEFAULT=false
SupportsDeleteTableHint bool
// Set to true if the dialect supports ORDER BY expressions in UPDATE statements (DEFAULT=false)
SupportsOrderByOnUpdate bool
// Set to true if the dialect supports LIMIT expressions in DELETE statements (DEFAULT=false)
Expand Down Expand Up @@ -395,6 +397,7 @@ func (sf SQLFragmentType) String() string {
func DefaultDialectOptions() *SQLDialectOptions {
return &SQLDialectOptions{
SupportsOrderByOnDelete: false,
SupportsDeleteTableHint: false,
SupportsOrderByOnUpdate: false,
SupportsLimitOnDelete: false,
SupportsLimitOnUpdate: false,
Expand Down

0 comments on commit b65f65c

Please sign in to comment.