Skip to content

internal/dinosql: Add DATE support #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/dinosql/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,18 @@ func (r Result) goInnerType(col core.Column) string {
case "bytea", "blob", "pg_catalog.bytea":
return "[]byte"

case "date":
if notNull {
return "time.Time"
}
return "sql.NullTime"

case "pg_catalog.time", "pg_catalog.timetz":
if notNull {
return "time.Time"
}
return "sql.NullTime"

case "pg_catalog.timestamp", "pg_catalog.timestamptz", "timestamptz":
if notNull {
return "time.Time"
Expand Down
16 changes: 14 additions & 2 deletions internal/dinosql/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ func TestColumnsToStruct(t *testing.T) {
func TestInnerType(t *testing.T) {
r := Result{}
types := map[string]string{
"timestamptz": "time.Time",
"integer": "int32",
"int": "int32",
"pg_catalog.int4": "int32",
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
"date": "time.Time",
"pg_catalog.time": "time.Time",
"pg_catalog.timetz": "time.Time",
"pg_catalog.timestamp": "time.Time",
"pg_catalog.timestamptz": "time.Time",
"timestamptz": "time.Time",
}
for k, v := range types {
dbType := k
Expand All @@ -113,10 +119,16 @@ func TestInnerType(t *testing.T) {
func TestNullInnerType(t *testing.T) {
r := Result{}
types := map[string]string{
"timestamptz": "sql.NullTime",
"integer": "sql.NullInt32",
"int": "sql.NullInt32",
"pg_catalog.int4": "sql.NullInt32",
// Date/Time Types https://www.postgresql.org/docs/current/datatype-datetime.html
"date": "sql.NullTime",
"pg_catalog.time": "sql.NullTime",
"pg_catalog.timetz": "sql.NullTime",
"pg_catalog.timestamp": "sql.NullTime",
"pg_catalog.timestamptz": "sql.NullTime",
"timestamptz": "sql.NullTime",
}
for k, v := range types {
dbType := k
Expand Down
26 changes: 26 additions & 0 deletions internal/dinosql/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,32 @@ func TestQueries(t *testing.T) {
SQL: "SELECT a, b, c, d FROM foo, bar",
},
},
{
"datetimes",
`
CREATE TABLE users (
d DATE,
t TIME,
t_notz TIME WITHOUT TIME ZONE,
t_tz TIME WITH TIME ZONE,
ts TIMESTAMP,
ts_notz TIMESTAMP WITHOUT TIME ZONE,
ts_tz TIMESTAMP WITH TIME ZONE
);
SELECT * FROM users;
`,
Query{
Columns: []core.Column{
{Table: public("users"), Name: "d", DataType: "date"},
{Table: public("users"), Name: "t", DataType: "pg_catalog.time"},
{Table: public("users"), Name: "t_notz", DataType: "pg_catalog.time"},
{Table: public("users"), Name: "t_tz", DataType: "pg_catalog.timetz"},
{Table: public("users"), Name: "ts", DataType: "pg_catalog.timestamp"},
{Table: public("users"), Name: "ts_notz", DataType: "pg_catalog.timestamp"},
{Table: public("users"), Name: "ts_tz", DataType: "pg_catalog.timestamptz"},
},
},
},
} {
test := tc
t.Run(test.name, func(t *testing.T) {
Expand Down