Skip to content

Commit 68f46d5

Browse files
committed
Better names for protocols and classes
ConnectionType is now Connection and Connection is now DBConnection
1 parent 42ebd18 commit 68f46d5

File tree

9 files changed

+71
-64
lines changed

9 files changed

+71
-64
lines changed

SQLite/Core/Connection.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public enum TransactionMode : String {
4242

4343

4444
/// Protocol to an SQLite connection
45-
public protocol ConnectionType {
45+
public protocol Connection {
4646

4747
/// Whether or not the database was opened in a read-only state.
4848
var readonly : Bool { get }
@@ -201,8 +201,8 @@ public protocol ConnectionType {
201201
/// must throw to roll the transaction back.
202202
///
203203
/// - Throws: `Result.Error`, and rethrows.
204-
func transaction(mode: TransactionMode, block: () throws -> Void) throws
205-
204+
func transaction(mode: TransactionMode, block: (Connection) throws -> Void) throws
205+
206206
// TODO: Consider not requiring a throw to roll back?
207207
// TODO: Consider removing ability to set a name?
208208
/// Runs a transaction with the given savepoint name (if omitted, it will
@@ -219,13 +219,15 @@ public protocol ConnectionType {
219219
/// The block must throw to roll the savepoint back.
220220
///
221221
/// - Throws: `SQLite.Result.Error`, and rethrows.
222-
func savepoint(name: String, block: () throws -> Void) throws
222+
func savepoint(name: String, block: (Connection) throws -> Void) throws
223223

224+
func sync<T>(block: () throws -> T) rethrows -> T
225+
224226
}
225227

226228

227229
/// A connection to SQLite.
228-
public final class Connection : ConnectionType, Equatable {
230+
public final class DBConnection : Connection, Equatable {
229231

230232
/// The location of a SQLite database.
231233
public enum Location {
@@ -498,10 +500,10 @@ public final class Connection : ConnectionType, Equatable {
498500
/// must throw to roll the transaction back.
499501
///
500502
/// - Throws: `Result.Error`, and rethrows.
501-
public func transaction(mode: TransactionMode = .Deferred, block: () throws -> Void) throws {
503+
public func transaction(mode: TransactionMode = .Deferred, block: (Connection) throws -> Void) throws {
502504
try transaction("BEGIN \(mode.rawValue) TRANSACTION", block, "COMMIT TRANSACTION", or: "ROLLBACK TRANSACTION")
503505
}
504-
506+
505507
// TODO: Consider not requiring a throw to roll back?
506508
// TODO: Consider removing ability to set a name?
507509
/// Runs a transaction with the given savepoint name (if omitted, it will
@@ -518,26 +520,26 @@ public final class Connection : ConnectionType, Equatable {
518520
/// The block must throw to roll the savepoint back.
519521
///
520522
/// - Throws: `SQLite.Result.Error`, and rethrows.
521-
public func savepoint(name: String = NSUUID().UUIDString, block: () throws -> Void) throws {
523+
public func savepoint(name: String = NSUUID().UUIDString, block: (Connection) throws -> Void) throws {
522524
let name = name.quote("'")
523525
let savepoint = "SAVEPOINT \(name)"
524-
526+
525527
try transaction(savepoint, block, "RELEASE \(savepoint)", or: "ROLLBACK TO \(savepoint)")
526528
}
527-
528-
private func transaction(begin: String, _ block: () throws -> Void, _ commit: String, or rollback: String) throws {
529+
530+
private func transaction(begin: String, _ block: (Connection) throws -> Void, _ commit: String, or rollback: String) throws {
529531
return try sync {
530532
try self.run(begin)
531533
do {
532-
try block()
534+
try block(self)
533535
} catch {
534536
try self.run(rollback)
535537
throw error
536538
}
537539
try self.run(commit)
538540
}
539541
}
540-
542+
541543
/// Interrupts any long-running queries.
542544
public func interrupt() {
543545
sqlite3_interrupt(handle)
@@ -767,7 +769,7 @@ public final class Connection : ConnectionType, Equatable {
767769

768770
// MARK: - Error Handling
769771

770-
func sync<T>(block: () throws -> T) rethrows -> T {
772+
public func sync<T>(block: () throws -> T) rethrows -> T {
771773
var success: T?
772774
var failure: ErrorType?
773775

@@ -800,15 +802,15 @@ public final class Connection : ConnectionType, Equatable {
800802

801803
}
802804

803-
extension Connection : CustomStringConvertible {
805+
extension DBConnection : CustomStringConvertible {
804806

805807
public var description: String {
806808
return String.fromCString(sqlite3_db_filename(handle, nil))!
807809
}
808810

809811
}
810812

811-
extension Connection.Location : CustomStringConvertible {
813+
extension DBConnection.Location : CustomStringConvertible {
812814

813815
public var description: String {
814816
switch self {
@@ -823,7 +825,7 @@ extension Connection.Location : CustomStringConvertible {
823825

824826
}
825827

826-
public func ==(lhs: Connection, rhs: Connection) -> Bool {
828+
public func ==(lhs: DBConnection, rhs: DBConnection) -> Bool {
827829
return lhs === rhs
828830
}
829831

@@ -860,7 +862,7 @@ public enum Result : ErrorType {
860862

861863
case Error(message: String, code: Int32, statement: Statement?)
862864

863-
init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {
865+
init?(errorCode: Int32, connection: DBConnection, statement: Statement? = nil) {
864866
guard !Result.successCodes.contains(errorCode) else { return nil }
865867

866868
let message = String.fromCString(sqlite3_errmsg(connection.handle))!

SQLite/Core/ConnectionPool.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ public protocol ConnectionPoolDelegate {
3939
// WAL mode.
4040
public final class ConnectionPool {
4141

42-
private let location : Connection.Location
43-
private var availableReadConnections = [Connection]()
44-
private var unavailableReadConnections = [Connection]()
42+
private let location : DBConnection.Location
43+
private var availableReadConnections = [DBConnection]()
44+
private var unavailableReadConnections = [DBConnection]()
4545
private let lockQueue : dispatch_queue_t
46-
private var writeConnection : Connection!
46+
private var writeConnection : DBConnection!
47+
private let writeQueue : dispatch_queue_t
4748

4849
public var delegate : ConnectionPoolDelegate?
4950

50-
public init(_ location: Connection.Location) throws {
51+
public init(_ location: DBConnection.Location) throws {
5152
self.location = location
52-
self.lockQueue = dispatch_queue_create("SQLite.ConnectionPool", DISPATCH_QUEUE_SERIAL)
53-
53+
self.lockQueue = dispatch_queue_create("SQLite.ConnectionPool.Lock", DISPATCH_QUEUE_SERIAL)
54+
self.writeQueue = dispatch_queue_create("SQLite.ConnectionPool.Write", DISPATCH_QUEUE_SERIAL)
5455
try writable.execute("PRAGMA locking_mode = EXCLUSIVE; PRAGMA journal_mode = WAL;")
5556
}
5657

@@ -64,12 +65,12 @@ public final class ConnectionPool {
6465

6566
// Connection that automatically returns itself
6667
// to the pool when it goes out of scope
67-
private class BorrowedConnection : ConnectionType, Equatable {
68+
private class BorrowedConnection : Connection, Equatable {
6869

6970
let pool : ConnectionPool
70-
let connection : Connection
71+
let connection : DBConnection
7172

72-
init(pool: ConnectionPool, connection: Connection) {
73+
init(pool: ConnectionPool, connection: DBConnection) {
7374
self.pool = pool
7475
self.connection = connection
7576
}
@@ -101,43 +102,47 @@ public final class ConnectionPool {
101102
@warn_unused_result func scalar(statement: String, _ bindings: [Binding?]) -> Binding? { return connection.scalar(statement, bindings) }
102103
@warn_unused_result func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding? { return connection.scalar(statement, bindings) }
103104

104-
func transaction(mode: TransactionMode, block: () throws -> Void) throws { return try connection.transaction(mode, block: block) }
105-
func savepoint(name: String, block: () throws -> Void) throws { return try connection.savepoint(name, block: block) }
105+
func transaction(mode: TransactionMode, block: (Connection) throws -> Void) throws { return try connection.transaction(mode, block: block) }
106+
func savepoint(name: String, block: (Connection) throws -> Void) throws { return try connection.savepoint(name, block: block) }
107+
108+
func sync<T>(block: () throws -> T) rethrows -> T { return try connection.sync(block) }
109+
func check(resultCode: Int32, statement: Statement? = nil) throws -> Int32 { return try connection.check(resultCode, statement: statement) }
106110

107111
}
108112

109113

110114
// Acquires a read/write connection to the database
111-
public var writable : ConnectionType {
115+
public var writable : DBConnection {
116+
112117

113118
var writeConnectionInit = dispatch_once_t()
114119
dispatch_once(&writeConnectionInit) {
115120
let flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_WAL
116-
self.writeConnection = try! Connection(self.location, flags: flags, dispatcher: ReentrantDispatcher("SQLite.WriteConnection"))
121+
self.writeConnection = try! DBConnection(self.location, flags: flags, dispatcher: ReentrantDispatcher("SQLite.WriteConnection"))
117122
self.writeConnection.busyTimeout = 2
118123
}
119124

120125
return writeConnection
121126
}
122127

123128
// Acquires a read only connection to the database
124-
public var readable : ConnectionType {
129+
public var readable : Connection {
125130

126131
var borrowed : BorrowedConnection!
127132

128133
repeat {
129134

130135
dispatch_sync(lockQueue) {
131136

132-
let connection : Connection
137+
let connection : DBConnection
133138

134139
if let availableConnection = self.availableReadConnections.popLast() {
135140
connection = availableConnection
136141
}
137142
else if self.delegate?.poolShouldAddConnection(self) ?? true {
138143

139144
let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL
140-
connection = try! Connection(self.location, flags: flags, dispatcher: ImmediateDispatcher())
145+
connection = try! DBConnection(self.location, flags: flags, dispatcher: ImmediateDispatcher())
141146
connection.busyTimeout = 2
142147

143148
self.delegate?.pool(self, didAddConnection: connection)

SQLite/Core/Statement.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public final class Statement {
2929

3030
private var handle: COpaquePointer = nil
3131

32-
private let connection: Connection
32+
private let connection: DBConnection
3333

34-
init(_ connection: Connection, _ SQL: String) throws {
34+
init(_ connection: DBConnection, _ SQL: String) throws {
3535
self.connection = connection
3636
try connection.check(sqlite3_prepare_v2(connection.handle, SQL, -1, &handle, nil))
3737
}

SQLite/Extensions/FTS4.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ extension Tokenizer : CustomStringConvertible {
140140

141141
}
142142

143-
extension Connection {
143+
extension DBConnection {
144144

145145
public func registerTokenizer(submoduleName: String, next: String -> (String, Range<String.Index>)?) throws {
146146
try check(_SQLiteRegisterTokenizer(handle, Tokenizer.moduleName, submoduleName) { input, offset, length in

SQLite/Typed/CustomFunctions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// THE SOFTWARE.
2323
//
2424

25-
public extension Connection {
25+
public extension DBConnection {
2626

2727
/// Creates or redefines a custom SQL function.
2828
///

SQLite/Typed/Query.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ public struct Row {
10511051
}
10521052

10531053
// FIXME: rdar://problem/18673897 // subscript<T>…
1054-
1054+
10551055
public subscript(column: Expression<Blob>) -> Blob {
10561056
return get(column)
10571057
}

SQLiteTests/ConnectionPoolTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ConnectionPoolTests : SQLiteTestCase {
1010
func testConcurrentAccess() {
1111

1212
let _ = try? NSFileManager.defaultManager().removeItemAtPath("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite")
13-
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLiteswiftTests.sqlite"))
13+
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite"))
1414

1515
let conn = pool.writable
1616
try! conn.execute("CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT)")
@@ -53,7 +53,7 @@ class ConnectionPoolTests : SQLiteTestCase {
5353

5454
}
5555

56-
for x in 10..<500 {
56+
for x in 10..<50000 {
5757

5858
let name = "test" + String(x)
5959
let idx = Int(rand()) % 5
@@ -75,13 +75,13 @@ class ConnectionPoolTests : SQLiteTestCase {
7575
func testAutoRelease() {
7676

7777
let _ = try? NSFileManager.defaultManager().removeItemAtPath("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite")
78-
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLiteswiftTests.sqlite"))
78+
let pool = try! ConnectionPool(.URI("\(NSTemporaryDirectory())/SQLite.swift Pool Tests.sqlite"))
7979

8080
do {
8181
try! pool.readable.execute("SELECT 1")
8282
}
8383

8484
XCTAssertEqual(pool.totalReadableConnectionCount, pool.availableReadableConnectionCount)
8585
}
86-
86+
8787
}

0 commit comments

Comments
 (0)