From 7bf8b05563b339ed2bd16cbecbe0f0be5b02aa3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 Jul 2015 19:37:48 +0200 Subject: [PATCH] Documentation WIP --- GRDB/Database.swift | 97 +++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/GRDB/Database.swift b/GRDB/Database.swift index dc59987be..07c50bad4 100644 --- a/GRDB/Database.swift +++ b/GRDB/Database.swift @@ -47,7 +47,9 @@ public final class Database { /** Returns a select statement that can be reused. - let statement = db.selectStatement("SELECT * FROM persons WHERE id = ?") + let statement = try db.selectStatement("SELECT * FROM persons WHERE age > ?") + let moreThanTwentyCount = statement.fetchOne(Int.self, bindings: [20])! + let moreThanThirtyCount = statement.fetchOne(Int.self, bindings: [30])! This method may throw a SQLiteError. @@ -71,7 +73,9 @@ public final class Database { /** Returns an update statement that can be reused. - let statement = db.updateStatement("INSERT INTO persons (name) VALUES (?)") + let statement = try db.updateStatement("INSERT INTO persons (name) VALUES (?)") + try statement.execute(bindings: ["Arthur"]) + try statement.execute(bindings: ["Barbara"]) This method may throw a SQLiteError. @@ -114,48 +118,6 @@ public final class Database { case Rollback } - /** - Executes a block inside an SQLite transaction. - - If the block throws an error, the transaction is rollbacked and the error is - rethrown. - - - parameter type: The transaction type (default Exclusive) - See https://www.sqlite.org/lang_transaction.html - - parameter block: A block that executes SQL statements and return either - .Commit or .Rollback. - */ - public func inTransaction(type: TransactionType = .Exclusive, block: () throws -> TransactionCompletion) throws { - var completion: TransactionCompletion = .Rollback - var dbError: ErrorType? = nil - - try beginTransaction(type) - - do { - completion = try block() - } catch { - completion = .Rollback - dbError = error - } - - do { - switch completion { - case .Commit: - try commit() - case .Rollback: - try rollback() - } - } catch { - if dbError == nil { - dbError = error - } - } - - if let dbError = dbError { - throw dbError - } - } - // MARK: - Miscellaneous @@ -208,6 +170,53 @@ public final class Database { } } + /** + Executes a block inside an SQLite transaction. + + try dbQueue.inTransaction do { + try db.execute("INSERT ...") + return .Commit + } + + If the block throws an error, the transaction is rollbacked and the error is + rethrown. + + - parameter type: The transaction type + See https://www.sqlite.org/lang_transaction.html + - parameter block: A block that executes SQL statements and return either + .Commit or .Rollback. + */ + func inTransaction(type: TransactionType, block: () throws -> TransactionCompletion) throws { + var completion: TransactionCompletion = .Rollback + var dbError: ErrorType? = nil + + try beginTransaction(type) + + do { + completion = try block() + } catch { + completion = .Rollback + dbError = error + } + + do { + switch completion { + case .Commit: + try commit() + case .Rollback: + try rollback() + } + } catch { + if dbError == nil { + dbError = error + } + } + + if let dbError = dbError { + throw dbError + } + } + private func beginTransaction(type: TransactionType = .Exclusive) throws { switch type { case .Deferred: