Skip to content

Commit 4c9f713

Browse files
authored
internal/dinosql: Add DATE support (#196)
Generate the correct types for: - DATE - TIME [WITHOUT TIME ZONE] - TIME WITH TIME ZONE
2 parents 40c71de + 4358879 commit 4c9f713

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

internal/dinosql/gen.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,18 @@ func (r Result) goInnerType(col core.Column) string {
552552
case "bytea", "blob", "pg_catalog.bytea":
553553
return "[]byte"
554554

555+
case "date":
556+
if notNull {
557+
return "time.Time"
558+
}
559+
return "sql.NullTime"
560+
561+
case "pg_catalog.time", "pg_catalog.timetz":
562+
if notNull {
563+
return "time.Time"
564+
}
565+
return "sql.NullTime"
566+
555567
case "pg_catalog.timestamp", "pg_catalog.timestamptz", "timestamptz":
556568
if notNull {
557569
return "time.Time"

internal/dinosql/gen_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,16 @@ func TestColumnsToStruct(t *testing.T) {
9393
func TestInnerType(t *testing.T) {
9494
r := Result{}
9595
types := map[string]string{
96-
"timestamptz": "time.Time",
9796
"integer": "int32",
9897
"int": "int32",
9998
"pg_catalog.int4": "int32",
99+
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
100+
"date": "time.Time",
101+
"pg_catalog.time": "time.Time",
102+
"pg_catalog.timetz": "time.Time",
103+
"pg_catalog.timestamp": "time.Time",
104+
"pg_catalog.timestamptz": "time.Time",
105+
"timestamptz": "time.Time",
100106
}
101107
for k, v := range types {
102108
dbType := k
@@ -113,10 +119,16 @@ func TestInnerType(t *testing.T) {
113119
func TestNullInnerType(t *testing.T) {
114120
r := Result{}
115121
types := map[string]string{
116-
"timestamptz": "sql.NullTime",
117122
"integer": "sql.NullInt32",
118123
"int": "sql.NullInt32",
119124
"pg_catalog.int4": "sql.NullInt32",
125+
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
126+
"date": "sql.NullTime",
127+
"pg_catalog.time": "sql.NullTime",
128+
"pg_catalog.timetz": "sql.NullTime",
129+
"pg_catalog.timestamp": "sql.NullTime",
130+
"pg_catalog.timestamptz": "sql.NullTime",
131+
"timestamptz": "sql.NullTime",
120132
}
121133
for k, v := range types {
122134
dbType := k

internal/dinosql/query_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,32 @@ func TestQueries(t *testing.T) {
813813
SQL: "SELECT a, b, c, d FROM foo, bar",
814814
},
815815
},
816+
{
817+
"datetimes",
818+
`
819+
CREATE TABLE users (
820+
d DATE,
821+
t TIME,
822+
t_notz TIME WITHOUT TIME ZONE,
823+
t_tz TIME WITH TIME ZONE,
824+
ts TIMESTAMP,
825+
ts_notz TIMESTAMP WITHOUT TIME ZONE,
826+
ts_tz TIMESTAMP WITH TIME ZONE
827+
);
828+
SELECT * FROM users;
829+
`,
830+
Query{
831+
Columns: []core.Column{
832+
{Table: public("users"), Name: "d", DataType: "date"},
833+
{Table: public("users"), Name: "t", DataType: "pg_catalog.time"},
834+
{Table: public("users"), Name: "t_notz", DataType: "pg_catalog.time"},
835+
{Table: public("users"), Name: "t_tz", DataType: "pg_catalog.timetz"},
836+
{Table: public("users"), Name: "ts", DataType: "pg_catalog.timestamp"},
837+
{Table: public("users"), Name: "ts_notz", DataType: "pg_catalog.timestamp"},
838+
{Table: public("users"), Name: "ts_tz", DataType: "pg_catalog.timestamptz"},
839+
},
840+
},
841+
},
816842
} {
817843
test := tc
818844
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)