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

Function buildInsertQueryValues

persistence/sql/batch/create.go:136–190  ·  view source on GitHub ↗
(columns []string, models []*T, opts *createOpts)

Source from the content-addressed store, hash-verified

134}
135
136func buildInsertQueryValues[T any](columns []string, models []*T, opts *createOpts) (values []any, err error) {
137 for _, m := range models {
138 m := reflect.ValueOf(m)
139
140 now := opts.now()
141 // Append model fields to args
142 for _, c := range columns {
143 field := opts.mapper.FieldByName(m, c)
144
145 switch c {
146 case "created_at":
147 if pop.IsZeroOfUnderlyingType(field.Interface()) {
148 field.Set(reflect.ValueOf(now))
149 }
150 case "updated_at":
151 field.Set(reflect.ValueOf(now))
152 case "id":
153 if field.Interface().(uuid.UUID) != uuid.Nil {
154 break // breaks switch, not for
155 } else if opts.dialect == dbal.DriverCockroachDB && !opts.partialInserts {
156 // This is a special case:
157 // 1. We're using cockroach
158 // 2. It's the primary key field ("ID")
159 // 3. A UUID was not yet set.
160 //
161 // If all these conditions meet, the VALUE statement will look as such:
162 //
163 // (gen_random_uuid(), ?, ?, ?, ...)
164 //
165 // For that reason, we do not add the ID value to the list of arguments,
166 // because one of the arguments is using a built-in and thus doesn't need a value.
167 continue // break switch, not for
168 }
169
170 id, err := uuid.NewV4()
171 if err != nil {
172 return nil, err
173 }
174 field.Set(reflect.ValueOf(id))
175 }
176
177 values = append(values, field.Interface())
178
179 // Special-handling for *sqlxx.NullTime: mapper.FieldByName sets this to a zero time.Time,
180 // but we want a nil pointer instead.
181 if i, ok := field.Interface().(*sqlxx.NullTime); ok {
182 if time.Time(*i).IsZero() {
183 field.Set(reflect.Zero(field.Type()))
184 }
185 }
186 }
187 }
188
189 return values, nil
190}
191
192type createOpts struct {
193 partialInserts bool

Callers 2

CreateFunction · 0.70

Calls 4

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

Tested by 1