diff --git a/boil/query_builders.go b/boil/query_builders.go index 097b1a2..a3e646a 100644 --- a/boil/query_builders.go +++ b/boil/query_builders.go @@ -173,6 +173,49 @@ func buildUpdateQuery(q *Query) (*bytes.Buffer, []interface{}) { return buf, args } +// BuildUpsertQuery builds a SQL statement string using the upsertData provided. +func BuildUpsertQuery(tableName string, updateOnConflict bool, ret, update, conflict, whitelist []string) string { + conflict = strmangle.IdentQuoteSlice(conflict) + whitelist = strmangle.IdentQuoteSlice(whitelist) + ret = strmangle.IdentQuoteSlice(ret) + + buf := strmangle.GetBuffer() + defer strmangle.PutBuffer(buf) + + fmt.Fprintf( + buf, + "INSERT INTO %s (%s) VALUES (%s) ON CONFLICT ", + tableName, + strings.Join(whitelist, ", "), + strmangle.Placeholders(len(whitelist), 1, 1), + ) + + if !updateOnConflict || len(update) == 0 { + buf.WriteString("DO NOTHING") + } else { + buf.WriteByte('(') + buf.WriteString(strings.Join(conflict, ", ")) + buf.WriteString(") DO UPDATE SET ") + + for i, v := range update { + if i != 0 { + buf.WriteByte(',') + } + quoted := strmangle.IdentQuote(v) + buf.WriteString(quoted) + buf.WriteString(" = EXCLUDED.") + buf.WriteString(quoted) + } + } + + if len(ret) != 0 { + buf.WriteString(" RETURNING ") + buf.WriteString(strings.Join(ret, ", ")) + } + + return buf.String() +} + func writeModifiers(q *Query, buf *bytes.Buffer, args *[]interface{}) { if len(q.groupBy) != 0 { fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ", ")) diff --git a/imports.go b/imports.go index 8ace24f..c10fa98 100644 --- a/imports.go +++ b/imports.go @@ -158,14 +158,9 @@ var defaultTemplateImports = imports{ var defaultSingletonTemplateImports = map[string]imports{ "boil_queries": { - standard: importList{ - `"fmt"`, - `"strings"`, - }, thirdParty: importList{ `"github.com/vattle/sqlboiler/boil"`, `"github.com/vattle/sqlboiler/boil/qm"`, - `"github.com/vattle/sqlboiler/strmangle"`, }, }, "boil_types": { diff --git a/templates/14_upsert.tpl b/templates/14_upsert.tpl index 369998a..e5d5f94 100644 --- a/templates/14_upsert.tpl +++ b/templates/14_upsert.tpl @@ -54,7 +54,7 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo copy(conflict, {{$varNameSingular}}PrimaryKeyColumns) } - query := generateUpsertQuery("{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist) + query := boil.BuildUpsertQuery("{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist) if boil.DebugMode { fmt.Fprintln(boil.DebugWriter, query) diff --git a/templates/singleton/boil_queries.tpl b/templates/singleton/boil_queries.tpl index ab01981..6cb607d 100644 --- a/templates/singleton/boil_queries.tpl +++ b/templates/singleton/boil_queries.tpl @@ -11,46 +11,3 @@ func NewQuery(exec boil.Executor, mods ...qm.QueryMod) *boil.Query { return q } - -// generateUpsertQuery builds a SQL statement string using the upsertData provided. -func generateUpsertQuery(tableName string, updateOnConflict bool, ret, update, conflict, whitelist []string) string { - conflict = strmangle.IdentQuoteSlice(conflict) - whitelist = strmangle.IdentQuoteSlice(whitelist) - ret = strmangle.IdentQuoteSlice(ret) - - buf := strmangle.GetBuffer() - defer strmangle.PutBuffer(buf) - - fmt.Fprintf( - buf, - "INSERT INTO %s (%s) VALUES (%s) ON CONFLICT ", - tableName, - strings.Join(whitelist, ", "), - strmangle.Placeholders(len(whitelist), 1, 1), - ) - - if !updateOnConflict || len(update) == 0 { - buf.WriteString("DO NOTHING") - } else { - buf.WriteByte('(') - buf.WriteString(strings.Join(conflict, ", ")) - buf.WriteString(") DO UPDATE SET ") - - for i, v := range update { - if i != 0 { - buf.WriteByte(',') - } - quoted := strmangle.IdentQuote(v) - buf.WriteString(quoted) - buf.WriteString(" = EXCLUDED.") - buf.WriteString(quoted) - } - } - - if len(ret) != 0 { - buf.WriteString(" RETURNING ") - buf.WriteString(strings.Join(ret, ", ")) - } - - return buf.String() -}