Commit 60079c93 authored by gwenn's avatar gwenn

Refactor ScanValue.

parent d36a1dad
...@@ -175,7 +175,7 @@ func (r *rowsImpl) Next(dest []driver.Value) error { ...@@ -175,7 +175,7 @@ func (r *rowsImpl) Next(dest []driver.Value) error {
return io.EOF return io.EOF
} }
for i := range dest { for i := range dest {
dest[i] = r.s.s.ScanValue(i, true) dest[i], _ = r.s.s.ScanValue(i, true)
/*if !driver.IsScanValue(dest[i]) { /*if !driver.IsScanValue(dest[i]) {
panic("Invalid type returned by ScanValue") panic("Invalid type returned by ScanValue")
}*/ }*/
......
...@@ -662,12 +662,11 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) { ...@@ -662,12 +662,11 @@ 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: case sql.Scanner:
v := s.ScanValue(index, false) var v interface{}
v, isNull = s.ScanValue(index, false)
err = value.Scan(v) err = value.Scan(v)
isNull = v == nil
case *interface{}: case *interface{}:
*value = s.ScanValue(index, false) *value, isNull = s.ScanValue(index, false)
isNull = *value == nil
default: default:
return false, s.specificError("unsupported type in Scan: %T", value) return false, s.specificError("unsupported type in Scan: %T", value)
} }
...@@ -687,38 +686,36 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) { ...@@ -687,38 +686,36 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
// //
// Calls sqlite3_column_(blob|double|int|int64|text) depending on columns type. // Calls sqlite3_column_(blob|double|int|int64|text) depending on columns type.
// (See http://sqlite.org/c3ref/column_blob.html) // (See http://sqlite.org/c3ref/column_blob.html)
func (s *Stmt) ScanValue(index int, blob bool) (value interface{}) { func (s *Stmt) ScanValue(index int, blob bool) (interface{}, bool) {
switch s.ColumnType(index) { switch s.ColumnType(index) {
case Null: case Null:
value = nil return nil, true
case Text: case Text:
if blob { if blob {
p := C.sqlite3_column_blob(s.stmt, C.int(index)) p := C.sqlite3_column_blob(s.stmt, C.int(index))
n := C.sqlite3_column_bytes(s.stmt, C.int(index)) n := C.sqlite3_column_bytes(s.stmt, C.int(index))
value = C.GoBytes(p, n) return C.GoBytes(p, n), false
} else { } else {
p := C.sqlite3_column_text(s.stmt, C.int(index)) p := C.sqlite3_column_text(s.stmt, C.int(index))
value = C.GoString((*C.char)(unsafe.Pointer(p))) return C.GoString((*C.char)(unsafe.Pointer(p))), false
} }
case Integer: case Integer:
value = int64(C.sqlite3_column_int64(s.stmt, C.int(index))) return int64(C.sqlite3_column_int64(s.stmt, C.int(index))), false
case Float: case Float:
value = float64(C.sqlite3_column_double(s.stmt, C.int(index))) return float64(C.sqlite3_column_double(s.stmt, C.int(index))), false
case Blob: case Blob:
p := C.sqlite3_column_blob(s.stmt, C.int(index)) p := C.sqlite3_column_blob(s.stmt, C.int(index))
n := C.sqlite3_column_bytes(s.stmt, C.int(index)) n := C.sqlite3_column_bytes(s.stmt, C.int(index))
// value = (*[1 << 30]byte)(unsafe.Pointer(p))[:n] // value = (*[1 << 30]byte)(unsafe.Pointer(p))[:n]
value = C.GoBytes(p, n) // The memory space used to hold strings and BLOBs is freed automatically. return C.GoBytes(p, n), false // The memory space used to hold strings and BLOBs is freed automatically.
default:
panic("The column type is not one of SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL")
} }
return panic("The column type is not one of SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL")
} }
// ScanValues is like ScanValue on several columns. // ScanValues is like ScanValue on several columns.
func (s *Stmt) ScanValues(values []interface{}) { func (s *Stmt) ScanValues(values []interface{}) {
for i := range values { for i := range values {
values[i] = s.ScanValue(i, false) values[i], _ = s.ScanValue(i, false)
} }
} }
......
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