From f544db98ccf4918ec2ec090a4d74450ef45b4b1c Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 6 Mar 2016 12:27:17 -0800 Subject: [PATCH 1/2] Add SQLiteRows.DeclTypes() method --- sqlite3.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index db34b23..afeb99e 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -893,6 +893,17 @@ func (rc *SQLiteRows) Columns() []string { return rc.cols } +// Return column types. +func (rc *SQLiteRows) DeclTypes() []string { + if 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)))) + } + } + return rc.decltype +} + // Move cursor to next. func (rc *SQLiteRows) Next(dest []driver.Value) error { rv := C.sqlite3_step(rc.s.s) @@ -907,12 +918,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { return nil } - if 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.DeclTypes() for i := range dest { switch C.sqlite3_column_type(rc.s.s, C.int(i)) { From 4f5821ada67fa024a5d417664e0d6c47a71c5c6c Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Mon, 7 Mar 2016 01:15:24 -0800 Subject: [PATCH 2/2] Test SQLiteRows.DeclTypes() --- sqlite3_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/sqlite3_test.go b/sqlite3_test.go index 9efd313..c7996c6 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1279,6 +1279,41 @@ func TestAggregatorRegistration(t *testing.T) { } } +func TestDeclTypes(t *testing.T) { + + d := SQLiteDriver{} + + conn, err := d.Open(":memory:") + if err != nil { + t.Fatal("Failed to begin transaction:", err) + } + defer conn.Close() + + sqlite3conn := conn.(*SQLiteConn) + + _, err = sqlite3conn.Exec("create table foo (id integer not null primary key, name text)", nil) + if err != nil { + t.Fatal("Failed to create table:", err) + } + + _, err = sqlite3conn.Exec("insert into foo(name) values(\"bar\")", nil) + if err != nil { + t.Fatal("Failed to insert:", err) + } + + rs, err := sqlite3conn.Query("select * from foo", nil) + if err != nil { + t.Fatal("Failed to select:", err) + } + defer rs.Close() + + declTypes := rs.(*SQLiteRows).DeclTypes() + + if !reflect.DeepEqual(declTypes, []string{"integer", "text"}) { + t.Fatal("Unexpected declTypes:", declTypes) + } +} + var customFunctionOnce sync.Once func BenchmarkCustomFunctions(b *testing.B) {