@@ -29,15 +29,6 @@ import CSQLite
29
29
private let vfsName = " unix-excl "
30
30
31
31
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
-
41
32
// Connection pool for accessing an SQLite database
42
33
// with multiple readers & a single writer. Utilizes
43
34
// WAL mode.
@@ -49,12 +40,29 @@ public final class ConnectionPool {
49
40
private let lockQueue : dispatch_queue_t
50
41
private var writeConnection : DirectConnection !
51
42
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] ( )
53
61
54
62
public init ( _ location: DirectConnection . Location ) throws {
55
63
self . location = location
56
64
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; " ) }
58
66
}
59
67
60
68
public var totalReadableConnectionCount : Int {
@@ -123,9 +131,14 @@ public final class ConnectionPool {
123
131
self . writeConnection = try ! DirectConnection ( self . location, flags: flags, dispatcher: ReentrantDispatcher ( " SQLite.ConnectionPool.Write " ) , vfsName: vfsName)
124
132
self . writeConnection. busyTimeout = 2
125
133
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)
128
136
}
137
+
138
+ for setupProcessor in self . setup {
139
+ try ! setupProcessor ( self . writeConnection)
140
+ }
141
+
129
142
}
130
143
131
144
return writeConnection
@@ -140,23 +153,32 @@ public final class ConnectionPool {
140
153
141
154
dispatch_sync ( lockQueue) {
142
155
156
+ // Ensure database is open
157
+ self . writable
158
+
143
159
let connection : DirectConnection
144
160
145
161
if let availableConnection = self . availableReadConnections. popLast ( ) {
146
162
connection = availableConnection
147
163
}
148
- else if self . delegate ? . poolShouldAddConnection ( self ) ?? true {
164
+ else {
149
165
150
166
let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX
151
167
152
168
connection = try ! DirectConnection ( self . location, flags: flags, dispatcher: ImmediateDispatcher ( ) , vfsName: vfsName)
153
169
connection. busyTimeout = 2
154
170
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
+ }
156
181
157
- }
158
- else {
159
- return
160
182
}
161
183
162
184
self . unavailableReadConnections. append ( connection)
0 commit comments