aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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),