Skip to content

Commit 4e10c03

Browse files
committed
Fixed swift gen not getting array from input
1 parent 383a4fc commit 4e10c03

6 files changed

Lines changed: 100 additions & 9 deletions

File tree

Sources/Compiler/Gen/Language.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension Language {
108108
return text.description
109109
case let .rowParam(param):
110110
let qs = interpolatedQuestionMarks(
111-
for: statement.parameters.count > 1 ? param.name : "input"
111+
for: statement.parameters.count > 1 ? "input.\(param.name)" : "input"
112112
)
113113
return "(\(qs))"
114114
}
@@ -173,8 +173,8 @@ extension Language {
173173
)
174174
}
175175
case .array(let values):
176-
result.append(.arrayStart(name: name ?? "input", elementName: "element"))
177-
result.append(contentsOf: bindings(for: values, index: &index, owner: "element"))
176+
result.append(.arrayStart(name: name ?? "input", owner: owner, elementName: "element"))
177+
result.append(contentsOf: bindings(for: values, index: &index, name: "element"))
178178
result.append(.arrayEnd)
179179
case .encoded(let storage, _, let adapter):
180180
result.append(
@@ -448,7 +448,7 @@ public struct GeneratedQuery {
448448
isOptional: Bool = false,
449449
adapter: (adapter: AdapterReference, storage: String)? = nil
450450
)
451-
case arrayStart(name: String, elementName: String)
451+
case arrayStart(name: String, owner: String?, elementName: String)
452452
case arrayEnd
453453
}
454454
}

Sources/Compiler/Gen/SwiftLanguage.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,12 @@ public struct SwiftLanguage: Language {
775775
}
776776

777777
writer.write(")")
778-
case let .arrayStart(name, elementName):
779-
writer.write(line: "for ", elementName, " in ", name, " {")
778+
case let .arrayStart(name, owner, elementName):
779+
writer.write(line: "for ", elementName, " in ")
780+
if let owner {
781+
writer.write(owner, ".")
782+
}
783+
writer.write(name, " {")
780784
writer.indent()
781785
case .arrayEnd:
782786
writer.unindent()

Tests/CompilerTests/Compiler/CompileInsert.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,24 @@ WHERE excluded.name = 'bob';
167167
-- CHECK: TABLES
168168
-- CHECK: user
169169
INSERT INTO user (name) VALUES ('joe') RETURNING id;
170+
171+
CREATE TABLE foo (
172+
bar INTEGER,
173+
baz INTEGER,
174+
PRIMARY KEY (bar, baz)
175+
);
176+
177+
-- CHECK: SIGNATURE
178+
-- CHECK: PARAMETERS
179+
-- CHECK: PARAMETER
180+
-- CHECK: TYPE INTEGER?
181+
-- CHECK: INDEX 1
182+
-- CHECK: NAME bar
183+
-- CHECK: PARAMETER
184+
-- CHECK: TYPE INTEGER?
185+
-- CHECK: INDEX 2
186+
-- CHECK: NAME baz
187+
-- CHECK: TABLES
188+
-- CHECK: foo
189+
INSERT INTO foo VALUES (?, ?)
190+
ON CONFLICT (bar, baz) DO NOTHING;

Tests/CompilerTests/CompilerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CompilerTests: XCTestCase {
1919
}
2020

2121
func testInsert() throws {
22-
try checkQueries(compile: "CompileInsert")
22+
try checkQueries(compile: "CompileInsert", dump: true)
2323
}
2424

2525
func testUpdate() throws {

Tests/CompilerTests/Gen/Queries.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ INNER JOIN bar ON foo.intPk = bar.intPk;
2525

2626
selectWithManyInputs:
2727
SELECT * FROM foo WHERE intPk = ? AND textNotNull = ?;
28+
29+
inputIsArray:
30+
DELETE FROM bar WHERE intPk IN ?;
31+
32+
inputContainsArray:
33+
DELETE FROM bar WHERE intPk IN ? AND barNotNullText = ?;

Tests/CompilerTests/Gen/Swift.output

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ struct SelectWithManyInputsInput: Hashable, Sendable {
214214
let textNotNull: String
215215
}
216216

217+
struct InputContainsArrayInput: Hashable, Sendable {
218+
let intPks: [Int]
219+
let barNotNullText: String
220+
}
221+
217222
struct QueriesQueries: ConnectionWrapper, Sendable {
218223
let connection: any Connection
219224
var insertFooReturningFoo: any InsertFooReturningFooQuery
@@ -223,6 +228,8 @@ struct QueriesQueries: ConnectionWrapper, Sendable {
223228
var hasEmbeddedFoo: any HasEmbeddedFooQuery
224229
var bothColumnsShouldNotBeNullable: any BothColumnsShouldNotBeNullableQuery
225230
var selectWithManyInputs: any SelectWithManyInputsQuery
231+
var inputIsArray: any InputIsArrayQuery
232+
var inputContainsArray: any InputContainsArrayQuery
226233

227234
static func noop(
228235
insertFooReturningFoo: any InsertFooReturningFooQuery = Queries.Fail(),
@@ -231,7 +238,9 @@ struct QueriesQueries: ConnectionWrapper, Sendable {
231238
selectSingleFoo: any SelectSingleFooQuery = Queries.Just(),
232239
hasEmbeddedFoo: any HasEmbeddedFooQuery = Queries.Just(),
233240
bothColumnsShouldNotBeNullable: any BothColumnsShouldNotBeNullableQuery = Queries.Just(),
234-
selectWithManyInputs: any SelectWithManyInputsQuery = Queries.Just()
241+
selectWithManyInputs: any SelectWithManyInputsQuery = Queries.Just(),
242+
inputIsArray: any InputIsArrayQuery = Queries.Just(),
243+
inputContainsArray: any InputContainsArrayQuery = Queries.Just()
235244
) -> QueriesQueries {
236245
QueriesQueries(
237246
connection: NoopConnection(),
@@ -241,7 +250,9 @@ struct QueriesQueries: ConnectionWrapper, Sendable {
241250
selectSingleFoo: selectSingleFoo,
242251
hasEmbeddedFoo: hasEmbeddedFoo,
243252
bothColumnsShouldNotBeNullable: bothColumnsShouldNotBeNullable,
244-
selectWithManyInputs: selectWithManyInputs
253+
selectWithManyInputs: selectWithManyInputs,
254+
inputIsArray: inputIsArray,
255+
inputContainsArray: inputContainsArray
245256
)
246257
}
247258

@@ -358,6 +369,39 @@ struct QueriesQueries: ConnectionWrapper, Sendable {
358369
try statement.bind(value: input.intPk, to: 1)
359370
try statement.bind(value: input.textNotNull, to: 2)
360371
return try statement.fetchOne(adapters: adapters)
372+
},
373+
inputIsArray: DatabaseQuery<[Int], ()>(
374+
.write,
375+
in: connection,
376+
watchingTables: ["bar"]
377+
) { input, tx in
378+
let statement = try Otter.Statement(
379+
"""
380+
DELETE FROM bar WHERE intPk IN (\(input.sqlQuestionMarks))
381+
""",
382+
transaction: tx
383+
)
384+
for element in input {
385+
try statement.bind(value: element, to: 1)
386+
}
387+
_ = try statement.step()
388+
},
389+
inputContainsArray: DatabaseQuery<InputContainsArrayInput, ()>(
390+
.write,
391+
in: connection,
392+
watchingTables: ["bar"]
393+
) { input, tx in
394+
let statement = try Otter.Statement(
395+
"""
396+
DELETE FROM bar WHERE intPk IN (\(input.intPks.sqlQuestionMarks))AND barNotNullText = ?
397+
""",
398+
transaction: tx
399+
)
400+
for element in input.intPks {
401+
try statement.bind(value: element, to: 1)
402+
}
403+
try statement.bind(value: input.barNotNullText, to: 2)
404+
_ = try statement.step()
361405
}
362406
)
363407
}
@@ -464,3 +508,19 @@ extension Query where Input == SelectWithManyInputsInput {
464508
observe(SelectWithManyInputsInput(intPk: intPk, textNotNull: textNotNull))
465509
}
466510
}
511+
512+
typealias InputIsArrayQuery = Query<[Int], ()>
513+
typealias InputContainsArrayQuery = Query<InputContainsArrayInput, ()>
514+
extension Query where Input == InputContainsArrayInput {
515+
func execute(intPks: [Int], barNotNullText: String) async throws -> Output {
516+
try await execute(InputContainsArrayInput(intPks: intPks, barNotNullText: barNotNullText))
517+
}
518+
519+
func execute(intPks: [Int], barNotNullText: String, tx: borrowing Transaction) throws -> Output {
520+
try execute(InputContainsArrayInput(intPks: intPks, barNotNullText: barNotNullText), tx: tx)
521+
}
522+
523+
func observe(intPks: [Int], barNotNullText: String) -> QueryStream<Output> {
524+
observe(InputContainsArrayInput(intPks: intPks, barNotNullText: barNotNullText))
525+
}
526+
}

0 commit comments

Comments
 (0)