2016-08-01 07:10:10 +02:00
|
|
|
// NewQueryG initializes a new Query using the passed in QueryMods
|
|
|
|
func NewQueryG(mods ...qm.QueryMod) *boil.Query {
|
|
|
|
return NewQuery(boil.GetDB(), mods...)
|
2016-04-19 04:02:32 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
// NewQuery initializes a new Query using the passed in QueryMods
|
|
|
|
func NewQuery(exec boil.Executor, mods ...qm.QueryMod) *boil.Query {
|
2016-04-23 13:54:24 +02:00
|
|
|
q := &boil.Query{}
|
|
|
|
boil.SetExecutor(q, exec)
|
2016-06-14 16:01:28 +02:00
|
|
|
qm.Apply(q, mods...)
|
2016-04-19 04:02:32 +02:00
|
|
|
|
|
|
|
return q
|
|
|
|
}
|
2016-08-14 06:21:26 +02:00
|
|
|
|
|
|
|
// 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),
|
|
|
|
)
|
|
|
|
|
2016-08-21 07:44:49 +02:00
|
|
|
if !updateOnConflict || len(update) == 0 {
|
2016-08-14 06:21:26 +02:00
|
|
|
buf.WriteString("DO NOTHING")
|
|
|
|
} else {
|
|
|
|
buf.WriteByte('(')
|
|
|
|
buf.WriteString(strings.Join(conflict, ", "))
|
2016-08-21 07:44:49 +02:00
|
|
|
buf.WriteString(") DO UPDATE SET ")
|
2016-08-14 06:21:26 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|