From 430103cc9e7fbacbb9fce303268b967388ee5d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dobros=C5=82aw=20=C5=BBybort?= Date: Thu, 29 Nov 2012 13:26:24 +0100 Subject: [PATCH] Add support for extracting `2006-01-02 15:04:05.000` formatted datetime. --- sqlite3.go | 8 ++++-- sqlite3_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 90a1241..91c5760 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -28,6 +28,7 @@ import ( const SQLiteTimestampFormat = "2006-01-02 15:04:05" const SQLiteDateFormat = "2006-01-02" +const SQLiteDatetimeFormat = "2006-01-02 15:04:05.000" func init() { sql.Register("sqlite3", &SQLiteDriver{}) @@ -311,12 +312,15 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { case C.SQLITE_TEXT: var err error 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) if err != nil { dest[i], err = time.Parse(SQLiteDateFormat, s) if err != nil { - return err + dest[i], err = time.Parse(SQLiteDatetimeFormat, s) + if err != nil { + return err + } } } } else { diff --git a/sqlite3_test.go b/sqlite3_test.go index 4ff8d6c..68d9680 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -438,7 +438,7 @@ func TestDateOnlyTimestamp(t *testing.T) { defer rows.Close() if !rows.Next() { - if er := rows.Err() ; er != nil { + if er := rows.Err(); er != nil { t.Fatal(er) } else { t.Fatalf("Unable to extract row containing date-only timestamp") @@ -448,7 +448,7 @@ func TestDateOnlyTimestamp(t *testing.T) { var entryId int64 var entryPublished time.Time - if er := rows.Scan(&entryId, &entryPublished) ; er != nil { + if er := rows.Scan(&entryId, &entryPublished); er != nil { t.Fatal(er) } @@ -460,3 +460,71 @@ func TestDateOnlyTimestamp(t *testing.T) { 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") + } + +}