Merge pull request #6 from lye/master

Fix marshaling error in Go->SQLite boolean values
This commit is contained in:
mattn 2012-02-06 20:19:51 -08:00
commit 93ab2db408
2 changed files with 52 additions and 1 deletions

View File

@ -169,7 +169,7 @@ func (s *SQLiteStmt) bind(args []interface{}) error {
rv = C.sqlite3_bind_int(s.s, n, C.int(v)) rv = C.sqlite3_bind_int(s.s, n, C.int(v))
case bool: case bool:
if bool(v) { if bool(v) {
rv = C.sqlite3_bind_int(s.s, n, -1) rv = C.sqlite3_bind_int(s.s, n, 1)
} else { } else {
rv = C.sqlite3_bind_int(s.s, n, 0) rv = C.sqlite3_bind_int(s.s, n, 0)
} }

View File

@ -209,3 +209,54 @@ func TestDelete(t *testing.T) {
t.Errorf("Fetched row but expected not rows") 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")
}
}
}