Skip to content

Commit 8cc5a6e

Browse files
authored
Merge pull request #398 from JavaNut13/master
Scalar functions should throw errors
2 parents a66d0fa + 5aed9d6 commit 8cc5a6e

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
@@ -236,8 +236,8 @@ public final class Connection {
236236
/// - bindings: A list of parameters to bind to the statement.
237237
///
238238
/// - Returns: The first value of the first row returned.
239-
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) -> Binding? {
240-
return scalar(statement, bindings)
239+
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? {
240+
return try scalar(statement, bindings)
241241
}
242242

243243
/// Runs a single SQL statement (with optional parameter bindings),
@@ -250,8 +250,8 @@ public final class Connection {
250250
/// - bindings: A list of parameters to bind to the statement.
251251
///
252252
/// - Returns: The first value of the first row returned.
253-
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) -> Binding? {
254-
return try! prepare(statement).scalar(bindings)
253+
@warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding? {
254+
return try prepare(statement).scalar(bindings)
255255
}
256256

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

271271
// MARK: - Transactions
@@ -556,11 +556,11 @@ public final class Connection {
556556
///
557557
/// - block: A collation function that takes two strings and returns the
558558
/// comparison result.
559-
public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) {
559+
public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) throws {
560560
let box: Collation = { lhs, rhs in
561561
Int32(block(lhs: String.fromCString(UnsafePointer<Int8>(lhs))!, rhs: String.fromCString(UnsafePointer<Int8>(rhs))!).rawValue)
562562
}
563-
try! check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
563+
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
564564
unsafeBitCast(callback, Collation.self)(lhs, rhs)
565565
}, nil))
566566
collations[collation] = box

SQLite/Core/Statement.swift

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

160160
reset(clearBindings: false)
161-
try! step()
161+
try step()
162162
return row[0]
163163
}
164164

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

172172

173173
/// - Parameter bindings: A dictionary of named parameters to bind to the
174174
/// statement.
175175
///
176176
/// - Returns: The first value of the first row returned.
177-
@warn_unused_result public func scalar(bindings: [String: Binding?]) -> Binding? {
178-
return bind(bindings).scalar()
177+
@warn_unused_result public func scalar(bindings: [String: Binding?]) throws -> Binding? {
178+
return try bind(bindings).scalar()
179179
}
180180

181181
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
@@ -202,7 +202,7 @@ class FTS4IntegrationTests : SQLiteTestCase {
202202
AssertSQL("CREATE VIRTUAL TABLE \"emails\" USING fts4(\"subject\", \"body\", tokenize=\"SQLite.swift\" \"tokenizer\")")
203203

204204
try! db.run(emails.insert(subject <- "Aún más cáfe!"))
205-
XCTAssertEqual(1, db.scalar(emails.filter(emails.match("aun")).count))
205+
XCTAssertEqual(1, try! db.scalar(emails.filter(emails.match("aun")).count))
206206
}
207207
#endif
208208
}

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)