aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-03-09 10:05:17 -0500
committerPaul Duncan <pabs@pablotron.org>2016-03-09 10:05:17 -0500
commitda8e370c2f9e8fa11c3c52cff9de44a0d201bf98 (patch)
treea413e6a4c583e72a30def3b018a8dbd8eb74393d
parent102fb8e76df142bac2a74a62e3370f8bf168da90 (diff)
downloadold-guff-da8e370c2f9e8fa11c3c52cff9de44a0d201bf98.tar.bz2
old-guff-da8e370c2f9e8fa11c3c52cff9de44a0d201bf98.zip
use savepoints instead of transactions
-rw-r--r--src/guff/database.cr20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/guff/database.cr b/src/guff/database.cr
index 9201a38..220c7ef 100644
--- a/src/guff/database.cr
+++ b/src/guff/database.cr
@@ -19,11 +19,13 @@ module Guff
def initialize(path)
super(path)
+ @savepoint_id = 0_i64
query(SQL[:pragma_foreign_keys])
end
def initialize(path, &block)
super(path)
+ @savepoint_id = 0_i64
query(SQL[:pragma_foreign_keys])
begin
@@ -123,16 +125,28 @@ module Guff
end
def transaction(&block)
+ # get next savepoint id
+ id = next_savepoint_id
+
begin
- query("BEGIN")
+ query("SAVEPOINT %s" % [id])
block.call
- query("COMMIT")
rescue e
- query("ROLLBACK")
+ query("ROLLBACK TO %s" % [id])
raise e
+ ensure
+ query("RELEASE %s" % [id])
end
end
+ private def next_savepoint_id : String
+ # increment savepoint counter
+ @savepoint_id += 1
+
+ # return savepoint id
+ "guff_savepoint_%s" % [@savepoint_id]
+ end
+
private def run(
sql : String,
args : Hash(String, String),