Skip to content

Add parsing for ALTER TABLE tbl_name {DISABLE | ENABLE} KEYS #2205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,10 @@ public String toString() {
b.append("DISCARD TABLESPACE");
} else if (operation == AlterOperation.IMPORT_TABLESPACE) {
b.append("IMPORT TABLESPACE");
} else if (operation == AlterOperation.DISABLE_KEYS) {
b.append("DISABLE KEYS");
} else if (operation == AlterOperation.ENABLE_KEYS) {
b.append("ENABLE KEYS");
} else if (operation == AlterOperation.ENGINE) {
b.append("ENGINE ");
if (useEqual) {
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, COLLATE, 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, DISCARD_TABLESPACE, IMPORT_TABLESPACE;
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, COLLATE, 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, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS;

public static AlterOperation from(String operation) {
return Enum.valueOf(AlterOperation.class, operation.toUpperCase());
Expand Down
14 changes: 14 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -8110,6 +8110,20 @@ AlterExpression AlterExpression():
}
)
|
(
(tk = <K_DISABLE>)
(tk2 = <K_KEYS>) {
alterExp.setOperation(AlterOperation.DISABLE_KEYS);
}
)
|
(
(tk = <K_ENABLE>)
(tk2 = <K_KEYS>) {
alterExp.setOperation(AlterOperation.ENABLE_KEYS);
}
)
|
(<K_AUTO_INCREMENT> {alterExp.setOperation(AlterOperation.SET_TABLE_OPTION);}
["=" { alterExp.setUseEqual(true);} ]
tk=<S_LONG> {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1479,4 +1479,25 @@ public void testImportTablespace() throws JSQLParserException {
alter.getAlterExpressions().get(0).getOperation().toString());
assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testAlterTableKeys() throws JSQLParserException {
// Test for DISABLE KEYS
String sqlDisable = "ALTER TABLE tbl_name DISABLE KEYS";
Statement stmtDisable = CCJSqlParserUtil.parse(sqlDisable);
assertTrue(stmtDisable instanceof Alter);
Alter alterDisable = (Alter) stmtDisable;
assertEquals("tbl_name", alterDisable.getTable().getFullyQualifiedName());
AlterExpression alterExpDisable = alterDisable.getAlterExpressions().get(0);
assertEquals(AlterOperation.DISABLE_KEYS, alterExpDisable.getOperation());

// Test for ENABLE KEYS
String sqlEnable = "ALTER TABLE tbl_name ENABLE KEYS";
Statement stmtEnable = CCJSqlParserUtil.parse(sqlEnable);
assertTrue(stmtEnable instanceof Alter);
Alter alterEnable = (Alter) stmtEnable;
assertEquals("tbl_name", alterEnable.getTable().getFullyQualifiedName());
AlterExpression alterExpEnable = alterEnable.getAlterExpressions().get(0);
assertEquals(AlterOperation.ENABLE_KEYS, alterExpEnable.getOperation());
}
}
Loading