Skip to content

Commit 11a21ad

Browse files
authored
internal/dinosql: Add support for numeric types (#228)
* internal/dinosql: Add support for numeric types
2 parents ed447d6 + 891d4a0 commit 11a21ad

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

internal/dinosql/gen.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,16 @@ func (r Result) goInnerType(col core.Column) string {
551551
}
552552
return "sql.NullFloat64" // TODO: Change to sql.NullFloat32 after updating the go.mod file
553553

554+
case "pg_catalog.numeric":
555+
// Since the Go standard library does not have a decimal type, lib/pq
556+
// returns numerics as strings.
557+
//
558+
// https://github.com/lib/pq/issues/648
559+
if notNull {
560+
return "string"
561+
}
562+
return "sql.NullString"
563+
554564
case "bool", "pg_catalog.bool":
555565
if notNull {
556566
return "bool"

internal/dinosql/gen_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,19 @@ func TestColumnsToStruct(t *testing.T) {
9393
func TestInnerType(t *testing.T) {
9494
r := Result{}
9595
types := map[string]string{
96-
"integer": "int32",
97-
"int": "int32",
98-
"pg_catalog.int4": "int32",
99-
"string": "string",
100-
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
96+
// Numeric Types
97+
// https://www.postgresql.org/docs/current/datatype-numeric.html
98+
"integer": "int32",
99+
"int": "int32",
100+
"pg_catalog.int4": "int32",
101+
"pg_catalog.numeric": "string",
102+
103+
// Character Types
104+
// https://www.postgresql.org/docs/current/datatype-character.html
105+
"string": "string",
106+
107+
// Date/Time Types
108+
// https://www.postgresql.org/docs/current/datatype-datetime.html
101109
"date": "time.Time",
102110
"pg_catalog.time": "time.Time",
103111
"pg_catalog.timetz": "time.Time",
@@ -120,11 +128,19 @@ func TestInnerType(t *testing.T) {
120128
func TestNullInnerType(t *testing.T) {
121129
r := Result{}
122130
types := map[string]string{
123-
"integer": "sql.NullInt32",
124-
"int": "sql.NullInt32",
125-
"pg_catalog.int4": "sql.NullInt32",
126-
"string": "sql.NullString",
127-
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
131+
// Numeric Types
132+
// https://www.postgresql.org/docs/current/datatype-numeric.html
133+
"integer": "sql.NullInt32",
134+
"int": "sql.NullInt32",
135+
"pg_catalog.int4": "sql.NullInt32",
136+
"pg_catalog.numeric": "sql.NullString",
137+
138+
// Character Types
139+
// https://www.postgresql.org/docs/current/datatype-character.html
140+
"string": "sql.NullString",
141+
142+
// Date/Time Types
143+
// https://www.postgresql.org/docs/current/datatype-datetime.html
128144
"date": "sql.NullTime",
129145
"pg_catalog.time": "sql.NullTime",
130146
"pg_catalog.timetz": "sql.NullTime",

0 commit comments

Comments
 (0)