Implements Execer/Queryer. Close #60, #82, #113

This commit is contained in:
mattn 2014-06-25 11:41:58 +09:00
parent a59fbb40eb
commit d56eb93ecb
1 changed files with 60 additions and 73 deletions

View File

@ -119,18 +119,14 @@ type SQLiteRows struct {
// Commit transaction. // Commit transaction.
func (tx *SQLiteTx) Commit() error { func (tx *SQLiteTx) Commit() error {
if err := tx.c.exec("COMMIT"); err != nil { _, err := tx.c.exec("COMMIT")
return err return err
}
return nil
} }
// Rollback transaction. // Rollback transaction.
func (tx *SQLiteTx) Rollback() error { func (tx *SQLiteTx) Rollback() error {
if err := tx.c.exec("ROLLBACK"); err != nil { _, err := tx.c.exec("ROLLBACK")
return err return err
}
return nil
} }
// AutoCommit return which currently auto commit or not. // AutoCommit return which currently auto commit or not.
@ -146,81 +142,72 @@ func (c *SQLiteConn) lastError() Error {
} }
} }
// TODO: Execer & Queryer currently disabled // Implements Execer
// https://github.com/mattn/go-sqlite3/issues/82 func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
//// Implements Execer if len(args) == 0 {
//func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) { return c.exec(query)
// 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()
// query = tail
// }
//}
//
//// 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()
// query = tail
// }
//}
func (c *SQLiteConn) exec(cmd string) error { for {
s, err := c.Prepare(query)
if err != nil {
return nil, err
}
na := s.NumInput()
res, err := s.Exec(args[:na])
if err != nil && err != driver.ErrSkip {
s.Close()
return nil, err
}
args = args[na:]
tail := s.(*SQLiteStmt).t
if tail == "" {
return res, nil
}
s.Close()
query = tail
}
}
// Implements Queryer
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
for {
s, err := c.Prepare(query)
if err != nil {
return nil, err
}
na := s.NumInput()
rows, err := s.Query(args[:na])
if err != nil && err != driver.ErrSkip {
s.Close()
return nil, err
}
args = args[na:]
tail := s.(*SQLiteStmt).t
if tail == "" {
return rows, nil
}
s.Close()
query = tail
}
}
func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
pcmd := C.CString(cmd) pcmd := C.CString(cmd)
defer C.free(unsafe.Pointer(pcmd)) defer C.free(unsafe.Pointer(pcmd))
rv := C.sqlite3_exec(c.db, pcmd, nil, nil, nil) rv := C.sqlite3_exec(c.db, pcmd, nil, nil, nil)
if rv != C.SQLITE_OK { if rv != C.SQLITE_OK {
return c.lastError() return nil, c.lastError()
} }
return nil return &SQLiteResult{
int64(C._sqlite3_last_insert_rowid(c.db)),
int64(C._sqlite3_changes(c.db)),
}, nil
} }
// Begin transaction. // Begin transaction.
func (c *SQLiteConn) Begin() (driver.Tx, error) { func (c *SQLiteConn) Begin() (driver.Tx, error) {
if err := c.exec("BEGIN"); err != nil { if _, err := c.exec("BEGIN"); err != nil {
return nil, err return nil, err
} }
return &SQLiteTx{c}, nil return &SQLiteTx{c}, nil