Skip to content

Allow stored procedures to be defined without BEGIN/END #1834

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

aharpervc
Copy link
Contributor

@aharpervc aharpervc commented Apr 30, 2025

For SQL Server, you can make a stored procedure without begin/end (docs ref). Otherwise, it parses the same way.

To differentiate with/without in the parser, the stored procedure struct's statements field was changed from Vec<Statement> (where begin/end are required & implicit) to a BeginEndStatements, where the begin/end tokens are explicit. They're empty when missing & written when present.

This PR also includes the fix to allow EOF to end a statement list from #1831 (so whichever merges first, I'll rebase accordingly)

The diff is perhaps larger than expected due to the question of canonical semicolons for procedure statement bodies. Formerly, a semicolon after the last statement in a procedure was non-canonical (because they were added via join... so perhaps not particular intentional for it to have been that way); a BeginEndStatements statements list will always write them out.

An additional test case example without begin/end has been added as well.

@aharpervc aharpervc marked this pull request as ready for review April 30, 2025 18:47
Comment on lines 15103 to 15113
let begin_token: AttachedToken = self
.expect_keyword(Keyword::BEGIN)
.map(AttachedToken)
.unwrap_or_else(|_| AttachedToken::empty());
let statements = self.parse_statement_list(&[Keyword::END])?;
let end_token = match &begin_token.0.token {
Token::Word(w) if w.keyword == Keyword::BEGIN => {
AttachedToken(self.expect_keyword(Keyword::END)?)
}
_ => AttachedToken::empty(),
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/ast/mod.rs Outdated
Comment on lines 3747 to 3753
body: Vec<Statement>,
body: BeginEndStatements,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It it an abuse of BeginEndStatements to potentially have the begin/end token be empty? This could alternately be an enum of BeginEndStatements and Sequence, similar to elsewhere

aharpervc added 7 commits May 2, 2025 10:59
- similar to functions & procedures, this dialect can define triggers with a multi statement block
- there's no `EXECUTE` keyword here, so that means the `exec_body` used by other dialects becomes an `Option`, and our `statements` is also optional for them
- formerly, a semicolon after the last statement in a procedure was non-canonical (because they were added via `join`); a `BeginEndStatements` statements list will always write them out
- `BeginEndStatements` begin/end tokens won't be written when empty
- EOF now concludes parsing a statement list
@aharpervc aharpervc force-pushed the mssql-create-procedure-body branch from dd8382e to ffd3b6a Compare May 2, 2025 15:53
@aharpervc
Copy link
Contributor Author

I have temporarily rebased this branch on #1810 to pick up the new helper function and similarly use the enum pattern to distinguish between Sequence & BeginEndStatements.

…tokens

- this further consolidates with existing patterns
@aharpervc aharpervc force-pushed the mssql-create-procedure-body branch from ffd3b6a to 038d3c2 Compare May 2, 2025 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant