diff --git a/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java b/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java index 4fd1f4209..3a8f25910 100644 --- a/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java +++ b/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java @@ -61,6 +61,8 @@ public class AlterExpression implements Serializable { private String characterSet; private String collation; private String lockOption; + private String algorithmOption; + private String engineOption; private String commentText; private String tableOption; @@ -438,6 +440,22 @@ public void setLockOption(String lockOption) { this.lockOption = lockOption; } + public String getAlgorithmOption() { + return algorithmOption; + } + + public void setAlgorithmOption(String algorithmOption) { + this.algorithmOption = algorithmOption; + } + + public String getEngineOption() { + return engineOption; + } + + public void setEngineOption(String engineOption) { + this.engineOption = engineOption; + } + public boolean getUseEqual() { return useEqual; } @@ -478,6 +496,24 @@ public String toString() { b.append(optionalSpecifier); } else if (operation == AlterOperation.SET_TABLE_OPTION) { b.append(tableOption); + } else if (operation == AlterOperation.ENGINE) { + b.append("ENGINE "); + if (useEqual) { + b.append("= "); + } + b.append(engineOption); + } else if (operation == AlterOperation.ALGORITHM) { + b.append("ALGORITHM "); + if (useEqual) { + b.append("= "); + } + b.append(algorithmOption); + } else if (operation == AlterOperation.LOCK) { + b.append("LOCK "); + if (useEqual) { + b.append("= "); + } + b.append(lockOption); } else if (getOldIndex() != null) { b.append("RENAME"); switch (operation) { diff --git a/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java b/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java index fceeb9cae..053f6892a 100644 --- a/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java +++ b/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java @@ -10,7 +10,7 @@ package net.sf.jsqlparser.statement.alter; public enum AlterOperation { - ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, TRUNCATE_PARTITION, SET_TABLE_OPTION, LOCK; + ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, TRUNCATE_PARTITION, SET_TABLE_OPTION, ENGINE, FORCE, LOCK; public static AlterOperation from(String operation) { return Enum.valueOf(AlterOperation.class, operation.toUpperCase()); diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 07748213c..cfe72baca 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -7167,7 +7167,6 @@ AlterExpression AlterExpression(): ( ( { alterExp.setOperation(AlterOperation.ADD); - System.out.println("test"); } | { alterExp.setOperation(AlterOperation.ALTER); } @@ -7438,12 +7437,16 @@ AlterExpression AlterExpression(): ) ) | + ( + { alterExp.setOperation(AlterOperation.FORCE); } + ) + | ( { alterExp.setOperation(AlterOperation.ALGORITHM); } ["=" { alterExp.setUseEqual(true);} ] - sk3 = RelObjectName() {alterExp.addParameters(sk3); } + sk3 = RelObjectName() {alterExp.setAlgorithmOption(sk3); } ) | ( @@ -7453,6 +7456,11 @@ AlterExpression AlterExpression(): ["=" { alterExp.setUseEqual(true);} ] sk3 = RelObjectName() {alterExp.setLockOption(sk3); } ) + | + ( {alterExp.setOperation(AlterOperation.ENGINE);} + ["=" { alterExp.setUseEqual(true);} ] + sk3 = RelObjectName() {alterExp.setEngineOption(sk3); } + ) | LOOKAHEAD(2) { alterExp.setOperation(AlterOperation.RENAME); } [ { alterExp.hasColumn(true);} ] ( tk= | tk= ) { alterExp.setColOldName(tk.image); } @@ -7516,17 +7524,6 @@ AlterExpression AlterExpression(): } ) | - ( {alterExp.setOperation(AlterOperation.SET_TABLE_OPTION);} - ["=" { alterExp.setUseEqual(true);} ] - tk= { - if (alterExp.getUseEqual()) { - alterExp.setTableOption("ENGINE = " + tk.image); - } else { - alterExp.setTableOption("ENGINE " + tk.image); - } - } - ) - | LOOKAHEAD(2) ( (( {alterExp.setOperation(AlterOperation.RENAME_INDEX);} | {alterExp.setOperation(AlterOperation.RENAME_KEY);}) diff --git a/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java b/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java index 03ec3e85e..28af60b71 100644 --- a/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java @@ -1354,8 +1354,48 @@ public void testIssue2114AlterTableEngine() throws JSQLParserException { assertEquals(1, alterExpressions.size()); AlterExpression engineExp = alterExpressions.get(0); - assertEquals(AlterOperation.SET_TABLE_OPTION, engineExp.getOperation()); - assertEquals(engineExp.getTableOption(), "ENGINE = InnoDB"); + assertEquals(AlterOperation.ENGINE, engineExp.getOperation()); + assertEquals(engineExp.getEngineOption(), "InnoDB"); assertSqlCanBeParsedAndDeparsed(sql); } + + @Test + public void testIssue2118AlterTableForceAndEngine() throws JSQLParserException { + String sql1 = "ALTER TABLE my_table FORCE"; + Statement stmt1 = CCJSqlParserUtil.parse(sql1); + assertTrue(stmt1 instanceof Alter); + Alter alter1 = (Alter) stmt1; + List alterExpressions1 = alter1.getAlterExpressions(); + assertNotNull(alterExpressions1); + assertEquals(1, alterExpressions1.size()); + + AlterExpression forceExp = alterExpressions1.get(0); + assertEquals(AlterOperation.FORCE, forceExp.getOperation()); + assertSqlCanBeParsedAndDeparsed(sql1); + + String sql2 = "ALTER TABLE tbl_name FORCE, ENGINE=InnoDB, ALGORITHM=INPLACE, LOCK=NONE"; + Statement stmt2 = CCJSqlParserUtil.parse(sql2); + assertTrue(stmt2 instanceof Alter); + Alter alter2 = (Alter) stmt2; + List alterExpressions2 = alter2.getAlterExpressions(); + assertNotNull(alterExpressions2); + assertEquals(4, alterExpressions2.size()); + + AlterExpression forceExp2 = alterExpressions2.get(0); + assertEquals(AlterOperation.FORCE, forceExp2.getOperation()); + + AlterExpression engineExp = alterExpressions2.get(1); + assertEquals(AlterOperation.ENGINE, engineExp.getOperation()); + assertEquals(engineExp.getEngineOption(), "InnoDB"); + + AlterExpression algorithmExp = alterExpressions2.get(2); + assertEquals(AlterOperation.ALGORITHM, algorithmExp.getOperation()); + assertEquals("INPLACE", algorithmExp.getAlgorithmOption()); + + AlterExpression lockExp = alterExpressions2.get(3); + assertEquals(AlterOperation.LOCK, lockExp.getOperation()); + assertEquals("NONE", lockExp.getLockOption()); + + assertSqlCanBeParsedAndDeparsed(sql2); + } }