From 07f9c9c30fbc180b71842b9282da7b5fa3d40fc9 Mon Sep 17 00:00:00 2001 From: mattn Date: Tue, 24 Mar 2015 00:46:49 +0900 Subject: [PATCH] Implement number-named parameters. Close #187 --- sqlite3.go | 22 +++++++++++++++++++--- sqlite3_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index d384202..65865f3 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -122,6 +122,7 @@ type SQLiteStmt struct { c *SQLiteConn s *C.sqlite3_stmt nv int + nn []string t string closed bool cls bool @@ -382,7 +383,14 @@ func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) { t = strings.TrimSpace(C.GoString(tail)) } nv := int(C.sqlite3_bind_parameter_count(s)) - ss := &SQLiteStmt{c: c, s: s, nv: nv, t: t} + var nn []string + for i := 0; i < nv; i++ { + pn := C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1))) + if len(pn) > 1 && pn[0] == '$' && 48 <= pn[1] && pn[1] <= 57 { + nn = append(nn, C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1)))) + } + } + ss := &SQLiteStmt{c: c, s: s, nv: nv, nn: nn, t: t} runtime.SetFinalizer(ss, (*SQLiteStmt).Close) return ss, nil } @@ -423,8 +431,16 @@ func (s *SQLiteStmt) bind(args []driver.Value) error { var vargs []bindArg narg := len(args) vargs = make([]bindArg, narg) - for i, v := range args { - vargs[i] = bindArg{i + 1, v} + if len(s.nn) > 0 { + for i, v := range s.nn { + if pi, err := strconv.Atoi(v[1:]); err == nil { + vargs[i] = bindArg{pi, args[i]} + } + } + } else { + for i, v := range args { + vargs[i] = bindArg{i + 1, v} + } } for _, varg := range vargs { diff --git a/sqlite3_test.go b/sqlite3_test.go index 81113fc..aa86011 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -909,3 +909,39 @@ func TestVersion(t *testing.T) { t.Errorf("Version failed %q, %d, %q\n", s, n, id) } } + +func TestNumberNamedParams(t *testing.T) { + tempFilename := TempFilename() + db, err := sql.Open("sqlite3", tempFilename) + if err != nil { + t.Fatal("Failed to open database:", err) + } + defer os.Remove(tempFilename) + defer db.Close() + + _, err = db.Exec(` + create table foo (id integer, name text, extra text); + `) + if err != nil { + t.Error("Failed to call db.Query:", err) + } + + _, err = db.Exec(`insert into foo(id, name, extra) values($1, $2, $2)`, 1, "foo") + if err != nil { + t.Error("Failed to call db.Exec:", err) + } + + row := db.QueryRow(`select id, extra from foo where id = $1 and extra = $2`, 1, "foo") + if row == nil { + t.Error("Failed to call db.QueryRow") + } + var id int + var extra string + err = row.Scan(&id, &extra) + if err != nil { + t.Error("Failed to db.Scan:", err) + } + if id != 1 || extra != "foo" { + t.Error("Failed to db.QueryRow: not matched results") + } +}