-
Notifications
You must be signed in to change notification settings - Fork 1
/
save.go
130 lines (97 loc) · 2.88 KB
/
save.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package makroud
import (
"context"
"github.com/pkg/errors"
"github.com/ulule/loukoum/v3"
"github.com/ulule/loukoum/v3/builder"
"github.com/ulule/makroud/reflectx"
)
// Save inserts or updates the given instance.
func Save(ctx context.Context, driver Driver, model Model) error {
err := save(ctx, driver, model)
if err != nil {
return errors.Wrap(err, "makroud: cannot execute save")
}
return nil
}
func save(ctx context.Context, driver Driver, model Model) error {
if driver == nil {
return errors.WithStack(ErrInvalidDriver)
}
schema, err := GetSchema(driver, model)
if err != nil {
return err
}
values := loukoum.Map{}
returning := []string{}
pk := schema.PrimaryKey()
id, hasPK := pk.ValueOpt(model)
err = generateSaveQuery(schema, model, hasPK, &returning, values)
if err != nil {
return err
}
builder, err := getSaveBuilder(driver, model, pk, hasPK, id, &returning, values)
if err != nil {
return err
}
err = Exec(ctx, driver, builder, model)
// Ignore no rows error if returning is empty.
if IsErrNoRows(err) && len(returning) == 0 {
return nil
}
return err
}
func generateSaveQuery(schema *Schema, model Model, hasPK bool, returning *[]string, values loukoum.Map) error {
instance := reflectx.GetIndirectValue(model)
for _, column := range schema.fields {
if column.IsPrimaryKey() {
continue
}
name := column.ColumnName()
value, err := reflectx.GetFieldValueWithIndexes(instance, column.FieldIndex())
if err != nil {
return err
}
if !hasPK && column.HasDefault() && reflectx.IsZero(value) {
(*returning) = append((*returning), name)
} else if column.IsUpdatedKey() && hasPK {
values[name] = loukoum.Raw("NOW()")
(*returning) = append((*returning), name)
} else {
values[name] = value
}
}
return nil
}
func getSaveBuilder(driver Driver, model Model, pk PrimaryKey,
hasPK bool, id interface{}, returning *[]string, values loukoum.Map) (builder.Builder, error) {
if !hasPK {
switch pk.Default() {
case PrimaryKeyDBDefault:
(*returning) = append((*returning), pk.ColumnName())
case PrimaryKeyULIDDefault:
ulid := GenerateULID(driver)
values[pk.ColumnName()] = ulid
(*returning) = append((*returning), pk.ColumnName())
case PrimaryKeyUUIDV1Default:
uuid := GenerateUUIDV1(driver)
values[pk.ColumnName()] = uuid
(*returning) = append((*returning), pk.ColumnName())
case PrimaryKeyUUIDV4Default:
uuid := GenerateUUIDV4(driver)
values[pk.ColumnName()] = uuid
(*returning) = append((*returning), pk.ColumnName())
default:
return nil, errors.Errorf("unsupported primary key type: %s", pk.Default())
}
builder := loukoum.Insert(model.TableName()).
Set(values).
Returning((*returning))
return builder, nil
}
builder := loukoum.Update(model.TableName()).
Set(values).
Where(loukoum.Condition(pk.ColumnName()).Equal(id)).
Returning((*returning))
return builder, nil
}