Skip to content

Commit

Permalink
feat mysql alter force,engine,algorithm,lock (#2121)
Browse files Browse the repository at this point in the history
Co-authored-by: mj-db <[email protected]>
  • Loading branch information
minleejae and mj-db authored Dec 9, 2024
1 parent e122bcf commit 18c1a2c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
23 changes: 10 additions & 13 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -7167,7 +7167,6 @@ AlterExpression AlterExpression():
(
(
<K_ADD> { alterExp.setOperation(AlterOperation.ADD);
System.out.println("test");
}
|
<K_ALTER> { alterExp.setOperation(AlterOperation.ALTER); }
Expand Down Expand Up @@ -7438,12 +7437,16 @@ AlterExpression AlterExpression():
)
)
|
(
<K_FORCE>{ alterExp.setOperation(AlterOperation.FORCE); }
)
|
(
<K_ALGORITHM> {
alterExp.setOperation(AlterOperation.ALGORITHM);
}
["=" { alterExp.setUseEqual(true);} ]
sk3 = RelObjectName() {alterExp.addParameters(sk3); }
sk3 = RelObjectName() {alterExp.setAlgorithmOption(sk3); }
)
|
(
Expand All @@ -7453,6 +7456,11 @@ AlterExpression AlterExpression():
["=" { alterExp.setUseEqual(true);} ]
sk3 = RelObjectName() {alterExp.setLockOption(sk3); }
)
|
(<K_ENGINE> {alterExp.setOperation(AlterOperation.ENGINE);}
["=" { alterExp.setUseEqual(true);} ]
sk3 = RelObjectName() {alterExp.setEngineOption(sk3); }
)
|
LOOKAHEAD(2) <K_RENAME> { alterExp.setOperation(AlterOperation.RENAME); } [ <K_COLUMN> { alterExp.hasColumn(true);} ]
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> ) { alterExp.setColOldName(tk.image); }
Expand Down Expand Up @@ -7516,17 +7524,6 @@ AlterExpression AlterExpression():
}
)
|
(<K_ENGINE> {alterExp.setOperation(AlterOperation.SET_TABLE_OPTION);}
["=" { alterExp.setUseEqual(true);} ]
tk=<S_IDENTIFIER> {
if (alterExp.getUseEqual()) {
alterExp.setTableOption("ENGINE = " + tk.image);
} else {
alterExp.setTableOption("ENGINE " + tk.image);
}
}
)
|
LOOKAHEAD(2)
(<K_RENAME> ((<K_INDEX> {alterExp.setOperation(AlterOperation.RENAME_INDEX);}
| <K_KEY> {alterExp.setOperation(AlterOperation.RENAME_KEY);})
Expand Down
44 changes: 42 additions & 2 deletions src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlterExpression> 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<AlterExpression> 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);
}
}

0 comments on commit 18c1a2c

Please sign in to comment.