Skip to content

Commit 9548d17

Browse files
committed
Replace pool delegate with setup closures
1 parent 2a5908b commit 9548d17

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

SQLite/Core/ConnectionPool.swift

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ import CSQLite
2929
private let vfsName = "unix-excl"
3030

3131

32-
/// Connection pool delegate
33-
public protocol ConnectionPoolDelegate {
34-
35-
func poolShouldAddConnection(pool: ConnectionPool) -> Bool
36-
func pool(pool: ConnectionPool, didAddConnection: Connection)
37-
38-
}
39-
40-
4132
// Connection pool for accessing an SQLite database
4233
// with multiple readers & a single writer. Utilizes
4334
// WAL mode.
@@ -49,12 +40,29 @@ public final class ConnectionPool {
4940
private let lockQueue : dispatch_queue_t
5041
private var writeConnection : DirectConnection!
5142

52-
public var delegate : ConnectionPoolDelegate?
43+
public var foreignKeys : Bool {
44+
get {
45+
return internalSetup[.ForeignKeys] != nil
46+
}
47+
set {
48+
internalSetup[.ForeignKeys] = newValue ? { try $0.execute("PRAGMA foreign_keys = ON;") } : nil
49+
}
50+
}
51+
52+
public typealias ConnectionProcessor = Connection throws -> Void
53+
public var setup = [ConnectionProcessor]()
54+
55+
private enum InternalOption {
56+
case WriteAheadLogging
57+
case ForeignKeys
58+
}
59+
60+
private var internalSetup = [InternalOption: ConnectionProcessor]()
5361

5462
public init(_ location: DirectConnection.Location) throws {
5563
self.location = location
5664
self.lockQueue = dispatch_queue_create("SQLite.ConnectionPool.Lock", DISPATCH_QUEUE_SERIAL)
57-
try writable.execute("PRAGMA journal_mode = WAL;")
65+
self.internalSetup[.WriteAheadLogging] = { try $0.execute("PRAGMA journal_mode = WAL;") }
5866
}
5967

6068
public var totalReadableConnectionCount : Int {
@@ -123,9 +131,14 @@ public final class ConnectionPool {
123131
self.writeConnection = try! DirectConnection(self.location, flags: flags, dispatcher: ReentrantDispatcher("SQLite.ConnectionPool.Write"), vfsName: vfsName)
124132
self.writeConnection.busyTimeout = 2
125133

126-
if let delegate = self.delegate {
127-
delegate.pool(self, didAddConnection: self.writeConnection)
134+
for setupProcessor in self.internalSetup.values {
135+
try! setupProcessor(self.writeConnection)
128136
}
137+
138+
for setupProcessor in self.setup {
139+
try! setupProcessor(self.writeConnection)
140+
}
141+
129142
}
130143

131144
return writeConnection
@@ -140,23 +153,32 @@ public final class ConnectionPool {
140153

141154
dispatch_sync(lockQueue) {
142155

156+
// Ensure database is open
157+
self.writable
158+
143159
let connection : DirectConnection
144160

145161
if let availableConnection = self.availableReadConnections.popLast() {
146162
connection = availableConnection
147163
}
148-
else if self.delegate?.poolShouldAddConnection(self) ?? true {
164+
else {
149165

150166
let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
151167

152168
connection = try! DirectConnection(self.location, flags: flags, dispatcher: ImmediateDispatcher(), vfsName: vfsName)
153169
connection.busyTimeout = 2
154170

155-
self.delegate?.pool(self, didAddConnection: connection)
171+
for (type, setupProcessor) in self.internalSetup {
172+
if type == .WriteAheadLogging {
173+
continue
174+
}
175+
try! setupProcessor(connection)
176+
}
177+
178+
for setupProcessor in self.setup {
179+
try! setupProcessor(connection)
180+
}
156181

157-
}
158-
else {
159-
return
160182
}
161183

162184
self.unavailableReadConnections.append(connection)

SQLiteTests/ConnectionPoolTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ class ConnectionPoolTests : SQLiteTestCase {
66
override func setUp() {
77
super.setUp()
88
}
9+
10+
func testConnectionSetupClosures() {
11+
12+
let _ = try? NSFileManager.defaultManager().removeItemAtPath("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite")
13+
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite"))
14+
15+
pool.foreignKeys = true
16+
pool.setup.append { try $0.execute("CREATE TABLE IF NOT EXISTS test(value INT)") }
17+
18+
XCTAssertTrue(try pool.readable.scalar("PRAGMA foreign_keys") as! Int64 == 1)
19+
try! pool.writable.execute("INSERT INTO test(value) VALUES (1)")
20+
try! pool.readable.execute("SELECT value FROM test")
21+
}
922

1023
func testConcurrentAccess2() {
1124

0 commit comments

Comments
 (0)