database/sql: treat pointers as nullable types like encoding/json

- convert from nil pointers to the nil interface{}
- dereference non-nil pointers
- convert from nil interface{}s to nil pointers
- allocate pointers for non-nil interface{}s
- tests for all of the above

R=golang-dev, bradfitz, rsc, rogpeppe
CC=golang-dev
https://golang.org/cl/5630052
diff --git a/src/pkg/database/sql/convert_test.go b/src/pkg/database/sql/convert_test.go
index 34ee939..9c362d7 100644
--- a/src/pkg/database/sql/convert_test.go
+++ b/src/pkg/database/sql/convert_test.go
@@ -13,6 +13,7 @@
 )
 
 var someTime = time.Unix(123, 0)
+var answer int64 = 42
 
 type conversionTest struct {
 	s, d interface{} // source and destination
@@ -27,6 +28,8 @@
 	wantbool  bool // used if d is of type *bool
 	wanterr   string
 	wantiface interface{}
+	wantptr   *int64 // if non-nil, *d's pointed value must be equal to *wantptr
+	wantnil   bool   // if true, *d must be *int64(nil)
 }
 
 // Target variables for scanning into.
@@ -42,6 +45,7 @@
 	scanf32    float32
 	scanf64    float64
 	scantime   time.Time
+	scanptr    *int64
 	scaniface  interface{}
 )
 
@@ -98,6 +102,10 @@
 	{s: "1.5", d: &scanf32, wantf32: float32(1.5)},
 	{s: "1.5", d: &scanf64, wantf64: float64(1.5)},
 
+	// Pointers
+	{s: interface{}(nil), d: &scanptr, wantnil: true},
+	{s: int64(42), d: &scanptr, wantptr: &answer},
+
 	// To interface{}
 	{s: float64(1.5), d: &scaniface, wantiface: float64(1.5)},
 	{s: int64(1), d: &scaniface, wantiface: int64(1)},
@@ -107,6 +115,10 @@
 	{s: nil, d: &scaniface},
 }
 
+func intPtrValue(intptr interface{}) interface{} {
+	return reflect.Indirect(reflect.Indirect(reflect.ValueOf(intptr))).Int()
+}
+
 func intValue(intptr interface{}) int64 {
 	return reflect.Indirect(reflect.ValueOf(intptr)).Int()
 }
@@ -162,6 +174,16 @@
 		if !ct.wanttime.IsZero() && !ct.wanttime.Equal(timeValue(ct.d)) {
 			errf("want time %v, got %v", ct.wanttime, timeValue(ct.d))
 		}
+		if ct.wantnil && *ct.d.(**int64) != nil {
+			errf("want nil, got %v", intPtrValue(ct.d))
+		}
+		if ct.wantptr != nil {
+			if *ct.d.(**int64) == nil {
+				errf("want pointer to %v, got nil", *ct.wantptr)
+			} else if *ct.wantptr != intPtrValue(ct.d) {
+				errf("want pointer to %v, got %v", *ct.wantptr, intPtrValue(ct.d))
+			}
+		}
 		if ifptr, ok := ct.d.(*interface{}); ok {
 			if !reflect.DeepEqual(ct.wantiface, scaniface) {
 				errf("want interface %#v, got %#v", ct.wantiface, scaniface)