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