Big refactor of generated code
- Stop generating helper functions for each model - Move Insert/Update/Upsert query generation helpers to strmangle - Add tests for query generation helpers - Delete a lot of the Insert/Upsert tests that test the query generation helpers. - Use tx for more of the tests.
This commit is contained in:
parent
8a7a9a35e8
commit
efa1fbb80a
13 changed files with 231 additions and 227 deletions
templates/singleton
|
@ -11,3 +11,46 @@ 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 {
|
||||
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()
|
||||
}
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
// M type is for providing columns and column values to UpdateAll.
|
||||
type M map[string]interface{}
|
||||
|
||||
type upsertData struct {
|
||||
conflict []string
|
||||
update []string
|
||||
whitelist []string
|
||||
returning []string
|
||||
}
|
||||
|
||||
// ErrSyncFail occurs during insert when the record could not be retrieved in
|
||||
// order to populate default value information. This usually happens when LastInsertId
|
||||
// fails or there was a primary key configuration that was not resolvable.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue