Skip to content

Scalar functions should throw errors #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions SQLite/Core/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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<Int8>(lhs))!, rhs: String.fromCString(UnsafePointer<Int8>(rhs))!).rawValue)
}
try! check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
try check(sqlite3_create_collation_v2(handle, collation, SQLITE_UTF8, unsafeBitCast(box, UnsafeMutablePointer<Void>.self), { callback, _, lhs, _, rhs in
unsafeBitCast(callback, Collation.self)(lhs, rhs)
}, nil))
collations[collation] = box
Expand Down
14 changes: 7 additions & 7 deletions SQLite/Core/Statement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,30 @@ 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()
}


/// - Parameter 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(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 {
Expand Down
20 changes: 10 additions & 10 deletions SQLite/Typed/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -932,30 +932,30 @@ extension Connection {
}
}

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

public func scalar<V : Value>(query: ScalarQuery<V?>) -> V.ValueType? {
public func scalar<V : Value>(query: ScalarQuery<V?>) 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<V : Value>(query: Select<V>) -> V {
public func scalar<V : Value>(query: Select<V>) throws -> V {
let expression = query.expression
return value(scalar(expression.template, expression.bindings))
return value(try scalar(expression.template, expression.bindings))
}

public func scalar<V : Value>(query: Select<V?>) -> V.ValueType? {
public func scalar<V : Value>(query: Select<V?>) 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.
Expand Down
30 changes: 15 additions & 15 deletions SQLiteTests/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion SQLiteTests/FTS4Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions SQLiteTests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 <- "[email protected]"))
XCTAssertEqual(rowid, db.pluck(users)![id])
XCTAssertEqual(rowid, try! db.pluck(users)![id])
}

func test_insert() {
Expand Down