Skip to content

Add SQL DML and cursor statement support#2838

Closed
allieseb wants to merge 2 commits intoTypeCobolTeam:developfrom
allieseb:feature/sql-dml-cursor-statements
Closed

Add SQL DML and cursor statement support#2838
allieseb wants to merge 2 commits intoTypeCobolTeam:developfrom
allieseb:feature/sql-dml-cursor-statements

Conversation

@allieseb
Copy link
Copy Markdown

@allieseb allieseb commented Mar 5, 2026

Summary

  • Add parsing support for 7 SQL DML and cursor statements: INSERT, UPDATE, DELETE, DECLARE CURSOR, OPEN CURSOR, CLOSE CURSOR, FETCH
  • Refactor SQL statement class naming for consistency: SqlCloseStatementCloseCursorStatement, SqlOpenStatementOpenCursorStatement, SqlDeleteStatementDeleteSqlStatement
  • Consolidate SqlCodeElementBuilder with improved parsing logic

Changes

  • CobolCodeElements.g4: Add ANTLR grammar rules for SQL DML/cursor statements
  • SqlCodeElementBuilder.cs: Implement builder methods for new SQL statements
  • Statements/: Add CloseCursorStatement, OpenCursorStatement, DeleteSqlStatement; update DeclareCursorStatement, FetchStatement, InsertStatement, UpdateStatement
  • Nodes/: Add DeleteSql node; update CloseCursor, OpenCursor, DeclareCursor, Fetch, Insert, Update
  • CupParser/: Wire new statements through ProgramClassBuilder, dispatcher, and listener
  • CodeElementType.cs, StatementElement.cs, CobolLanguageLevelVisitor.cs: Register new code element types

Test plan

  • Verify existing TypeCobol unit tests pass
  • Test SQL DML statement parsing with sample COBOL programs containing embedded SQL
  • Verify cursor lifecycle (DECLARE → OPEN → FETCH → CLOSE) is correctly parsed

allieseb and others added 2 commits March 4, 2026 21:28
Extend the EXEC SQL parser with support for 7 new statement types
that were previously scanned as raw ExecText tokens:

  - INSERT INTO ... VALUES / INSERT INTO ... SELECT
  - UPDATE ... SET ... WHERE
  - DELETE FROM ... WHERE
  - DECLARE CURSOR ... FOR fullselect
  - OPEN cursor
  - CLOSE cursor
  - FETCH cursor INTO :host-var

These are the most common DB2 embedded SQL statements found in COBOL
production programs and were missing from the parsed SQL subset.

For each statement the full pipeline is wired:
  1. ANTLR grammar rules (CobolCodeElements.g4)
  2. CodeElementType + StatementType enums
  3. IsSqlStatementStartingKeyword() for error recovery
  4. Typed CodeElement classes (Sql/CodeElements/Statements/)
  5. AST Node classes (Sql/Nodes/)
  6. SqlCodeElementBuilder.Create*() extraction methods
  7. CobolCodeElementBuilder.Enter*() ANTLR callbacks
  8. IASTVisitor Visit() overloads + AbstractAstVisitor defaults
  9. IProgramClassBuilder / ProgramClassBuilder / Dispatcher / Listener
  10. CUP terminals and sqlStatement alternatives (both .cup files)

Naming: DELETE, OPEN and CLOSE are prefixed Sql* (SqlDeleteStatement,
SqlOpenStatement, SqlCloseStatement, SqlDelete node) to avoid
collision with the existing COBOL-native statements of the same name.

A minimal searchCondition rule (expr = expr) is introduced for the
WHERE clause; it is intentionally simple and marked TODO for future
expansion (AND/OR/BETWEEN/IN/LIKE).

Requires EnableSqlParsing = true in TypeCobolOptions (defaults to
false) for the scanner to tokenize SQL keywords inside EXEC SQL blocks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tement classes

- Rename SqlCloseStatement → CloseCursorStatement
- Rename SqlOpenStatement → OpenCursorStatement
- Rename SqlDeleteStatement → DeleteSqlStatement
- Rename SqlDelete node → DeleteSql
- Consolidate SqlCodeElementBuilder with improved parsing logic
- Align naming conventions across SQL statement/node hierarchy

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@fm-117 fm-117 self-requested a review March 19, 2026 08:32
Copy link
Copy Markdown
Contributor

@fm-117 fm-117 left a comment

Choose a reason for hiding this comment

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

Some general remarks:

  • granularity of the PR makes it hard to review as you add 7 new statements in one go. We usually work on one statement at a time as this help to focus on a single syntax diagram. See for example #2284.
  • the grammar definitions are incomplete, some optional clauses are not defined here. This is fine but adding a TODO in CobolCodeElements.g4 could help identify the missing parts for each statement

TODO (but you already identified some of these tasks)

  • Create at least one unit test for each statement using the rdzSQL format
    • You'll need to update the Comparator for that purpose to account for new statements
  • Update existing unit tests affected by the change

@@ -678,14 +685,14 @@ sourceComputerParagraph:
// The PROGRAM COLLATING SEQUENCE clause does not apply to DBCS
// data items or data items of usage NATIONAL.
// If the PROGRAM COLLATING SEQUENCE clause is omitted, the EBCDIC
// collating sequence is used. (See Appendix C, �EBCDIC and ASCII collating
// sequences,� on page 569.)
// collating sequence is used. (See Appendix C, �EBCDIC and ASCII collating
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unwanted file encoding change ?

// --- INSERT ---
insertStatement: SQL_INSERT SQL_INTO tableOrViewOrCorrelationName insertColumnList? (SQL_VALUES LeftParenthesisSeparator insertValueList RightParenthesisSeparator | fullselect);
insertColumnList: LeftParenthesisSeparator column_name (SQL_CommaSeparator column_name)* RightParenthesisSeparator;
insertValueList: sqlExpression (SQL_CommaSeparator sqlExpression)*;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can be deleted, use repeatedSourceValue instead


// --- UPDATE ---
updateStatement: SQL_UPDATE tableOrViewOrCorrelationName SQL_SET updateAssignmentClause (SQL_CommaSeparator updateAssignmentClause)* updateWhereClause?;
updateAssignmentClause: column_name EqualOperator sqlExpression;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is incomplete but ok for simple assignment clauses

allieseb added a commit to allieseb/TypeCobol that referenced this pull request Mar 22, 2026
- Fix file encoding: preserve original Windows-1252 bytes in grammar
  comments (no more UTF-8 replacement characters in diff)
- Remove redundant insertValueList rule, reuse repeatedSourceValue
  as suggested by reviewer
- Add TODO comments in CobolCodeElements.g4 for all incomplete
  optional clauses (INSERT, UPDATE, DELETE, DECLARE CURSOR)
- Update SqlCodeElementBuilder to navigate sourceValue tree for
  host variable extraction (FindHostVariable helper)
- Add Comparator Visit methods for all new SQL statement types
  (InsertStatement, UpdateStatement, SqlDeleteStatement,
  DeclareCursorStatement, UnsupportedSqlStatement)
- Create dedicated rdzSQL unit tests (one per statement type):
  ExecSqlWithInsertStatement, ExecSqlWithUpdateStatement,
  ExecSqlWithDeleteStatement, ExecSqlWithDeclareCursorStatement
- Update ExecSqlWithUnsupportedStatement test: remove now-supported
  statements, keep only OPEN/FETCH/CLOSE
- Build verified: 0 errors (885 pre-existing warnings)

Addresses review from fm-117 on PR TypeCobolTeam#2838.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@fm-117
Copy link
Copy Markdown
Contributor

fm-117 commented Apr 7, 2026

Will be recreated separately.

@fm-117 fm-117 closed this Apr 7, 2026
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.

2 participants