Skip to content

Commit 2bbdf2c

Browse files
committed
Add SQLite.Values typealias
It represents a dictionary of values, you know, `[String: Datatype?]`. Handy for preparing inserts, updates and the like: let alice: Values = ["email": "[email protected]"] users.insert(alice) Signed-off-by: Stephen Celis <[email protected]>
1 parent 91ac404 commit 2bbdf2c

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

SQLite Common/Query.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
// THE SOFTWARE.
2222
//
2323

24+
/// A dictionary mapping column names to values.
25+
public typealias Values = [String: Datatype?]
26+
2427
/// A query object. Used to build SQL statements with a collection of chainable
2528
/// helper functions.
2629
public struct Query {
@@ -316,7 +319,7 @@ public struct Query {
316319
return database.prepare(Swift.join(" ", parts), bindings)
317320
}
318321

319-
private func insertStatement(values: [String: Datatype?]) -> Statement {
322+
private func insertStatement(values: Values) -> Statement {
320323
var (parts, bindings) = (["INSERT INTO \(tableName)"], self.bindings)
321324
let valuesClause = Swift.join(", ", map(values) { columnName, value in
322325
bindings.append(value)
@@ -327,7 +330,7 @@ public struct Query {
327330
return database.prepare(Swift.join(" ", parts), bindings)
328331
}
329332

330-
private func updateStatement(values: [String: Datatype?]) -> Statement {
333+
private func updateStatement(values: Values) -> Statement {
331334
var (parts, bindings) = (["UPDATE \(tableName)"], [Datatype?]())
332335
let valuesClause = Swift.join(", ", map(values) { columnName, value in
333336
bindings.append(value)
@@ -385,13 +388,11 @@ public struct Query {
385388

386389
// MARK: - Array
387390

388-
public typealias Element = [String: Datatype?]
389-
390391
/// The first result (or nil if the query has no results).
391-
public var first: Element? { return limit(1).generate().next() }
392+
public var first: Values? { return limit(1).generate().next() }
392393

393394
/// The last result (or nil if the query has no results).
394-
public var last: Element? { return reverse(self).first }
395+
public var last: Values? { return reverse(self).first }
395396

396397
/// Returns true if the query has no results.
397398
public var isEmpty: Bool { return first == nil }
@@ -403,7 +404,7 @@ public struct Query {
403404
/// :param: values A dictionary of column names to values.
404405
///
405406
/// :returns: The statement.
406-
public func insert(values: [String: Datatype?]) -> Statement {
407+
public func insert(values: Values) -> Statement {
407408
return insert(values).statement
408409
}
409410

@@ -412,7 +413,7 @@ public struct Query {
412413
/// :param: values A dictionary of column names to values.
413414
///
414415
/// :returns: The row ID.
415-
public func insert(values: [String: Datatype?]) -> Int? {
416+
public func insert(values: Values) -> Int? {
416417
return insert(values).ID
417418
}
418419

@@ -421,7 +422,7 @@ public struct Query {
421422
/// :param: values A dictionary of column names to values.
422423
///
423424
/// :returns: The row ID and statement.
424-
public func insert(values: [String: Datatype?]) -> (ID: Int?, statement: Statement) {
425+
public func insert(values: Values) -> (ID: Int?, statement: Statement) {
425426
let statement = insertStatement(values).run()
426427
return (statement.failed ? nil : database.lastID, statement)
427428
}
@@ -431,7 +432,7 @@ public struct Query {
431432
/// :param: values A dictionary of column names to values.
432433
///
433434
/// :returns: The statement.
434-
public func update(values: [String: Datatype?]) -> Statement {
435+
public func update(values: Values) -> Statement {
435436
return update(values).statement
436437
}
437438

@@ -440,7 +441,7 @@ public struct Query {
440441
/// :param: values A dictionary of column names to values.
441442
///
442443
/// :returns: The number of updated rows.
443-
public func update(values: [String: Datatype?]) -> Int {
444+
public func update(values: Values) -> Int {
444445
return update(values).changes
445446
}
446447

@@ -449,7 +450,7 @@ public struct Query {
449450
/// :param: values A dictionary of column names to values.
450451
///
451452
/// :returns: The number of updated rows and statement.
452-
public func update(values: [String: Datatype?]) -> (changes: Int, statement: Statement) {
453+
public func update(values: Values) -> (changes: Int, statement: Statement) {
453454
let statement = updateStatement(values).run()
454455
return (statement.failed ? 0 : database.lastChanges ?? 0, statement)
455456
}
@@ -552,13 +553,11 @@ extension Query: SequenceType {
552553
// MARK: - GeneratorType
553554
public struct QueryGenerator: GeneratorType {
554555

555-
public typealias Element = [String: Datatype?]
556-
557556
private var statement: Statement
558557

559558
private init(_ statement: Statement) { self.statement = statement }
560559

561-
public func next() -> Element? {
560+
public func next() -> Values? {
562561
statement.next()
563562
return statement.values
564563
}

0 commit comments

Comments
 (0)