Skip to content

Commit 08abe3e

Browse files
committed
propagate errors in Statement initialization
1 parent da2d434 commit 08abe3e

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

Source/Core/Connection.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ public final class Connection {
140140
/// - bindings: A list of parameters to bind to the statement.
141141
///
142142
/// - Returns: A prepared statement.
143-
@warn_unused_result public func prepare(statement: String, _ bindings: Binding?...) -> Statement {
144-
if !bindings.isEmpty { return prepare(statement, bindings) }
145-
return Statement(self, statement)
143+
@warn_unused_result public func prepare(statement: String, _ bindings: Binding?...) throws -> Statement {
144+
if !bindings.isEmpty { return try prepare(statement, bindings) }
145+
return try Statement(self, statement)
146146
}
147147

148148
/// Prepares a single SQL statement and binds parameters to it.
@@ -154,8 +154,8 @@ public final class Connection {
154154
/// - bindings: A list of parameters to bind to the statement.
155155
///
156156
/// - Returns: A prepared statement.
157-
@warn_unused_result public func prepare(statement: String, _ bindings: [Binding?]) -> Statement {
158-
return prepare(statement).bind(bindings)
157+
@warn_unused_result public func prepare(statement: String, _ bindings: [Binding?]) throws -> Statement {
158+
return try prepare(statement).bind(bindings)
159159
}
160160

161161
/// Prepares a single SQL statement and binds parameters to it.
@@ -167,8 +167,8 @@ public final class Connection {
167167
/// - bindings: A dictionary of named parameters to bind to the statement.
168168
///
169169
/// - Returns: A prepared statement.
170-
@warn_unused_result public func prepare(statement: String, _ bindings: [String: Binding?]) -> Statement {
171-
return prepare(statement).bind(bindings)
170+
@warn_unused_result public func prepare(statement: String, _ bindings: [String: Binding?]) throws -> Statement {
171+
return try prepare(statement).bind(bindings)
172172
}
173173

174174
// MARK: - Run
@@ -230,8 +230,8 @@ public final class Connection {
230230
/// - bindings: A list of parameters to bind to the statement.
231231
///
232232
/// - Returns: The first value of the first row returned.
233-
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) -> Binding? {
234-
return scalar(statement, bindings)
233+
@warn_unused_result public func scalar(statement: String, _ bindings: Binding?...) throws -> Binding? {
234+
return try scalar(statement, bindings)
235235
}
236236

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

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

265265
// MARK: - Transactions

Source/Core/Statement.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public final class Statement {
2929

3030
private let connection: Connection
3131

32-
init(_ connection: Connection, _ SQL: String) {
32+
init(_ connection: Connection, _ SQL: String) throws {
3333
self.connection = connection
34-
try! connection.check(sqlite3_prepare_v2(connection.handle, SQL, -1, &handle, nil))
34+
try connection.check(sqlite3_prepare_v2(connection.handle, SQL, -1, &handle, nil))
3535
}
3636

3737
deinit {

Source/Typed/Query.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -857,23 +857,23 @@ public struct Delete : ExpressionType {
857857

858858
extension Connection {
859859

860-
public func prepare(query: QueryType) -> AnySequence<Row> {
860+
public func prepare(query: QueryType) throws -> AnySequence<Row> {
861861
let expression = query.expression
862-
let statement = prepare(expression.template, expression.bindings)
862+
let statement = try prepare(expression.template, expression.bindings)
863863

864-
let columnNames: [String: Int] = {
864+
let columnNames: [String: Int] = try {
865865
var (columnNames, idx) = ([String: Int](), 0)
866866
column: for each in query.clauses.select.columns ?? [Expression<Void>(literal: "*")] {
867867
var names = each.expression.template.characters.split { $0 == "." }.map(String.init)
868868
let column = names.removeLast()
869869
let namespace = names.joinWithSeparator(".")
870870

871-
func expandGlob(namespace: Bool) -> QueryType -> Void {
872-
return { query in
871+
func expandGlob(namespace: Bool) -> (QueryType throws -> Void) {
872+
return { (query: QueryType) throws -> (Void) in
873873
var q = query.dynamicType.init(query.clauses.from.name, database: query.clauses.from.database)
874874
q.clauses.select = query.clauses.select
875875
let e = q.expression
876-
var names = self.prepare(e.template, e.bindings).columnNames.map { $0.quote() }
876+
var names = try self.prepare(e.template, e.bindings).columnNames.map { $0.quote() }
877877
if namespace { names = names.map { "\(query.tableName().expression.template).\($0)" } }
878878
for name in names { columnNames[name] = idx++ }
879879
}
@@ -886,14 +886,14 @@ extension Connection {
886886
if !namespace.isEmpty {
887887
for q in queries {
888888
if q.tableName().expression.template == namespace {
889-
expandGlob(true)(q)
889+
try expandGlob(true)(q)
890890
continue column
891891
}
892892
}
893893
fatalError("no such table: \(namespace)")
894894
}
895895
for q in queries {
896-
expandGlob(query.clauses.join.count > 0)(q)
896+
try expandGlob(query.clauses.join.count > 0)(q)
897897
}
898898
continue
899899
}
@@ -908,30 +908,30 @@ extension Connection {
908908
}
909909
}
910910

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

916-
public func scalar<V : Value>(query: ScalarQuery<V?>) -> V.ValueType? {
916+
public func scalar<V : Value>(query: ScalarQuery<V?>) throws -> V.ValueType? {
917917
let expression = query.expression
918-
guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
918+
guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
919919
return V.fromDatatypeValue(value)
920920
}
921921

922-
public func scalar<V : Value>(query: Select<V>) -> V {
922+
public func scalar<V : Value>(query: Select<V>) throws -> V {
923923
let expression = query.expression
924-
return value(scalar(expression.template, expression.bindings))
924+
return value(try scalar(expression.template, expression.bindings))
925925
}
926926

927-
public func scalar<V : Value>(query: Select<V?>) -> V.ValueType? {
927+
public func scalar<V : Value>(query: Select<V?>) throws -> V.ValueType? {
928928
let expression = query.expression
929-
guard let value = scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
929+
guard let value = try scalar(expression.template, expression.bindings) as? V.Datatype else { return nil }
930930
return V.fromDatatypeValue(value)
931931
}
932932

933-
public func pluck(query: QueryType) -> Row? {
934-
return prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
933+
public func pluck(query: QueryType) throws -> Row? {
934+
return try prepare(query.limit(1, query.clauses.limit?.offset)).generate().next()
935935
}
936936

937937
/// Runs an `Insert` query.

0 commit comments

Comments
 (0)