Commit e10dbb7e authored by gwenn's avatar gwenn

A conversion func can be passed to Scan methods.

parent fe6e2908
......@@ -125,21 +125,28 @@ func ExampleStmt_Scan() {
check(err)
defer db.Close()
s, err := db.Prepare("SELECT 1 as id, 'Go' as name UNION SELECT 2, 'SQLite'")
s, err := db.Prepare("SELECT 1 as id, 'Go' as name, 'Y' as status UNION SELECT 2, 'SQLite', 'yes'")
check(err)
defer s.Finalize()
var id int
var name string
var status bool
converter := func(value interface{}) (bool, error) {
status = value == "Y" || value == "yes"
return false, nil
}
err = s.Select(func(s *sqlite.Stmt) (err error) {
if err = s.Scan(&id, &name); err != nil {
if err = s.Scan(&id, &name, converter); err != nil {
return
}
fmt.Println(id, name)
fmt.Println(id, name, status)
return
})
// Output: 1 Go
// 2 SQLite
// Output: 1 Go true
// 2 SQLite true
}
func ExampleNewBackup() {
......
......@@ -68,6 +68,7 @@ func (s *Stmt) specificError(msg string, a ...interface{}) error {
}
// SQL statement
// (See http://sqlite.org/c3ref/stmt.html)
type Stmt struct {
c *Conn
stmt *C.sqlite3_stmt
......@@ -562,6 +563,7 @@ func (s *Stmt) ScanByName(name string, value interface{}) (bool, error) {
// (*)*float64
// (*)*[]byte
// *interface{}
// func(interface{}) (bool, error)
//
// Returns true when column is null.
// Calls sqlite3_column_(blob|double|int|int64|text) depending on arg type.
......@@ -656,6 +658,8 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
case *interface{}:
*value = s.ScanValue(index)
isNull = *value == nil
case func(interface{}) (bool, error):
isNull, err = value(s.ScanValue(index))
default:
return false, s.specificError("unsupported type in Scan: %s", reflect.TypeOf(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