aboutsummaryrefslogtreecommitdiff
path: root/src/database/sql/sql.go
diff options
context:
space:
mode:
authorDaniel Theophanes <kardianos@gmail.com>2016-10-03 09:49:25 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2016-10-17 07:56:35 +0000
commit707a83341b8c7973f4e0fce731fa279c618f233b (patch)
tree3436b372be1d863a3e5f2f2e837fbabe577252c8 /src/database/sql/sql.go
parent99df54f19696e26bea8d6a052d8d91ddb1e4ea65 (diff)
downloadgo-707a83341b8c7973f4e0fce731fa279c618f233b.tar.xz
database/sql: add option to use named parameter in query arguments
Modify the new Context methods to take a name-value driver struct. This will require more modifications to drivers to use, but will reduce the overall number of structures that need to be maintained over time. Fixes #12381 Change-Id: I30747533ce418a1be5991a0c8767a26e8451adbd Reviewed-on: https://go-review.googlesource.com/30166 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/database/sql/sql.go')
-rw-r--r--src/database/sql/sql.go23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go
index 970334269d..616acb2be1 100644
--- a/src/database/sql/sql.go
+++ b/src/database/sql/sql.go
@@ -67,6 +67,27 @@ func Drivers() []string {
return list
}
+// NamedParam may be passed into query parameter arguments to associate
+// a named placeholder with a value.
+type NamedParam struct {
+ // Name of the parameter placeholder. If empty the ordinal position in the
+ // argument list will be used.
+ Name string
+
+ // Value of the parameter. It may be assigned the same value types as
+ // the query arguments.
+ Value interface{}
+}
+
+// Param provides a more concise way to create NamedParam values.
+func Param(name string, value interface{}) NamedParam {
+ // This method exists because the go1compat promise
+ // doesn't guarantee that structs don't grow more fields,
+ // so unkeyed struct literals are a vet error. Thus, we don't
+ // want to encourage sql.NamedParam{name, value}.
+ return NamedParam{Name: name, Value: value}
+}
+
// RawBytes is a byte slice that holds a reference to memory owned by
// the database itself. After a Scan into a RawBytes, the slice is only
// valid until the next call to Next, Scan, or Close.
@@ -1064,7 +1085,7 @@ func (db *DB) exec(ctx context.Context, query string, args []interface{}, strate
}()
if execer, ok := dc.ci.(driver.Execer); ok {
- var dargs []driver.Value
+ var dargs []driver.NamedValue
dargs, err = driverArgs(nil, args)
if err != nil {
return nil, err