formatWhereKeyValue handles each key-value pair of the parameter map.
(in formatWhereKeyValueInput)
| 714 | |
| 715 | // formatWhereKeyValue handles each key-value pair of the parameter map. |
| 716 | func 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 { |
no test coverage detected
searching dependent graphs…