From abc8991d4d4bb67031d3ee9e387f3e2a4006b567 Mon Sep 17 00:00:00 2001 From: Dimitri Roche Date: Sat, 26 Jan 2019 09:16:35 -0500 Subject: [PATCH 1/4] column types text, varchar, *char return as strings: As opposed to []byte arrays. This brings sqlite closer in line with other dbs like postgres, allowing downstream consumers to assume the scanned value is string across underlying dbs. --- sqlite3.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index f731d20..ac713d6 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -223,6 +223,7 @@ var SQLiteTimestampFormats = []string{ const ( columnDate string = "date" columnDatetime string = "datetime" + columnText string = "text" columnTimestamp string = "timestamp" ) @@ -2061,10 +2062,15 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { t = t.In(rc.s.c.loc) } dest[i] = t + case columnText: + dest[i] = s default: - dest[i] = []byte(s) + if strings.Contains(strings.ToLower(rc.decltype[i]), "char") { + dest[i] = s + } else { + dest[i] = []byte(s) + } } - } } return nil From ae5cbb218c63c58afbe7237343b8b3ef28723a4b Mon Sep 17 00:00:00 2001 From: Dimitri Roche Date: Sat, 26 Jan 2019 14:54:27 -0500 Subject: [PATCH 2/4] column_type SQLITE_TEXT returned as string by default --- sqlite3.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index ac713d6..1cf9451 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -223,7 +223,6 @@ var SQLiteTimestampFormats = []string{ const ( columnDate string = "date" columnDatetime string = "datetime" - columnText string = "text" columnTimestamp string = "timestamp" ) @@ -2062,14 +2061,8 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { t = t.In(rc.s.c.loc) } dest[i] = t - case columnText: - dest[i] = s default: - if strings.Contains(strings.ToLower(rc.decltype[i]), "char") { - dest[i] = s - } else { - dest[i] = []byte(s) - } + dest[i] = s } } } From 5e7aedf6859708d1be6fe961dac3811e986fa4c3 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 11 Feb 2019 00:48:49 +0900 Subject: [PATCH 3/4] Add test --- sqlite3_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sqlite3_test.go b/sqlite3_test.go index 16bbb10..28b3c96 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1670,6 +1670,26 @@ func TestAuthorizer(t *testing.T) { } } +func TestNonColumnString(t *testing.T) { + db, err := sql.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + var x interface{} + if err := db.QueryRow("SELECT 'hello'").Scan(&x); err != nil { + t.Fatal(err) + } + s, ok := x.(string) + if !ok { + t.Fatal("non-column string must return string") + } + if s != "hello" { + t.Fatalf("non-column string must return %q but got %q", "hello", s) + } +} + func TestNilAndEmptyBytes(t *testing.T) { db, err := sql.Open("sqlite3", ":memory:") if err != nil { From 4731d0e6ab56c15f06c00505ccc0f22182a1d531 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 11 Feb 2019 01:36:01 +0900 Subject: [PATCH 4/4] Print type of result --- sqlite3_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlite3_test.go b/sqlite3_test.go index 28b3c96..3ef8533 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1683,7 +1683,7 @@ func TestNonColumnString(t *testing.T) { } s, ok := x.(string) if !ok { - t.Fatal("non-column string must return string") + t.Fatalf("non-column string must return string but got %T", x) } if s != "hello" { t.Fatalf("non-column string must return %q but got %q", "hello", s)