Skip to content

Commit ea4c49b

Browse files
committed
Cleanup throws and trys
It didn't make a lot of sense to being requiring `try` when calling operators that were building queries. Other functions like `scalar` and `pluck` are now correctly throwing errors
1 parent 612a99f commit ea4c49b

13 files changed

+69
-69
lines changed

SQLite/Core/Connection.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public protocol Connection {
156156
/// - bindings: A list of parameters to bind to the statement.
157157
///
158158
/// - Returns: The first value of the first row returned.
159-
@warn_unused_result func scalar(statement: String, _ bindings: Binding?...) -> Binding?
159+
@warn_unused_result func scalar(statement: String, _ bindings: Binding?...) throws -> Binding?
160160

161161
/// Runs a single SQL statement (with optional parameter bindings),
162162
/// returning the first value of the first row.
@@ -168,7 +168,7 @@ public protocol Connection {
168168
/// - bindings: A list of parameters to bind to the statement.
169169
///
170170
/// - Returns: The first value of the first row returned.
171-
@warn_unused_result func scalar(statement: String, _ bindings: [Binding?]) -> Binding?
171+
@warn_unused_result func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding?
172172

173173
/// Runs a single SQL statement (with optional parameter bindings),
174174
/// returning the first value of the first row.
@@ -180,7 +180,7 @@ public protocol Connection {
180180
/// - bindings: A dictionary of named parameters to bind to the statement.
181181
///
182182
/// - Returns: The first value of the first row returned.
183-
@warn_unused_result func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding?
183+
@warn_unused_result func scalar(statement: String, _ bindings: [String: Binding?]) throws -> Binding?
184184

185185
// MARK: - Transactions
186186

@@ -454,8 +454,8 @@ public final class DirectConnection : Connection, Equatable {
454454
/// - bindings: A list of parameters to bind to the statement.
455455
///
456456
/// - Returns: The first value of the first row returned.
457-
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) -> Binding? {
458-
return scalar(statement, bindings)
457+
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? {
458+
return try scalar(statement, bindings)
459459
}
460460

461461
/// Runs a single SQL statement (with optional parameter bindings),
@@ -468,8 +468,8 @@ public final class DirectConnection : Connection, Equatable {
468468
/// - bindings: A list of parameters to bind to the statement.
469469
///
470470
/// - Returns: The first value of the first row returned.
471-
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) -> Binding? {
472-
return try! prepare(statement).scalar(bindings)
471+
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding? {
472+
return try prepare(statement).scalar(bindings)
473473
}
474474

475475
/// Runs a single SQL statement (with optional parameter bindings),
@@ -482,8 +482,8 @@ public final class DirectConnection : Connection, Equatable {
482482
/// - bindings: A dictionary of named parameters to bind to the statement.
483483
///
484484
/// - Returns: The first value of the first row returned.
485-
@warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding? {
486-
return try! prepare(statement).scalar(bindings)
485+
@warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) throws -> Binding? {
486+
return try prepare(statement).scalar(bindings)
487487
}
488488

489489
// MARK: - Transactions
@@ -771,11 +771,11 @@ public final class DirectConnection : Connection, Equatable {
771771
///
772772
/// - block: A collation function that takes two strings and returns the
773773
/// comparison result.
774-
public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) {
774+
public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) throws {
775775
let box: Collation = { lhs, rhs in
776776
Int32(block(lhs: String.fromCString(UnsafePointer<Int8>(lhs))!, rhs: String.fromCString(UnsafePointer<Int8>(rhs))!).rawValue)
777777
}
778-
try! check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
778+
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
779779
unsafeBitCast(callback, Collation.self)(lhs, rhs)
780780
}, nil))
781781
collations[collation] = box

SQLite/Core/ConnectionPool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public final class ConnectionPool {
108108
func run(statement: String, _ bindings: [Binding?]) throws -> Statement { return try connection.run(statement, bindings) }
109109
func run(statement: String, _ bindings: [String: Binding?]) throws -> Statement { return try connection.run(statement, bindings) }
110110

111-
@warn_unused_result func scalar(statement: String, _ bindings: Binding?...) -> Binding? { return connection.scalar(statement, bindings) }
112-
@warn_unused_result func scalar(statement: String, _ bindings: [Binding?]) -> Binding? { return connection.scalar(statement, bindings) }
113-
@warn_unused_result func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding? { return connection.scalar(statement, bindings) }
111+
@warn_unused_result func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? { return try connection.scalar(statement, bindings) }
112+
@warn_unused_result func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding? { return try connection.scalar(statement, bindings) }
113+
@warn_unused_result func scalar(statement: String, _ bindings: [String: Binding?]) throws -> Binding? { return try connection.scalar(statement, bindings) }
114114

115115
func transaction(mode: TransactionMode, block: (Connection) throws -> Void) throws { return try connection.transaction(mode, block: block) }
116116
func savepoint(name: String, block: (Connection) throws -> Void) throws { return try connection.savepoint(name, block: block) }

SQLite/Core/Statement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public final class Statement {
154154
}
155155

156156
reset(clearBindings: false)
157-
try! step()
157+
try step()
158158
return row[0]
159159
}
160160

SQLite/Typed/Expression.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,28 @@ extension ExpressionType {
110110

111111
extension ExpressionType where UnderlyingType : Value {
112112

113-
public init(value: UnderlyingType) throws {
114-
self.init("?", [try value.datatypeValue()])
113+
public init(value: UnderlyingType) {
114+
self.init("?", [try! value.datatypeValue()])
115115
}
116116

117117
}
118118

119119
extension ExpressionType where UnderlyingType : _OptionalType, UnderlyingType.WrappedType : Value {
120120

121121
public static var null: Self {
122-
return try! self.init(value: nil)
122+
return self.init(value: nil)
123123
}
124124

125-
public init(value: UnderlyingType.WrappedType?) throws {
126-
self.init("?", [try value?.datatypeValue()])
125+
public init(value: UnderlyingType.WrappedType?) {
126+
self.init("?", [try! value?.datatypeValue()])
127127
}
128128

129129
}
130130

131131
extension Value {
132132

133133
public var expression: Expression<Void> {
134-
return try! Expression(value: self).expression
134+
return Expression(value: self).expression
135135
}
136136

137137
}

SQLite/Typed/Operators.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,15 @@ public func ==<V : Value where V.Datatype : Equatable>(lhs: Expression<V?>, rhs:
335335
public func ==<V : Value where V.Datatype : Equatable>(lhs: Expression<V>, rhs: V) -> Expression<Bool> {
336336
return "=".infix(lhs, rhs)
337337
}
338-
public func ==<V : Value where V.Datatype : Equatable>(lhs: Expression<V?>, rhs: V?) throws -> Expression<Bool?> {
339-
guard let rhs = rhs else { return "IS".infix(lhs, try Expression<V?>(value: nil)) }
338+
public func ==<V : Value where V.Datatype : Equatable>(lhs: Expression<V?>, rhs: V?) -> Expression<Bool?> {
339+
guard let rhs = rhs else { return "IS".infix(lhs, Expression<V?>(value: nil)) }
340340
return "=".infix(lhs, rhs)
341341
}
342342
public func ==<V : Value where V.Datatype : Equatable>(lhs: V, rhs: Expression<V>) -> Expression<Bool> {
343343
return "=".infix(lhs, rhs)
344344
}
345-
public func ==<V : Value where V.Datatype : Equatable>(lhs: V?, rhs: Expression<V?>) throws -> Expression<Bool?> {
346-
guard let lhs = lhs else { return "IS".infix(try Expression<V?>(value: nil), rhs) }
345+
public func ==<V : Value where V.Datatype : Equatable>(lhs: V?, rhs: Expression<V?>) -> Expression<Bool?> {
346+
guard let lhs = lhs else { return "IS".infix(Expression<V?>(value: nil), rhs) }
347347
return "=".infix(lhs, rhs)
348348
}
349349

@@ -362,15 +362,15 @@ public func !=<V : Value where V.Datatype : Equatable>(lhs: Expression<V?>, rhs:
362362
public func !=<V : Value where V.Datatype : Equatable>(lhs: Expression<V>, rhs: V) -> Expression<Bool> {
363363
return infix(lhs, rhs)
364364
}
365-
public func !=<V : Value where V.Datatype : Equatable>(lhs: Expression<V?>, rhs: V?) throws -> Expression<Bool?> {
366-
guard let rhs = rhs else { return "IS NOT".infix(lhs, try Expression<V?>(value: nil)) }
365+
public func !=<V : Value where V.Datatype : Equatable>(lhs: Expression<V?>, rhs: V?) -> Expression<Bool?> {
366+
guard let rhs = rhs else { return "IS NOT".infix(lhs, Expression<V?>(value: nil)) }
367367
return infix(lhs, rhs)
368368
}
369369
public func !=<V : Value where V.Datatype : Equatable>(lhs: V, rhs: Expression<V>) -> Expression<Bool> {
370370
return infix(lhs, rhs)
371371
}
372-
public func !=<V : Value where V.Datatype : Equatable>(lhs: V?, rhs: Expression<V?>) throws -> Expression<Bool?> {
373-
guard let lhs = lhs else { return "IS NOT".infix(try Expression<V?>(value: nil), rhs) }
372+
public func !=<V : Value where V.Datatype : Equatable>(lhs: V?, rhs: Expression<V?>) -> Expression<Bool?> {
373+
guard let lhs = lhs else { return "IS NOT".infix(Expression<V?>(value: nil), rhs) }
374374
return infix(lhs, rhs)
375375
}
376376

SQLite/Typed/Query.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ extension Connection {
939939

940940
public func scalar<V : Value>(query: ScalarQuery<V?>) throws -> V.ValueType? {
941941
let expression = query.expression
942-
guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
942+
guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
943943
return try V.fromDatatypeValue(value)
944944
}
945945

@@ -950,12 +950,12 @@ extension Connection {
950950

951951
public func scalar<V : Value>(query: Select<V?>) throws -> V.ValueType? {
952952
let expression = query.expression
953-
guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
953+
guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
954954
return try V.fromDatatypeValue(value)
955955
}
956956

957-
public func pluck(query: QueryType) -> Row? {
958-
return try! prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
957+
public func pluck(query: QueryType) throws -> Row? {
958+
return try prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
959959
}
960960

961961
/// Runs an `Insert` query.

SQLite/Typed/Setter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public struct Setter {
5353
self.value = value
5454
}
5555

56-
private init<V : Value>(column: Expression<V?>, value: V?) throws {
56+
private init<V : Value>(column: Expression<V?>, value: V?) {
5757
self.column = column
58-
self.value = try Expression<V?>(value: value)
58+
self.value = Expression<V?>(value: value)
5959
}
6060

6161
}
@@ -80,8 +80,8 @@ public func <-<V : Value>(column: Expression<V?>, value: Expression<V>) -> Sette
8080
public func <-<V : Value>(column: Expression<V?>, value: Expression<V?>) -> Setter {
8181
return Setter(column: column, value: value)
8282
}
83-
public func <-<V : Value>(column: Expression<V?>, value: V?) throws -> Setter {
84-
return try Setter(column: column, value: value)
83+
public func <-<V : Value>(column: Expression<V?>, value: V?) -> Setter {
84+
return Setter(column: column, value: value)
8585
}
8686

8787
public func +=(column: Expression<String>, value: Expression<String>) -> Setter {

SQLiteTests/ConnectionPoolTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ConnectionPoolTests : SQLiteTestCase {
1515
pool.foreignKeys = true
1616
pool.setup.append { try $0.execute("CREATE TABLE IF NOT EXISTS test(value INT)") }
1717

18-
XCTAssertTrue(pool.readable.scalar("PRAGMA foreign_keys") as! Int64 == 1)
18+
XCTAssertTrue(try pool.readable.scalar("PRAGMA foreign_keys") as! Int64 == 1)
1919
try! pool.writable.execute("INSERT INTO test(value) VALUES (1)")
2020
try! pool.readable.execute("SELECT value FROM test")
2121
}
@@ -45,10 +45,10 @@ class ConnectionPoolTests : SQLiteTestCase {
4545
let conn = self.pool.readable
4646

4747
let stmt = try! conn.prepare("SELECT name FROM test WHERE id = ?")
48-
var curr = stmt.scalar(x) as! String
48+
var curr = try! stmt.scalar(x) as! String
4949
while !quit {
5050

51-
let now = stmt.scalar(x) as! String
51+
let now = try! stmt.scalar(x) as! String
5252
if now != curr {
5353
//print(now)
5454
curr = now
@@ -96,7 +96,7 @@ class ConnectionPoolTests : SQLiteTestCase {
9696

9797
while !finished {
9898

99-
let val = self.pool.readable.scalar("SELECT value FROM test")
99+
let val = try! self.pool.readable.scalar("SELECT value FROM test")
100100
assert(val != nil, "DB query returned nil result set")
101101

102102
}

SQLiteTests/ConnectionTests.swift

Lines changed: 18 additions & 18 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, 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)
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)
9494
AssertSQL("SELECT count(*) FROM users WHERE admin = 0", 4)
9595
}
9696

@@ -238,7 +238,7 @@ class ConnectionTests : SQLiteTestCase {
238238
try! db.transaction {_ in
239239
try self.InsertUser("alice")
240240
}
241-
XCTAssertEqual(1, db.scalar("SELECT count(*) FROM users") as? Int64)
241+
XCTAssertEqual(1, try! 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, db.scalar("SELECT count(*) FROM users") as? Int64)
255+
XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users") as? Int64)
256256
}
257257
}
258258

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

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

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

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

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

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

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

303303
func test_interrupt_interruptsLongRunningQuery() {
304304
try! InsertUsers("abcdefghijklmnopqrstuvwxyz".characters.map { String($0) })
305-
db.createFunction("sleep") { args in
305+
try! db.createFunction("sleep") { args in
306306
usleep(UInt32((args[0] as? Double ?? Double(args[0] as? Int64 ?? 1)) * 1_000_000))
307307
return nil
308308
}

SQLiteTests/CoreFunctionsTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class CoreFunctionsTests : XCTestCase {
129129
}
130130

131131
func test_contains_buildsExpressionWithInOperator() {
132-
AssertSQL("(\"string\" IN ('hello', 'world'))", ["hello", "world"].contains(string))
133-
AssertSQL("(\"stringOptional\" IN ('hello', 'world'))", ["hello", "world"].contains(stringOptional))
132+
AssertSQL("(\"string\" IN ('hello', 'world'))", try ["hello", "world"].contains(string))
133+
AssertSQL("(\"stringOptional\" IN ('hello', 'world'))", try ["hello", "world"].contains(stringOptional))
134134
}
135135

136136
}

SQLiteTests/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, db.scalar(emails.filter(emails.match("aun")).count))
74+
XCTAssertEqual(1, try! db.scalar(emails.filter(emails.match("aun")).count))
7575
}
7676

7777
}

SQLiteTests/QueryTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ class QueryIntegrationTests : SQLiteTestCase {
306306
}
307307

308308
func test_scalar() {
309-
XCTAssertEqual(0, db.scalar(users.count))
310-
XCTAssertEqual(false, db.scalar(users.exists))
309+
XCTAssertEqual(0, try db.scalar(users.count))
310+
XCTAssertEqual(false, try db.scalar(users.exists))
311311

312312
try! InsertUsers("alice")
313-
XCTAssertEqual(1, db.scalar(users.select(id.average)))
313+
XCTAssertEqual(1, try db.scalar(users.select(id.average)))
314314
}
315315

316316
func test_pluck() {
317317
let rowid = try! db.run(users.insert(email <- "[email protected]"))
318-
XCTAssertEqual(rowid, db.pluck(users)![id])
318+
XCTAssertEqual(rowid, try! db.pluck(users)![id])
319319
}
320320

321321
func test_insert() {

0 commit comments

Comments
 (0)