Skip to content

Commit

Permalink
Adds multirow insert method (#153)
Browse files Browse the repository at this point in the history
* feat: Adds multirow insert method
  • Loading branch information
NeedleInAJayStack authored Sep 23, 2024
1 parent 3c8c6aa commit e0b35ff
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/SQLKit/Builders/Implementations/SQLInsertBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ public final class SQLInsertBuilder: SQLQueryBuilder, SQLReturningBuilder/*, SQL
self.insert.values.append(values)
return self
}

/// Add multiple sequences of values each inserted as a separate row.
@inlinable
@discardableResult
public func rows<S1, S2>(_ valueSets: S1) -> Self
where S1: Sequence, S2: Sequence,
S1.Element == S2, S2.Element == any SQLExpression
{
valueSets.reduce(self) { $0.values(Array($1)) }
}

/// Specify a `SELECT` query to generate rows to insert.
///
Expand Down
86 changes: 86 additions & 0 deletions Tests/SQLKitTests/SQLInsertUpsertTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,92 @@ final class SQLInsertUpsertTests: XCTestCase {
is: "INSERT INTO ``planets`` (``id``, ``name``) SELECT ``id``, ``name`` FROM ``other_planets``"
)
}

func testInsertValuesEncodable() throws {
// Test variadic values method
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name", "color"])
.values("Jupiter", "orange"),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2)"
)

// Test array values method
XCTAssertSerialization(
of: db.insert(into: "planets")
.columns(["name", "color"])
.values(["Jupiter", "orange"]),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2)"
)

// Test nested array values method
XCTAssertSerialization(
of: db.insert(into: "planets")
.columns(["name", "color"])
.values([["Jupiter", "orange"],["Mars", "red"]]),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2)"
)

// Test multiple values calls make multiple rows
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name", "color"])
.values(["Jupiter", "orange"])
.values(["Mars", "red"]),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2), (&3, &4)"
)

// Test single-value input method
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name"])
.values(["Jupiter"]),
is: "INSERT INTO ``planets`` (``name``) VALUES (&1)"
)
}

func testInsertValuesExpression() throws {
// Test variadic values method
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name", "color"])
.values(SQLBind("Jupiter"), SQLBind("orange")),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2)"
)

// Test array values method
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name", "color"])
.values([SQLBind("Jupiter"), SQLBind("orange")]),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2)"
)

// Test nested array values method
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name", "color"])
.rows([[SQLBind("Jupiter"), SQLBind("orange")],[SQLBind("Mars"), SQLBind("red")]]),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2), (&3, &4)"
)

// Test multiple values calls make multiple rows
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name", "color"])
.values(["Jupiter", "orange"])
.values([SQLBind("Mars"), SQLBind("red")]),
is: "INSERT INTO ``planets`` (``name``, ``color``) VALUES (&1, &2), (&3, &4)"
)

// Test single-value input method
XCTAssertSerialization(
of: self.db.insert(into: "planets")
.columns(["name"])
.values([SQLBind("Jupiter")]),
is: "INSERT INTO ``planets`` (``name``) VALUES (&1)"
)
}

// MARK: - Upsert

Expand Down

0 comments on commit e0b35ff

Please sign in to comment.