Skip to content

Commit 14573e4

Browse files
committed
Remove replace(...) in favor of insert(or:...)
It's more flexible to expose all the ON CONFLICT actions. Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent 38a585c commit 14573e4

File tree

3 files changed

+30
-53
lines changed

3 files changed

+30
-53
lines changed

Documentation/Index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ We can insert rows into a table by calling a [query’s](#queries) `insert` func
398398
``` swift
399399
users.insert(email <- "alice@mac.com", name <- "Alice")?
400400
// INSERT INTO "users" ("email", "name") VALUES ('alice@mac.com', 'Alice')
401+
402+
users.insert(or: .Replace, email <- "alice@mac.com", name <- "Alice B.")
403+
// INSERT OR REPLACE INTO "users" ("email", "name") VALUES ('alice@mac.com', 'Alice B.')
401404
```
402405

403406
The `insert` function can return several different types that are useful in different contexts.

SQLite Tests/QueryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ class QueryTests: SQLiteTestCase {
328328
}
329329

330330
func test_replace_replaceRows() {
331-
XCTAssertEqual(1, users.replace(email <- "alice@example.com", age <- 30).rowid!)
331+
XCTAssertEqual(1, users.insert(or: .Replace, email <- "alice@example.com", age <- 30).rowid!)
332332
AssertSQL("INSERT OR REPLACE INTO \"users\" (\"email\", \"age\") VALUES ('alice@example.com', 30)")
333333

334-
XCTAssertEqual(1, users.replace(id <- 1, email <- "betty@example.com", age <- 30).rowid!)
334+
XCTAssertEqual(1, users.insert(or: .Replace, id <- 1, email <- "betty@example.com", age <- 30).rowid!)
335335
AssertSQL("INSERT OR REPLACE INTO \"users\" (\"id\", \"email\", \"age\") VALUES (1, 'betty@example.com', 30)")
336336
}
337337

SQLite/Query.swift

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public struct Query {
362362

363363
}
364364

365-
private func insertStatement(values: [Setter], or: OnConflict? = nil) -> Statement {
365+
private func insertStatement(or: OnConflict? = nil, _ values: [Setter]) -> Statement {
366366
var insertClause = "INSERT"
367367
if let or = or { insertClause = "\(insertClause) OR \(or.rawValue)" }
368368
var expressions: [Expressible] = [Expression<()>(literal: "\(insertClause) INTO \(tableName.unaliased.SQL)")]
@@ -434,41 +434,55 @@ public struct Query {
434434

435435
/// Runs an INSERT statement against the query.
436436
///
437-
/// :param: values A list of values to set.
437+
/// :param: action An action to run in case of a conflict.
438+
/// :param: value A value to set.
439+
/// :param: more A list of additional values to set.
438440
///
439441
/// :returns: The statement.
440-
public func insert(value: Setter, _ more: Setter...) -> Statement { return insert([value] + more).statement }
442+
public func insert(or action: OnConflict? = nil, _ value: Setter, _ more: Setter...) -> Statement {
443+
return insert([value] + more).statement
444+
}
441445

442446
/// Runs an INSERT statement against the query.
443447
///
444-
/// :param: values A list of values to set.
448+
/// :param: action An action to run in case of a conflict.
449+
/// :param: value A value to set.
450+
/// :param: more A list of additional values to set.
445451
///
446452
/// :returns: The rowid.
447-
public func insert(value: Setter, _ more: Setter...) -> Int64? { return insert([value] + more).rowid }
453+
public func insert(or action: OnConflict? = nil, _ value: Setter, _ more: Setter...) -> Int64? {
454+
return insert(or: action, [value] + more).rowid
455+
}
448456

449457
/// Runs an INSERT statement against the query.
450458
///
451-
/// :param: values A list of values to set.
459+
/// :param: action An action to run in case of a conflict.
460+
/// :param: value A value to set.
461+
/// :param: more A list of additional values to set.
452462
///
453463
/// :returns: The rowid and statement.
454-
public func insert(value: Setter, _ more: Setter...) -> (rowid: Int64?, statement: Statement) {
455-
return insert([value] + more)
464+
public func insert(or action: OnConflict? = nil, _ value: Setter, _ more: Setter...) -> (rowid: Int64?, statement: Statement) {
465+
return insert(or: action, [value] + more)
456466
}
457467

458468
/// Runs an INSERT statement against the query.
459469
///
470+
/// :param: action An action to run in case of a conflict.
460471
/// :param: values An array of values to set.
461472
///
462473
/// :returns: The rowid.
463-
public func insert(values: [Setter]) -> Int64? { return insert(values).rowid }
474+
public func insert(or action: OnConflict? = nil, _ values: [Setter]) -> Int64? {
475+
return insert(or: action, values).rowid
476+
}
464477

465478
/// Runs an INSERT statement against the query.
466479
///
480+
/// :param: action An action to run in case of a conflict.
467481
/// :param: values An array of values to set.
468482
///
469483
/// :returns: The rowid and statement.
470-
public func insert(values: [Setter]) -> (rowid: Int64?, statement: Statement) {
471-
let statement = insertStatement(values).run()
484+
public func insert(or action: OnConflict? = nil, _ values: [Setter]) -> (rowid: Int64?, statement: Statement) {
485+
let statement = insertStatement(or: action, values).run()
472486
return (statement.failed ? nil : database.lastInsertRowid, statement)
473487
}
474488

@@ -491,46 +505,6 @@ public struct Query {
491505
return (statement.failed ? nil : database.lastInsertRowid, statement)
492506
}
493507

494-
/// Runs a REPLACE statement against the query.
495-
///
496-
/// :param: values A list of values to set.
497-
///
498-
/// :returns: The statement.
499-
public func replace(values: Setter...) -> Statement { return replace(values).statement }
500-
501-
/// Runs a REPLACE statement against the query.
502-
///
503-
/// :param: values A list of values to set.
504-
///
505-
/// :returns: The rowid.
506-
public func replace(values: Setter...) -> Int64? { return replace(values).rowid }
507-
508-
/// Runs a REPLACE statement against the query.
509-
///
510-
/// :param: values A list of values to set.
511-
///
512-
/// :returns: The rowid and statement.
513-
public func replace(values: Setter...) -> (rowid: Int64?, statement: Statement) {
514-
return replace(values)
515-
}
516-
517-
/// Runs a REPLACE statement against the query.
518-
///
519-
/// :param: values An array of values to set.
520-
///
521-
/// :returns: The rowid.
522-
public func replace(values: [Setter]) -> Int64? { return replace(values).rowid }
523-
524-
/// Runs a REPLACE statement against the query.
525-
///
526-
/// :param: values An array of values to set.
527-
///
528-
/// :returns: The rowid and statement.
529-
public func replace(values: [Setter]) -> (rowid: Int64?, statement: Statement) {
530-
let statement = insertStatement(values, or: .Replace).run()
531-
return (statement.failed ? nil : database.lastInsertRowid, statement)
532-
}
533-
534508
/// Runs an UPDATE statement against the query.
535509
///
536510
/// :param: values A list of values to set.

0 commit comments

Comments
 (0)