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 {
return io.EOF
}
for i := range dest {
value := r.s.s.ScanValue(i)
switch value := value.(type) {
case string: // "All string values must be converted to []byte."
dest[i] = []byte(value)
default:
dest[i] = value
}
dest[i] = r.s.s.ScanValue(i, true)
/*if !driver.IsScanValue(dest[i]) {
panic("Invalid type returned by ScanValue")
}*/
}
return nil
}
......
......@@ -656,10 +656,10 @@ 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 *interface{}:
*value = s.ScanValue(index)
*value = s.ScanValue(index, false)
isNull = *value == nil
case func(interface{}) (bool, error):
isNull, err = value(s.ScanValue(index))
isNull, err = value(s.ScanValue(index, false))
default:
return false, s.specificError("unsupported type in Scan: %T", value)
}
......@@ -672,20 +672,26 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
// Destination type is decided by SQLite.
// The returned value will be of one of the following types:
// nil
// string
// string (exception if blob is true)
// int64
// float64
// []byte
//
// Calls sqlite3_column_(blob|double|int|int64|text) depending on columns type.
// (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) {
case Null:
value = nil
case Text:
p := C.sqlite3_column_text(s.stmt, C.int(index))
value = C.GoString((*C.char)(unsafe.Pointer(p)))
if blob {
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:
value = int64(C.sqlite3_column_int64(s.stmt, C.int(index)))
case Float:
......@@ -704,7 +710,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
// ScanValues is like ScanValue on several columns.
func (s *Stmt) ScanValues(values []interface{}) {
for i := range values {
values[i] = s.ScanValue(i)
values[i] = s.ScanValue(i, false)
}
}
......
......@@ -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", 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