Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Masterminds#82] Added FROM clause to update builder #256

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type updateData struct {
Prefixes []Sqlizer
Table string
SetClauses []setClause
From Sqlizer
WhereParts []Sqlizer
OrderBys []string
Limit string
Expand Down Expand Up @@ -96,6 +97,14 @@ func (d *updateData) ToSql() (sqlStr string, args []interface{}, err error) {
}
sql.WriteString(strings.Join(setSqls, ", "))

if d.From != nil {
sql.WriteString(" FROM ")
args, err = appendToSql([]Sqlizer{d.From}, sql, "", args)
if err != nil {
return
}
}

if len(d.WhereParts) > 0 {
sql.WriteString(" WHERE ")
args, err = appendToSql(d.WhereParts, sql, " AND ", args)
Expand Down Expand Up @@ -219,6 +228,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.Set(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.Set(b, "From", Alias(from, alias)).(UpdateBuilder)
}

// Where adds WHERE expressions to the query.
//
// See SelectBuilder.Where for more information.
Expand Down
23 changes: 23 additions & 0 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,26 @@ func TestUpdateBuilderNoRunner(t *testing.T) {
_, err := b.Exec()
assert.Equal(t, RunnerNotSet, err)
}

func TestUpdateBuilderFrom(t *testing.T) {
sql, _, err := Update("employees").Set("sales_count", 100).From("accounts").Where("accounts.name = ?", "ACME").ToSql()
assert.NoError(t, err)
assert.Equal(t, "UPDATE employees SET sales_count = ? FROM accounts WHERE accounts.name = ?", sql)
}

func TestUpdateBuilderFromSelect(t *testing.T) {
sql, _, err := Update("employees").
Set("sales_count", 100).
FromSelect(Select("id").
From("accounts").
Where("accounts.name = ?", "ACME"), "subquery").
Where("employees.account_id = subquery.id").ToSql()
assert.NoError(t, err)

expectedSql :=
"UPDATE employees " +
"SET sales_count = ? " +
"FROM (SELECT id FROM accounts WHERE accounts.name = ?) AS subquery " +
"WHERE employees.account_id = subquery.id"
assert.Equal(t, expectedSql, sql)
}