Skip to content

Commit 5869b0e

Browse files
committed
Merge pull request stephencelis#290 from kujenga/swift-2
propagate errors in Statement initialization
2 parents da2d434 + 2144caa commit 5869b0e

File tree

7 files changed

+39
-39
lines changed

7 files changed

+39
-39
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ let insert = users.insert(name <- "Alice", email <- "alice@mac.com")
5656
let rowid = try db.run(insert)
5757
// INSERT INTO "users" ("name", "email") VALUES ('Alice', 'alice@mac.com')
5858

59-
for user in db.prepare(users) {
60-
println("id: \(user[id]), name: \(user[name]), email: \(user[email])")
59+
for user in try db.prepare(users) {
60+
print("id: \(user[id]), name: \(user[name]), email: \(user[email])")
6161
// id: 1, name: Optional("Alice"), email: alice@mac.com
6262
}
6363
// SELECT * FROM "users"
6464

6565
let alice = users.filter(id == rowid)
6666

67-
try db.run(alice.update(email <- email.replace("mac.com", "me.com")))
67+
try db.run(alice.update(email <- email.replace("mac.com", with: "me.com")))
6868
// UPDATE "users" SET "email" = replace("email", 'mac.com', 'me.com')
6969
// WHERE ("id" = 1)
7070

@@ -79,17 +79,17 @@ SQLite.swift also works as a lightweight, Swift-friendly wrapper over the C
7979
API.
8080

8181
``` swift
82-
let stmt = db.prepare("INSERT INTO users (email) VALUES (?)")
82+
let stmt = try db.prepare("INSERT INTO users (email) VALUES (?)")
8383
for email in ["betty@icloud.com", "cathy@icloud.com"] {
84-
stmt.run(email)
84+
try stmt.run(email)
8585
}
8686

8787
db.totalChanges // 3
8888
db.changes // 1
8989
db.lastInsertRowid // 3
9090

91-
for row in db.prepare("SELECT id, email FROM users") {
92-
println("id: \(row[0]), email: \(row[1])")
91+
for row in try db.prepare("SELECT id, email FROM users") {
92+
print("id: \(row[0]), email: \(row[1])")
9393
// id: Optional(2), email: Optional("betty@icloud.com")
9494
// id: Optional(3), email: Optional("cathy@icloud.com")
9595
}

SQLite.playground/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ try! db.run(users.create { t in
1919
let rowid = try! db.run(users.insert(email <- "alice@mac.com"))
2020
let alice = users.filter(id == rowid)
2121

22-
for user in db.prepare(users) {
22+
for user in try! db.prepare(users) {
2323
print("id: \(user[id]), email: \(user[email])")
2424
}
2525

@@ -37,7 +37,7 @@ try! db.run(emails.insert(
3737

3838
let row = db.pluck(emails.match("hello"))
3939

40-
let query = db.prepare(emails.match("hello"))
40+
let query = try! db.prepare(emails.match("hello"))
4141
for row in query {
4242
print(row[subject])
4343
}

Source/Core/Connection.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public final class Connection {
140140
/// - bindings: A list of parameters to bind to the statement.
141141
///
142142
/// - Returns: A prepared statement.
143-
@warn_unused_result public func prepare(statement: String, _ bindings: Binding?...) -> Statement {
144-
if !bindings.isEmpty { return prepare(statement, bindings) }
145-
return Statement(self, statement)
143+
@warn_unused_result public func prepare(statement: String, _ bindings: Binding?...) throws -> Statement {
144+
if !bindings.isEmpty { return try prepare(statement, bindings) }
145+
return try Statement(self, statement)
146146
}
147147

148148
/// Prepares a single SQL statement and binds parameters to it.
@@ -154,8 +154,8 @@ public final class Connection {
154154
/// - bindings: A list of parameters to bind to the statement.
155155
///
156156
/// - Returns: A prepared statement.
157-
@warn_unused_result public func prepare(statement: String, _ bindings: [Binding?]) -> Statement {
158-
return prepare(statement).bind(bindings)
157+
@warn_unused_result public func prepare(statement: String, _ bindings: [Binding?]) throws -> Statement {
158+
return try prepare(statement).bind(bindings)
159159
}
160160

161161
/// Prepares a single SQL statement and binds parameters to it.
@@ -167,8 +167,8 @@ public final class Connection {
167167
/// - bindings: A dictionary of named parameters to bind to the statement.
168168
///
169169
/// - Returns: A prepared statement.
170-
@warn_unused_result public func prepare(statement: String, _ bindings: [String: Binding?]) -> Statement {
171-
return prepare(statement).bind(bindings)
170+
@warn_unused_result public func prepare(statement: String, _ bindings: [String: Binding?]) throws -> Statement {
171+
return try prepare(statement).bind(bindings)
172172
}
173173

174174
// MARK: - Run
@@ -245,7 +245,7 @@ public final class Connection {
245245
///
246246
/// - Returns: The first value of the first row returned.
247247
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) -> Binding? {
248-
return prepare(statement).scalar(bindings)
248+
return try! prepare(statement).scalar(bindings)
249249
}
250250

251251
/// Runs a single SQL statement (with optional parameter bindings),
@@ -259,7 +259,7 @@ public final class Connection {
259259
///
260260
/// - Returns: The first value of the first row returned.
261261
@warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding? {
262-
return prepare(statement).scalar(bindings)
262+
return try! prepare(statement).scalar(bindings)
263263
}
264264

265265
// MARK: - Transactions

Source/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 let connection: Connection
3131

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

3737
deinit {

Source/Typed/Query.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -857,23 +857,23 @@ public struct Delete : ExpressionType {
857857

858858
extension Connection {
859859

860-
public func prepare(query: QueryType) -> AnySequence<Row> {
860+
public func prepare(query: QueryType) throws -> AnySequence<Row> {
861861
let expression = query.expression
862-
let statement = prepare(expression.template, expression.bindings)
862+
let statement = try prepare(expression.template, expression.bindings)
863863

864-
let columnNames: [String: Int] = {
864+
let columnNames: [String: Int] = try {
865865
var (columnNames, idx) = ([String: Int](), 0)
866866
column: for each in query.clauses.select.columns ?? [Expression<Void>(literal: "*")] {
867867
var names = each.expression.template.characters.split { $0 == "." }.map(String.init)
868868
let column = names.removeLast()
869869
let namespace = names.joinWithSeparator(".")
870870

871-
func expandGlob(namespace: Bool) -> QueryType -> Void {
872-
return { query in
871+
func expandGlob(namespace: Bool) -> (QueryType throws -> Void) {
872+
return { (query: QueryType) throws -> (Void) in
873873
var q = query.dynamicType.init(query.clauses.from.name, database: query.clauses.from.database)
874874
q.clauses.select = query.clauses.select
875875
let e = q.expression
876-
var names = self.prepare(e.template, e.bindings).columnNames.map { $0.quote() }
876+
var names = try self.prepare(e.template, e.bindings).columnNames.map { $0.quote() }
877877
if namespace { names = names.map { "\(query.tableName().expression.template).\($0)" } }
878878
for name in names { columnNames[name] = idx++ }
879879
}
@@ -886,14 +886,14 @@ extension Connection {
886886
if !namespace.isEmpty {
887887
for q in queries {
888888
if q.tableName().expression.template == namespace {
889-
expandGlob(true)(q)
889+
try expandGlob(true)(q)
890890
continue column
891891
}
892892
}
893893
fatalError("no such table: \(namespace)")
894894
}
895895
for q in queries {
896-
expandGlob(query.clauses.join.count > 0)(q)
896+
try expandGlob(query.clauses.join.count > 0)(q)
897897
}
898898
continue
899899
}
@@ -931,7 +931,7 @@ extension Connection {
931931
}
932932

933933
public func pluck(query: QueryType) -> Row? {
934-
return prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
934+
return try! prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
935935
}
936936

937937
/// Runs an `Insert` query.

Tests/ConnectionTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ class ConnectionTests : SQLiteTestCase {
7272
}
7373

7474
func test_prepare_preparesAndReturnsStatements() {
75-
_ = db.prepare("SELECT * FROM users WHERE admin = 0")
76-
_ = db.prepare("SELECT * FROM users WHERE admin = ?", 0)
77-
_ = db.prepare("SELECT * FROM users WHERE admin = ?", [0])
78-
_ = db.prepare("SELECT * FROM users WHERE admin = $admin", ["$admin": 0])
75+
_ = try! db.prepare("SELECT * FROM users WHERE admin = 0")
76+
_ = try! db.prepare("SELECT * FROM users WHERE admin = ?", 0)
77+
_ = try! db.prepare("SELECT * FROM users WHERE admin = ?", [0])
78+
_ = try! db.prepare("SELECT * FROM users WHERE admin = $admin", ["$admin": 0])
7979
}
8080

8181
func test_run_preparesRunsAndReturnsStatements() {
@@ -113,7 +113,7 @@ class ConnectionTests : SQLiteTestCase {
113113
}
114114

115115
func test_transaction_beginsAndCommitsTransactions() {
116-
let stmt = db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")
116+
let stmt = try! db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")
117117

118118
try! db.transaction {
119119
try stmt.run()
@@ -126,7 +126,7 @@ class ConnectionTests : SQLiteTestCase {
126126
}
127127

128128
func test_transaction_beginsAndRollsTransactionsBack() {
129-
let stmt = db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")
129+
let stmt = try! db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")
130130

131131
do {
132132
try db.transaction {
@@ -162,7 +162,7 @@ class ConnectionTests : SQLiteTestCase {
162162

163163
func test_savepoint_beginsAndRollsSavepointsBack() {
164164
let db = self.db
165-
let stmt = db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")
165+
let stmt = try! db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")
166166

167167
do {
168168
try db.savepoint("1") {
@@ -307,7 +307,7 @@ class ConnectionTests : SQLiteTestCase {
307307
return nil
308308
}
309309

310-
let stmt = db.prepare("SELECT *, sleep(?) FROM users", 0.1)
310+
let stmt = try! db.prepare("SELECT *, sleep(?) FROM users", 0.1)
311311
try! stmt.run()
312312

313313
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(10 * NSEC_PER_MSEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), db.interrupt)

Tests/QueryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class QueryIntegrationTests : SQLiteTestCase {
278278
// MARK: -
279279

280280
func test_select() {
281-
for _ in db.prepare(users) {
281+
for _ in try! db.prepare(users) {
282282
// FIXME
283283
}
284284

@@ -288,7 +288,7 @@ class QueryIntegrationTests : SQLiteTestCase {
288288
let alice = try! db.run(users.insert(email <- "alice@example.com"))
289289
try! db.run(users.insert(email <- "betsy@example.com", managerId <- alice))
290290

291-
for user in db.prepare(users.join(managers, on: managers[id] == users[managerId])) {
291+
for user in try! db.prepare(users.join(managers, on: managers[id] == users[managerId])) {
292292
user[users[managerId]]
293293
}
294294
}

0 commit comments

Comments
 (0)