mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
245 lines
7.5 KiB
Swift
245 lines
7.5 KiB
Swift
/// A nicer name than COpaquePointer for SQLite connection handle
|
|
typealias SQLiteConnection = COpaquePointer
|
|
|
|
/**
|
|
A Database connection.
|
|
|
|
You don't create a database directly. Instead, you use a DatabaseQueue:
|
|
|
|
let dbQueue = DatabaseQueue(...)
|
|
|
|
// The Database is the `db` in the closure:
|
|
dbQueue.inDatabase { db in
|
|
db.execute(...)
|
|
}
|
|
*/
|
|
public final class Database {
|
|
|
|
// MARK: - Select Statements
|
|
|
|
/**
|
|
Returns a select statement that can be reused.
|
|
|
|
let statement = db.selectStatement("SELECT * FROM persons WHERE age > ?")
|
|
let moreThanTwentyCount = Int.fetchOne(statement, arguments: [20])!
|
|
let moreThanThirtyCount = Int.fetchOne(statement, arguments: [30])!
|
|
|
|
- parameter sql: An SQL query.
|
|
- returns: A SelectStatement.
|
|
*/
|
|
public func selectStatement(sql: String) -> SelectStatement {
|
|
return try! SelectStatement(database: self, sql: sql)
|
|
}
|
|
|
|
|
|
// MARK: - Update Statements
|
|
|
|
/**
|
|
Returns an update statement that can be reused.
|
|
|
|
let statement = try db.updateStatement("INSERT INTO persons (name) VALUES (?)")
|
|
try statement.execute(arguments: ["Arthur"])
|
|
try statement.execute(arguments: ["Barbara"])
|
|
|
|
This method may throw a DatabaseError.
|
|
|
|
- parameter sql: An SQL query.
|
|
- returns: An UpdateStatement.
|
|
- throws: A DatabaseError whenever a SQLite error occurs.
|
|
*/
|
|
public func updateStatement(sql: String) throws -> UpdateStatement {
|
|
return try UpdateStatement(database: self, sql: sql)
|
|
}
|
|
|
|
/**
|
|
Executes an update statement.
|
|
|
|
db.excute("INSERT INTO persons (name) VALUES (?)", arguments: ["Arthur"])
|
|
|
|
This method may throw a DatabaseError.
|
|
|
|
- parameter sql: An SQL query.
|
|
- parameter arguments: Optional query arguments.
|
|
- returns: A DatabaseChanges.
|
|
- throws: A DatabaseError whenever a SQLite error occurs.
|
|
*/
|
|
public func execute(sql: String, arguments: StatementArguments? = nil) throws -> DatabaseChanges {
|
|
let statement = try updateStatement(sql)
|
|
return try statement.execute(arguments: arguments)
|
|
}
|
|
|
|
|
|
/**
|
|
Executes multiple SQL statements (separated by a semi-colon).
|
|
|
|
try db.executeMultiStatement(
|
|
"INSERT INTO persons (name) VALUES ('Harry');" +
|
|
"INSERT INTO persons (name) VALUES ('Ron');" +
|
|
"INSERT INTO persons (name) VALUES ('Hermione');")
|
|
|
|
This method may throw a DatabaseError.
|
|
|
|
- parameter sql: SQL containing multiple statements separated by semi-colons.
|
|
- returns: A DatabaseChanges. Note that insertedRowID will always be nil.
|
|
- throws: A DatabaseError whenever a SQLite error occurs.
|
|
*/
|
|
public func executeMultiStatement(sql: String) throws -> DatabaseChanges {
|
|
if let trace = self.configuration.trace {
|
|
trace(sql: sql, arguments: nil)
|
|
}
|
|
|
|
let changedRowsBefore = sqlite3_total_changes(self.sqliteConnection)
|
|
|
|
let code = sqlite3_exec(self.sqliteConnection, sql, nil, nil, nil)
|
|
guard code == SQLITE_OK else {
|
|
throw DatabaseError(code: code, message: self.lastErrorMessage, sql: sql, arguments: nil)
|
|
}
|
|
|
|
let changedRowsAfter = sqlite3_total_changes(self.sqliteConnection)
|
|
return DatabaseChanges(changedRowCount: changedRowsAfter - changedRowsBefore, insertedRowID: nil)
|
|
}
|
|
|
|
|
|
// MARK: - Transactions
|
|
|
|
/// A SQLite transaction type. See https://www.sqlite.org/lang_transaction.html
|
|
public enum TransactionType {
|
|
case Deferred
|
|
case Immediate
|
|
case Exclusive
|
|
}
|
|
|
|
/// The end of a transaction: Commit, or Rollback
|
|
public enum TransactionCompletion {
|
|
case Commit
|
|
case Rollback
|
|
}
|
|
|
|
|
|
// MARK: - Miscellaneous
|
|
|
|
/**
|
|
Returns whether a table exists.
|
|
|
|
- parameter tableName: A table name.
|
|
- returns: true if the table exists.
|
|
*/
|
|
public func tableExists(tableName: String) -> Bool {
|
|
// SQlite identifiers are case-insensitive, case-preserving (http://www.alberton.info/dbms_identifiers_and_case_sensitivity.html)
|
|
if let _ = Row.fetchOne(self, "SELECT \"sql\" FROM sqlite_master WHERE \"type\" = 'table' AND LOWER(name) = ?", arguments: [tableName.lowercaseString]) {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - Non public
|
|
|
|
/// The database configuration
|
|
let configuration: Configuration
|
|
|
|
/// The SQLite connection handle
|
|
let sqliteConnection = SQLiteConnection()
|
|
|
|
/// The last error message
|
|
var lastErrorMessage: String? { return String.fromCString(sqlite3_errmsg(sqliteConnection)) }
|
|
|
|
init(path: String, configuration: Configuration) throws {
|
|
self.configuration = configuration
|
|
|
|
// See https://www.sqlite.org/c3ref/open.html
|
|
let code = sqlite3_open_v2(path, &sqliteConnection, configuration.sqliteOpenFlags, nil)
|
|
if code != SQLITE_OK {
|
|
throw DatabaseError(code: code, message: String.fromCString(sqlite3_errmsg(sqliteConnection)))
|
|
}
|
|
|
|
if configuration.foreignKeysEnabled {
|
|
try execute("PRAGMA foreign_keys = ON")
|
|
}
|
|
}
|
|
|
|
// Initializes an in-memory database
|
|
convenience init(configuration: Configuration) {
|
|
try! self.init(path: ":memory:", configuration: configuration)
|
|
}
|
|
|
|
deinit {
|
|
if sqliteConnection != nil {
|
|
sqlite3_close(sqliteConnection)
|
|
}
|
|
}
|
|
|
|
/**
|
|
Executes a block inside a database 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.
|
|
- throws: The error thrown by the block.
|
|
*/
|
|
func inTransaction(type: TransactionType, block: () throws -> TransactionCompletion) rethrows {
|
|
var completion: TransactionCompletion = .Rollback
|
|
var dbError: ErrorType? = nil
|
|
|
|
try! beginTransaction(type)
|
|
|
|
do {
|
|
completion = try block()
|
|
} catch {
|
|
completion = .Rollback
|
|
dbError = error
|
|
}
|
|
|
|
switch completion {
|
|
case .Commit:
|
|
try! commit()
|
|
case .Rollback:
|
|
try! rollback()
|
|
}
|
|
|
|
if let dbError = dbError {
|
|
try { () -> Void in throw dbError }()
|
|
}
|
|
}
|
|
|
|
private func beginTransaction(type: TransactionType = .Exclusive) throws {
|
|
switch type {
|
|
case .Deferred:
|
|
try execute("BEGIN DEFERRED TRANSACTION")
|
|
case .Immediate:
|
|
try execute("BEGIN IMMEDIATE TRANSACTION")
|
|
case .Exclusive:
|
|
try execute("BEGIN EXCLUSIVE TRANSACTION")
|
|
}
|
|
}
|
|
|
|
private func rollback() throws {
|
|
try execute("ROLLBACK TRANSACTION")
|
|
}
|
|
|
|
private func commit() throws {
|
|
try execute("COMMIT TRANSACTION")
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - Error Management
|
|
|
|
@noreturn func fatalDatabaseError(error: DatabaseError) {
|
|
func throwDataBasase(error: DatabaseError) throws {
|
|
throw error
|
|
}
|
|
try! throwDataBasase(error)
|
|
fatalError("Should not happen")
|
|
}
|