Skip to content
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

Add support for datetime columns declared with a precision #1221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ import (
"io"
"net/url"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -2145,11 +2146,14 @@ func (rc *SQLiteRows) Columns() []string {
return rc.cols
}

var lengthSuffixRegexp = regexp.MustCompile(`\(\d+\)\z`)

func (rc *SQLiteRows) declTypes() []string {
if rc.s.s != nil && rc.decltype == nil {
rc.decltype = make([]string, rc.nc)
for i := 0; i < rc.nc; i++ {
rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
rc.decltype[i] = lengthSuffixRegexp.ReplaceAllString(rc.decltype[i], "")
}
}
return rc.decltype
Expand Down
19 changes: 13 additions & 6 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func TestTimestamp(t *testing.T) {
defer db.Close()

_, err = db.Exec("DROP TABLE foo")
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP, dt DATETIME)")
_, err = db.Exec("CREATE TABLE foo(id INTEGER, ts timeSTAMP, dt DATETIME, dt6 DATETIME(6))")
if err != nil {
t.Fatal("Failed to create table:", err)
}
Expand Down Expand Up @@ -660,13 +660,13 @@ func TestTimestamp(t *testing.T) {
{"2012-11-04T00:00:00.000Z", timestamp3},
}
for i := range tests {
_, err = db.Exec("INSERT INTO foo(id, ts, dt) VALUES(?, ?, ?)", i, tests[i].value, tests[i].value)
_, err = db.Exec("INSERT INTO foo(id, ts, dt, dt6) VALUES(?, ?, ?, ?)", i, tests[i].value, tests[i].value, tests[i].value)
if err != nil {
t.Fatal("Failed to insert timestamp:", err)
}
}

rows, err := db.Query("SELECT id, ts, dt FROM foo ORDER BY id ASC")
rows, err := db.Query("SELECT id, ts, dt, dt6 FROM foo ORDER BY id ASC")
if err != nil {
t.Fatal("Unable to query foo table:", err)
}
Expand All @@ -675,9 +675,9 @@ func TestTimestamp(t *testing.T) {
seen := 0
for rows.Next() {
var id int
var ts, dt time.Time
var ts, dt, dt6 time.Time

if err := rows.Scan(&id, &ts, &dt); err != nil {
if err := rows.Scan(&id, &ts, &dt, &dt6); err != nil {
t.Error("Unable to scan results:", err)
continue
}
Expand All @@ -687,11 +687,14 @@ func TestTimestamp(t *testing.T) {
}
seen++
if !tests[id].expected.Equal(ts) {
t.Errorf("Timestamp value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt)
t.Errorf("Timestamp value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, ts)
}
if !tests[id].expected.Equal(dt) {
t.Errorf("Datetime value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt)
}
if !tests[id].expected.Equal(dt6) {
t.Errorf("Datetime(6) value for id %v (%v) should be %v, not %v", id, tests[id].value, tests[id].expected, dt6)
}
if timezone(tests[id].expected) != timezone(ts) {
t.Errorf("Timezone for id %v (%v) should be %v, not %v", id, tests[id].value,
timezone(tests[id].expected), timezone(ts))
Expand All @@ -700,6 +703,10 @@ func TestTimestamp(t *testing.T) {
t.Errorf("Timezone for id %v (%v) should be %v, not %v", id, tests[id].value,
timezone(tests[id].expected), timezone(dt))
}
if timezone(tests[id].expected) != timezone(dt6) {
t.Errorf("Timezone for id %v (%v) should be %v, not %v", id, tests[id].value,
timezone(tests[id].expected), timezone(dt6))
}
}

if seen != len(tests) {
Expand Down