Skip to content

Commit

Permalink
fix: sql.NullString mistaken as custom struct (#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
CL-Jeremy authored Oct 26, 2024
1 parent b4e1de0 commit 87c77b8
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions dialect/pgdialect/sqltype.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgdialect

import (
"database/sql"
"encoding/json"
"net"
"reflect"
Expand All @@ -27,13 +28,6 @@ const (
pgTypeSerial = "SERIAL" // 4 byte autoincrementing integer
pgTypeBigSerial = "BIGSERIAL" // 8 byte autoincrementing integer

// Character Types
pgTypeChar = "CHAR" // fixed length string (blank padded)
pgTypeText = "TEXT" // variable length string without limit

// JSON Types
pgTypeJSON = "JSON" // text representation of json data

// Binary Data Types
pgTypeBytea = "BYTEA" // binary string
)
Expand All @@ -42,6 +36,7 @@ var (
ipType = reflect.TypeOf((*net.IP)(nil)).Elem()
ipNetType = reflect.TypeOf((*net.IPNet)(nil)).Elem()
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
nullStringType = reflect.TypeOf((*sql.NullString)(nil)).Elem()
)

func (d *Dialect) DefaultVarcharLen() int {
Expand Down Expand Up @@ -77,6 +72,8 @@ func fieldSQLType(field *schema.Field) string {

func sqlType(typ reflect.Type) string {
switch typ {
case nullStringType: // typ.Kind() == reflect.Struct, test for exact match
return sqltype.VarChar
case ipType:
return pgTypeInet
case ipNetType:
Expand All @@ -92,7 +89,7 @@ func sqlType(typ reflect.Type) string {
}

switch typ.Kind() {
case reflect.Map, reflect.Struct:
case reflect.Map, reflect.Struct: // except typ == nullStringType, see above
if sqlType == sqltype.VarChar {
return sqltype.JSONB
}
Expand Down

0 comments on commit 87c77b8

Please sign in to comment.