From 6865865265dab5ba96d6f64c2126d555e28bc386 Mon Sep 17 00:00:00 2001 From: jander Date: Fri, 25 May 2012 17:01:03 +0800 Subject: [PATCH 1/5] Handle bool values with "BOOLEAN" columns. --- sqlite3.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index bff9aec..161da5e 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -284,9 +284,12 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { switch C.sqlite3_column_type(rc.s.s, C.int(i)) { case C.SQLITE_INTEGER: val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i))) - if rc.decltype[i] == "timestamp" { + switch rc.decltype[i]{ + case "timestamp": dest[i] = time.Unix(val, 0) - } else { + case "boolean": + dest[i] = val>0 + default: dest[i] = val } case C.SQLITE_FLOAT: From 1bfaa5b7d2baac623d83ac7ae4247fcd8055f6c9 Mon Sep 17 00:00:00 2001 From: jander Date: Fri, 25 May 2012 23:16:03 +0800 Subject: [PATCH 2/5] add bool type test: TestBoolean --- sqlite3_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/sqlite3_test.go b/sqlite3_test.go index b46666b..8d4be1f 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -339,3 +339,84 @@ func TestTimestamp(t *testing.T) { t.Errorf("Expected error from \"nonsense\" timestamp") } } + + +func TestBoolean(t *testing.T) { + db, err := sql.Open("sqlite3", ":memory:") + if err != nil { + t.Errorf("Failed to open database:", err) + return + } + defer db.Close() + + _, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)") + if err != nil { + t.Errorf("Failed to create table:", err) + return + } + + bool1 := true + _, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(1, ?)", bool1) + if err != nil { + t.Errorf("Failed to insert boolean:", err) + return + } + + bool2 := false + _, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(2, ?)", bool2) + if err != nil { + t.Errorf("Failed to insert boolean:", err) + return + } + + bool3 := "nonsense" + _, err = db.Exec("INSERT INTO foo(id, fbool) VALUES(3, ?)", bool3) + if err != nil { + t.Errorf("Failed to insert nonsense:", err) + return + } + + rows, err := db.Query("SELECT id, fbool FROM foo where (fbool is ?) or (fbool is ?);", true, false) + if err != nil { + t.Errorf("Unable to query foo table:", err) + return + } + + seen := 0 + + var id int + var fbool bool + + for rows.Next() { + if err := rows.Scan(&id, &fbool); err != nil { + t.Errorf("Unable to scan results:", err) + continue + } + + if id == 1 && fbool != bool1 { + t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool) + } + + if id == 2 && fbool != bool2 { + t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool) + } + seen ++ + } + + if seen != 2 { + t.Errorf("Expected to see two bool") + } + + // make sure "nonsense" triggered an error + rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3) + if err != nil { + t.Errorf("Unable to query foo table:", err) + return + } + + rows.Next() + err = rows.Scan(&id, &fbool) + if err == nil { + t.Errorf("Expected error from \"nonsense\" bool") + } +} From ed17eae07aa835fffe64c6fbab3fc42ce2caa644 Mon Sep 17 00:00:00 2001 From: jander Date: Sun, 27 May 2012 00:09:12 +0800 Subject: [PATCH 3/5] fix TestBoolean --- sqlite3_test.go | 66 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/sqlite3_test.go b/sqlite3_test.go index 8d4be1f..f4b80bc 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -342,12 +342,13 @@ func TestTimestamp(t *testing.T) { func TestBoolean(t *testing.T) { - db, err := sql.Open("sqlite3", ":memory:") + db, err := sql.Open("sqlite3", "./foo.db") if err != nil { t.Errorf("Failed to open database:", err) return } defer db.Close() + defer os.Remove("./foo.db") _, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)") if err != nil { @@ -376,36 +377,61 @@ func TestBoolean(t *testing.T) { return } - rows, err := db.Query("SELECT id, fbool FROM foo where (fbool is ?) or (fbool is ?);", true, false) + rows, err := db.Query("SELECT id, fbool FROM foo where fbool is ?", bool1) if err != nil { t.Errorf("Unable to query foo table:", err) return } - - seen := 0 + counter := 0 var id int var fbool bool - - for rows.Next() { + + for rows.Next(){ if err := rows.Scan(&id, &fbool); err != nil { t.Errorf("Unable to scan results:", err) - continue + return } - - if id == 1 && fbool != bool1 { - t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool) - } - - if id == 2 && fbool != bool2 { - t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool) - } - seen ++ + counter ++ } - - if seen != 2 { - t.Errorf("Expected to see two bool") + + if counter != 1{ + t.Errorf("Expected 1 row but %v", counter) + return } + + if id!=1 && fbool != true { + t.Errorf("Value for id 1 should be %v, not %v", bool1, fbool) + return + } + + + rows, err = db.Query("SELECT id, fbool FROM foo where fbool is ?", bool2) + if err != nil { + t.Errorf("Unable to query foo table:", err) + return + } + + counter = 0 + + for rows.Next(){ + if err := rows.Scan(&id, &fbool); err != nil { + t.Errorf("Unable to scan results:", err) + return + } + counter ++ + } + + if counter != 1{ + t.Errorf("Expected 1 row but %v", counter) + return + } + + if id != 2 && fbool != false { + t.Errorf("Value for id 2 should be %v, not %v", bool2, fbool) + return + } + // make sure "nonsense" triggered an error rows, err = db.Query("SELECT id, fbool FROM foo where id=?;", 3) @@ -419,4 +445,4 @@ func TestBoolean(t *testing.T) { if err == nil { t.Errorf("Expected error from \"nonsense\" bool") } -} +} \ No newline at end of file From 2f4a8f3f2c1ea6b1dc24a4d6ea55ef720ba9f1d4 Mon Sep 17 00:00:00 2001 From: jander Date: Sun, 27 May 2012 00:10:29 +0800 Subject: [PATCH 4/5] Update sqlite3_test.go --- sqlite3_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqlite3_test.go b/sqlite3_test.go index f4b80bc..f01d3e7 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -347,8 +347,9 @@ func TestBoolean(t *testing.T) { t.Errorf("Failed to open database:", err) return } - defer db.Close() + defer os.Remove("./foo.db") + defer db.Close() _, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)") if err != nil { From d869678d3cece282e9f5dcf2466eef00ba2978c0 Mon Sep 17 00:00:00 2001 From: jander Date: Sun, 27 May 2012 00:13:36 +0800 Subject: [PATCH 5/5] Update sqlite3_test.go --- sqlite3_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/sqlite3_test.go b/sqlite3_test.go index f01d3e7..6c689a1 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -349,7 +349,6 @@ func TestBoolean(t *testing.T) { } defer os.Remove("./foo.db") - defer db.Close() _, err = db.Exec("CREATE TABLE foo(id INTEGER, fbool BOOLEAN)") if err != nil {