-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: database/sql: add NullTime support #30305
Comments
This seems to be taken from https://godoc.org/github.com/lib/pq#NullTime ? |
That's interesting, looks similar, but I was just mimicking the way NullString is handled in go/src/database/sql/sql.go See this line pasted here for ease : // from go/src/database/sql/sql.go
type NullString struct {
String string
Valid bool // Valid is true if String is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullString) Scan(value interface{}) error {
if value == nil {
ns.String, ns.Valid = "", false
return nil
}
ns.Valid = true
return convertAssign(&ns.String, value)
}
// Value implements the driver Valuer interface.
func (ns NullString) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return ns.String, nil
} The implementation in pq is different, note how in pq there is no call to convertAssign. |
convertAssign will need to look some possible time format for each RDBMS's serialization formats like below. |
convertAssign already supports time.Time see go/src/database/sql/convert.go Line 268 in a10b4cf
|
Yes that is a part of code that driver return time.Time. I mean case that driver return string instead of time.Time. For example, NullString is possible to convert number to string. If add NullTime, most of users expect convertion between string and time.Time. But some RDBMS does not return time.Time alawys. For example |
I see! Will have to look into this deeper. Very good point! |
/cc @kardianos |
@mattn While that is true, the current situation is that NullTime is now defined separately for various databases such as postgresql, sqlite, etc, in packages all over the place. Having a NullTime in database/sql would greatly simplify this. What we need to is a mechanism to also allow parsing strings and separate fields as a time. It's then up to the driver writers contribute to the conversion what the driver returns to this type. Like this example for sqlite3: https://github.com/xo/xoutil/blob/master/xoutil.go. |
@kardianos, any thoughts? It seems fine to me. |
Change https://golang.org/cl/170699 mentions this issue: |
Would it be an idea to extend the Nullable types to include NullTime as well?
Something like
Just copy paste into ...go/src/database/sql/sql.go, should work.
The text was updated successfully, but these errors were encountered: