Commit 02b7f855 authored by gwenn's avatar gwenn

Add support to pointer to pointer in ScanByIndex.

parent fed8fd1d
......@@ -790,7 +790,7 @@ func (s *Stmt) ColumnType(index int) Type {
// var id int
// var name string
// for sqlite.Must(stmt.Next()) {
// stmt.NamedScan("name", &name, "id", &id)
// err = stmt.NamedScan("name", &name, "id", &id)
// // TODO error handling
// fmt.Println(id, name)
// }
......@@ -909,18 +909,84 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
case nil:
case *string:
*value, isNull = s.ScanText(index)
case **string:
var st string
st, isNull = s.ScanText(index)
if isNull {
*value = nil
} else {
**value = st
}
case *int:
*value, isNull, err = s.ScanInt(index)
case **int:
var i int
i, isNull, err = s.ScanInt(index)
if err == nil {
if isNull {
*value = nil
} else {
**value = i
}
}
case *int64:
*value, isNull, err = s.ScanInt64(index)
case **int64:
var i int64
i, isNull, err = s.ScanInt64(index)
if err == nil {
if isNull {
*value = nil
} else {
**value = i
}
}
case *byte:
*value, isNull, err = s.ScanByte(index)
case **byte:
var b byte
b, isNull, err = s.ScanByte(index)
if err == nil {
if isNull {
*value = nil
} else {
**value = b
}
}
case *bool:
*value, isNull, err = s.ScanBool(index)
case **bool:
var b bool
b, isNull, err = s.ScanBool(index)
if err == nil {
if isNull {
*value = nil
} else {
**value = b
}
}
case *float64:
*value, isNull, err = s.ScanDouble(index)
case **float64:
var f float64
f, isNull, err = s.ScanDouble(index)
if err == nil {
if isNull {
*value = nil
} else {
**value = f
}
}
case *[]byte:
*value, isNull = s.ScanBlob(index)
case **[]byte:
var bs []byte
bs, isNull = s.ScanBlob(index)
if isNull {
*value = nil
} else {
**value = bs
}
case *interface{}:
*value = s.ScanValue(index)
isNull = *value == nil
......@@ -948,8 +1014,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
value = nil
case Text:
p := C.sqlite3_column_text(s.stmt, C.int(index))
n := C.sqlite3_column_bytes(s.stmt, C.int(index))
value = C.GoStringN((*C.char)(unsafe.Pointer(p)), n)
value = C.GoString((*C.char)(unsafe.Pointer(p)))
case Integer:
value = int64(C.sqlite3_column_int64(s.stmt, C.int(index)))
case Float:
......@@ -966,7 +1031,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
// Like ScanValue on several columns
func (s *Stmt) ScanValues(values []interface{}) {
for i := 0; i < len(values); i++ {
for i := range values {
values[i] = s.ScanValue(i)
}
}
......@@ -980,8 +1045,7 @@ func (s *Stmt) ScanText(index int) (value string, isNull bool) {
if p == nil {
isNull = true
} else {
n := C.sqlite3_column_bytes(s.stmt, C.int(index))
value = C.GoStringN((*C.char)(unsafe.Pointer(p)), n)
value = C.GoString((*C.char)(unsafe.Pointer(p)))
}
return
}
......
......@@ -379,3 +379,29 @@ func TestLoadExtension(t *testing.T) {
checkNoError(t, err, "load extension error: %s")
}
*/
func TestScanNull(t *testing.T) {
db := open(t)
defer db.Close()
s, err := db.Prepare("select null")
checkNoError(t, err, "prepare error: %s")
defer s.Finalize()
if !Must(s.Next()) {
t.Fatal("no result")
}
var pi *int
null := Must(s.ScanByIndex(0, &pi))
if !null {
t.Errorf("Expected null value")
} else if pi != nil {
t.Errorf("Expected nil but got %p\n", pi)
}
var ps *string
null = Must(s.ScanByIndex(0, &ps))
if !null {
t.Errorf("Expected null value")
} else if ps != nil {
t.Errorf("Expected nil but got %p\n", ps)
}
}
\ No newline at end of file
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