Skip to content

Commit 46709ea

Browse files
authored
add spec for transactions and nested transactions (#12)
1 parent c39220e commit 46709ea

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Diff for: spec/driver_spec.cr

+53
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,57 @@ describe Driver do
242242
File.delete(DB_FILENAME)
243243
end
244244
end
245+
246+
describe "transactions" do
247+
it "can read inside transaction and rollback after" do
248+
with_db do |db|
249+
db.exec "create table person (name varchar(25))"
250+
db.transaction do |tx|
251+
tx.connection.scalar("select count(*) from person").should eq(0)
252+
tx.connection.exec "insert into person (name) values (?)", "John Doe"
253+
tx.connection.scalar("select count(*) from person").should eq(1)
254+
tx.rollback
255+
end
256+
db.scalar("select count(*) from person").should eq(0)
257+
end
258+
end
259+
260+
it "can read inside transaction or after commit" do
261+
with_db do |db|
262+
db.exec "create table person (name varchar(25))"
263+
db.transaction do |tx|
264+
tx.connection.scalar("select count(*) from person").should eq(0)
265+
tx.connection.exec "insert into person (name) values (?)", "John Doe"
266+
tx.connection.scalar("select count(*) from person").should eq(1)
267+
# using other connection
268+
db.scalar("select count(*) from person").should eq(0)
269+
end
270+
db.scalar("select count(*) from person").should eq(1)
271+
end
272+
end
273+
end
274+
275+
describe "nested transactions" do
276+
it "can read inside transaction and rollback after" do
277+
with_db do |db|
278+
db.exec "create table person (name varchar(25))"
279+
db.transaction do |tx_0|
280+
tx_0.connection.scalar("select count(*) from person").should eq(0)
281+
tx_0.connection.exec "insert into person (name) values (?)", "John Doe"
282+
tx_0.transaction do |tx_1|
283+
tx_1.connection.exec "insert into person (name) values (?)", "Sarah"
284+
tx_1.connection.scalar("select count(*) from person").should eq(2)
285+
tx_1.transaction do |tx_2|
286+
tx_2.connection.exec "insert into person (name) values (?)", "Jimmy"
287+
tx_2.connection.scalar("select count(*) from person").should eq(3)
288+
tx_2.rollback
289+
end
290+
end
291+
tx_0.connection.scalar("select count(*) from person").should eq(2)
292+
tx_0.rollback
293+
end
294+
db.scalar("select count(*) from person").should eq(0)
295+
end
296+
end
297+
end
245298
end

Diff for: src/sqlite3/connection.cr

+30
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ class SQLite3::Connection < DB::Connection
3333
LibSQLite3.close_v2(self)
3434
end
3535

36+
# :nodoc:
37+
def perform_begin_transaction
38+
self.prepared.exec "BEGIN"
39+
end
40+
41+
# :nodoc:
42+
def perform_commit_transaction
43+
self.prepared.exec "COMMIT"
44+
end
45+
46+
# :nodoc:
47+
def perform_rollback_transaction
48+
self.prepared.exec "ROLLBACK"
49+
end
50+
51+
# :nodoc:
52+
def perform_create_savepoint(name)
53+
self.prepared.exec "SAVEPOINT #{name}"
54+
end
55+
56+
# :nodoc:
57+
def perform_release_savepoint(name)
58+
self.prepared.exec "RELEASE SAVEPOINT #{name}"
59+
end
60+
61+
# :nodoc:
62+
def perform_rollback_savepoint(name)
63+
self.prepared.exec "ROLLBACK TO #{name}"
64+
end
65+
3666
# Dump the database to another SQLite3 database. This can be used for backing up a SQLite3 Database
3767
# to disk or the opposite
3868
def dump(to : SQLite3::Connection)

0 commit comments

Comments
 (0)