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