From 275bdb282a390e7dcf8b698e04ce0c6f3aa1970a Mon Sep 17 00:00:00 2001 From: lye Date: Mon, 6 Feb 2012 22:56:36 +0000 Subject: [PATCH 1/2] Added additional testcase for boolean roundtrips This test creates a simple table, inserts some dummy boolean values, then pulls them back out to ensure they are marshalled correctly. --- sqlite3_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/sqlite3_test.go b/sqlite3_test.go index 035ffa8..a76d86f 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -209,3 +209,54 @@ func TestDelete(t *testing.T) { t.Errorf("Fetched row but expected not rows") } } + +func TestBooleanRoundtrip(t *testing.T) { + db, err := sql.Open("sqlite3", "./foo.db") + if err != nil { + t.Errorf("Tailed to open database:", err) + return + } + defer os.Remove("./foo.db") + + _, err = db.Exec("DROP TABLE foo") + _, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)") + if err != nil { + t.Errorf("Failed to create table:", err) + return + } + + _, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true) + if err != nil { + t.Errorf("Failed to insert true value:", err) + return + } + + _, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false) + if err != nil { + t.Errorf("Failed to insert false value:", err) + return + } + + rows, err := db.Query("SELECT id, value FROM foo") + if err != nil { + t.Errorf("Unable to query foo table:", err) + return + } + + for rows.Next() { + var id int + var value bool + + if err := rows.Scan(&id, &value) ; err != nil { + t.Errorf("Unable to scan results:", err) + continue + } + + if id == 1 && !value { + t.Errorf("Value for id 1 should be true, not false") + + } else if id == 2 && value { + t.Errorf("Value for id 2 should be false, not true") + } + } +} From 3524ead0a51cad63768e5f2277b28455d1a4a968 Mon Sep 17 00:00:00 2001 From: lye Date: Mon, 6 Feb 2012 22:59:24 +0000 Subject: [PATCH 2/2] For boolean values, marshal true to SQLite 1, not -1 SQLite stores boolean values as an integer, serializing true as 1 and false as 0 [1], but it does not actually enforce this range. To match the documentation (and fix the broken test case), this patch makes a Go boolean true serialize properly to 1. [1] http://www.sqlite.org/datatype3.html --- sqlite3.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlite3.go b/sqlite3.go index 088b110..d8f7756 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -169,7 +169,7 @@ func (s *SQLiteStmt) bind(args []interface{}) error { rv = C.sqlite3_bind_int(s.s, n, C.int(v)) case bool: if bool(v) { - rv = C.sqlite3_bind_int(s.s, n, -1) + rv = C.sqlite3_bind_int(s.s, n, 1) } else { rv = C.sqlite3_bind_int(s.s, n, 0) }