diff --git a/README.md b/README.md index bb7325a6a..cf972e0bd 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ and missing syntax gets added on demand — [open an issue](https://github.com/J | **PostgreSQL ownership** | `DROP OWNED` · `REASSIGN OWNED` | | **Two-phase commit** | PostgreSQL `PREPARE TRANSACTION` · `COMMIT PREPARED` · `ROLLBACK PREPARED` · MySQL `XA START`/`END`/`PREPARE`/`COMMIT`/`ROLLBACK`/`RECOVER` | | **PostgreSQL maintenance** | `VACUUM` · `ANALYZE` · `CLUSTER` · `CHECKPOINT` | +| **Prepared-statement query plans** | `EXPLAIN EXECUTE` with optional arguments and PostgreSQL `EXPLAIN` options | | **PostgreSQL session statements** | `DISCARD` · `LISTEN` · `UNLISTEN` · `SHOW`/`RESET TIME ZONE`/`TRANSACTION ISOLATION LEVEL`/`SESSION AUTHORIZATION` · `COPY` options from before 9.0 | | **PostgreSQL foreign schema import** | `IMPORT FOREIGN SCHEMA … [LIMIT TO \| EXCEPT (…)] FROM SERVER … INTO … [OPTIONS (…)]` | | **PostgreSQL table and index options** | Bare `CAST`/`EXTRACT` keys with `Dialect.POSTGRESQL` · either order of HASH partition bound options · identity sequence `LOGGED`/`UNLOGGED` | diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 9f30c2d9b..4d3b1d4a4 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -5389,6 +5389,11 @@ public class CCJSqlParser extends AbstractJSqlParser { } } + private boolean isExplainPreparedExecutionDialect() { + String dialect = getAsString(Feature.dialect); + return dialect == null || Dialect.POSTGRESQL.name().equals(dialect); + } + private void validatePostgresqlExplain(ExplainStatement explain) throws ParseException { if (explain.getTable() != null) { throw new ParseException("PostgreSQL EXPLAIN requires a statement"); @@ -10479,7 +10484,7 @@ ExplainStatement MySqlExplain(): ( schema=RelObjectName() | token= { schema = token.image; } ) { explain.setSchemaName(schema); } ] - statement=ExplainableStatement() { explain.setStatement(statement); } + statement=ExplainableStatement(false) { explain.setStatement(statement); } ) { return explain; } } @@ -10495,8 +10500,8 @@ ExplainStatement.Option MySqlExplainFormat(): { return mySqlExplainFormat(token); } } -/** The statement EXPLAIN explains: a query or a DML statement. */ -Statement ExplainableStatement(): +/** The statement EXPLAIN explains: a query, DML, or prepared execution. */ +Statement ExplainableStatement(boolean allowPreparedExecution): { List> with = null; Statement statement; @@ -10507,6 +10512,16 @@ Statement ExplainableStatement(): LOOKAHEAD({ getToken(1).kind == K_REPLACE && isMySqlUtilityDialect() }) statement = Upsert() | + LOOKAHEAD(2, ColumnIdentifier(), { isExplainPreparedExecutionDialect() }) + statement = Execute() + { + Execute execute = (Execute) statement; + requireDdlSyntax(allowPreparedExecution && execute.getUsingVariables() == null + && (execute.getExprList() == null + || execute.getExprList() instanceof ParenthesedExpressionList), + "EXPLAIN EXECUTE accepts a prepared statement name and optional parenthesized arguments"); + } + | [ LOOKAHEAD(2) with=WithList() ] ( statement = SelectWithWithItems( with ) { if (with != null) { widen(statement, first, null); } } @@ -10540,7 +10555,7 @@ ExplainStatement Explain(): LOOKAHEAD(2) "(" options = PostgresqlExplainOptions() ")" { parenthesized = true; } | options = ExplainStatementOptions() ) - statement = ExplainableStatement() + statement = ExplainableStatement(tk.kind == K_EXPLAIN) { es = new ExplainStatement(tk.image, statement, options); es.setParenthesizedOptions(parenthesized); diff --git a/src/test/java/net/sf/jsqlparser/statement/ExplainTest.java b/src/test/java/net/sf/jsqlparser/statement/ExplainTest.java index 205fef5c6..0883e30a1 100644 --- a/src/test/java/net/sf/jsqlparser/statement/ExplainTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/ExplainTest.java @@ -13,8 +13,10 @@ import static org.assertj.core.api.Assertions.assertThat; import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.statement.delete.Delete; +import net.sf.jsqlparser.statement.execute.Execute; import org.junit.jupiter.api.Test; public class ExplainTest { @@ -24,6 +26,24 @@ public void testDescribe() throws JSQLParserException { assertSqlCanBeParsedAndDeparsed("EXPLAIN SELECT * FROM mytable"); } + @Test + public void testExplainPreparedExecutionWithoutDialect() throws JSQLParserException { + ExplainStatement explain = + (ExplainStatement) assertSqlCanBeParsedAndDeparsed("EXPLAIN EXECUTE q"); + assertThat(explain.getStatement()).isInstanceOf(Execute.class); + ExplainStatement bare = + (ExplainStatement) assertSqlCanBeParsedAndDeparsed("EXPLAIN EXECUTE"); + assertThat(bare.getTable().getName()).isEqualTo("EXECUTE"); + } + + @Test + public void testExplainExecuteAsMySqlTableAndColumn() throws JSQLParserException { + ExplainStatement explain = (ExplainStatement) assertSqlCanBeParsedAndDeparsed( + "EXPLAIN EXECUTE q", true, parser -> parser.withDialect(Dialect.MYSQL)); + assertThat(explain.getTable().getName()).isEqualTo("EXECUTE"); + assertThat(explain.getStatement()).isNull(); + } + @Test public void testAnalyze() throws JSQLParserException { assertSqlCanBeParsedAndDeparsed("EXPLAIN ANALYZE SELECT * FROM mytable"); diff --git a/src/test/java/net/sf/jsqlparser/statement/PostgresqlExplainTest.java b/src/test/java/net/sf/jsqlparser/statement/PostgresqlExplainTest.java index f0cef624d..3d7b769e9 100644 --- a/src/test/java/net/sf/jsqlparser/statement/PostgresqlExplainTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/PostgresqlExplainTest.java @@ -17,6 +17,7 @@ import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.statement.ExplainStatement.Option; import net.sf.jsqlparser.statement.ExplainStatement.OptionType; +import net.sf.jsqlparser.statement.execute.Execute; import net.sf.jsqlparser.test.TestUtils; import net.sf.jsqlparser.util.TablesNamesFinder; import org.junit.jupiter.api.Test; @@ -78,6 +79,34 @@ void rejectInvalidPostgresqlOptions(String sql) { parser -> parser.withDialect(Dialect.POSTGRESQL).withUnsupportedStatements(false))); } + @ParameterizedTest + @ValueSource(strings = { + "EXPLAIN EXECUTE q", + "EXPLAIN EXECUTE q (42)", + "EXPLAIN (FORMAT JSON) EXECUTE q (42)", + "EXPLAIN ANALYZE VERBOSE EXECUTE q (42, 7)" + }) + void explainPreparedExecution(String sql) throws JSQLParserException { + ExplainStatement explain = assertInstanceOf(ExplainStatement.class, + TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true, + parser -> parser.withDialect(Dialect.POSTGRESQL))); + Execute execute = assertInstanceOf(Execute.class, explain.getStatement()); + assertEquals("q", execute.getName()); + assertEquals(Execute.ExecType.EXECUTE, execute.getExecType()); + } + + @ParameterizedTest + @ValueSource(strings = { + "SUMMARIZE EXECUTE q", + "EXPLAIN EXECUTE q 42", + "EXPLAIN EXECUTE q USING @x", + "EXPLAIN CALL q()" + }) + void rejectOtherExecutionForms(String sql) { + assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql, + parser -> parser.withDialect(Dialect.POSTGRESQL).withUnsupportedStatements(false))); + } + @Test void preserveRepetitionsAndLegacyMapAccess() throws JSQLParserException { ExplainStatement explain = (ExplainStatement) TestUtils.assertSqlCanBeParsedAndDeparsed(