Skip to content

Commit

Permalink
Merge pull request #331 from uptrace/fix/bc-faq
Browse files Browse the repository at this point in the history
chore: add UpdateFQN in backwards compatible manner
  • Loading branch information
vmihailenco authored Nov 29, 2021
2 parents 65ea902 + caf2ab9 commit 6d5b152
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 9 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ func (db *DB) Formatter() schema.Formatter {
return db.fmter
}

// UpdateFQN returns a fully qualified column name. For MySQL, it returns the column name with
// the table alias. For other RDBMS, it returns just the column name.
func (db *DB) UpdateFQN(alias, column string) Ident {
if db.HasFeature(feature.UpdateMultiTable) {
return Ident(alias + "." + column)
}
return Ident(column)
}

// HasFeature uses feature package to report whether the underlying DBMS supports this feature.
func (db *DB) HasFeature(feat feature.Feature) bool {
return db.fmter.HasFeature(feat)
Expand Down
4 changes: 1 addition & 3 deletions internal/dbtest/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,7 @@ func testMultiUpdate(t *testing.T, db *bun.DB) {
With("src", selq).
TableExpr("models AS dest").
Table("src").
Apply(func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = src.str", q.FQN("dest", "str"))
}).
Set("? = src.str", db.UpdateFQN("dest", "str")).
Where("dest.id = src.id").
Exec(ctx)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/dbtest/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func testBulkUpdate(t *testing.T, db *bun.DB) {
Apply(func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set(
"? = UPPER(book.title)",
q.FQN("book", "title"),
q.FQN("title"),
)
}).
Where("book.id = _data.id").
Expand Down
9 changes: 6 additions & 3 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,12 @@ func (q *UpdateQuery) afterUpdateHook(ctx context.Context) error {

// FQN returns a fully qualified column name. For MySQL, it returns the column name with
// the table alias. For other RDBMS, it returns just the column name.
func (q *UpdateQuery) FQN(alias, column string) Ident {
if q.db.fmter.HasFeature(feature.UpdateMultiTable) {
return Ident(alias + "." + column)
func (q *UpdateQuery) FQN(column string) Ident {
if q.table == nil {
panic("UpdateQuery.FQN requires a model")
}
if q.db.HasFeature(feature.UpdateMultiTable) {
return Ident(q.table.Alias + "." + column)
}
return Ident(column)
}

0 comments on commit 6d5b152

Please sign in to comment.