Skip to content

Commit a1121c4

Browse files
committed
Change ConnectionPool API and get rid of Connection protocol.
1 parent a688d18 commit a1121c4

File tree

8 files changed

+168
-314
lines changed

8 files changed

+168
-314
lines changed

SQLite/Core/Connection.swift

Lines changed: 9 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -46,193 +46,8 @@ public enum TransactionMode : String {
4646
}
4747

4848

49-
/// Protocol to an SQLite connection
50-
public protocol Connection {
51-
52-
/// Whether or not the database was opened in a read-only state.
53-
var readonly: Bool { get }
54-
55-
/// The last rowid inserted into the database via this connection.
56-
var lastInsertRowid: Int64? { get }
57-
58-
/// The last number of changes (inserts, updates, or deletes) made to the
59-
/// database via this connection.
60-
var changes: Int { get }
61-
62-
/// The total number of changes (inserts, updates, or deletes) made to the
63-
/// database via this connection.
64-
var totalChanges : Int { get }
65-
66-
// MARK: - Execute
67-
68-
/// Executes a batch of SQL statements.
69-
///
70-
/// - Parameter SQL: A batch of zero or more semicolon-separated SQL
71-
/// statements.
72-
///
73-
/// - Throws: `Result.Error` if query execution fails.
74-
func execute(SQL: String) throws
75-
76-
// MARK: - Prepare
77-
78-
/// Prepares a single SQL statement (with optional parameter bindings).
79-
///
80-
/// - Parameters:
81-
///
82-
/// - statement: A single SQL statement.
83-
///
84-
/// - bindings: A list of parameters to bind to the statement.
85-
///
86-
/// - Returns: A prepared statement.
87-
@warn_unused_result func prepare(statement: String, _ bindings: Binding?...) throws -> Statement
88-
89-
/// Prepares a single SQL statement and binds parameters to it.
90-
///
91-
/// - Parameters:
92-
///
93-
/// - statement: A single SQL statement.
94-
///
95-
/// - bindings: A list of parameters to bind to the statement.
96-
///
97-
/// - Returns: A prepared statement.
98-
@warn_unused_result func prepare(statement: String, _ bindings: [Binding?]) throws -> Statement
99-
100-
/// Prepares a single SQL statement and binds parameters to it.
101-
///
102-
/// - Parameters:
103-
///
104-
/// - statement: A single SQL statement.
105-
///
106-
/// - bindings: A dictionary of named parameters to bind to the statement.
107-
///
108-
/// - Returns: A prepared statement.
109-
@warn_unused_result func prepare(statement: String, _ bindings: [String: Binding?]) throws -> Statement
110-
111-
// MARK: - Run
112-
113-
/// Runs a single SQL statement (with optional parameter bindings).
114-
///
115-
/// - Parameters:
116-
///
117-
/// - statement: A single SQL statement.
118-
///
119-
/// - bindings: A list of parameters to bind to the statement.
120-
///
121-
/// - Throws: `Result.Error` if query execution fails.
122-
///
123-
/// - Returns: The statement.
124-
func run(statement: String, _ bindings: Binding?...) throws -> Statement
125-
126-
/// Prepares, binds, and runs a single SQL statement.
127-
///
128-
/// - Parameters:
129-
///
130-
/// - statement: A single SQL statement.
131-
///
132-
/// - bindings: A list of parameters to bind to the statement.
133-
///
134-
/// - Throws: `Result.Error` if query execution fails.
135-
///
136-
/// - Returns: The statement.
137-
func run(statement: String, _ bindings: [Binding?]) throws -> Statement
138-
139-
/// Prepares, binds, and runs a single SQL statement.
140-
///
141-
/// - Parameters:
142-
///
143-
/// - statement: A single SQL statement.
144-
///
145-
/// - bindings: A dictionary of named parameters to bind to the statement.
146-
///
147-
/// - Throws: `Result.Error` if query execution fails.
148-
///
149-
/// - Returns: The statement.
150-
func run(statement: String, _ bindings: [String: Binding?]) throws -> Statement
151-
152-
// MARK: - Scalar
153-
154-
/// Runs a single SQL statement (with optional parameter bindings),
155-
/// returning the first value of the first row.
156-
///
157-
/// - Parameters:
158-
///
159-
/// - statement: A single SQL statement.
160-
///
161-
/// - bindings: A list of parameters to bind to the statement.
162-
///
163-
/// - Returns: The first value of the first row returned.
164-
@warn_unused_result func scalar(statement: String, _ bindings: Binding?...) -> Binding?
165-
166-
/// Runs a single SQL statement (with optional parameter bindings),
167-
/// returning the first value of the first row.
168-
///
169-
/// - Parameters:
170-
///
171-
/// - statement: A single SQL statement.
172-
///
173-
/// - bindings: A list of parameters to bind to the statement.
174-
///
175-
/// - Returns: The first value of the first row returned.
176-
@warn_unused_result func scalar(statement: String, _ bindings: [Binding?]) -> Binding?
177-
178-
/// Runs a single SQL statement (with optional parameter bindings),
179-
/// returning the first value of the first row.
180-
///
181-
/// - Parameters:
182-
///
183-
/// - statement: A single SQL statement.
184-
///
185-
/// - bindings: A dictionary of named parameters to bind to the statement.
186-
///
187-
/// - Returns: The first value of the first row returned.
188-
@warn_unused_result func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding?
189-
190-
// MARK: - Transactions
191-
192-
// TODO: Consider not requiring a throw to roll back?
193-
/// Runs a transaction with the given mode.
194-
///
195-
/// - Note: Transactions cannot be nested. To nest transactions, see
196-
/// `savepoint()`, instead.
197-
///
198-
/// - Parameters:
199-
///
200-
/// - mode: The mode in which a transaction acquires a lock.
201-
///
202-
/// Default: `.Deferred`
203-
///
204-
/// - block: A closure to run SQL statements within the transaction.
205-
/// The transaction will be committed when the block returns. The block
206-
/// must throw to roll the transaction back.
207-
///
208-
/// - Throws: `Result.Error`, and rethrows.
209-
func transaction(mode: TransactionMode, block: (Connection) throws -> Void) throws
210-
211-
// TODO: Consider not requiring a throw to roll back?
212-
// TODO: Consider removing ability to set a name?
213-
/// Runs a transaction with the given savepoint name (if omitted, it will
214-
/// generate a UUID).
215-
///
216-
/// - SeeAlso: `transaction()`.
217-
///
218-
/// - Parameters:
219-
///
220-
/// - savepointName: A unique identifier for the savepoint (optional).
221-
///
222-
/// - block: A closure to run SQL statements within the transaction.
223-
/// The savepoint will be released (committed) when the block returns.
224-
/// The block must throw to roll the savepoint back.
225-
///
226-
/// - Throws: `SQLite.Result.Error`, and rethrows.
227-
func savepoint(name: String, block: (Connection) throws -> Void) throws
228-
229-
func sync<T>(block: () throws -> T) rethrows -> T
230-
231-
}
232-
233-
23449
/// A connection to SQLite.
235-
public final class DirectConnection : Connection, Equatable {
50+
public final class Connection : Equatable {
23651

23752
/// The location of a SQLite database.
23853
public enum Location {
@@ -273,24 +88,12 @@ public final class DirectConnection : Connection, Equatable {
27388
/// Default: `false`.
27489
///
27590
/// - Returns: A new database connection.
276-
public convenience init(_ location: Location = .InMemory, readonly: Bool = false, vfsName: String? = nil) throws {
91+
public convenience init(_ location: Location = .InMemory, readonly: Bool = false) throws {
27792
let flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE
278-
try self.init(location, flags: flags, dispatcher: ReentrantDispatcher("SQLite.Connection"), vfsName: vfsName)
93+
try self.init(location, flags: flags, dispatcher: ReentrantDispatcher("SQLite.Connection"), vfsName: nil)
27994
}
280-
281-
/// Initializes a new SQLite connection.
282-
///
283-
/// - Parameters:
284-
///
285-
/// - location: The location of the database. Creates a new database if it
286-
/// doesn’t already exist (unless in read-only mode).
287-
///
288-
/// - flags: SQLite open flags
289-
///
290-
/// - dispatcher: Dispatcher synchronization blocks
291-
///
292-
/// - Returns: A new database connection.
293-
public init(_ location: Location, flags: Int32, dispatcher: Dispatcher, vfsName: String? = nil) throws {
95+
96+
init(_ location: Location, flags: Int32, dispatcher: Dispatcher, vfsName: String? = nil) throws {
29497
self.dispatcher = dispatcher
29598
if let vfsName = vfsName {
29699
try check(sqlite3_open_v2(location.description, &_handle, flags, vfsName))
@@ -812,15 +615,15 @@ public final class DirectConnection : Connection, Equatable {
812615

813616
}
814617

815-
extension DirectConnection : CustomStringConvertible {
618+
extension Connection : CustomStringConvertible {
816619

817620
public var description: String {
818621
return String.fromCString(sqlite3_db_filename(handle, nil))!
819622
}
820623

821624
}
822625

823-
extension DirectConnection.Location : CustomStringConvertible {
626+
extension Connection.Location : CustomStringConvertible {
824627

825628
public var description: String {
826629
switch self {
@@ -835,7 +638,7 @@ extension DirectConnection.Location : CustomStringConvertible {
835638

836639
}
837640

838-
public func ==(lhs: DirectConnection, rhs: DirectConnection) -> Bool {
641+
public func == (lhs: Connection, rhs: Connection) -> Bool {
839642
return lhs === rhs
840643
}
841644

@@ -872,7 +675,7 @@ public enum Result : ErrorType {
872675

873676
case Error(message: String, code: Int32, statement: Statement?)
874677

875-
init?(errorCode: Int32, connection: DirectConnection, statement: Statement? = nil) {
678+
init?(errorCode: Int32, connection: Connection, statement: Statement? = nil) {
876679
guard !Result.successCodes.contains(errorCode) else { return nil }
877680

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

0 commit comments

Comments
 (0)