adopt forked squirrel supporting from UPDATE FROM (#19896)
Fork https://github.com/Masterminds/squirrel as https://github.com/lieut-data/squirrel with the following changes: * https://github.com/Masterminds/squirrel/pull/256 supporting FROM clause to update builder (Postgres) * Extension of above to support multiple FROM in UPDATE (Postgres) * Support for multiple tables in UPDATE (MySQL) This PR then leverages those changes to simplify a query that previously had to be coded by hand and duplicate for each of MySQL and Postgres.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3f5eb5f6f7
Коммит
7b5ac343f0
2
vendor/github.com/Masterminds/squirrel/squirrel_ctx.go
сгенерированный
поставляемый
2
vendor/github.com/Masterminds/squirrel/squirrel_ctx.go
сгенерированный
поставляемый
@@ -32,7 +32,7 @@ type QueryRowerContext interface {
|
||||
QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner
|
||||
}
|
||||
|
||||
// RunnerContext groups the Runner interface, along with the Contect versions of each of
|
||||
// RunnerContext groups the Runner interface, along with the Context versions of each of
|
||||
// its methods
|
||||
type RunnerContext interface {
|
||||
Runner
|
||||
|
||||
8
vendor/github.com/Masterminds/squirrel/statement.go
сгенерированный
поставляемый
8
vendor/github.com/Masterminds/squirrel/statement.go
сгенерированный
поставляемый
@@ -22,8 +22,8 @@ func (b StatementBuilderType) Replace(into string) InsertBuilder {
|
||||
}
|
||||
|
||||
// Update returns a UpdateBuilder for this StatementBuilderType.
|
||||
func (b StatementBuilderType) Update(table string) UpdateBuilder {
|
||||
return UpdateBuilder(b).Table(table)
|
||||
func (b StatementBuilderType) Update(tables ...string) UpdateBuilder {
|
||||
return UpdateBuilder(b).Table(tables...)
|
||||
}
|
||||
|
||||
// Delete returns a DeleteBuilder for this StatementBuilderType.
|
||||
@@ -76,8 +76,8 @@ func Replace(into string) InsertBuilder {
|
||||
// Update returns a new UpdateBuilder with the given table name.
|
||||
//
|
||||
// See UpdateBuilder.Table.
|
||||
func Update(table string) UpdateBuilder {
|
||||
return StatementBuilder.Update(table)
|
||||
func Update(tables ...string) UpdateBuilder {
|
||||
return StatementBuilder.Update(tables...)
|
||||
}
|
||||
|
||||
// Delete returns a new DeleteBuilder with the given table name.
|
||||
|
||||
40
vendor/github.com/Masterminds/squirrel/update.go
сгенерированный
поставляемый
40
vendor/github.com/Masterminds/squirrel/update.go
сгенерированный
поставляемый
@@ -14,8 +14,9 @@ type updateData struct {
|
||||
PlaceholderFormat PlaceholderFormat
|
||||
RunWith BaseRunner
|
||||
Prefixes []Sqlizer
|
||||
Table string
|
||||
Tables []string
|
||||
SetClauses []setClause
|
||||
From []Sqlizer
|
||||
WhereParts []Sqlizer
|
||||
OrderBys []string
|
||||
Limit string
|
||||
@@ -54,7 +55,7 @@ func (d *updateData) QueryRow() RowScanner {
|
||||
}
|
||||
|
||||
func (d *updateData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||
if len(d.Table) == 0 {
|
||||
if len(d.Tables) == 0 {
|
||||
err = fmt.Errorf("update statements must specify a table")
|
||||
return
|
||||
}
|
||||
@@ -75,7 +76,7 @@ func (d *updateData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||
}
|
||||
|
||||
sql.WriteString("UPDATE ")
|
||||
sql.WriteString(d.Table)
|
||||
sql.WriteString(strings.Join(d.Tables, ", "))
|
||||
|
||||
sql.WriteString(" SET ")
|
||||
setSqls := make([]string, len(d.SetClauses))
|
||||
@@ -100,6 +101,14 @@ func (d *updateData) ToSql() (sqlStr string, args []interface{}, err error) {
|
||||
}
|
||||
sql.WriteString(strings.Join(setSqls, ", "))
|
||||
|
||||
if len(d.From) > 0 {
|
||||
sql.WriteString(" FROM ")
|
||||
args, err = appendToSql(d.From, sql, ", ", args)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(d.WhereParts) > 0 {
|
||||
sql.WriteString(" WHERE ")
|
||||
args, err = appendToSql(d.WhereParts, sql, " AND ", args)
|
||||
@@ -208,8 +217,16 @@ func (b UpdateBuilder) PrefixExpr(expr Sqlizer) UpdateBuilder {
|
||||
}
|
||||
|
||||
// Table sets the table to be updated.
|
||||
func (b UpdateBuilder) Table(table string) UpdateBuilder {
|
||||
return builder.Set(b, "Table", table).(UpdateBuilder)
|
||||
// Additional tables are used with supporting databases to implicitly join.
|
||||
func (b UpdateBuilder) Table(tables ...string) UpdateBuilder {
|
||||
nonEmptyTables := make([]string, 0, len(tables))
|
||||
for _, table := range tables {
|
||||
if table != "" {
|
||||
nonEmptyTables = append(nonEmptyTables, table)
|
||||
}
|
||||
}
|
||||
|
||||
return builder.Set(b, "Tables", nonEmptyTables).(UpdateBuilder)
|
||||
}
|
||||
|
||||
// Set adds SET clauses to the query.
|
||||
@@ -233,6 +250,19 @@ func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
// From adds FROM clause to the query
|
||||
// FROM is valid construct in postgresql only.
|
||||
func (b UpdateBuilder) From(from string) UpdateBuilder {
|
||||
return builder.Append(b, "From", newPart(from)).(UpdateBuilder)
|
||||
}
|
||||
|
||||
// FromSelect sets a subquery into the FROM clause of the query.
|
||||
func (b UpdateBuilder) FromSelect(from SelectBuilder, alias string) UpdateBuilder {
|
||||
// Prevent misnumbered parameters in nested selects (#183).
|
||||
from = from.PlaceholderFormat(Question)
|
||||
return builder.Append(b, "From", Alias(from, alias)).(UpdateBuilder)
|
||||
}
|
||||
|
||||
// Where adds WHERE expressions to the query.
|
||||
//
|
||||
// See SelectBuilder.Where for more information.
|
||||
|
||||
Ссылка в новой задаче
Block a user