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/ctxutil.go | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'src/database/sql/ctxutil.go') diff --git a/src/database/sql/ctxutil.go b/src/database/sql/ctxutil.go index e1d4c03c9a..173f6a9d2b 100644 --- a/src/database/sql/ctxutil.go +++ b/src/database/sql/ctxutil.go @@ -50,9 +50,13 @@ func ctxDriverPrepare(ctx context.Context, ci driver.Conn, query string) (driver } } -func ctxDriverExec(ctx context.Context, execer driver.Execer, query string, dargs []driver.Value) (driver.Result, error) { +func ctxDriverExec(ctx context.Context, execer driver.Execer, query string, nvdargs []driver.NamedValue) (driver.Result, error) { if execerCtx, is := execer.(driver.ExecerContext); is { - return execerCtx.ExecContext(ctx, query, dargs) + return execerCtx.ExecContext(ctx, query, nvdargs) + } + dargs, err := namedValueToValue(nvdargs) + if err != nil { + return nil, err } if ctx.Done() == context.Background().Done() { return execer.Exec(query, dargs) @@ -90,9 +94,13 @@ func ctxDriverExec(ctx context.Context, execer driver.Execer, query string, darg } } -func ctxDriverQuery(ctx context.Context, queryer driver.Queryer, query string, dargs []driver.Value) (driver.Rows, error) { +func ctxDriverQuery(ctx context.Context, queryer driver.Queryer, query string, nvdargs []driver.NamedValue) (driver.Rows, error) { if queryerCtx, is := queryer.(driver.QueryerContext); is { - return queryerCtx.QueryContext(ctx, query, dargs) + return queryerCtx.QueryContext(ctx, query, nvdargs) + } + dargs, err := namedValueToValue(nvdargs) + if err != nil { + return nil, err } if ctx.Done() == context.Background().Done() { return queryer.Query(query, dargs) @@ -130,9 +138,13 @@ func ctxDriverQuery(ctx context.Context, queryer driver.Queryer, query string, d } } -func ctxDriverStmtExec(ctx context.Context, si driver.Stmt, dargs []driver.Value) (driver.Result, error) { +func ctxDriverStmtExec(ctx context.Context, si driver.Stmt, nvdargs []driver.NamedValue) (driver.Result, error) { if siCtx, is := si.(driver.StmtExecContext); is { - return siCtx.ExecContext(ctx, dargs) + return siCtx.ExecContext(ctx, nvdargs) + } + dargs, err := namedValueToValue(nvdargs) + if err != nil { + return nil, err } if ctx.Done() == context.Background().Done() { return si.Exec(dargs) @@ -170,9 +182,13 @@ func ctxDriverStmtExec(ctx context.Context, si driver.Stmt, dargs []driver.Value } } -func ctxDriverStmtQuery(ctx context.Context, si driver.Stmt, dargs []driver.Value) (driver.Rows, error) { +func ctxDriverStmtQuery(ctx context.Context, si driver.Stmt, nvdargs []driver.NamedValue) (driver.Rows, error) { if siCtx, is := si.(driver.StmtQueryContext); is { - return siCtx.QueryContext(ctx, dargs) + return siCtx.QueryContext(ctx, nvdargs) + } + dargs, err := namedValueToValue(nvdargs) + if err != nil { + return nil, err } if ctx.Done() == context.Background().Done() { return si.Query(dargs) @@ -253,3 +269,14 @@ func ctxDriverBegin(ctx context.Context, ci driver.Conn) (driver.Tx, error) { return r.txi, r.err } } + +func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) { + dargs := make([]driver.Value, len(named)) + for n, param := range named { + if len(param.Name) > 0 { + return nil, errors.New("sql: driver does not support the use of Named Parameters") + } + dargs[n] = param.Value + } + return dargs, nil +} -- cgit v1.3