Commit 85370baf authored by gwenn's avatar gwenn

Fix NamedValue

parent 96e70462
...@@ -356,7 +356,7 @@ func (s *Stmt) bindNamedValue(args []driver.NamedValue) error { ...@@ -356,7 +356,7 @@ func (s *Stmt) bindNamedValue(args []driver.NamedValue) error {
return err return err
} }
} else { } else {
index, err := s.BindParameterIndex(v.Name) index, err := s.BindParameterIndex(":" + v.Name) // TODO "$" and "@"
if err != nil { if err != nil {
return err return err
} }
......
...@@ -23,6 +23,7 @@ const ( ...@@ -23,6 +23,7 @@ const (
"UPDATE test SET name = 'El Barto' WHERE name = 'Bart';" + "UPDATE test SET name = 'El Barto' WHERE name = 'Bart';" +
"DELETE FROM test WHERE name = 'Bart';" "DELETE FROM test WHERE name = 'Bart';"
insert = "INSERT INTO test (name) VALUES (?)" insert = "INSERT INTO test (name) VALUES (?)"
insert_named = "INSERT INTO test (name) VALUES (:name)"
query = "SELECT * FROM test WHERE name LIKE ?" query = "SELECT * FROM test WHERE name LIKE ?"
) )
...@@ -104,6 +105,19 @@ func TestSqlInsert(t *testing.T) { ...@@ -104,6 +105,19 @@ func TestSqlInsert(t *testing.T) {
assert.Equal(t, int64(1), changes, "rowsAffected") 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) { func TestSqlExecWithIllegalCmd(t *testing.T) {
db := sqlCreate(ddl+dml, t) db := sqlCreate(ddl+dml, t)
defer checkSqlDbClose(db, t) defer checkSqlDbClose(db, t)
...@@ -160,6 +174,23 @@ func TestSqlPrepare(t *testing.T) { ...@@ -160,6 +174,23 @@ func TestSqlPrepare(t *testing.T) {
assert.Equal(t, int64(1), changes, "rowsAffected") 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) { func TestRowsWithStmtClosed(t *testing.T) {
db := sqlCreate(ddl+dml, t) db := sqlCreate(ddl+dml, t)
defer checkSqlDbClose(db, 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