From 000bd1b200c63a2e0049dcc10083e4d4bdc83d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 23 Apr 2016 09:56:47 +0200 Subject: [PATCH] Database.isInsideTransaction --- CHANGELOG.md | 4 ++++ GRDB/Core/Database.swift | 6 ++++++ TODO.md | 1 - 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a947b6734..d14333a7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ Release Notes ## Next Version +**New** + +- `Database.isInsideTransaction` is true if database is currently inside a transaction. + **Fixed** - FetchRequest.reverse() sorts by reversed RowID when no base ordering has been specified. diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 36f5f1be6..17265714d 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -70,6 +70,9 @@ public final class Database { return Int(sqlite3_total_changes(sqliteConnection)) } + /// True if the database connection is currently in a transaction. + public private(set) var isInsideTransaction: Bool = false + var lastErrorMessage: String? { return String.fromCString(sqlite3_errmsg(sqliteConnection)) } @@ -1034,14 +1037,17 @@ extension Database { case .Exclusive: try execute("BEGIN EXCLUSIVE TRANSACTION") } + isInsideTransaction = true } private func rollback() throws { try execute("ROLLBACK TRANSACTION") + isInsideTransaction = false } private func commit() throws { try execute("COMMIT TRANSACTION") + isInsideTransaction = false } /// Add a transaction observer, so that it gets notified of all diff --git a/TODO.md b/TODO.md index 81269e1c0..1c121f959 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,3 @@ -- [ ] Add Database.isInTransaction - [ ] Support for WITHOUT ROWID tables (check didInsertWithRowID and FetchRequest.reverse()) - [ ] What is the SQLITE_OPEN_WAL open flag? - [ ] Read https://github.com/ccgus/fmdb/issues/262 and understand https://lists.apple.com/archives/cocoa-dev/2012/Aug/msg00527.html