Skip to content

Commit 4358879

Browse files
committed
more datetime
1 parent b344b07 commit 4358879

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

internal/dinosql/gen.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,19 @@ func (r Result) goInnerType(col core.Column) string {
552552
case "bytea", "blob", "pg_catalog.bytea":
553553
return "[]byte"
554554

555-
case "pg_catalog.timestamp", "pg_catalog.timestamptz", "timestamptz", "date":
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+
567+
case "pg_catalog.timestamp", "pg_catalog.timestamptz", "timestamptz":
556568
if notNull {
557569
return "time.Time"
558570
}

internal/dinosql/gen_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +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",
100-
"date": "time.Time",
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",
101106
}
102107
for k, v := range types {
103108
dbType := k
@@ -114,11 +119,16 @@ func TestInnerType(t *testing.T) {
114119
func TestNullInnerType(t *testing.T) {
115120
r := Result{}
116121
types := map[string]string{
117-
"timestamptz": "sql.NullTime",
118122
"integer": "sql.NullInt32",
119123
"int": "sql.NullInt32",
120124
"pg_catalog.int4": "sql.NullInt32",
121-
"date": "sql.NullTime",
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",
122132
}
123133
for k, v := range types {
124134
dbType := k

internal/dinosql/query_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,28 @@ func TestQueries(t *testing.T) {
814814
},
815815
},
816816
{
817-
"date",
818-
`
819-
CREATE TABLE users ( birthday DATE );
820-
SELECT birthday FROM users;
821-
`,
822-
Query{
823-
Columns: []core.Column{
824-
{Table: public("users"), Name: "birthday", DataType: "date"},
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"},
825839
},
826840
},
827841
},

0 commit comments

Comments
 (0)