improve gdb.CheckValueForLocalType for pgsql (#2040)

This commit is contained in:
Gin
2022-08-08 19:56:06 +08:00
committed by GitHub
parent 82a3391937
commit 4ded89d453
3 changed files with 27 additions and 18 deletions

View File

@ -24,6 +24,8 @@ const (
typeDatetime = "datetime"
typeInt64Bytes = "int64-bytes"
typeUint64Bytes = "uint64-bytes"
typeJson = "json"
typeJsonb = "jsonb"
)
func generateStructDefinition(ctx context.Context, in generateStructDefinitionInput) string {
@ -84,7 +86,7 @@ func generateStructFieldDefinition(
typeName = "uint64"
// Special type handle.
case "json", "jsonb":
case typeJson, typeJsonb:
if in.GJsonSupport {
typeName = "*gjson.Json"
} else {

View File

@ -125,19 +125,17 @@ func (d *Driver) ConvertValueForLocal(ctx context.Context, fieldType string, fie
typeName, _ := gregex.ReplaceString(`\(.+\)`, "", fieldType)
typeName = strings.ToLower(typeName)
switch typeName {
// For pgsql, int2 = smallint and int4 = integer.
case "int2", "int4":
return gconv.Int(gconv.String(fieldValue)), nil
// For pgsql, int8 = bigint.
case "int8":
if gstr.ContainsI(fieldType, "unsigned") {
return gconv.Uint64(gconv.String(fieldValue)), nil
}
return gconv.Int64(gconv.String(fieldValue)), nil
// Int32 slice.
case
"_int2":
if gstr.ContainsI(fieldType, "unsigned") {
gconv.Uints(gconv.String(fieldValue))
}
"_int2", "_int4":
return gconv.Ints(
gstr.ReplaceByMap(gconv.String(fieldValue),
map[string]string{
@ -149,10 +147,7 @@ func (d *Driver) ConvertValueForLocal(ctx context.Context, fieldType string, fie
// Int64 slice.
case
"_int4", "_int8":
if gstr.ContainsI(fieldType, "unsigned") {
gconv.Uint64(gconv.String(fieldValue))
}
"_int8":
return gconv.Int64s(
gstr.ReplaceByMap(gconv.String(fieldValue),
map[string]string{

View File

@ -23,6 +23,7 @@ const (
typeUint = "uint"
typeInt64 = "int64"
typeUint64 = "uint64"
typeIntSlice = "[]int"
typeInt64Slice = "[]int64"
typeUint64Slice = "[]uint64"
typeInt64Bytes = "int64-bytes"
@ -31,6 +32,8 @@ const (
typeFloat64 = "float64"
typeBytes = "[]byte"
typeBool = "bool"
typeJson = "json"
typeJsonb = "jsonb"
)
// CheckValueForLocalType checks and returns corresponding type for given db type.
@ -58,6 +61,8 @@ func CheckValueForLocalType(ctx context.Context, fieldType string, fieldValue in
return typeBytes, nil
case
"int2", // For pgsql, int2 = smallint.
"int4", // For pgsql, int4 = integer.
"int",
"tinyint",
"small_int",
@ -70,13 +75,8 @@ func CheckValueForLocalType(ctx context.Context, fieldType string, fieldValue in
}
return typeInt, nil
case "_int4", "_int8":
if gstr.ContainsI(fieldType, "unsigned") {
return typeUint64Slice, nil
}
return typeInt64Slice, nil
case
"int8", // For pgsql, int8 = bigint.
"big_int",
"bigint",
"bigserial":
@ -85,6 +85,12 @@ func CheckValueForLocalType(ctx context.Context, fieldType string, fieldValue in
}
return typeInt64, nil
case "_int2", "_int4":
return typeIntSlice, nil
case "_int8":
return typeInt64Slice, nil
case "real":
return typeFloat32, nil
@ -124,6 +130,12 @@ func CheckValueForLocalType(ctx context.Context, fieldType string, fieldValue in
"timestamptz":
return typeDatetime, nil
case "json":
return typeJson, nil
case "jsonb":
return typeJsonb, nil
default:
// Auto-detect field type, using key match.
switch {