Commit 85370baf authored by gwenn's avatar gwenn

Fix NamedValue

parent 96e70462
......@@ -356,7 +356,7 @@ func (s *Stmt) bindNamedValue(args []driver.NamedValue) error {
return err
}
} else {
index, err := s.BindParameterIndex(v.Name)
index, err := s.BindParameterIndex(":" + v.Name) // TODO "$" and "@"
if err != nil {
return err
}
......
......@@ -22,8 +22,9 @@ const (
"INSERT INTO test (name) VALUES ('Lisa');" +
"UPDATE test SET name = 'El Barto' WHERE name = 'Bart';" +
"DELETE FROM test WHERE name = 'Bart';"
insert = "INSERT INTO test (name) VALUES (?)"
query = "SELECT * FROM test WHERE name LIKE ?"
insert = "INSERT INTO test (name) VALUES (?)"
insert_named = "INSERT INTO test (name) VALUES (:name)"
query = "SELECT * FROM test WHERE name LIKE ?"
)
func sqlOpen(t *testing.T) *sql.DB {
......@@ -104,6 +105,19 @@ func TestSqlInsert(t *testing.T) {
assert.Equal(t, int64(1), changes, "rowsAffected")
}
func TestSqlInsertNamed(t *testing.T) {
db := sqlCreate(ddl, t)
defer checkSqlDbClose(db, t)
result, err := db.Exec(insert_named, sql.Named("name", "Bart"))
checkNoError(t, err, "Error updating data: %s")
id, err := result.LastInsertId()
checkNoError(t, err, "Error while calling LastInsertId: %s")
assert.Equal(t, int64(1), id, "lastInsertId")
changes, err := result.RowsAffected()
checkNoError(t, err, "Error while calling RowsAffected: %s")
assert.Equal(t, int64(1), changes, "rowsAffected")
}
func TestSqlExecWithIllegalCmd(t *testing.T) {
db := sqlCreate(ddl+dml, t)
defer checkSqlDbClose(db, t)
......@@ -160,6 +174,23 @@ func TestSqlPrepare(t *testing.T) {
assert.Equal(t, int64(1), changes, "rowsAffected")
}
func TestSqlPrepareNamed(t *testing.T) {
db := sqlCreate(ddl+dml, t)
defer checkSqlDbClose(db, t)
stmt, err := db.Prepare(insert_named)
checkNoError(t, err, "Error while preparing stmt: %s")
defer checkSqlStmtClose(stmt, t)
result, err := stmt.Exec(sql.Named("name", "Bart"))
checkNoError(t, err, "Error while executing stmt: %s")
id, err := result.LastInsertId()
checkNoError(t, err, "Error while calling LastInsertId: %s")
assert.Equal(t, int64(3), id, "lastInsertId")
changes, err := result.RowsAffected()
checkNoError(t, err, "Error while calling RowsAffected: %s")
assert.Equal(t, int64(1), changes, "rowsAffected")
}
func TestRowsWithStmtClosed(t *testing.T) {
db := sqlCreate(ddl+dml, t)
defer checkSqlDbClose(db, t)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment