From 707a83341b8c7973f4e0fce731fa279c618f233b Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Mon, 3 Oct 2016 09:49:25 -0700 Subject: 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/database/sql/driver/driver.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/database/sql/driver') diff --git a/src/database/sql/driver/driver.go b/src/database/sql/driver/driver.go index b3d83f3ff4..6cc970f688 100644 --- a/src/database/sql/driver/driver.go +++ b/src/database/sql/driver/driver.go @@ -24,6 +24,16 @@ import ( // time.Time type Value interface{} +// NamedValue holds both the value name and value. +// The Ordinal is the position of the parameter starting from one and is always set. +// If the Name is not empty it should be used for the parameter identifier and +// not the ordinal position. +type NamedValue struct { + Name string + Ordinal int + Value Value +} + // Driver is the interface that must be implemented by a database // driver. type Driver interface { @@ -71,7 +81,7 @@ type Execer interface { // ExecerContext is like execer, but must honor the context timeout and return // when the context is cancelled. type ExecerContext interface { - ExecContext(ctx context.Context, query string, args []Value) (Result, error) + ExecContext(ctx context.Context, query string, args []NamedValue) (Result, error) } // Queryer is an optional interface that may be implemented by a Conn. @@ -88,7 +98,7 @@ type Queryer interface { // QueryerContext is like Queryer, but most honor the context timeout and return // when the context is cancelled. type QueryerContext interface { - QueryContext(ctx context.Context, query string, args []Value) (Rows, error) + QueryContext(ctx context.Context, query string, args []NamedValue) (Rows, error) } // Conn is a connection to a database. It is not used concurrently @@ -174,13 +184,13 @@ type Stmt interface { // StmtExecContext enhances the Stmt interface by providing Exec with context. type StmtExecContext interface { // ExecContext must honor the context timeout and return when it is cancelled. - ExecContext(ctx context.Context, args []Value) (Result, error) + ExecContext(ctx context.Context, args []NamedValue) (Result, error) } // StmtQueryContext enhances the Stmt interface by providing Query with context. type StmtQueryContext interface { // QueryContext must honor the context timeout and return when it is cancelled. - QueryContext(ctx context.Context, args []Value) (Rows, error) + QueryContext(ctx context.Context, args []NamedValue) (Rows, error) } // ColumnConverter may be optionally implemented by Stmt if the -- cgit v1.3