Loading...
Loading...
1 <= statements.length <= 50 Each statement is a non-empty uppercase string with length <= 200 Cursor names are alphanumeric strings starting with a letter At most one cursor lifecycle per input
You are building a lightweight SQL linter for DB2 embedded SQL programs. Your task is to parse and validate cursor lifecycle statements. In DB2 embedded SQL, a cursor must follow a strict lifecycle:
A valid cursor program must follow this exact order, and each statement must reference the same cursor name.
Given a list of DB2 embedded SQL statements (as strings), determine if the cursor lifecycle is valid. Return "VALID" if the statements follow the correct order and reference the same cursor name, otherwise return "INVALID" along with the reason.
Input: A list of SQL statement strings (each trimmed, uppercase). The statements may include non-cursor SQL lines which should be ignored.
Output: A string — either "VALID" or "INVALID: <reason>" where reason is one of:
"MISSING DECLARE""MISSING OPEN""MISSING FETCH""MISSING CLOSE""WRONG ORDER""CURSOR NAME MISMATCH"INSERT, UPDATE, CONNECT) are ignored.DECLARE CURSOR, OPEN, FETCH, CLOSE) and the cursor name.Example 1 — Valid lifecycle:
DECLARE C1 CURSOR FOR SELECT * FROM EMPLOYEES
OPEN C1
FETCH C1 INTO :HV1, :HV2
CLOSE C1
Output: "VALID"
Example 2 — Missing OPEN:
DECLARE C1 CURSOR FOR SELECT * FROM EMPLOYEES
FETCH C1 INTO :HV1
CLOSE C1
Output: "INVALID: MISSING OPEN"
Example 3 — Cursor name mismatch:
DECLARE C1 CURSOR FOR SELECT * FROM EMPLOYEES
OPEN C1
FETCH C2 INTO :HV1
CLOSE C1
Output: "INVALID: CURSOR NAME MISMATCH"