Skip to content

Commit a57ea5c

Browse files
doc: document error recovery and UnsupportedStatement
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent b3d3a8e commit a57ea5c

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

src/site/sphinx/usage.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,43 @@ For guidance with the API, use `JSQLFormatter <http://jsqlformatter.manticore-pr
145145
</pre>
146146
</div>
147147

148+
Error Handling
149+
==============================
150+
151+
There are two features for handling errors
152+
153+
- ``parser.withErrorRecovery(true)`` will continue to the next statement separator and return an empty statement.
154+
- ``parser.withUnsupportedStatements(true)`` will return an instance of the `UnsupportedStatement` class, although the first statement **must** be a regular statement
155+
156+
.. code-block:: java
157+
:caption: Error Recovery
158+
159+
CCJSqlParser parser = new CCJSqlParser(
160+
"select * from mytable; select from; select * from mytable2" );
161+
Statements statements = parser.withErrorRecovery().Statements();
162+
163+
// 3 statements, the failing one set to NULL
164+
assertEquals(3, statements.size());
165+
assertNull(statements.get(1));
166+
167+
// errors are recorded
168+
assertEquals(1, parser.getParseErrors().size());
169+
170+
.. code-block:: java
171+
:caption: Unsupported Statement
172+
173+
Statements statements = CCJSqlParserUtil.parseStatements(
174+
"select * from mytable; select from; select * from mytable2; select 4;"
175+
, parser -> parser.withUnsupportedStatements() );
176+
177+
// 4 statements with one Unsupported Statement holding the content
178+
assertEquals(4, statements.size());
179+
assertInstanceOf(UnsupportedStatement.class, statements.get(1));
180+
assertEquals("select from", statements.get(1).toString());
181+
182+
// no errors records, because a statement has been returned
183+
assertEquals(0, parser.getParseErrors().size());
184+
148185
149186
Use the Visitor Patterns
150187
==============================

0 commit comments

Comments
 (0)