-
-
Notifications
You must be signed in to change notification settings - Fork 81
async/await prepared statement API #390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabianfett
merged 13 commits into
vapor:main
from
mariosangiorgio:async-prepared-statement
Aug 18, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62b61ab
async/await prepared statements
mariosangiorgio 8283d8b
Address PR feedbacks
mariosangiorgio 9574725
Apply suggestions from code review
mariosangiorgio b03c251
PR feedback and state machine tests
mariosangiorgio 800ceaf
Add test to `PostgresConnectionTests` to test prepared statements
mariosangiorgio a426205
Explicit switch over all the state machine states
mariosangiorgio abe1cd1
PostgresBindings.append that use the default context
mariosangiorgio ff0e139
More assertions in `testPreparedStatement`
mariosangiorgio 725e778
Fix a build issue with Swift 5.6
mariosangiorgio 44309e3
Apply suggestions from code review
mariosangiorgio 0740293
Fix build issues
mariosangiorgio 660e78d
Add more `PostgresConnectionTests` cases
mariosangiorgio da87ce0
Apply suggestions from code review
mariosangiorgio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
Sources/PostgresNIO/New/Connection State Machine/PreparedStatementStateMachine.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import NIOCore | ||
|
||
struct PreparedStatementStateMachine { | ||
enum State { | ||
case preparing([PreparedStatementContext]) | ||
mariosangiorgio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case prepared(RowDescription?) | ||
case error(PSQLError) | ||
mariosangiorgio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var preparedStatements: [String: State] = [:] | ||
|
||
enum LookupAction { | ||
case prepareStatement | ||
case waitForAlreadyInFlightPreparation | ||
case executeStatement(RowDescription?) | ||
case returnError(PSQLError) | ||
} | ||
|
||
mutating func lookup(preparedStatement: PreparedStatementContext) -> LookupAction { | ||
if let state = self.preparedStatements[preparedStatement.name] { | ||
switch state { | ||
case .preparing(var statements): | ||
statements.append(preparedStatement) | ||
self.preparedStatements[preparedStatement.name] = .preparing(statements) | ||
return .waitForAlreadyInFlightPreparation | ||
case .prepared(let rowDescription): | ||
return .executeStatement(rowDescription) | ||
case .error(let error): | ||
return .returnError(error) | ||
} | ||
} else { | ||
self.preparedStatements[preparedStatement.name] = .preparing([preparedStatement]) | ||
return .prepareStatement | ||
} | ||
} | ||
|
||
struct PreparationCompleteAction { | ||
var statements: [PreparedStatementContext] | ||
var rowDescription: RowDescription? | ||
} | ||
|
||
mutating func preparationComplete( | ||
name: String, | ||
rowDescription: RowDescription? | ||
) -> PreparationCompleteAction { | ||
guard let state = self.preparedStatements[name] else { | ||
fatalError("Unknown prepared statement \(name)") | ||
} | ||
switch state { | ||
case .preparing(let statements): | ||
// When sending the bindings we are going to ask for binary data. | ||
if var rowDescription = rowDescription { | ||
for i in 0..<rowDescription.columns.count { | ||
rowDescription.columns[i].format = .binary | ||
} | ||
self.preparedStatements[name] = .prepared(rowDescription) | ||
return PreparationCompleteAction( | ||
statements: statements, | ||
rowDescription: rowDescription | ||
) | ||
} else { | ||
self.preparedStatements[name] = .prepared(nil) | ||
return PreparationCompleteAction( | ||
statements: statements, | ||
rowDescription: nil | ||
) | ||
} | ||
case .prepared, .error: | ||
preconditionFailure("Preparation completed happened in an unexpected state \(state)") | ||
} | ||
} | ||
|
||
struct ErrorHappenedAction { | ||
var statements: [PreparedStatementContext] | ||
var error: PSQLError | ||
} | ||
|
||
mutating func errorHappened(name: String, error: PSQLError) -> ErrorHappenedAction { | ||
guard let state = self.preparedStatements[name] else { | ||
fatalError("Unknown prepared statement \(name)") | ||
} | ||
switch state { | ||
case .preparing(let statements): | ||
self.preparedStatements[name] = .error(error) | ||
return ErrorHappenedAction( | ||
statements: statements, | ||
error: error | ||
) | ||
case .prepared, .error: | ||
preconditionFailure("Error happened in an unexpected state \(state)") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.