Move buildUpsertQuery to boil

This commit is contained in:
Patrick O'brien 2016-09-01 11:20:16 +10:00
parent 87b1f90c43
commit 8f6191c4d2
4 changed files with 44 additions and 49 deletions

View file

@ -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, ", "))

View file

@ -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": {

View file

@ -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)

View file

@ -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()
}