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 { ...@@ -790,7 +790,7 @@ func (s *Stmt) ColumnType(index int) Type {
// var id int // var id int
// var name string // var name string
// for sqlite.Must(stmt.Next()) { // for sqlite.Must(stmt.Next()) {
// stmt.NamedScan("name", &name, "id", &id) // err = stmt.NamedScan("name", &name, "id", &id)
// // TODO error handling // // TODO error handling
// fmt.Println(id, name) // fmt.Println(id, name)
// } // }
...@@ -909,18 +909,84 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) { ...@@ -909,18 +909,84 @@ func (s *Stmt) ScanByIndex(index int, value interface{}) (bool, error) {
case nil: case nil:
case *string: case *string:
*value, isNull = s.ScanText(index) *value, isNull = s.ScanText(index)
case **string:
var st string
st, isNull = s.ScanText(index)
if isNull {
*value = nil
} else {
**value = st
}
case *int: case *int:
*value, isNull, err = s.ScanInt(index) *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: case *int64:
*value, isNull, err = s.ScanInt64(index) *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: case *byte:
*value, isNull, err = s.ScanByte(index) *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: case *bool:
*value, isNull, err = s.ScanBool(index) *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: case *float64:
*value, isNull, err = s.ScanDouble(index) *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: case *[]byte:
*value, isNull = s.ScanBlob(index) *value, isNull = s.ScanBlob(index)
case **[]byte:
var bs []byte
bs, isNull = s.ScanBlob(index)
if isNull {
*value = nil
} else {
**value = bs
}
case *interface{}: case *interface{}:
*value = s.ScanValue(index) *value = s.ScanValue(index)
isNull = *value == nil isNull = *value == nil
...@@ -948,8 +1014,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) { ...@@ -948,8 +1014,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
value = nil value = nil
case Text: case Text:
p := C.sqlite3_column_text(s.stmt, C.int(index)) p := C.sqlite3_column_text(s.stmt, C.int(index))
n := C.sqlite3_column_bytes(s.stmt, C.int(index)) value = C.GoString((*C.char)(unsafe.Pointer(p)))
value = C.GoStringN((*C.char)(unsafe.Pointer(p)), n)
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:
...@@ -966,7 +1031,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) { ...@@ -966,7 +1031,7 @@ func (s *Stmt) ScanValue(index int) (value interface{}) {
// Like ScanValue on several columns // Like ScanValue on several columns
func (s *Stmt) ScanValues(values []interface{}) { func (s *Stmt) ScanValues(values []interface{}) {
for i := 0; i < len(values); i++ { for i := range values {
values[i] = s.ScanValue(i) values[i] = s.ScanValue(i)
} }
} }
...@@ -980,8 +1045,7 @@ func (s *Stmt) ScanText(index int) (value string, isNull bool) { ...@@ -980,8 +1045,7 @@ func (s *Stmt) ScanText(index int) (value string, isNull bool) {
if p == nil { if p == nil {
isNull = true isNull = true
} else { } else {
n := C.sqlite3_column_bytes(s.stmt, C.int(index)) value = C.GoString((*C.char)(unsafe.Pointer(p)))
value = C.GoStringN((*C.char)(unsafe.Pointer(p)), n)
} }
return return
} }
......
...@@ -379,3 +379,29 @@ func TestLoadExtension(t *testing.T) { ...@@ -379,3 +379,29 @@ func TestLoadExtension(t *testing.T) {
checkNoError(t, err, "load extension error: %s") 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