mirror of https://github.com/mattn/go-sqlite3.git
commit
8706f7baf0
28
sqlite3.go
28
sqlite3.go
|
@ -213,7 +213,7 @@ func (s *SQLiteStmt) bind(args []driver.Value) error {
|
||||||
}
|
}
|
||||||
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v)))
|
rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v)))
|
||||||
case time.Time:
|
case time.Time:
|
||||||
b := []byte(v.Format(SQLiteTimestampFormat))
|
b := []byte(v.UTC().Format(SQLiteTimestampFormat))
|
||||||
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
|
||||||
}
|
}
|
||||||
if rv != C.SQLITE_OK {
|
if rv != C.SQLITE_OK {
|
||||||
|
@ -299,7 +299,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
||||||
case C.SQLITE_INTEGER:
|
case C.SQLITE_INTEGER:
|
||||||
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
|
||||||
switch rc.decltype[i] {
|
switch rc.decltype[i] {
|
||||||
case "timestamp":
|
case "timestamp", "datetime":
|
||||||
dest[i] = time.Unix(val, 0)
|
dest[i] = time.Unix(val, 0)
|
||||||
case "boolean":
|
case "boolean":
|
||||||
dest[i] = val > 0
|
dest[i] = val > 0
|
||||||
|
@ -325,21 +325,21 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
|
||||||
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)))))
|
||||||
|
|
||||||
switch dest[i].(type) {
|
switch rc.decltype[i] {
|
||||||
case *time.Time:
|
case "timestamp", "datetime":
|
||||||
if rc.decltype[i] == "timestamp" || rc.decltype[i] == "datetime" {
|
for {
|
||||||
dest[i], err = time.Parse(SQLiteTimestampFormat, s)
|
if dest[i], err = time.Parse(SQLiteTimestampFormat, s); err == nil {
|
||||||
if err != nil {
|
break
|
||||||
dest[i], err = time.Parse(SQLiteDateFormat, s)
|
|
||||||
if err != nil {
|
|
||||||
dest[i], err = time.Parse(SQLiteDatetimeFormat, s)
|
|
||||||
if err != nil {
|
|
||||||
dest[i] = s
|
|
||||||
}
|
}
|
||||||
|
if dest[i], err = time.Parse(SQLiteDateFormat, s); err == nil {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
if dest[i], err = time.Parse(SQLiteDatetimeFormat, s); err == nil {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
} else {
|
// The column is a time value, so return the zero time on parse failure.
|
||||||
dest[i] = s
|
dest[i] = time.Time{}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
dest[i] = s
|
dest[i] = s
|
||||||
|
|
|
@ -3,7 +3,6 @@ package sqlite
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -268,6 +267,12 @@ func TestTimestamp(t *testing.T) {
|
||||||
t.Fatal("Failed to insert nonsense:", err)
|
t.Fatal("Failed to insert nonsense:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timestamp4 := time.Date(2012, time.April, 6, 23, 22, 0, 0, time.FixedZone("TEST", -7*3600))
|
||||||
|
_, err = db.Exec("INSERT INTO foo(id, ts) VALUES(4, ?)", timestamp4)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Failed to insert timestamp:", err)
|
||||||
|
}
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id, ts FROM foo ORDER BY id ASC")
|
rows, err := db.Query("SELECT id, ts FROM foo ORDER BY id ASC")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to query foo table:", err)
|
t.Fatal("Unable to query foo table:", err)
|
||||||
|
@ -297,16 +302,24 @@ func TestTimestamp(t *testing.T) {
|
||||||
t.Errorf("Value for id 2 should be %v, not %v", timestamp2, ts)
|
t.Errorf("Value for id 2 should be %v, not %v", timestamp2, ts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if id == 3 {
|
||||||
|
seen += 1
|
||||||
|
if !ts.IsZero() {
|
||||||
|
t.Errorf("Value for id 3 should be the zero time, not %v", ts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if seen != 2 {
|
if id == 4 {
|
||||||
t.Error("Expected to see two valid timestamps")
|
seen += 1
|
||||||
|
if !timestamp4.Equal(ts) {
|
||||||
|
t.Errorf("Value for id 4 should be %v, not %v", timestamp4, ts)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure "nonsense" triggered an error
|
if seen != 4 {
|
||||||
err = rows.Err()
|
t.Error("Expected to see four timestamps")
|
||||||
if err == nil || !strings.Contains(err.Error(), "cannot parse \"nonsense\"") {
|
|
||||||
t.Error("Expected error from \"nonsense\" timestamp")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue