aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormattn <mattn.jp@gmail.com>2012-05-27 18:14:31 -0700
committermattn <mattn.jp@gmail.com>2012-05-27 18:14:31 -0700
commita407c70cfd983289954c1dae430d35a550567983 (patch)
treec41aac7d460ae2a21691b04be950f2975fe28f9c
parentremove Makefile. (diff)
parentUpdate sqlite3_test.go (diff)
downloadgolite-a407c70cfd983289954c1dae430d35a550567983.tar.gz
golite-a407c70cfd983289954c1dae430d35a550567983.tar.xz
Merge pull request #15 from jander/master
Handle bool values with "BOOLEAN" columns.
-rw-r--r--sqlite3.go7
-rw-r--r--sqlite3_test.go107
2 files changed, 112 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:
diff --git a/sqlite3_test.go b/sqlite3_test.go
index b46666b..6c689a1 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -339,3 +339,110 @@ func TestTimestamp(t *testing.T) {
t.Errorf("Expected error from \"nonsense\" timestamp")
}
}
+
+
+func TestBoolean(t *testing.T) {
+ db, err := sql.Open("sqlite3", "./foo.db")
+ if err != nil {
+ t.Errorf("Failed to open database:", err)
+ return
+ }
+
+ defer os.Remove("./foo.db")
+
+ _, 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 ?", bool1)
+ if err != nil {
+ t.Errorf("Unable to query foo table:", err)
+ return
+ }
+ counter := 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)
+ return
+ }
+ counter ++
+ }
+
+ 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)
+ 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")
+ }
+} \ No newline at end of file