Permalink
Browse files

The Transaction types have been removed and moved to the goDB library:

  • Loading branch information...
1 parent b1de7c4 commit 1ef4160b1e081b71dbf45b2b61d620b4104bc456 @feyeleanor committed Feb 5, 2011
Showing with 49 additions and 65 deletions.
  1. +0 −1 Makefile
  2. +49 −0 database.go
  3. +0 −47 transaction.go
  4. +0 −17 transaction_test.go
View
@@ -10,7 +10,6 @@ CGOFILES=\
result_column.go\
table.go\
backup.go\
- transaction.go\
progress.go
ifeq ($(GOOS),darwin)
View
@@ -4,6 +4,7 @@ package sqlite3
import "C"
import "fmt"
import "os"
+import "strconv"
import "syscall"
import "time"
@@ -47,6 +48,7 @@ const(
ROW = Errno(100)
DONE = Errno(101)
ENCODER = Errno(1000)
+ SAVEPOINT = Errno(1001)
)
var errText = map[Errno]string {
@@ -79,12 +81,14 @@ var errText = map[Errno]string {
ROW: "sqlite3_step() has another row ready",
DONE: "sqlite3_step() has finished executing",
ENCODER: "blob encoding failed",
+ SAVEPOINT: "invalid or unknown savepoint identifier",
}
type Database struct {
handle *C.sqlite3
Filename string
Flags C.int
+ Savepoints []interface{}
}
func TransientDatabase() (db *Database) {
@@ -186,6 +190,51 @@ func (db *Database) Commit() (e os.Error) {
return
}
+func savepointID(id interface{}) (s string) {
+ switch id := id.(type) {
+ case string: s = id
+ case []byte: s = string(id)
+ case fmt.Stringer: s = id.String()
+ case int: s = strconv.Itoa(id)
+ case uint: s = strconv.Uitoa(id)
+ default: panic(SAVEPOINT)
+ }
+ return
+}
+
+func (db *Database) Mark(id interface{}) (e os.Error) {
+ if st, err := db.Prepare("SAVEPOINT ?", savepointID(id)); err == nil {
+ _, e = st.All()
+ } else {
+ e = err
+ }
+ return
+}
+
+func (db *Database) MergeSteps(id interface{}) (e os.Error) {
+ if st, err := db.Prepare("RELEASE SAVEPOINT ?", savepointID(id)); err == nil {
+ _, e = st.All()
+ } else {
+ e = err
+ }
+ return
+}
+
+func (db *Database) Release(id interface{}) (e os.Error) {
+ if st, err := db.Prepare("ROLLBACK TRANSACTION TO SAVEPOINT ?", savepointID(id)); err == nil {
+ _, e = st.All()
+ } else {
+ e = err
+ }
+ return
+}
+
+func (db *Database) SavePoints() (s []interface{}) {
+ s = make([]interface{}, len(db.Savepoints))
+ copy(s, db.Savepoints)
+ return
+}
+
func (db *Database) Load(source *Database, dbname string) (e os.Error) {
if dbname == "" {
dbname = "main"
View
@@ -1,47 +0,0 @@
-package sqlite3
-
-// #include <sqlite3.h>
-import "C"
-import "os"
-
-type TransactionalDatabase interface {
- Begin() os.Error
- Rollback() os.Error
- Commit() os.Error
-}
-
-type Transaction []func(db TransactionalDatabase)
-
-func (t Transaction) Execute(db TransactionalDatabase) (e os.Error) {
- defer func() {
- switch r := recover().(type) {
- case nil:
- e = db.Commit()
- case Errno:
- if r == OK {
- e = db.Commit()
- } else {
- if db.Rollback() != nil {
- panic(e)
- } else {
- e = r
- }
- }
- case os.Error:
- if db.Rollback() != nil {
- panic(e)
- } else {
- e = r
- }
- default:
- panic(r)
- }
- }()
-
- if e = db.Begin(); e == nil {
- for _, f := range t {
- f(db)
- }
- }
- return
-}
View
@@ -1,17 +0,0 @@
-package sqlite3
-
-import "testing"
-
-func TestSimpleTransaction(t *testing.T) {
- TransientSession(func(db *Database) {
- db.createTestTables(t, FOO, BAR)
- doNothing := func(d TransactionalDatabase) { }
- raiseOK := func(d TransactionalDatabase) { panic(OK) }
- raiseErrno := func(d TransactionalDatabase) { panic(MISUSE) }
-
- fatalOnError(t, Transaction{}.Execute(db), "empty transaction")
- fatalOnError(t, Transaction{ doNothing }.Execute(db), "inconsequential transaction")
- fatalOnError(t, Transaction{ raiseOK }.Execute(db), "transaction raises OK")
- fatalOnSuccess(t, Transaction{ raiseErrno }.Execute(db), "transaction raises Errno")
- })
-}

0 comments on commit 1ef4160

Please sign in to comment.