From da8e370c2f9e8fa11c3c52cff9de44a0d201bf98 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 9 Mar 2016 10:05:17 -0500 Subject: use savepoints instead of transactions --- src/guff/database.cr | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src') 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), -- cgit v1.2.3