diff --git a/SQLite/Core/Connection.swift b/SQLite/Core/Connection.swift index 866ed1bf..04fdd901 100644 --- a/SQLite/Core/Connection.swift +++ b/SQLite/Core/Connection.swift @@ -236,8 +236,8 @@ public final class Connection { /// - bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) -> Binding? { - return scalar(statement, bindings) + @warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? { + return try scalar(statement, bindings) } /// Runs a single SQL statement (with optional parameter bindings), @@ -250,8 +250,8 @@ public final class Connection { /// - bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) -> Binding? { - return try! prepare(statement).scalar(bindings) + @warn_unused_result public func scalar(statement: String, _ bindings: [Binding?]) throws -> Binding? { + return try prepare(statement).scalar(bindings) } /// Runs a single SQL statement (with optional parameter bindings), @@ -264,8 +264,8 @@ public final class Connection { /// - bindings: A dictionary of named parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) -> Binding? { - return try! prepare(statement).scalar(bindings) + @warn_unused_result public func scalar(statement: String, _ bindings: [String: Binding?]) throws -> Binding? { + return try prepare(statement).scalar(bindings) } // MARK: - Transactions @@ -556,11 +556,11 @@ public final class Connection { /// /// - block: A collation function that takes two strings and returns the /// comparison result. - public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) { + public func createCollation(collation: String, _ block: (lhs: String, rhs: String) -> ComparisonResult) throws { let box: Collation = { lhs, rhs in Int32(block(lhs: String.fromCString(UnsafePointer(lhs))!, rhs: String.fromCString(UnsafePointer(rhs))!).rawValue) } - try! check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer.self), { callback, _, lhs, _, rhs in + try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer.self), { callback, _, lhs, _, rhs in unsafeBitCast(callback, Collation.self)(lhs, rhs) }, nil)) collations[collation] = box diff --git a/SQLite/Core/Statement.swift b/SQLite/Core/Statement.swift index 9a4bfa1e..5e172b68 100644 --- a/SQLite/Core/Statement.swift +++ b/SQLite/Core/Statement.swift @@ -152,21 +152,21 @@ public final class Statement { /// - Parameter bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(bindings: Binding?...) -> Binding? { + @warn_unused_result public func scalar(bindings: Binding?...) throws -> Binding? { guard bindings.isEmpty else { - return scalar(bindings) + return try scalar(bindings) } reset(clearBindings: false) - try! step() + try step() return row[0] } /// - Parameter bindings: A list of parameters to bind to the statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(bindings: [Binding?]) -> Binding? { - return bind(bindings).scalar() + @warn_unused_result public func scalar(bindings: [Binding?]) throws -> Binding? { + return try bind(bindings).scalar() } @@ -174,8 +174,8 @@ public final class Statement { /// statement. /// /// - Returns: The first value of the first row returned. - @warn_unused_result public func scalar(bindings: [String: Binding?]) -> Binding? { - return bind(bindings).scalar() + @warn_unused_result public func scalar(bindings: [String: Binding?]) throws -> Binding? { + return try bind(bindings).scalar() } public func step() throws -> Bool { diff --git a/SQLite/Typed/Query.swift b/SQLite/Typed/Query.swift index e45034ca..36b3b2e1 100644 --- a/SQLite/Typed/Query.swift +++ b/SQLite/Typed/Query.swift @@ -932,30 +932,30 @@ extension Connection { } } - public func scalar(query: ScalarQuery) -> V { + public func scalar(query: ScalarQuery) throws -> V { let expression = query.expression - return value(scalar(expression.template, expression.bindings)) + return value(try scalar(expression.template, expression.bindings)) } - public func scalar(query: ScalarQuery) -> V.ValueType? { + public func scalar(query: ScalarQuery) throws -> V.ValueType? { let expression = query.expression - guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil } + guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil } return V.fromDatatypeValue(value) } - public func scalar(query: Select) -> V { + public func scalar(query: Select) throws -> V { let expression = query.expression - return value(scalar(expression.template, expression.bindings)) + return value(try scalar(expression.template, expression.bindings)) } - public func scalar(query: Select) -> V.ValueType? { + public func scalar(query: Select) throws -> V.ValueType? { let expression = query.expression - guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil } + guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil } return V.fromDatatypeValue(value) } - public func pluck(query: QueryType) -> Row? { - return try! prepare(query.limit(1, query.clauses.limit?.offset)).generate().next() + public func pluck(query: QueryType) throws -> Row? { + return try prepare(query.limit(1, query.clauses.limit?.offset)).generate().next() } /// Runs an `Insert` query. diff --git a/SQLiteTests/ConnectionTests.swift b/SQLiteTests/ConnectionTests.swift index aeec9b72..3d7dd3eb 100644 --- a/SQLiteTests/ConnectionTests.swift +++ b/SQLiteTests/ConnectionTests.swift @@ -87,10 +87,10 @@ class ConnectionTests : SQLiteTestCase { } func test_scalar_preparesRunsAndReturnsScalarValues() { - XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = 0") as? Int64) - XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = ?", 0) as? Int64) - XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = ?", [0]) as? Int64) - XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users WHERE admin = $admin", ["$admin": 0]) as? Int64) + XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = 0") as? Int64) + XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = ?", 0) as? Int64) + XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = ?", [0]) as? Int64) + XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users WHERE admin = $admin", ["$admin": 0]) as? Int64) AssertSQL("SELECT count(*) FROM users WHERE admin = 0", 4) } @@ -238,7 +238,7 @@ class ConnectionTests : SQLiteTestCase { try! db.transaction { try self.InsertUser("alice") } - XCTAssertEqual(1, db.scalar("SELECT count(*) FROM users") as? Int64) + XCTAssertEqual(1, try! db.scalar("SELECT count(*) FROM users") as? Int64) } } @@ -252,7 +252,7 @@ class ConnectionTests : SQLiteTestCase { } } catch { } - XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users") as? Int64) + XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users") as? Int64) } } @@ -268,36 +268,36 @@ class ConnectionTests : SQLiteTestCase { } } catch { } - XCTAssertEqual(0, db.scalar("SELECT count(*) FROM users") as? Int64) + XCTAssertEqual(0, try! db.scalar("SELECT count(*) FROM users") as? Int64) } } func test_createFunction_withArrayArguments() { db.createFunction("hello") { $0[0].map { "Hello, \($0)!" } } - XCTAssertEqual("Hello, world!", db.scalar("SELECT hello('world')") as? String) - XCTAssert(db.scalar("SELECT hello(NULL)") == nil) + XCTAssertEqual("Hello, world!", try! db.scalar("SELECT hello('world')") as? String) + XCTAssert(try! db.scalar("SELECT hello(NULL)") == nil) } func test_createFunction_createsQuotableFunction() { db.createFunction("hello world") { $0[0].map { "Hello, \($0)!" } } - XCTAssertEqual("Hello, world!", db.scalar("SELECT \"hello world\"('world')") as? String) - XCTAssert(db.scalar("SELECT \"hello world\"(NULL)") == nil) + XCTAssertEqual("Hello, world!", try! db.scalar("SELECT \"hello world\"('world')") as? String) + XCTAssert(try! db.scalar("SELECT \"hello world\"(NULL)") == nil) } func test_createCollation_createsCollation() { - db.createCollation("NODIACRITIC") { lhs, rhs in + try! db.createCollation("NODIACRITIC") { lhs, rhs in return lhs.compare(rhs, options: .DiacriticInsensitiveSearch) } - XCTAssertEqual(1, db.scalar("SELECT ? = ? COLLATE NODIACRITIC", "cafe", "café") as? Int64) + XCTAssertEqual(1, try! db.scalar("SELECT ? = ? COLLATE NODIACRITIC", "cafe", "café") as? Int64) } func test_createCollation_createsQuotableCollation() { - db.createCollation("NO DIACRITIC") { lhs, rhs in + try! db.createCollation("NO DIACRITIC") { lhs, rhs in return lhs.compare(rhs, options: .DiacriticInsensitiveSearch) } - XCTAssertEqual(1, db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as? Int64) + XCTAssertEqual(1, try! db.scalar("SELECT ? = ? COLLATE \"NO DIACRITIC\"", "cafe", "café") as? Int64) } func test_interrupt_interruptsLongRunningQuery() { diff --git a/SQLiteTests/FTS4Tests.swift b/SQLiteTests/FTS4Tests.swift index c1071972..8b4e4f96 100644 --- a/SQLiteTests/FTS4Tests.swift +++ b/SQLiteTests/FTS4Tests.swift @@ -202,7 +202,7 @@ class FTS4IntegrationTests : SQLiteTestCase { AssertSQL("CREATE VIRTUAL TABLE \"emails\" USING fts4(\"subject\", \"body\", tokenize=\"SQLite.swift\" \"tokenizer\")") try! db.run(emails.insert(subject <- "Aún más cáfe!")) - XCTAssertEqual(1, db.scalar(emails.filter(emails.match("aun")).count)) + XCTAssertEqual(1, try! db.scalar(emails.filter(emails.match("aun")).count)) } #endif } diff --git a/SQLiteTests/QueryTests.swift b/SQLiteTests/QueryTests.swift index b76da61e..f584bfcf 100644 --- a/SQLiteTests/QueryTests.swift +++ b/SQLiteTests/QueryTests.swift @@ -306,16 +306,16 @@ class QueryIntegrationTests : SQLiteTestCase { } func test_scalar() { - XCTAssertEqual(0, db.scalar(users.count)) - XCTAssertEqual(false, db.scalar(users.exists)) + XCTAssertEqual(0, try! db.scalar(users.count)) + XCTAssertEqual(false, try! db.scalar(users.exists)) try! InsertUsers("alice") - XCTAssertEqual(1, db.scalar(users.select(id.average))) + XCTAssertEqual(1, try! db.scalar(users.select(id.average))) } func test_pluck() { let rowid = try! db.run(users.insert(email <- "alice@example.com")) - XCTAssertEqual(rowid, db.pluck(users)![id]) + XCTAssertEqual(rowid, try! db.pluck(users)![id]) } func test_insert() {