Skip to content

Commit d56eb93

Browse files
committed
Implements Execer/Queryer. Close #60, #82, #113
1 parent a59fbb4 commit d56eb93

File tree

1 file changed

+61
-74
lines changed

1 file changed

+61
-74
lines changed

sqlite3.go

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,14 @@ type SQLiteRows struct {
119119

120120
// Commit transaction.
121121
func (tx *SQLiteTx) Commit() error {
122-
if err := tx.c.exec("COMMIT"); err != nil {
123-
return err
124-
}
125-
return nil
122+
_, err := tx.c.exec("COMMIT")
123+
return err
126124
}
127125

128126
// Rollback transaction.
129127
func (tx *SQLiteTx) Rollback() error {
130-
if err := tx.c.exec("ROLLBACK"); err != nil {
131-
return err
132-
}
133-
return nil
128+
_, err := tx.c.exec("ROLLBACK")
129+
return err
134130
}
135131

136132
// AutoCommit return which currently auto commit or not.
@@ -146,81 +142,72 @@ func (c *SQLiteConn) lastError() Error {
146142
}
147143
}
148144

149-
// TODO: Execer & Queryer currently disabled
150-
// https://github.com/mattn/go-sqlite3/issues/82
151-
//// Implements Execer
152-
//func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
153-
// tx, err := c.Begin()
154-
// if err != nil {
155-
// return nil, err
156-
// }
157-
// for {
158-
// s, err := c.Prepare(query)
159-
// if err != nil {
160-
// tx.Rollback()
161-
// return nil, err
162-
// }
163-
// na := s.NumInput()
164-
// res, err := s.Exec(args[:na])
165-
// if err != nil && err != driver.ErrSkip {
166-
// tx.Rollback()
167-
// s.Close()
168-
// return nil, err
169-
// }
170-
// args = args[na:]
171-
// tail := s.(*SQLiteStmt).t
172-
// if tail == "" {
173-
// tx.Commit()
174-
// return res, nil
175-
// }
176-
// s.Close()
177-
// query = tail
178-
// }
179-
//}
180-
//
181-
//// Implements Queryer
182-
//func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
183-
// tx, err := c.Begin()
184-
// if err != nil {
185-
// return nil, err
186-
// }
187-
// for {
188-
// s, err := c.Prepare(query)
189-
// if err != nil {
190-
// tx.Rollback()
191-
// return nil, err
192-
// }
193-
// na := s.NumInput()
194-
// rows, err := s.Query(args[:na])
195-
// if err != nil && err != driver.ErrSkip {
196-
// tx.Rollback()
197-
// s.Close()
198-
// return nil, err
199-
// }
200-
// args = args[na:]
201-
// tail := s.(*SQLiteStmt).t
202-
// if tail == "" {
203-
// tx.Commit()
204-
// return rows, nil
205-
// }
206-
// s.Close()
207-
// query = tail
208-
// }
209-
//}
210-
211-
func (c *SQLiteConn) exec(cmd string) error {
145+
// Implements Execer
146+
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
147+
if len(args) == 0 {
148+
return c.exec(query)
149+
}
150+
151+
for {
152+
s, err := c.Prepare(query)
153+
if err != nil {
154+
return nil, err
155+
}
156+
na := s.NumInput()
157+
res, err := s.Exec(args[:na])
158+
if err != nil && err != driver.ErrSkip {
159+
s.Close()
160+
return nil, err
161+
}
162+
args = args[na:]
163+
tail := s.(*SQLiteStmt).t
164+
if tail == "" {
165+
return res, nil
166+
}
167+
s.Close()
168+
query = tail
169+
}
170+
}
171+
172+
// Implements Queryer
173+
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
174+
for {
175+
s, err := c.Prepare(query)
176+
if err != nil {
177+
return nil, err
178+
}
179+
na := s.NumInput()
180+
rows, err := s.Query(args[:na])
181+
if err != nil && err != driver.ErrSkip {
182+
s.Close()
183+
return nil, err
184+
}
185+
args = args[na:]
186+
tail := s.(*SQLiteStmt).t
187+
if tail == "" {
188+
return rows, nil
189+
}
190+
s.Close()
191+
query = tail
192+
}
193+
}
194+
195+
func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
212196
pcmd := C.CString(cmd)
213197
defer C.free(unsafe.Pointer(pcmd))
214198
rv := C.sqlite3_exec(c.db, pcmd, nil, nil, nil)
215199
if rv != C.SQLITE_OK {
216-
return c.lastError()
200+
return nil, c.lastError()
217201
}
218-
return nil
202+
return &SQLiteResult{
203+
int64(C._sqlite3_last_insert_rowid(c.db)),
204+
int64(C._sqlite3_changes(c.db)),
205+
}, nil
219206
}
220207

221208
// Begin transaction.
222209
func (c *SQLiteConn) Begin() (driver.Tx, error) {
223-
if err := c.exec("BEGIN"); err != nil {
210+
if _, err := c.exec("BEGIN"); err != nil {
224211
return nil, err
225212
}
226213
return &SQLiteTx{c}, nil

0 commit comments

Comments
 (0)