-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
24 lines (20 loc) · 941 Bytes
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package sqlx
import (
"github.com/go-courier/sqlx/v2/builder"
)
func InsertToDB(db DBExecutor, model builder.Model, zeroFields []string, additions ...builder.Addition) builder.SqlExpr {
table := db.T(model)
cols, vals := table.ColumnsAndValuesByFieldValues(FieldValuesFromModel(table, model, zeroFields...))
return builder.Insert().Into(table, additions...).Values(cols, vals...)
}
func AsAssignments(db DBExecutor, model builder.Model, zeroFields ...string) builder.Assignments {
table := db.T(model)
return table.AssignmentsByFieldValues(FieldValuesFromModel(table, model, zeroFields...))
}
func FieldValuesFromModel(table *builder.Table, model builder.Model, zeroFields ...string) builder.FieldValues {
fieldValues := builder.FieldValuesFromStructByNonZero(model, zeroFields...)
if autoIncrementCol := table.AutoIncrement(); autoIncrementCol != nil {
delete(fieldValues, autoIncrementCol.FieldName)
}
return fieldValues
}