Skip to content

Implement BREAK and CONTINUE statements for WHILE loops with SQL-99 LEAVE/ITERATE support - #2263

Merged
mathiasrw merged 6 commits into
developfrom
copilot/add-break-continue-statements
Nov 30, 2025
Merged

mathiasrw merged 6 commits into
developfrom
copilot/add-break-continue-statements

Conversation

Copilot AI commented Nov 20, 2025 •

Copy link
Copy Markdown
Contributor

Thank you for the time you are putting into AlaSQL!

Adds control flow statements BREAK and CONTINUE to exit or skip iterations in WHILE loops, supporting both T-SQL and SQL-99 syntax.

Changes

  • Control flow via exceptions: Introduced BreakException and ContinueException using a helper function createControlFlowException() for cleaner code
  • WHILE loop: Added try-catch in While.prototype.execute to handle control flow exceptions - catches BreakException to exit the loop and ContinueException to skip to the next iteration
  • BREAK/CONTINUE (T-SQL): Modified to throw exceptions instead of returning static values
  • LEAVE/ITERATE (SQL-99): Added as aliases for BREAK/CONTINUE for SQL-99 compatibility
  • BeginEnd fix: Implemented synchronous execution path (was previously non-functional), allowing control flow exceptions to propagate naturally. The async path was pre-existing code.

Example (T-SQL syntax)

SET @a = 0;
SET @result = 0;
WHILE @a < 10
BEGIN
    SET @a = @a + 1;
    IF @a = 8 BREAK;           -- Exits loop when @a reaches 8
    IF @a % 2 = 0 CONTINUE;    -- Skips even numbers
    SET @result = @result + @a;
END;
-- @result = 16 (sum of 1,3,5,7)

Example (SQL-99 syntax)

SET @a = 0;
SET @result = 0;
WHILE @a < 10
BEGIN
    SET @a = @a + 1;
    IF @a = 8 LEAVE;           -- SQL-99 alias for BREAK
    IF @a % 2 = 0 ITERATE;     -- SQL-99 alias for CONTINUE
    SET @result = @result + @a;
END;
-- @result = 16 (sum of 1,3,5,7)

Exceptions propagate through nested BEGIN...END blocks correctly. Test suite (test/test37.js) covers 14 tests including simple loops, nested blocks, edge cases, and SQL-99 LEAVE/ITERATE aliases.

Notes

  • SQL-99 compatibility: Both T-SQL (BREAK/CONTINUE) and SQL-99 (LEAVE/ITERATE) keywords are now supported. This maintains backward compatibility while working towards SQL-99 compliance.

  • File organization: The file header was corrected from "CREATE VIEW" to accurately reflect the WHILE/BREAK/CONTINUE/BeginEnd content. The code was already in this file before these changes.

  • Fixes Add BREAK and CONTINUE statements with more reliable WHILE statement #37

Original prompt

This section details on the original issue you should resolve

<issue_title>Add BREAK and CONTINUE statements with more reliable WHILE statement</issue_title>
<issue_description>Add statements like:

    SET @a = 1;
    WHILE @a < 10
    BEGIN
        IF @a = 8 BREAK;
        SET @a += 1;
        IF @a = 5 CONTINUE;
        SET @a += 1;
     END;

</issue_description>

<agent_instructions>start by making a testfile named test37.js, describe the needed behavour based on SQL-99

Then try to solve it. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Has this been resolved?

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 20, 2025 21:55
…nsive tests

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Add BREAK and CONTINUE statements with reliable WHILE statement Implement BREAK and CONTINUE statements for WHILE loops Nov 20, 2025
Copilot AI requested a review from mathiasrw November 20, 2025 22:02
Comment thread src/69while.js Outdated

@mathiasrw mathiasrw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread src/69while.js Outdated
Comment thread src/69while.js
Comment thread src/69while.js
Comment thread test/test37.js Outdated
Comment thread src/69while.js
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
@mathiasrw

Copy link
Copy Markdown
Member

@copilot

Since AlaSQL already had WHILE with BEGIN...END blocks, following T-SQL syntax maintains backward compatibility.

Could we support both? Its important that we work towards SQL99 compatibility.

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>

Copilot AI commented Nov 30, 2025 •

Copy link
Copy Markdown
Contributor Author

@copilot

Since AlaSQL already had WHILE with BEGIN...END blocks, following T-SQL syntax maintains backward compatibility.
...

Added SQL-99 LEAVE and ITERATE as aliases for BREAK and CONTINUE respectively. Both syntaxes now work. See commit bc54ba8.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • upload.wikimedia.org
    • Triggering command: /usr/local/bin/node /usr/local/bin/node node_modules/mocha/bin/mocha.js ./test --reporter dot --bail (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Implement BREAK and CONTINUE statements for WHILE loops Implement BREAK and CONTINUE statements for WHILE loops with SQL-99 LEAVE/ITERATE support Nov 30, 2025
@mathiasrw
mathiasrw marked this pull request as ready for review November 30, 2025 10:22
@mathiasrw
mathiasrw merged commit 09a1ce4 into develop Nov 30, 2025
20 checks passed
@mathiasrw
mathiasrw deleted the copilot/add-break-continue-statements branch November 30, 2025 12:23
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.

Add BREAK and CONTINUE statements with more reliable WHILE statement

2 participants