Implement number-named parameters. Close #187
parent
ff38c8ec02
commit
07f9c9c30f
22
sqlite3.go
22
sqlite3.go
|
@ -122,6 +122,7 @@ type SQLiteStmt struct {
|
||||||
c *SQLiteConn
|
c *SQLiteConn
|
||||||
s *C.sqlite3_stmt
|
s *C.sqlite3_stmt
|
||||||
nv int
|
nv int
|
||||||
|
nn []string
|
||||||
t string
|
t string
|
||||||
closed bool
|
closed bool
|
||||||
cls bool
|
cls bool
|
||||||
|
@ -382,7 +383,14 @@ func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
|
||||||
t = strings.TrimSpace(C.GoString(tail))
|
t = strings.TrimSpace(C.GoString(tail))
|
||||||
}
|
}
|
||||||
nv := int(C.sqlite3_bind_parameter_count(s))
|
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)
|
runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
|
||||||
return ss, nil
|
return ss, nil
|
||||||
}
|
}
|
||||||
|
@ -423,8 +431,16 @@ func (s *SQLiteStmt) bind(args []driver.Value) error {
|
||||||
var vargs []bindArg
|
var vargs []bindArg
|
||||||
narg := len(args)
|
narg := len(args)
|
||||||
vargs = make([]bindArg, narg)
|
vargs = make([]bindArg, narg)
|
||||||
for i, v := range args {
|
if len(s.nn) > 0 {
|
||||||
vargs[i] = bindArg{i + 1, v}
|
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 {
|
for _, varg := range vargs {
|
||||||
|
|
|
@ -909,3 +909,39 @@ func TestVersion(t *testing.T) {
|
||||||
t.Errorf("Version failed %q, %d, %q\n", s, n, id)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue