Commit 2b1abf75 authored by Daniel Theophanes's avatar Daniel Theophanes Committed by Brad Fitzpatrick

database/sql: rename NamedParam to NamedArg and Param to Named

Be consistent with the argument names already provided. Also
parameter is the variable, argument is the value.

Fixes #18099

Change-Id: Idb3f4e9ffc214036c721ddb4f614ec6c95bb7778
Reviewed-on: https://go-review.googlesource.com/33660
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent feacaca7
...@@ -42,7 +42,7 @@ func driverArgs(ds *driverStmt, args []interface{}) ([]driver.NamedValue, error) ...@@ -42,7 +42,7 @@ func driverArgs(ds *driverStmt, args []interface{}) ([]driver.NamedValue, error)
var err error var err error
nv := &nvargs[n] nv := &nvargs[n]
nv.Ordinal = n + 1 nv.Ordinal = n + 1
if np, ok := arg.(NamedParam); ok { if np, ok := arg.(NamedArg); ok {
arg = np.Value arg = np.Value
nvargs[n].Name = np.Name nvargs[n].Name = np.Name
} }
...@@ -59,7 +59,7 @@ func driverArgs(ds *driverStmt, args []interface{}) ([]driver.NamedValue, error) ...@@ -59,7 +59,7 @@ func driverArgs(ds *driverStmt, args []interface{}) ([]driver.NamedValue, error)
for n, arg := range args { for n, arg := range args {
nv := &nvargs[n] nv := &nvargs[n]
nv.Ordinal = n + 1 nv.Ordinal = n + 1
if np, ok := arg.(NamedParam); ok { if np, ok := arg.(NamedArg); ok {
arg = np.Value arg = np.Value
nv.Name = np.Name nv.Name = np.Name
} }
......
...@@ -69,9 +69,9 @@ func Drivers() []string { ...@@ -69,9 +69,9 @@ func Drivers() []string {
return list return list
} }
// NamedParam may be passed into query parameter arguments to associate // A NamedArg used as an argument to Query or Exec
// a named placeholder with a value. // binds to the corresponding named parameter in the SQL statement.
type NamedParam struct { type NamedArg struct {
_Named_Fields_Required struct{} _Named_Fields_Required struct{}
// Name of the parameter placeholder. If empty the ordinal position in the // Name of the parameter placeholder. If empty the ordinal position in the
...@@ -83,13 +83,24 @@ type NamedParam struct { ...@@ -83,13 +83,24 @@ type NamedParam struct {
Value interface{} Value interface{}
} }
// Param provides a more concise way to create NamedParam values. // Named provides a more concise way to create NamedArg values.
func Param(name string, value interface{}) NamedParam { //
// Example usage:
//
// db.ExecContext(ctx, `
// delete from Invoice
// where
// TimeCreated < @end
// and TimeCreated >= @start;`,
// sql.Named("start", startTime),
// sql.Named("end", endTime),
// )
func Named(name string, value interface{}) NamedArg {
// This method exists because the go1compat promise // This method exists because the go1compat promise
// doesn't guarantee that structs don't grow more fields, // doesn't guarantee that structs don't grow more fields,
// so unkeyed struct literals are a vet error. Thus, we don't // so unkeyed struct literals are a vet error. Thus, we don't
// want to encourage sql.NamedParam{name, value}. // want to allow sql.NamedArg{name, value}.
return NamedParam{Name: name, Value: value} return NamedArg{Name: name, Value: value}
} }
// IsolationLevel is the transaction isolation level stored in Context. // IsolationLevel is the transaction isolation level stored in Context.
......
...@@ -468,15 +468,15 @@ func TestMultiResultSetQuery(t *testing.T) { ...@@ -468,15 +468,15 @@ func TestMultiResultSetQuery(t *testing.T) {
} }
} }
func TestQueryNamedParam(t *testing.T) { func TestQueryNamedArg(t *testing.T) {
db := newTestDB(t, "people") db := newTestDB(t, "people")
defer closeDB(t, db) defer closeDB(t, db)
prepares0 := numPrepares(t, db) prepares0 := numPrepares(t, db)
rows, err := db.Query( rows, err := db.Query(
// Ensure the name and age parameters only match on placeholder name, not position. // Ensure the name and age parameters only match on placeholder name, not position.
"SELECT|people|age,name|name=?name,age=?age", "SELECT|people|age,name|name=?name,age=?age",
Param("?age", 2), Named("?age", 2),
Param("?name", "Bob"), Named("?name", "Bob"),
) )
if err != nil { if err != nil {
t.Fatalf("Query: %v", err) t.Fatalf("Query: %v", err)
......
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