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

dialect/sql: expose the underlying builder of UpdateSet #3541

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions dialect/sql/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,13 +890,13 @@ func (i *InsertBuilder) OnConflict(opts ...ConflictOption) *InsertBuilder {

// UpdateSet describes a set of changes of the `DO UPDATE` clause.
type UpdateSet struct {
*UpdateBuilder
columns []string
update *UpdateBuilder
}

// Table returns the table the `UPSERT` statement is executed on.
func (u *UpdateSet) Table() *SelectTable {
return Dialect(u.update.dialect).Table(u.update.table)
return Dialect(u.UpdateBuilder.dialect).Table(u.UpdateBuilder.table)
}

// Columns returns all columns in the `INSERT` statement.
Expand All @@ -906,24 +906,24 @@ func (u *UpdateSet) Columns() []string {

// UpdateColumns returns all columns in the `UPDATE` statement.
func (u *UpdateSet) UpdateColumns() []string {
return append(u.update.nulls, u.update.columns...)
return append(u.UpdateBuilder.nulls, u.UpdateBuilder.columns...)
}

// Set sets a column to a given value.
func (u *UpdateSet) Set(column string, v any) *UpdateSet {
u.update.Set(column, v)
u.UpdateBuilder.Set(column, v)
return u
}

// Add adds a numeric value to the given column.
func (u *UpdateSet) Add(column string, v any) *UpdateSet {
u.update.Add(column, v)
u.UpdateBuilder.Add(column, v)
return u
}

// SetNull sets a column as null value.
func (u *UpdateSet) SetNull(column string) *UpdateSet {
u.update.SetNull(column)
u.UpdateBuilder.SetNull(column)
return u
}

Expand All @@ -935,14 +935,14 @@ func (u *UpdateSet) SetIgnore(name string) *UpdateSet {
// SetExcluded sets the column name to its EXCLUDED/VALUES value.
// For example, "c" = "excluded"."c", or `c` = VALUES(`c`).
func (u *UpdateSet) SetExcluded(name string) *UpdateSet {
switch u.update.Dialect() {
switch u.UpdateBuilder.Dialect() {
case dialect.MySQL:
u.update.Set(name, ExprFunc(func(b *Builder) {
u.UpdateBuilder.Set(name, ExprFunc(func(b *Builder) {
b.WriteString("VALUES(").Ident(name).WriteByte(')')
}))
default:
t := Dialect(u.update.dialect).Table("excluded")
u.update.Set(name, Expr(t.C(name)))
t := Dialect(u.UpdateBuilder.dialect).Table("excluded")
u.UpdateBuilder.Set(name, Expr(t.C(name)))
}
return u
}
Expand Down Expand Up @@ -1019,12 +1019,12 @@ func (i *InsertBuilder) writeConflict(b *Builder) {
if len(i.conflict.action.update) == 0 {
b.AddError(errors.New("missing action for 'DO UPDATE SET' clause"))
}
u := &UpdateSet{columns: i.columns, update: Dialect(i.dialect).Update(i.table)}
u.update.Builder = *b
u := &UpdateSet{UpdateBuilder: Dialect(i.dialect).Update(i.table), columns: i.columns}
u.Builder = *b
for _, f := range i.conflict.action.update {
f(u)
}
u.update.writeSetter(b)
u.writeSetter(b)
if p := i.conflict.action.where; p != nil {
p.qualifier = i.table
b.WriteString(" WHERE ").Join(p)
Expand Down