Add support for extracting `2006-01-02 15:04:05.000` formatted datetime.

This commit is contained in:
Dobrosław Żybort 2012-11-29 13:26:24 +01:00
parent 68952ca066
commit 430103cc9e
2 changed files with 76 additions and 4 deletions

View File

@ -28,6 +28,7 @@ import (
const SQLiteTimestampFormat = "2006-01-02 15:04:05" const SQLiteTimestampFormat = "2006-01-02 15:04:05"
const SQLiteDateFormat = "2006-01-02" const SQLiteDateFormat = "2006-01-02"
const SQLiteDatetimeFormat = "2006-01-02 15:04:05.000"
func init() { func init() {
sql.Register("sqlite3", &SQLiteDriver{}) sql.Register("sqlite3", &SQLiteDriver{})
@ -311,12 +312,15 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
case C.SQLITE_TEXT: case C.SQLITE_TEXT:
var err error var err error
s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i))))) s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))))
if rc.decltype[i] == "timestamp" { if rc.decltype[i] == "timestamp" || rc.decltype[i] == "datetime" {
dest[i], err = time.Parse(SQLiteTimestampFormat, s) dest[i], err = time.Parse(SQLiteTimestampFormat, s)
if err != nil { if err != nil {
dest[i], err = time.Parse(SQLiteDateFormat, s) dest[i], err = time.Parse(SQLiteDateFormat, s)
if err != nil { if err != nil {
return err dest[i], err = time.Parse(SQLiteDatetimeFormat, s)
if err != nil {
return err
}
} }
} }
} else { } else {

View File

@ -438,7 +438,7 @@ func TestDateOnlyTimestamp(t *testing.T) {
defer rows.Close() defer rows.Close()
if !rows.Next() { if !rows.Next() {
if er := rows.Err() ; er != nil { if er := rows.Err(); er != nil {
t.Fatal(er) t.Fatal(er)
} else { } else {
t.Fatalf("Unable to extract row containing date-only timestamp") t.Fatalf("Unable to extract row containing date-only timestamp")
@ -448,7 +448,7 @@ func TestDateOnlyTimestamp(t *testing.T) {
var entryId int64 var entryId int64
var entryPublished time.Time var entryPublished time.Time
if er := rows.Scan(&entryId, &entryPublished) ; er != nil { if er := rows.Scan(&entryId, &entryPublished); er != nil {
t.Fatal(er) t.Fatal(er)
} }
@ -460,3 +460,71 @@ func TestDateOnlyTimestamp(t *testing.T) {
t.Errorf("Extracted time does not match inserted value") t.Errorf("Extracted time does not match inserted value")
} }
} }
func TestDatetime(t *testing.T) {
db, err := sql.Open("sqlite3", "./datetime.db")
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer func() {
db.Close()
os.Remove("./datetime.db")
}()
_, err = db.Exec("DROP TABLE datetimetest")
_, err = db.Exec(`
CREATE TABLE datetimetest
( entry_id INTEGER
, my_datetime DATETIME
)
`)
if err != nil {
t.Fatal("Failed to create table:", err)
}
datetime := "2006-01-02 15:04:05.003"
_, err = db.Exec(`
INSERT INTO datetimetest(entry_id, my_datetime)
VALUES(1, ?)`, datetime)
if err != nil {
t.Fatal("Failed to insert datetime:", err)
}
rows, err := db.Query(
"SELECT entry_id, my_datetime FROM datetimetest ORDER BY entry_id ASC")
if err != nil {
t.Fatal("Unable to query datetimetest table:", err)
}
defer rows.Close()
if !rows.Next() {
if er := rows.Err(); er != nil {
t.Fatal(er)
} else {
t.Fatalf("Unable to extract row containing datetime")
}
}
var entryId int
var myDatetime time.Time
if err := rows.Scan(&entryId, &myDatetime); err != nil {
t.Error("Unable to scan results:", err)
}
if entryId != 1 {
t.Errorf("EntryId does not match inserted value")
}
if myDatetime.Year() != 2006 ||
myDatetime.Month() != 1 ||
myDatetime.Day() != 2 ||
myDatetime.Hour() != 15 ||
myDatetime.Minute() != 4 ||
myDatetime.Second() != 5 ||
myDatetime.Nanosecond() != 3000000 {
t.Errorf("Extracted time does not match inserted value")
}
}