Commit d7bba9c3 authored by gwenn's avatar gwenn

Add suppport to standard sql.Scanner in Scan methods.

parent e5176627
...@@ -38,6 +38,7 @@ func Example() { ...@@ -38,6 +38,7 @@ func Example() {
fmt.Println(name) fmt.Println(name)
return return
}) })
check(err)
// Output: gosqlite driver // Output: gosqlite driver
} }
...@@ -117,10 +118,18 @@ func ExampleStmt_NamedScan() { ...@@ -117,10 +118,18 @@ func ExampleStmt_NamedScan() {
fmt.Println(id, name) fmt.Println(id, name)
return return
}) })
check(err)
// Output: 1 Go // Output: 1 Go
// 2 SQLite // 2 SQLite
} }
type YesOrNo bool
func (b *YesOrNo) Scan(src interface{}) error {
*b = YesOrNo(src == "Y" || src == "yes")
return nil
}
func ExampleStmt_Scan() { func ExampleStmt_Scan() {
db, err := sqlite.Open(":memory:") db, err := sqlite.Open(":memory:")
check(err) check(err)
...@@ -132,20 +141,16 @@ func ExampleStmt_Scan() { ...@@ -132,20 +141,16 @@ func ExampleStmt_Scan() {
var id int var id int
var name string var name string
var status bool var status YesOrNo
converter := func(value interface{}) (bool, error) {
status = value == "Y" || value == "yes"
return false, nil
}
err = s.Select(func(s *sqlite.Stmt) (err error) { err = s.Select(func(s *sqlite.Stmt) (err error) {
if err = s.Scan(&id, &name, converter); err != nil { if err = s.Scan(&id, &name, &status); err != nil {
return return
} }
fmt.Println(id, name, status) fmt.Println(id, name, status)
return return
}) })
check(err)
// Output: 1 Go true // Output: 1 Go true
// 2 SQLite true // 2 SQLite true
} }
......
...@@ -32,6 +32,7 @@ static int my_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt ...@@ -32,6 +32,7 @@ static int my_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt
import "C" import "C"
import ( import (
"database/sql"
"errors" "errors"
"fmt" "fmt"
"time" "time"
...@@ -682,11 +683,13 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) { ...@@ -682,11 +683,13 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
} }
case *time.Time: // go fix doesn't like this type! case *time.Time: // go fix doesn't like this type!
*value, isNull, err = s.ScanTime(index) *value, isNull, err = s.ScanTime(index)
case sql.Scanner:
v := s.ScanValue(index, false)
err = value.Scan(v)
isNull = v == nil
case *interface{}: case *interface{}:
*value = s.ScanValue(index, false) *value = s.ScanValue(index, false)
isNull = *value == nil isNull = *value == nil
case func(interface{}) (bool, error):
isNull, err = value(s.ScanValue(index, false))
default: default:
return false, s.specificError("unsupported type in Scan: %T", value) return false, s.specificError("unsupported type in Scan: %T", value)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment