MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / ResolveUpdate

Function ResolveUpdate

cmd/cql-proxy/resolver/resolver.go:292–406  ·  view source on GitHub ↗

ResolveUpdate resolves update object as sql update set statement.

(q map[string]interface{}, availFields FieldMap)

Source from the content-addressed store, hash-verified

290
291// ResolveUpdate resolves update object as sql update set statement.
292func ResolveUpdate(q map[string]interface{}, availFields FieldMap) (
293 fields FieldMap, statement string, args []interface{}, err error) {
294 fields = FieldMap{}
295 var (
296 subStatements []string
297 useDollarOp bool
298 useNormalSet bool
299 )
300
301 if len(q) == 0 {
302 err = errors.New("update to empty object is not supported")
303 return
304 }
305
306 for k, v := range q {
307 switch {
308 case k == "$currentDate" || k == "$inc" || k == "$max" || k == "$min" || k == "$mul" || k == "$set":
309 useDollarOp = true
310
311 if useNormalSet {
312 err = errors.New("could not use both normal field update and $ prefixed ops")
313 return
314 }
315
316 // value must be an object
317 var (
318 ov map[string]interface{}
319 ok bool
320 )
321
322 if ov, ok = v.(map[string]interface{}); !ok {
323 err = errors.Errorf("$ operator needs object argument")
324 return
325 }
326
327 for field, argument := range ov {
328 if !availFields[field] {
329 err = errors.Errorf("unknown field: %s", field)
330 return
331 }
332
333 fields[field] = true
334
335 if k == "$currentDate" {
336 if isLiteral(argument) && asBool(argument) {
337 subStatements = append(subStatements, fmt.Sprintf(`"%s" = ?`, field))
338 args = append(args, time.Now().UTC())
339 } else if oa, ok := argument.(map[string]interface{}); ok && isString(oa["$type"]) {
340 switch oa["$type"] {
341 case "date":
342 subStatements = append(subStatements, fmt.Sprintf(`"%s" = ?`, field))
343 args = append(args, time.Now().UTC().Format("2006-01-02"))
344 case "timestamp":
345 subStatements = append(subStatements, fmt.Sprintf(`"%s" = ?`, field))
346 args = append(args, time.Now().Unix())
347 case "datetime":
348 // mongodb does not support $currentDate with datetime
349 // this my special treatment for stupid users

Callers 1

UpdateFunction · 0.85

Calls 6

isLiteralFunction · 0.85
asBoolFunction · 0.85
isStringFunction · 0.85
ErrorfMethod · 0.80
NewMethod · 0.65
FormatMethod · 0.45

Tested by

no test coverage detected