MCPcopy
hub / github.com/ory/kratos / buildInsertQueryValues

Function buildInsertQueryValues

oryx/sqlxx/batch/create.go:113–169  ·  view source on GitHub ↗
(dialect string, mapper *reflectx.Mapper, columns []string, models []*T, nowFunc func() time.Time)

Source from the content-addressed store, hash-verified

111}
112
113func buildInsertQueryValues[T any](dialect string, mapper *reflectx.Mapper, columns []string, models []*T, nowFunc func() time.Time) (values []any, err error) {
114 for _, m := range models {
115 m := reflect.ValueOf(m)
116
117 now := nowFunc()
118 // Append model fields to args
119 for _, c := range columns {
120 field := mapper.FieldByName(m, c)
121
122 switch c {
123 case "created_at":
124 if pop.IsZeroOfUnderlyingType(field.Interface()) {
125 field.Set(reflect.ValueOf(now))
126 }
127 case "updated_at":
128 field.Set(reflect.ValueOf(now))
129 case "id":
130 if value, ok := field.Interface().(uuid.UUID); ok && value != uuid.Nil {
131 break // breaks switch, not for
132 } else if value, ok := field.Interface().(string); ok && len(value) > 0 {
133 break // breaks switch, not for
134 } else if dialect == dbal.DriverCockroachDB {
135 // This is a special case:
136 // 1. We're using cockroach
137 // 2. It's the primary key field ("ID")
138 // 3. A UUID was not yet set.
139 //
140 // If all these conditions meet, the VALUE statement will look as such:
141 //
142 // (gen_random_uuid(), ?, ?, ?, ...)
143 //
144 // For that reason, we do not add the ID value to the list of arguments,
145 // because one of the arguments is using a built-in and thus doesn't need a value.
146 continue // break switch, not for
147 }
148
149 id, err := uuid.NewV4()
150 if err != nil {
151 return nil, err
152 }
153 field.Set(reflect.ValueOf(id))
154 }
155
156 values = append(values, field.Interface())
157
158 // Special-handling for *sqlxx.NullTime: mapper.FieldByName sets this to a zero time.Time,
159 // but we want a nil pointer instead.
160 if i, ok := field.Interface().(*sqlxx.NullTime); ok {
161 if time.Time(*i).IsZero() {
162 field.Set(reflect.Zero(field.Type()))
163 }
164 }
165 }
166 }
167
168 return values, nil
169}
170

Callers 1

CreateFunction · 0.70

Calls 4

InterfaceMethod · 0.65
SetMethod · 0.45
IsZeroMethod · 0.45
TypeMethod · 0.45

Tested by

no test coverage detected