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

Method ConvertValueForField

contrib/drivers/pgsql/pgsql_convert.go:25–48  ·  view source on GitHub ↗

ConvertValueForField converts value to database acceptable value.

(ctx context.Context, fieldType string, fieldValue any)

Source from the content-addressed store, hash-verified

23
24// ConvertValueForField converts value to database acceptable value.
25func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue any) (any, error) {
26 if g.IsNil(fieldValue) {
27 return d.Core.ConvertValueForField(ctx, fieldType, fieldValue)
28 }
29
30 var fieldValueKind = reflect.TypeOf(fieldValue).Kind()
31
32 if fieldValueKind == reflect.Slice {
33 // For bytea type, pass []byte directly without any conversion.
34 if _, ok := fieldValue.([]byte); ok && gstr.Contains(fieldType, "bytea") {
35 return d.Core.ConvertValueForField(ctx, fieldType, fieldValue)
36 }
37 // For pgsql, json or jsonb require '[]'
38 if !gstr.Contains(fieldType, "json") {
39 fieldValue = gstr.ReplaceByMap(gconv.String(fieldValue),
40 map[string]string{
41 "[": "{",
42 "]": "}",
43 },
44 )
45 }
46 }
47 return d.Core.ConvertValueForField(ctx, fieldType, fieldValue)
48}
49
50// CheckLocalTypeForField checks and returns corresponding local golang type for given db type.
51// The parameter `fieldType` is in lower case, like:

Callers

nothing calls this directly

Calls 6

IsNilFunction · 0.92
ContainsFunction · 0.92
ReplaceByMapFunction · 0.92
StringFunction · 0.92
KindMethod · 0.80
ConvertValueForFieldMethod · 0.65

Tested by

no test coverage detected