Execer/Queryer should use transaction

This commit is contained in:
mattn 2013-09-12 09:11:01 +09:00
parent d8f315ab83
commit 7dadd98d75
1 changed files with 14 additions and 0 deletions

View File

@ -136,20 +136,27 @@ func (c *SQLiteConn) AutoCommit() bool {
// Implements Execer
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
tx, err := c.Begin()
if err != nil {
return nil, err
}
for {
s, err := c.Prepare(query)
if err != nil {
tx.Rollback()
return nil, err
}
na := s.NumInput()
res, err := s.Exec(args[:na])
if err != nil && err != driver.ErrSkip {
tx.Rollback()
s.Close()
return nil, err
}
args = args[na:]
tail := s.(*SQLiteStmt).t
if tail == "" {
tx.Commit()
return res, nil
}
s.Close()
@ -159,20 +166,27 @@ func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, err
// Implements Queryer
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
tx, err := c.Begin()
if err != nil {
return nil, err
}
for {
s, err := c.Prepare(query)
if err != nil {
tx.Rollback()
return nil, err
}
na := s.NumInput()
rows, err := s.Query(args[:na])
if err != nil && err != driver.ErrSkip {
tx.Rollback()
s.Close()
return nil, err
}
args = args[na:]
tail := s.(*SQLiteStmt).t
if tail == "" {
tx.Commit()
return rows, nil
}
s.Close()