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() {
fmt.Println(name)
return
})
check(err)
// Output: gosqlite driver
}
......@@ -117,10 +118,18 @@ func ExampleStmt_NamedScan() {
fmt.Println(id, name)
return
})
check(err)
// Output: 1 Go
// 2 SQLite
}
type YesOrNo bool
func (b *YesOrNo) Scan(src interface{}) error {
*b = YesOrNo(src == "Y" || src == "yes")
return nil
}
func ExampleStmt_Scan() {
db, err := sqlite.Open(":memory:")
check(err)
......@@ -132,20 +141,16 @@ func ExampleStmt_Scan() {
var id int
var name string
var status bool
converter := func(value interface{}) (bool, error) {
status = value == "Y" || value == "yes"
return false, nil
}
var status YesOrNo
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
}
fmt.Println(id, name, status)
return
})
check(err)
// Output: 1 Go true
// 2 SQLite true
}
......
......@@ -32,6 +32,7 @@ static int my_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt
import "C"
import (
"database/sql"
"errors"
"fmt"
"time"
......@@ -682,11 +683,13 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
}
case *time.Time: // go fix doesn't like this type!
*value, isNull, err = s.ScanTime(index)
case sql.Scanner:
v := s.ScanValue(index, false)
err = value.Scan(v)
isNull = v == nil
case *interface{}:
*value = s.ScanValue(index, false)
isNull = *value == nil
case func(interface{}) (bool, error):
isNull, err = value(s.ScanValue(index, false))
default:
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