MCPcopy
hub / github.com/gogf/gf / formatWhereKeyValue

Function formatWhereKeyValue

database/gdb/gdb_func.go:716–802  ·  view source on GitHub ↗

formatWhereKeyValue handles each key-value pair of the parameter map.

(in formatWhereKeyValueInput)

Source from the content-addressed store, hash-verified

714
715// formatWhereKeyValue handles each key-value pair of the parameter map.
716func formatWhereKeyValue(in formatWhereKeyValueInput) (newArgs []any) {
717 var (
718 quotedKey = in.Db.GetCore().QuoteWord(in.Key)
719 holderCount = gstr.Count(quotedKey, "?")
720 )
721 if isKeyValueCanBeOmitEmpty(in.OmitEmpty, in.Type, quotedKey, in.Value) {
722 return in.Args
723 }
724 if in.Prefix != "" && !gstr.Contains(quotedKey, ".") {
725 quotedKey = in.Prefix + "." + quotedKey
726 }
727 if in.Buffer.Len() > 0 {
728 in.Buffer.WriteString(" AND ")
729 }
730 // If the value is type of slice, and there's only one '?' holder in
731 // the key string, it automatically adds '?' holder chars according to its arguments count
732 // and converts it to "IN" statement.
733 var (
734 reflectValue = reflect.ValueOf(in.Value)
735 reflectKind = reflectValue.Kind()
736 )
737 // Check if the value implements iString interface (like uuid.UUID).
738 // These types should be treated as single values, not arrays.
739 if reflectKind == reflect.Array {
740 if v, ok := in.Value.(iString); ok {
741 in.Value = v.String()
742 reflectKind = reflect.String
743 }
744 }
745 switch reflectKind {
746 // Slice argument.
747 case reflect.Slice, reflect.Array:
748 if holderCount == 0 {
749 in.Buffer.WriteString(quotedKey + " IN(?)")
750 in.Args = append(in.Args, in.Value)
751 } else {
752 if holderCount != reflectValue.Len() {
753 in.Buffer.WriteString(quotedKey)
754 in.Args = append(in.Args, in.Value)
755 } else {
756 in.Buffer.WriteString(quotedKey)
757 in.Args = append(in.Args, gconv.Interfaces(in.Value)...)
758 }
759 }
760
761 default:
762 if in.Value == nil || empty.IsNil(reflectValue) {
763 if gregex.IsMatchString(regularFieldNameRegPattern, in.Key) {
764 // The key is a single field name.
765 in.Buffer.WriteString(quotedKey + " IS NULL")
766 } else {
767 // The key may have operation chars.
768 in.Buffer.WriteString(quotedKey)
769 }
770 } else {
771 // It also supports "LIKE" statement, which we consider it an operator.
772 quotedKey = gstr.Trim(quotedKey)
773 if gstr.Pos(quotedKey, "?") == -1 {

Callers 1

formatWhereHolderFunction · 0.85

Calls 15

CountFunction · 0.92
ContainsFunction · 0.92
InterfacesFunction · 0.92
IsNilFunction · 0.92
IsMatchStringFunction · 0.92
TrimFunction · 0.92
PosFunction · 0.92
EqualFunction · 0.92
IsEmptyFunction · 0.92
isKeyValueCanBeOmitEmptyFunction · 0.85
WriteStringMethod · 0.80
KindMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…