Skip to content

Commit

Permalink
Return number of placeholders for upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Dec 3, 2024
1 parent 8e4189e commit 82da1f9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func (db *DB) Log(ctx context.Context, query string, counter *com.Counter) perio
}))
}

func BuildUpsertStatement(db *DB, stmt UpsertStatement) (string, error) {
func BuildUpsertStatement(db *DB, stmt UpsertStatement) (string, int, error) {
return NewQueryBuilder(db.DriverName()).UpsertStatement(stmt)
}

Expand Down
8 changes: 4 additions & 4 deletions database/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type QueryBuilder interface {
UpsertStatement(stmt UpsertStatement) (string, error)
UpsertStatement(stmt UpsertStatement) (string, int, error)

InsertStatement(stmt InsertStatement) string

Expand Down Expand Up @@ -43,7 +43,7 @@ type queryBuilder struct {
columnMap ColumnMap
}

func (qb *queryBuilder) UpsertStatement(stmt UpsertStatement) (string, error) {
func (qb *queryBuilder) UpsertStatement(stmt UpsertStatement) (string, int, error) {
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludedColumns())
into := stmt.Table()
if into == "" {
Expand All @@ -61,7 +61,7 @@ func (qb *queryBuilder) UpsertStatement(stmt UpsertStatement) (string, error) {
)
setFormat = `"%[1]s" = EXCLUDED."%[1]s"`
default:
return "", errors.New(fmt.Sprintf("unsupported driver: %s", qb.dbDriver))
return "", 0, errors.New(fmt.Sprintf("unsupported driver: %s", qb.dbDriver))
}

set := make([]string, 0, len(columns))
Expand All @@ -76,7 +76,7 @@ func (qb *queryBuilder) UpsertStatement(stmt UpsertStatement) (string, error) {
fmt.Sprintf(":%s", strings.Join(columns, ", :")),
clause,
strings.Join(set, ", "),
), nil
), len(columns), nil
}

func (qb *queryBuilder) InsertStatement(stmt InsertStatement) string {
Expand Down

0 comments on commit 82da1f9

Please sign in to comment.