aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/database/sql
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/database/sql')
-rw-r--r--libgo/go/database/sql/convert.go9
-rw-r--r--libgo/go/database/sql/fakedb_test.go14
-rw-r--r--libgo/go/database/sql/sql.go120
-rw-r--r--libgo/go/database/sql/sql_test.go187
4 files changed, 275 insertions, 55 deletions
diff --git a/libgo/go/database/sql/convert.go b/libgo/go/database/sql/convert.go
index 9835e38..e80420e 100644
--- a/libgo/go/database/sql/convert.go
+++ b/libgo/go/database/sql/convert.go
@@ -40,6 +40,9 @@ func convertAssign(dest, src interface{}) error {
case *string:
*d = s
return nil
+ case *[]byte:
+ *d = []byte(s)
+ return nil
}
case []byte:
switch d := dest.(type) {
@@ -50,6 +53,12 @@ func convertAssign(dest, src interface{}) error {
*d = s
return nil
}
+ case nil:
+ switch d := dest.(type) {
+ case *[]byte:
+ *d = nil
+ return nil
+ }
}
var sv reflect.Value
diff --git a/libgo/go/database/sql/fakedb_test.go b/libgo/go/database/sql/fakedb_test.go
index b0d137c..df25023 100644
--- a/libgo/go/database/sql/fakedb_test.go
+++ b/libgo/go/database/sql/fakedb_test.go
@@ -585,12 +585,26 @@ func converterForType(typ string) driver.ValueConverter {
switch typ {
case "bool":
return driver.Bool
+ case "nullbool":
+ return driver.Null{driver.Bool}
case "int32":
return driver.Int32
case "string":
return driver.NotNull{driver.String}
case "nullstring":
return driver.Null{driver.String}
+ case "int64":
+ // TODO(coopernurse): add type-specific converter
+ return driver.NotNull{driver.DefaultParameterConverter}
+ case "nullint64":
+ // TODO(coopernurse): add type-specific converter
+ return driver.Null{driver.DefaultParameterConverter}
+ case "float64":
+ // TODO(coopernurse): add type-specific converter
+ return driver.NotNull{driver.DefaultParameterConverter}
+ case "nullfloat64":
+ // TODO(coopernurse): add type-specific converter
+ return driver.Null{driver.DefaultParameterConverter}
case "datetime":
return driver.DefaultParameterConverter
}
diff --git a/libgo/go/database/sql/sql.go b/libgo/go/database/sql/sql.go
index a8bf2a8..34a7652 100644
--- a/libgo/go/database/sql/sql.go
+++ b/libgo/go/database/sql/sql.go
@@ -47,7 +47,6 @@ type RawBytes []byte
// // NULL value
// }
//
-// TODO(bradfitz): add other types.
type NullString struct {
String string
Valid bool // Valid is true if String is not NULL
@@ -71,6 +70,84 @@ func (ns NullString) SubsetValue() (interface{}, error) {
return ns.String, nil
}
+// NullInt64 represents an int64 that may be null.
+// NullInt64 implements the ScannerInto interface so
+// it can be used as a scan destination, similar to NullString.
+type NullInt64 struct {
+ Int64 int64
+ Valid bool // Valid is true if Int64 is not NULL
+}
+
+// ScanInto implements the ScannerInto interface.
+func (n *NullInt64) ScanInto(value interface{}) error {
+ if value == nil {
+ n.Int64, n.Valid = 0, false
+ return nil
+ }
+ n.Valid = true
+ return convertAssign(&n.Int64, value)
+}
+
+// SubsetValue implements the driver SubsetValuer interface.
+func (n NullInt64) SubsetValue() (interface{}, error) {
+ if !n.Valid {
+ return nil, nil
+ }
+ return n.Int64, nil
+}
+
+// NullFloat64 represents a float64 that may be null.
+// NullFloat64 implements the ScannerInto interface so
+// it can be used as a scan destination, similar to NullString.
+type NullFloat64 struct {
+ Float64 float64
+ Valid bool // Valid is true if Float64 is not NULL
+}
+
+// ScanInto implements the ScannerInto interface.
+func (n *NullFloat64) ScanInto(value interface{}) error {
+ if value == nil {
+ n.Float64, n.Valid = 0, false
+ return nil
+ }
+ n.Valid = true
+ return convertAssign(&n.Float64, value)
+}
+
+// SubsetValue implements the driver SubsetValuer interface.
+func (n NullFloat64) SubsetValue() (interface{}, error) {
+ if !n.Valid {
+ return nil, nil
+ }
+ return n.Float64, nil
+}
+
+// NullBool represents a bool that may be null.
+// NullBool implements the ScannerInto interface so
+// it can be used as a scan destination, similar to NullString.
+type NullBool struct {
+ Bool bool
+ Valid bool // Valid is true if Bool is not NULL
+}
+
+// ScanInto implements the ScannerInto interface.
+func (n *NullBool) ScanInto(value interface{}) error {
+ if value == nil {
+ n.Bool, n.Valid = false, false
+ return nil
+ }
+ n.Valid = true
+ return convertAssign(&n.Bool, value)
+}
+
+// SubsetValue implements the driver SubsetValuer interface.
+func (n NullBool) SubsetValue() (interface{}, error) {
+ if !n.Valid {
+ return nil, nil
+ }
+ return n.Bool, nil
+}
+
// ScannerInto is an interface used by Scan.
type ScannerInto interface {
// ScanInto assigns a value from a database driver.
@@ -479,8 +556,11 @@ func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
if err != nil {
return nil, err
}
- defer stmt.Close()
- return stmt.Query(args...)
+ rows, err := stmt.Query(args...)
+ if err == nil {
+ rows.closeStmt = stmt
+ }
+ return rows, err
}
// QueryRow executes a query that is expected to return at most one row.
@@ -824,6 +904,12 @@ func (rs *Rows) Scan(dest ...interface{}) error {
if !ok {
continue
}
+ if *b == nil {
+ // If the []byte is now nil (for a NULL value),
+ // don't fall through to below which would
+ // turn it into a non-nil 0-length byte slice
+ continue
+ }
if _, ok = dp.(*RawBytes); ok {
continue
}
@@ -865,17 +951,10 @@ func (r *Row) Scan(dest ...interface{}) error {
if r.err != nil {
return r.err
}
- defer r.rows.Close()
- if !r.rows.Next() {
- return ErrNoRows
- }
- err := r.rows.Scan(dest...)
- if err != nil {
- return err
- }
// TODO(bradfitz): for now we need to defensively clone all
- // []byte that the driver returned, since we're about to close
+ // []byte that the driver returned (not permitting
+ // *RawBytes in Rows.Scan), since we're about to close
// the Rows in our defer, when we return from this function.
// the contract with the driver.Next(...) interface is that it
// can return slices into read-only temporary memory that's
@@ -890,14 +969,17 @@ func (r *Row) Scan(dest ...interface{}) error {
if _, ok := dp.(*RawBytes); ok {
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
}
- b, ok := dp.(*[]byte)
- if !ok {
- continue
- }
- clone := make([]byte, len(*b))
- copy(clone, *b)
- *b = clone
}
+
+ defer r.rows.Close()
+ if !r.rows.Next() {
+ return ErrNoRows
+ }
+ err := r.rows.Scan(dest...)
+ if err != nil {
+ return err
+ }
+
return nil
}
diff --git a/libgo/go/database/sql/sql_test.go b/libgo/go/database/sql/sql_test.go
index 3fe9398..c5cadad 100644
--- a/libgo/go/database/sql/sql_test.go
+++ b/libgo/go/database/sql/sql_test.go
@@ -5,6 +5,7 @@
package sql
import (
+ "fmt"
"reflect"
"strings"
"testing"
@@ -310,6 +311,40 @@ func TestTxStmt(t *testing.T) {
}
}
+// Issue: http://golang.org/issue/2784
+// This test didn't fail before because we got luckly with the fakedb driver.
+// It was failing, and now not, in github.com/bradfitz/go-sql-test
+func TestTxQuery(t *testing.T) {
+ db := newTestDB(t, "")
+ defer closeDB(t, db)
+ exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool")
+ exec(t, db, "INSERT|t1|name=Alice")
+
+ tx, err := db.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer tx.Rollback()
+
+ r, err := tx.Query("SELECT|t1|name|")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !r.Next() {
+ if r.Err() != nil {
+ t.Fatal(r.Err())
+ }
+ t.Fatal("expected one row")
+ }
+
+ var x string
+ err = r.Scan(&x)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
+
// Tests fix for issue 2542, that we release a lock when querying on
// a closed connection.
func TestIssue2542Deadlock(t *testing.T) {
@@ -323,6 +358,34 @@ func TestIssue2542Deadlock(t *testing.T) {
}
}
+// Tests fix for issue 2788, that we bind nil to a []byte if the
+// value in the column is sql null
+func TestNullByteSlice(t *testing.T) {
+ db := newTestDB(t, "")
+ defer closeDB(t, db)
+ exec(t, db, "CREATE|t|id=int32,name=nullstring")
+ exec(t, db, "INSERT|t|id=10,name=?", nil)
+
+ var name []byte
+
+ err := db.QueryRow("SELECT|t|name|id=?", 10).Scan(&name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if name != nil {
+ t.Fatalf("name []byte should be nil for null column value, got: %#v", name)
+ }
+
+ exec(t, db, "INSERT|t|id=11,name=?", "bob")
+ err = db.QueryRow("SELECT|t|name|id=?", 11).Scan(&name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(name) != "bob" {
+ t.Fatalf("name []byte should be bob, got: %q", string(name))
+ }
+}
+
func TestQueryRowClosingStmt(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
@@ -341,64 +404,116 @@ func TestQueryRowClosingStmt(t *testing.T) {
}
}
+type nullTestRow struct {
+ nullParam interface{}
+ notNullParam interface{}
+ scanNullVal interface{}
+}
+
+type nullTestSpec struct {
+ nullType string
+ notNullType string
+ rows [6]nullTestRow
+}
+
func TestNullStringParam(t *testing.T) {
+ spec := nullTestSpec{"nullstring", "string", [6]nullTestRow{
+ nullTestRow{NullString{"aqua", true}, "", NullString{"aqua", true}},
+ nullTestRow{NullString{"brown", false}, "", NullString{"", false}},
+ nullTestRow{"chartreuse", "", NullString{"chartreuse", true}},
+ nullTestRow{NullString{"darkred", true}, "", NullString{"darkred", true}},
+ nullTestRow{NullString{"eel", false}, "", NullString{"", false}},
+ nullTestRow{"foo", NullString{"black", false}, nil},
+ }}
+ nullTestRun(t, spec)
+}
+
+func TestNullInt64Param(t *testing.T) {
+ spec := nullTestSpec{"nullint64", "int64", [6]nullTestRow{
+ nullTestRow{NullInt64{31, true}, 1, NullInt64{31, true}},
+ nullTestRow{NullInt64{-22, false}, 1, NullInt64{0, false}},
+ nullTestRow{22, 1, NullInt64{22, true}},
+ nullTestRow{NullInt64{33, true}, 1, NullInt64{33, true}},
+ nullTestRow{NullInt64{222, false}, 1, NullInt64{0, false}},
+ nullTestRow{0, NullInt64{31, false}, nil},
+ }}
+ nullTestRun(t, spec)
+}
+
+func TestNullFloat64Param(t *testing.T) {
+ spec := nullTestSpec{"nullfloat64", "float64", [6]nullTestRow{
+ nullTestRow{NullFloat64{31.2, true}, 1, NullFloat64{31.2, true}},
+ nullTestRow{NullFloat64{13.1, false}, 1, NullFloat64{0, false}},
+ nullTestRow{-22.9, 1, NullFloat64{-22.9, true}},
+ nullTestRow{NullFloat64{33.81, true}, 1, NullFloat64{33.81, true}},
+ nullTestRow{NullFloat64{222, false}, 1, NullFloat64{0, false}},
+ nullTestRow{10, NullFloat64{31.2, false}, nil},
+ }}
+ nullTestRun(t, spec)
+}
+
+func TestNullBoolParam(t *testing.T) {
+ spec := nullTestSpec{"nullbool", "bool", [6]nullTestRow{
+ nullTestRow{NullBool{false, true}, true, NullBool{false, true}},
+ nullTestRow{NullBool{true, false}, false, NullBool{false, false}},
+ nullTestRow{true, true, NullBool{true, true}},
+ nullTestRow{NullBool{true, true}, false, NullBool{true, true}},
+ nullTestRow{NullBool{true, false}, true, NullBool{false, false}},
+ nullTestRow{true, NullBool{true, false}, nil},
+ }}
+ nullTestRun(t, spec)
+}
+
+func nullTestRun(t *testing.T, spec nullTestSpec) {
db := newTestDB(t, "")
defer closeDB(t, db)
- exec(t, db, "CREATE|t|id=int32,name=string,favcolor=nullstring")
+ exec(t, db, fmt.Sprintf("CREATE|t|id=int32,name=string,nullf=%s,notnullf=%s", spec.nullType, spec.notNullType))
// Inserts with db.Exec:
- exec(t, db, "INSERT|t|id=?,name=?,favcolor=?", 1, "alice", NullString{"aqua", true})
- exec(t, db, "INSERT|t|id=?,name=?,favcolor=?", 2, "bob", NullString{"brown", false})
-
- _, err := db.Exec("INSERT|t|id=?,name=?,favcolor=?", 999, nil, nil)
- if err == nil {
- // TODO: this test fails, but it's just because
- // fakeConn implements the optional Execer interface,
- // so arguably this is the correct behavior. But
- // maybe I should flesh out the fakeConn.Exec
- // implementation so this properly fails.
- // t.Errorf("expected error inserting nil name with Exec")
- }
+ exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 1, "alice", spec.rows[0].nullParam, spec.rows[0].notNullParam)
+ exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 2, "bob", spec.rows[1].nullParam, spec.rows[1].notNullParam)
// Inserts with a prepared statement:
- stmt, err := db.Prepare("INSERT|t|id=?,name=?,favcolor=?")
+ stmt, err := db.Prepare("INSERT|t|id=?,name=?,nullf=?,notnullf=?")
if err != nil {
t.Fatalf("prepare: %v", err)
}
- if _, err := stmt.Exec(3, "chris", "chartreuse"); err != nil {
+ if _, err := stmt.Exec(3, "chris", spec.rows[2].nullParam, spec.rows[2].notNullParam); err != nil {
t.Errorf("exec insert chris: %v", err)
}
- if _, err := stmt.Exec(4, "dave", NullString{"darkred", true}); err != nil {
+ if _, err := stmt.Exec(4, "dave", spec.rows[3].nullParam, spec.rows[3].notNullParam); err != nil {
t.Errorf("exec insert dave: %v", err)
}
- if _, err := stmt.Exec(5, "eleanor", NullString{"eel", false}); err != nil {
- t.Errorf("exec insert dave: %v", err)
+ if _, err := stmt.Exec(5, "eleanor", spec.rows[4].nullParam, spec.rows[4].notNullParam); err != nil {
+ t.Errorf("exec insert eleanor: %v", err)
}
- // Can't put null name into non-nullstring column,
- if _, err := stmt.Exec(5, NullString{"", false}, nil); err == nil {
- t.Errorf("expected error inserting nil name with prepared statement Exec")
+ // Can't put null val into non-null col
+ if _, err := stmt.Exec(6, "bob", spec.rows[5].nullParam, spec.rows[5].notNullParam); err == nil {
+ t.Errorf("expected error inserting nil val with prepared statement Exec")
}
- type nameColor struct {
- name string
- favColor NullString
+ _, err = db.Exec("INSERT|t|id=?,name=?,nullf=?", 999, nil, nil)
+ if err == nil {
+ // TODO: this test fails, but it's just because
+ // fakeConn implements the optional Execer interface,
+ // so arguably this is the correct behavior. But
+ // maybe I should flesh out the fakeConn.Exec
+ // implementation so this properly fails.
+ // t.Errorf("expected error inserting nil name with Exec")
}
- wantMap := map[int]nameColor{
- 1: nameColor{"alice", NullString{"aqua", true}},
- 2: nameColor{"bob", NullString{"", false}},
- 3: nameColor{"chris", NullString{"chartreuse", true}},
- 4: nameColor{"dave", NullString{"darkred", true}},
- 5: nameColor{"eleanor", NullString{"", false}},
- }
- for id, want := range wantMap {
- var got nameColor
- if err := db.QueryRow("SELECT|t|name,favcolor|id=?", id).Scan(&got.name, &got.favColor); err != nil {
+ paramtype := reflect.TypeOf(spec.rows[0].nullParam)
+ bindVal := reflect.New(paramtype).Interface()
+
+ for i := 0; i < 5; i++ {
+ id := i + 1
+ if err := db.QueryRow("SELECT|t|nullf|id=?", id).Scan(bindVal); err != nil {
t.Errorf("id=%d Scan: %v", id, err)
}
- if got != want {
- t.Errorf("id=%d got %#v, want %#v", id, got, want)
+ bindValDeref := reflect.ValueOf(bindVal).Elem().Interface()
+ if !reflect.DeepEqual(bindValDeref, spec.rows[i].scanNullVal) {
+ t.Errorf("id=%d got %#v, want %#v", id, bindValDeref, spec.rows[i].scanNullVal)
}
}
}