Skip to content

Commit 1dd7439

Browse files
committed
INSERT statements should not return an optional
1 parent 566b962 commit 1dd7439

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

Sources/Compiler/Gen/Language.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ extension Language {
264264
// Will return an array if it returns many or optional if its a single result
265265
let singleOrMany: (GenerationType) -> GenerationType = {
266266
switch statement.outputCardinality {
267-
case .single: .optional($0)
267+
// INSERTs will always return a value so no need to do optional
268+
case .single: statement.isInsert ? $0 : .optional($0)
268269
case .many: .array($0)
269270
}
270271
}

Sources/Compiler/Sema/Statement.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public struct Statement {
3737
return definition?.name
3838
}
3939

40+
/// Whether or not the source syntax is an INSERT statement
41+
public var isInsert: Bool {
42+
if syntax is InsertStmtSyntax { return true }
43+
guard let definition = syntax as? QueryDefinitionStmtSyntax else { return false }
44+
return definition.statement is InsertStmtSyntax
45+
}
46+
4047
/// Replaces the definition with the given input
4148
public func with(definition: Definition?) -> Statement {
4249
return Statement(

Tests/CompilerTests/Gen/Queries.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
insertUser:
2-
INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?);
2+
INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id;
33

44
selectUsers:
55
SELECT * FROM user;

Tests/CompilerTests/Gen/Swift.output

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ protocol QueriesQueries: ConnectionWrapper {
203203

204204
struct QueriesQueriesNoop: QueriesQueries {
205205
let connection: any Connection = NoopConnection()
206-
let insertUser: AnyQuery<InsertUserInput, ()>
206+
let insertUser: AnyQuery<InsertUserInput, Int>
207207
let selectUsers: AnyQuery<(), [User]>
208208
let selectUserById: AnyQuery<Int, User?>
209209
let selectUserByIds: AnyQuery<[Int], [User]>
@@ -236,15 +236,15 @@ struct QueriesQueriesNoop: QueriesQueries {
236236
struct QueriesQueriesImpl: QueriesQueries {
237237
let connection: any Connection
238238

239-
var insertUser: DatabaseQuery<InsertUserInput, ()> {
240-
DatabaseQuery<InsertUserInput, ()>(
239+
var insertUser: DatabaseQuery<InsertUserInput, Int> {
240+
DatabaseQuery<InsertUserInput, Int>(
241241
.write,
242242
in: connection,
243243
watchingTables: ["user"]
244244
) { input, tx in
245245
let statement = try Otter.Statement(
246246
"""
247-
INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?)
247+
INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id
248248
""",
249249
transaction: tx
250250
)
@@ -255,7 +255,7 @@ struct QueriesQueriesImpl: QueriesQueries {
255255
try statement.bind(value: input.favoriteNumber, to: 5)
256256
try statement.bind(value: input.randomValue, to: 6)
257257
try statement.bind(value: input.bornOn, to: 7, using: CustomDateDatabaseValueAdapter.self, as: String.self)
258-
_ = try statement.step()
258+
return try statement.fetchOne()
259259
}
260260
}
261261

@@ -410,13 +410,13 @@ struct DB: Database{
410410
}
411411
}
412412

413-
typealias InsertUserQuery = Query<InsertUserInput, ()>
413+
typealias InsertUserQuery = Query<InsertUserInput, Int>
414414
extension Query where Input == InsertUserInput {
415415
func execute(id: Int, firstName: String, lastName: String, preference: Bool?, favoriteNumber: Int?, randomValue: SQLAny?, bornOn: Date?) async throws -> Output {
416416
try await execute(with: InsertUserInput(id: id, firstName: firstName, lastName: lastName, preference: preference, favoriteNumber: favoriteNumber, randomValue: randomValue, bornOn: bornOn))
417417
}
418418

419-
func execute(id: Int, firstName: String, lastName: String, preference: Bool?, favoriteNumber: Int?, randomValue: SQLAny?, bornOn: Date?, tx: borrowing Transaction) async throws -> Output {
419+
func execute(id: Int, firstName: String, lastName: String, preference: Bool?, favoriteNumber: Int?, randomValue: SQLAny?, bornOn: Date?, tx: borrowing Transaction) throws -> Output {
420420
try execute(with: InsertUserInput(id: id, firstName: firstName, lastName: lastName, preference: preference, favoriteNumber: favoriteNumber, randomValue: randomValue, bornOn: bornOn), tx: tx)
421421
}
422422

@@ -435,7 +435,7 @@ extension Query where Input == SelectUserWithManyInputsInput {
435435
try await execute(with: SelectUserWithManyInputsInput(id: id, firstName: firstName))
436436
}
437437

438-
func execute(id: Int, firstName: String, tx: borrowing Transaction) async throws -> Output {
438+
func execute(id: Int, firstName: String, tx: borrowing Transaction) throws -> Output {
439439
try execute(with: SelectUserWithManyInputsInput(id: id, firstName: firstName), tx: tx)
440440
}
441441

0 commit comments

Comments
 (0)