Skip to content

Commit 2144caa

Browse files
committed
update the readme and playground with error handling
1 parent 2ade01f commit 2144caa

File tree

7 files changed

+43
-43
lines changed

7 files changed

+43
-43
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ public final class Connection {
230230
/// - bindings: A list of parameters to bind to the statement.
231231
///
232232
/// - Returns: The first value of the first row returned.
233-
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? {
234-
return try scalar(statement, bindings)
233+
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) -> Binding? {
234+
return scalar(statement, bindings)
235235
}
236236

237237
/// Runs a single SQL statement (with optional parameter bindings),
@@ -244,8 +244,8 @@ public final class Connection {
244244
/// - bindings: A list of parameters to bind to the statement.
245245
///
246246
/// - Returns: The first value of the first row returned.
247-
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding? {
248-
return try prepare(statement).scalar(bindings)
247+
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) -> Binding? {
248+
return try! prepare(statement).scalar(bindings)
249249
}
250250

251251
/// Runs a single SQL statement (with optional parameter bindings),
@@ -258,8 +258,8 @@ public final class Connection {
258258
/// - bindings: A dictionary of named parameters to bind to the statement.
259259
///
260260
/// - Returns: The first value of the first row returned.
261-
@warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) throws -> Binding? {
262-
return try prepare(statement).scalar(bindings)
261+
@warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding? {
262+
return try! prepare(statement).scalar(bindings)
263263
}
264264

265265
// MARK: - Transactions

Source/Typed/Query.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -908,30 +908,30 @@ extension Connection {
908908
}
909909
}
910910

911-
public func scalar<V : Value>(query: ScalarQuery<V>) throws -> V {
911+
public func scalar<V : Value>(query: ScalarQuery<V>) -> V {
912912
let expression = query.expression
913-
return value(try scalar(expression.template, expression.bindings))
913+
return value(scalar(expression.template, expression.bindings))
914914
}
915915

916-
public func scalar<V : Value>(query: ScalarQuery<V?>) throws -> V.ValueType? {
916+
public func scalar<V : Value>(query: ScalarQuery<V?>) -> V.ValueType? {
917917
let expression = query.expression
918-
guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
918+
guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
919919
return V.fromDatatypeValue(value)
920920
}
921921

922-
public func scalar<V : Value>(query: Select<V>) throws -> V {
922+
public func scalar<V : Value>(query: Select<V>) -> V {
923923
let expression = query.expression
924-
return value(try scalar(expression.template, expression.bindings))
924+
return value(scalar(expression.template, expression.bindings))
925925
}
926926

927-
public func scalar<V : Value>(query: Select<V?>) throws -> V.ValueType? {
927+
public func scalar<V : Value>(query: Select<V?>) -> V.ValueType? {
928928
let expression = query.expression
929-
guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
929+
guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
930930
return V.fromDatatypeValue(value)
931931
}
932932

933-
public func pluck(query: QueryType) throws -> Row? {
934-
return try prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
933+
public func pluck(query: QueryType) -> Row? {
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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ class ConnectionTests : SQLiteTestCase {
8787
}
8888

8989
func test_scalar_preparesRunsAndReturnsScalarValues() {
90-
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = 0") as? Int64)
91-
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = ?", 0) as? Int64)
92-
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = ?", [0]) as? Int64)
93-
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = $admin", ["$admin": 0]) as? Int64)
90+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = 0") as? Int64)
91+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = ?", 0) as? Int64)
92+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = ?", [0]) as? Int64)
93+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = $admin", ["$admin": 0]) as? Int64)
9494
AssertSQL("SELECT count(*) FROM users WHERE admin = 0", 4)
9595
}
9696

@@ -238,7 +238,7 @@ class ConnectionTests : SQLiteTestCase {
238238
try! db.transaction {
239239
try self.InsertUser("alice")
240240
}
241-
XCTAssertEqual(1, try! db.scalar("SELECT count(*) FROM users") as? Int64)
241+
XCTAssertEqual(1, db.scalar("SELECT count(*) FROM users") as? Int64)
242242
}
243243
}
244244

@@ -252,7 +252,7 @@ class ConnectionTests : SQLiteTestCase {
252252
}
253253
} catch {
254254
}
255-
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users") as? Int64)
255+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users") as? Int64)
256256
}
257257
}
258258

@@ -268,36 +268,36 @@ class ConnectionTests : SQLiteTestCase {
268268
}
269269
} catch {
270270
}
271-
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users") as? Int64)
271+
XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users") as? Int64)
272272
}
273273
}
274274

275275
func test_createFunction_withArrayArguments() {
276276
db.createFunction("hello") { $0[0].map { "Hello, \($0)!" } }
277277

278-
XCTAssertEqual("Hello, world!", try! db.scalar("SELECT hello('world')") as? String)
279-
XCTAssert(try! db.scalar("SELECT hello(NULL)") == nil)
278+
XCTAssertEqual("Hello, world!", db.scalar("SELECT hello('world')") as? String)
279+
XCTAssert(db.scalar("SELECT hello(NULL)") == nil)
280280
}
281281

282282
func test_createFunction_createsQuotableFunction() {
283283
db.createFunction("hello world") { $0[0].map { "Hello, \($0)!" } }
284284

285-
XCTAssertEqual("Hello, world!", try! db.scalar("SELECT \"hello world\"('world')") as? String)
286-
XCTAssert(try! db.scalar("SELECT \"hello world\"(NULL)") == nil)
285+
XCTAssertEqual("Hello, world!", db.scalar("SELECT \"hello world\"('world')") as? String)
286+
XCTAssert(db.scalar("SELECT \"hello world\"(NULL)") == nil)
287287
}
288288

289289
func test_createCollation_createsCollation() {
290290
db.createCollation("NODIACRITIC") { lhs, rhs in
291291
return lhs.compare(rhs, options: .DiacriticInsensitiveSearch)
292292
}
293-
XCTAssertEqual(1, try! db.scalar("SELECT ? = ? COLLATE NODIACRITIC", "cafe", "café") as? Int64)
293+
XCTAssertEqual(1, db.scalar("SELECT ? = ? COLLATE NODIACRITIC", "cafe", "café") as? Int64)
294294
}
295295

296296
func test_createCollation_createsQuotableCollation() {
297297
db.createCollation("NO DIACRITIC") { lhs, rhs in
298298
return lhs.compare(rhs, options: .DiacriticInsensitiveSearch)
299299
}
300-
XCTAssertEqual(1, try! db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as? Int64)
300+
XCTAssertEqual(1, db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as? Int64)
301301
}
302302

303303
func test_interrupt_interruptsLongRunningQuery() {

Tests/FTS4Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class FTS4IntegrationTests : SQLiteTestCase {
7171
AssertSQL("CREATE VIRTUAL TABLE \"emails\" USING fts4(\"subject\", \"body\", tokenize=\"SQLite.swift\" \"tokenizer\")")
7272

7373
try! db.run(emails.insert(subject <- "Aún más cáfe!"))
74-
XCTAssertEqual(1, try! db.scalar(emails.filter(emails.match("aun")).count))
74+
XCTAssertEqual(1, db.scalar(emails.filter(emails.match("aun")).count))
7575
}
7676

7777
}

Tests/QueryTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,16 @@ class QueryIntegrationTests : SQLiteTestCase {
294294
}
295295

296296
func test_scalar() {
297-
XCTAssertEqual(0, try! db.scalar(users.count))
298-
XCTAssertEqual(false, try! db.scalar(users.exists))
297+
XCTAssertEqual(0, db.scalar(users.count))
298+
XCTAssertEqual(false, db.scalar(users.exists))
299299

300300
try! InsertUsers("alice")
301-
XCTAssertEqual(1, try! db.scalar(users.select(id.average)))
301+
XCTAssertEqual(1, db.scalar(users.select(id.average)))
302302
}
303303

304304
func test_pluck() {
305305
let rowid = try! db.run(users.insert(email <- "alice@example.com"))
306-
XCTAssertEqual(rowid, try! db.pluck(users)![id])
306+
XCTAssertEqual(rowid, db.pluck(users)![id])
307307
}
308308

309309
func test_insert() {

0 commit comments

Comments
 (0)