-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timestamp is time.Time in go; and date is NullDate, a defined struct in openmldb go sdk.
- Loading branch information
1 parent
01e2025
commit 3a285fa
Showing
5 changed files
with
105 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package openmldb | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"errors" | ||
"time" | ||
) | ||
|
||
var ( | ||
_ sql.Scanner = (*NullDate)(nil) | ||
) | ||
|
||
type NullDate struct { | ||
Time time.Time | ||
Valid bool // Valid is true if Time is not NULL | ||
} | ||
|
||
// Scan implements sql.Scanner for NullDate | ||
func (dt *NullDate) Scan(src any) error { | ||
switch val := src.(type) { | ||
case string: | ||
dval, err := time.Parse(time.DateOnly, val) | ||
if err != nil { | ||
dt.Valid = false | ||
return err | ||
} else { | ||
dt.Time = dval | ||
dt.Valid = true | ||
return nil | ||
} | ||
case NullDate: | ||
*dt = val | ||
return nil | ||
default: | ||
return errors.New("scan NullDate from unsupported type") | ||
} | ||
|
||
} | ||
|
||
// Value implements driver.Value for NullDate | ||
func (dt NullDate) Value() (driver.Value, error) { | ||
if !dt.Valid { | ||
return nil, nil | ||
} | ||
return dt.Time, nil | ||
|
||
} |