-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues with mapping of
TIMESTAMPZ[]
postgres type
Prior to this PR, sqlc was unable to map `TIMESTAMPZ[]` to `[]time.Time` due a limitation in the `pq` driver. This PR leverages a suggested fix from a Github issue in the `pq` repo.
- Loading branch information
Showing
6 changed files
with
101 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2024 Stacklok, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// PgTime/PgTimeArray is used to work around the pq driver's inability to map | ||
// TIMESTAMPZ[] to []time.Time without some hand holding. | ||
// Code taken mostly as-is from: https://github.com/lib/pq/issues/536#issuecomment-397849980 | ||
|
||
package db | ||
|
||
import ( | ||
"database/sql/driver" | ||
"errors" | ||
"time" | ||
|
||
"github.com/lib/pq" | ||
) | ||
|
||
// ErrParseData signifies that an error occurred while parsing SQL data | ||
var ErrParseData = errors.New("unable to parse SQL data") | ||
|
||
// PgTime wraps a time.Time | ||
type PgTime struct{ time.Time } | ||
|
||
// Scan implements the sql.Scanner interface | ||
func (t *PgTime) Scan(val interface{}) error { | ||
switch v := val.(type) { | ||
case time.Time: | ||
t.Time = v | ||
return nil | ||
case []uint8: // byte is the same as uint8: https://golang.org/pkg/builtin/#byte | ||
_t, err := pq.ParseTimestamp(nil, string(v)) | ||
if err != nil { | ||
return ErrParseData | ||
} | ||
t.Time = _t | ||
return nil | ||
case string: | ||
_t, err := pq.ParseTimestamp(nil, v) | ||
if err != nil { | ||
return ErrParseData | ||
} | ||
t.Time = _t | ||
return nil | ||
} | ||
return ErrParseData | ||
} | ||
|
||
// Value implements the driver.Valuer interface | ||
func (t *PgTime) Value() (driver.Value, error) { return pq.FormatTimestamp(t.Time), nil } | ||
|
||
// PgTimeArray wraps a time.Time slice to be used as a Postgres array | ||
// type PgTimeArray []time.Time | ||
type PgTimeArray []PgTime | ||
|
||
//type PgTimeArray []pq.NullTime | ||
|
||
// Scan implements the sql.Scanner interface | ||
func (a *PgTimeArray) Scan(src interface{}) error { | ||
return pq.GenericArray{A: a}.Scan(src) | ||
} | ||
|
||
// Value implements the driver.Valuer interface | ||
func (a *PgTimeArray) Value() (driver.Value, error) { | ||
return pq.GenericArray{A: a}.Value() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters