mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Rename DatabaseConfiguration to Configuration, add Configuration.readonly, and replace Configuration.verbose flag with customizable Configuration.trace function.
This commit is contained in:
1 parent
640b8dc60e
commit
c831a0a4b3
9 files changed
+42
-34
No files matched your search
@@ -51,7 +51,7 @@ class GRDBTestCase: XCTestCase {
|
||||
|
||||
self.databasePath = "/tmp/GRDB.sqlite"
|
||||
do { try NSFileManager.defaultManager().removeItemAtPath(databasePath) } catch { }
|
||||
let configuration = DatabaseConfiguration(verbose: true)
|
||||
let configuration = Configuration(trace: { NSLog("%@", $0) })
|
||||
self.dbQueue = try! DatabaseQueue(path: databasePath, configuration: configuration)
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
56C9E6641B468EB200DDC541 /* UpdateStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E65A1B468EB200DDC541 /* UpdateStatementTests.swift */; };
|
||||
56C9E6681B4696E100DDC541 /* SelectStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C9E6671B4696E100DDC541 /* SelectStatementTests.swift */; };
|
||||
56CC3C8F1B43A5A700A9A534 /* DatabaseQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */; };
|
||||
56CC3C931B43BF0100A9A534 /* DatabaseConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C921B43BF0100A9A534 /* DatabaseConfiguration.swift */; };
|
||||
56CC3C931B43BF0100A9A534 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C921B43BF0100A9A534 /* Configuration.swift */; };
|
||||
56CC3C951B43C0E600A9A534 /* DatabaseMigrator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */; };
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */; };
|
||||
56CC3C9B1B4464AE00A9A534 /* RowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */; };
|
||||
@@ -71,7 +71,7 @@
|
||||
56C9E65A1B468EB200DDC541 /* UpdateStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpdateStatementTests.swift; sourceTree = "<group>"; };
|
||||
56C9E6671B4696E100DDC541 /* SelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectStatementTests.swift; sourceTree = "<group>"; };
|
||||
56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueue.swift; sourceTree = "<group>"; };
|
||||
56CC3C921B43BF0100A9A534 /* DatabaseConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseConfiguration.swift; sourceTree = "<group>"; };
|
||||
56CC3C921B43BF0100A9A534 /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
|
||||
56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseMigrator.swift; sourceTree = "<group>"; };
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
|
||||
56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = "<group>"; };
|
||||
@@ -212,8 +212,8 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56E4BB801B45373600FD324B /* Bindings.swift */,
|
||||
56CC3C921B43BF0100A9A534 /* Configuration.swift */,
|
||||
565E191D1B42B2B600D32814 /* Database.swift */,
|
||||
56CC3C921B43BF0100A9A534 /* DatabaseConfiguration.swift */,
|
||||
56CC3C941B43C0E600A9A534 /* DatabaseMigrator.swift */,
|
||||
56CC3C8E1B43A5A700A9A534 /* DatabaseQueue.swift */,
|
||||
5699AE581B42CD7800C35172 /* Row.swift */,
|
||||
@@ -357,7 +357,7 @@
|
||||
5699AE5F1B42E59F00C35172 /* SQLiteValueConvertible.swift in Sources */,
|
||||
5699AE571B42C34A00C35172 /* Statement.swift in Sources */,
|
||||
56CC3C8F1B43A5A700A9A534 /* DatabaseQueue.swift in Sources */,
|
||||
56CC3C931B43BF0100A9A534 /* DatabaseConfiguration.swift in Sources */,
|
||||
56CC3C931B43BF0100A9A534 /* Configuration.swift in Sources */,
|
||||
56CC3C951B43C0E600A9A534 /* DatabaseMigrator.swift in Sources */,
|
||||
5699AE5D1B42D49C00C35172 /* UpdateStatement.swift in Sources */,
|
||||
565E191E1B42B2B600D32814 /* Database.swift in Sources */,
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Configuration.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 01/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
public struct Configuration {
|
||||
public typealias TraceFunction = (String) -> Void
|
||||
|
||||
public var foreignKeysEnabled: Bool
|
||||
public var readonly: Bool
|
||||
public var trace: TraceFunction?
|
||||
|
||||
public init(foreignKeysEnabled: Bool = true, readonly: Bool = false, trace: TraceFunction? = nil) {
|
||||
self.foreignKeysEnabled = foreignKeysEnabled
|
||||
self.readonly = readonly
|
||||
self.trace = trace
|
||||
}
|
||||
|
||||
var sqliteOpenFlags: Int32 {
|
||||
// See https://www.sqlite.org/c3ref/open.html
|
||||
return readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE)
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -14,13 +14,14 @@ public class Database {
|
||||
case Exclusive
|
||||
}
|
||||
|
||||
public let configuration: DatabaseConfiguration
|
||||
public let configuration: Configuration
|
||||
let sqliteConnection = SQLiteConnection()
|
||||
|
||||
init(path: String, configuration: DatabaseConfiguration = DatabaseConfiguration()) throws {
|
||||
init(path: String, configuration: Configuration) throws {
|
||||
self.configuration = configuration
|
||||
|
||||
// See https://www.sqlite.org/c3ref/open.html
|
||||
let code = sqlite3_open(path, &sqliteConnection)
|
||||
let code = sqlite3_open_v2(path, &sqliteConnection, configuration.sqliteOpenFlags, nil)
|
||||
try SQLiteError.checkCResultCode(code, sqliteConnection: sqliteConnection)
|
||||
|
||||
if configuration.foreignKeysEnabled {
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
//
|
||||
// DatabaseConfiguration.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 01/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
public struct DatabaseConfiguration {
|
||||
public let foreignKeysEnabled: Bool
|
||||
public let verbose: Bool
|
||||
|
||||
public init(foreignKeysEnabled: Bool = true, verbose: Bool = false) {
|
||||
self.foreignKeysEnabled = foreignKeysEnabled
|
||||
self.verbose = verbose
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public class DatabaseQueue {
|
||||
static var databaseQueueIDKey = unsafeBitCast(DatabaseQueue.self, UnsafePointer<Void>.self)
|
||||
lazy var databaseQueueID: DatabaseQueueID = unsafeBitCast(self, DatabaseQueueID.self)
|
||||
|
||||
public init(path: String = ":memory:", configuration: DatabaseConfiguration = DatabaseConfiguration()) throws {
|
||||
public init(path: String = ":memory:", configuration: Configuration = Configuration()) throws {
|
||||
queue = dispatch_queue_create("GRDB", nil)
|
||||
_database = try Database(path: path, configuration: configuration)
|
||||
dispatch_queue_set_specific(queue, DatabaseQueue.databaseQueueIDKey, databaseQueueID, nil)
|
||||
|
||||
@@ -51,7 +51,7 @@ extension SelectStatement {
|
||||
self.bindings = bindings
|
||||
}
|
||||
return AnySequence { () -> AnyGenerator<Row> in
|
||||
var logSQL = self.database.configuration.verbose
|
||||
var trace = self.database.configuration.trace
|
||||
return anyGenerator { () -> Row? in
|
||||
// Make sure values are not consumed in a different queue.
|
||||
//
|
||||
@@ -68,9 +68,9 @@ extension SelectStatement {
|
||||
fatalError("SelectStatement was not iterated on its database queue. Consider wrapping the results of the fetch in an Array before escaping the database queue.")
|
||||
}
|
||||
|
||||
if logSQL {
|
||||
NSLog("%@", self.sql)
|
||||
logSQL = false
|
||||
if let appliedTrace = trace {
|
||||
appliedTrace(self.sql)
|
||||
trace = nil
|
||||
}
|
||||
let code = sqlite3_step(self.sqliteStatement)
|
||||
switch code {
|
||||
|
||||
@@ -14,8 +14,8 @@ public final class UpdateStatement : Statement {
|
||||
self.bindings = bindings
|
||||
}
|
||||
reset()
|
||||
if database.configuration.verbose {
|
||||
NSLog("%@", sql)
|
||||
if let trace = database.configuration.trace {
|
||||
trace(sql)
|
||||
}
|
||||
let code = sqlite3_step(sqliteStatement)
|
||||
switch code {
|
||||
|
||||
Reference in new issue
Block a user