Commit 7beeeaa5 authored by gwenn's avatar gwenn

Changes Stmt.ScanValue method signature for driver.

parent 440c681f
...@@ -175,13 +175,10 @@ func (r *rowsImpl) Next(dest []driver.Value) error { ...@@ -175,13 +175,10 @@ func (r *rowsImpl) Next(dest []driver.Value) error {
return io.EOF return io.EOF
} }
for i := range dest { for i := range dest {
value := r.s.s.ScanValue(i) dest[i] = r.s.s.ScanValue(i, true)
switch value := value.(type) { /*if !driver.IsScanValue(dest[i]) {
case string: // "All string values must be converted to []byte." panic("Invalid type returned by ScanValue")
dest[i] = []byte(value) }*/
default:
dest[i] = value
}
} }
return nil return nil
} }
......
...@@ -656,10 +656,10 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) { ...@@ -656,10 +656,10 @@ 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 *interface{}: case *interface{}:
*value = s.ScanValue(index) *value = s.ScanValue(index, false)
isNull = *value == nil isNull = *value == nil
case func(interface{}) (bool, error): case func(interface{}) (bool, error):
isNull, err = value(s.ScanValue(index)) 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)
} }
...@@ -672,20 +672,26 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) { ...@@ -672,20 +672,26 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
// Destination type is decided by SQLite. // Destination type is decided by SQLite.
// The returned value will be of one of the following types: // The returned value will be of one of the following types:
// nil // nil
// string // string (exception if blob is true)
// int64 // int64
// float64 // float64
// []byte // []byte
// //
// 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) (value interface{}) { func (s *Stmt) ScanValue(index int, blob bool) (value interface{}) {
switch s.ColumnType(index) { switch s.ColumnType(index) {
case Null: case Null:
value = nil value = nil
case Text: case Text:
p := C.sqlite3_column_text(s.stmt, C.int(index)) if blob {
value = C.GoString((*C.char)(unsafe.Pointer(p))) p := C.sqlite3_column_blob(s.stmt, C.int(index))
n := C.sqlite3_column_bytes(s.stmt, C.int(index))
value = C.GoBytes(p, n)
} else {
p := C.sqlite3_column_text(s.stmt, C.int(index))
value = C.GoString((*C.char)(unsafe.Pointer(p)))
}
case Integer: case Integer:
value = int64(C.sqlite3_column_int64(s.stmt, C.int(index))) value = int64(C.sqlite3_column_int64(s.stmt, C.int(index)))
case Float: case Float:
...@@ -704,7 +710,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) { ...@@ -704,7 +710,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
// 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) values[i] = s.ScanValue(i, false)
} }
} }
......
...@@ -384,3 +384,17 @@ func TestScanValues(t *testing.T) { ...@@ -384,3 +384,17 @@ func TestScanValues(t *testing.T) {
assertEquals(t, "expected %v but got %v", nil, values[1]) assertEquals(t, "expected %v but got %v", nil, values[1])
assertEquals(t, "expected %v but got %v", int64(0), values[2]) assertEquals(t, "expected %v but got %v", int64(0), values[2])
} }
func TestScanBytes(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("SELECT 'test'")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(s, t)
if !Must(s.Next()) {
t.Fatal("no result")
}
blob, _ := s.ScanBlob(0)
assertEquals(t, "expected %v but got %v", "test", string(blob))
}
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