This commit is contained in:
mattn 2015-03-22 04:29:14 +09:00
parent e3990c31a0
commit ff38c8ec02
2 changed files with 3 additions and 77 deletions

View File

@ -382,18 +382,6 @@ 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))
/*
if nv > 0 {
pn := C.GoString(C.sqlite3_bind_parameter_name(s, 1))
// TODO: map argument for named parameters
if len(pn) > 0 && pn[0] == '$' && pn[1] != '1' {
nv = -1
}
if len(pn) > 0 && pn[0] != '?' {
nv = -1
}
}
*/
ss := &SQLiteStmt{c: c, s: s, nv: nv, t: t} ss := &SQLiteStmt{c: c, s: s, nv: nv, t: t}
runtime.SetFinalizer(ss, (*SQLiteStmt).Close) runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
return ss, nil return ss, nil
@ -434,36 +422,10 @@ func (s *SQLiteStmt) bind(args []driver.Value) error {
var vargs []bindArg var vargs []bindArg
narg := len(args) narg := len(args)
if s.nv == -1 {
/* TODO: map argument for named parameters
if narg == 1 {
if m, ok := args[0].(map[string]driver.Value); ok {
for k, v := range m {
pn := C.CString(k)
if pi := int(C.sqlite3_bind_parameter_index(s.s, pn)); pi > 0 {
vargs = append(vargs, bindArg{pi, v})
}
C.free(unsafe.Pointer(pn))
}
}
narg = 0
}
*/
if narg > 0 {
for i := 0; i < narg; i++ {
pn := C.CString(fmt.Sprint(i + 1))
if pi := int(C.sqlite3_bind_parameter_index(s.s, pn)); pi > 0 {
vargs = append(vargs, bindArg{pi, args[i]})
}
C.free(unsafe.Pointer(pn))
}
}
} else {
vargs = make([]bindArg, narg) vargs = make([]bindArg, narg)
for i, v := range args { for i, v := range args {
vargs[i] = bindArg{i + 1, v} vargs[i] = bindArg{i + 1, v}
} }
}
for _, varg := range vargs { for _, varg := range vargs {
n := C.int(varg.n) n := C.int(varg.n)

View File

@ -909,39 +909,3 @@ 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, name, extra 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")
}
}