Skip to content

Commit 709d137

Browse files
committed
Changed scalar functions to throw errors
1 parent ccf4392 commit 709d137

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

SQLite/Core/Connection.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ public final class Connection {
231231
/// - bindings: A list of parameters to bind to the statement.
232232
///
233233
/// - Returns: The first value of the first row returned.
234-
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) -> Binding? {
235-
return scalar(statement, bindings)
234+
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? {
235+
return try scalar(statement, bindings)
236236
}
237237

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

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

266266
// MARK: - Transactions
@@ -551,11 +551,11 @@ public final class Connection {
551551
///
552552
/// - block: A collation function that takes two strings and returns the
553553
/// comparison result.
554-
public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) {
554+
public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) throws {
555555
let box: Collation = { lhs, rhs in
556556
Int32(block(lhs: String.fromCString(UnsafePointer<Int8>(lhs))!, rhs: String.fromCString(UnsafePointer<Int8>(rhs))!).rawValue)
557557
}
558-
try! check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
558+
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
559559
unsafeBitCast(callback, Collation.self)(lhs, rhs)
560560
}, nil))
561561
collations[collation] = box

SQLite/Core/Statement.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,30 @@ public final class Statement {
148148
/// - Parameter bindings: A list of parameters to bind to the statement.
149149
///
150150
/// - Returns: The first value of the first row returned.
151-
@warn_unused_result public func scalar(bindings: Binding?...) -> Binding? {
151+
@warn_unused_result public func scalar(bindings: Binding?...) throws -> Binding? {
152152
guard bindings.isEmpty else {
153-
return scalar(bindings)
153+
return try scalar(bindings)
154154
}
155155

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

161161
/// - Parameter bindings: A list of parameters to bind to the statement.
162162
///
163163
/// - Returns: The first value of the first row returned.
164-
@warn_unused_result public func scalar(bindings: [Binding?]) -> Binding? {
165-
return bind(bindings).scalar()
164+
@warn_unused_result public func scalar(bindings: [Binding?]) throws -> Binding? {
165+
return try bind(bindings).scalar()
166166
}
167167

168168

169169
/// - Parameter bindings: A dictionary of named parameters to bind to the
170170
/// statement.
171171
///
172172
/// - Returns: The first value of the first row returned.
173-
@warn_unused_result public func scalar(bindings: [String: Binding?]) -> Binding? {
174-
return bind(bindings).scalar()
173+
@warn_unused_result public func scalar(bindings: [String: Binding?]) throws -> Binding? {
174+
return try bind(bindings).scalar()
175175
}
176176

177177
public func step() throws -> Bool {

SQLite/Typed/Query.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -932,30 +932,30 @@ extension Connection {
932932
}
933933
}
934934

935-
public func scalar<V : Value>(query: ScalarQuery<V>) -> V {
935+
public func scalar<V : Value>(query: ScalarQuery<V>) throws -> V {
936936
let expression = query.expression
937-
return value(scalar(expression.template, expression.bindings))
937+
return value(try scalar(expression.template, expression.bindings))
938938
}
939939

940-
public func scalar<V : Value>(query: ScalarQuery<V?>) -> V.ValueType? {
940+
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 V.fromDatatypeValue(value)
944944
}
945945

946-
public func scalar<V : Value>(query: Select<V>) -> V {
946+
public func scalar<V : Value>(query: Select<V>) throws -> V {
947947
let expression = query.expression
948-
return value(scalar(expression.template, expression.bindings))
948+
return value(try scalar(expression.template, expression.bindings))
949949
}
950950

951-
public func scalar<V : Value>(query: Select<V?>) -> V.ValueType? {
951+
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 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.

SQLiteTests/ConnectionTests.swift

Lines changed: 15 additions & 15 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 {
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,36 +268,36 @@ 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() {
276276
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() {
283283
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() {

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)