Skip to content

Commit aee3947

Browse files
AnEmortalKidJan Monterrubio
andauthored
implement feature (#972)
Co-authored-by: Jan Monterrubio <Jan.Monterrubio@Cerner.com>
1 parent 84c0d21 commit aee3947

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ public String toString() {
243243
b.append(operation).append(" ");
244244

245245
if (columnName != null) {
246-
b.append("COLUMN ").append(columnName);
246+
b.append("COLUMN ");
247+
if (operation == AlterOperation.RENAME) {
248+
b.append(columnOldName).append(" TO ");
249+
}
250+
b.append(columnName);
247251
} else if (getColDataTypeList() != null) {
248252
if (operation == AlterOperation.CHANGE) {
249253
if (optionalSpecifier != null) {

src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
package net.sf.jsqlparser.statement.alter;
1111

1212
public enum AlterOperation {
13-
ADD, ALTER, DROP, MODIFY, CHANGE, ALGORITHM;
13+
ADD, ALTER, DROP, MODIFY, CHANGE, ALGORITHM, RENAME;
1414
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
266266
| <K_REFERENCES:"REFERENCES">
267267
| <K_REGEXP: "REGEXP">
268268
| <K_RLIKE: "RLIKE">
269+
| <K_RENAME:"RENAME">
269270
| <K_REPLACE:"REPLACE">
270271
| <K_RESTRICT: "RESTRICT">
271272
| <K_RETURNING: "RETURNING">
@@ -4496,6 +4497,15 @@ AlterExpression AlterExpression():
44964497
["=" { alterExp.setUseEqual(true);} ]
44974498
sk3 = RelObjectName() {alterExp.addParameters(sk3); }
44984499
)
4500+
|
4501+
(<K_RENAME> {alterExp.setOperation(AlterOperation.RENAME);}
4502+
(
4503+
<K_COLUMN>
4504+
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { alterExp.setColOldName(tk.image);}
4505+
(<K_TO>)
4506+
(tk2=<S_IDENTIFIER> | tk2=<S_QUOTED_IDENTIFIER>) { alterExp.setColumnName(tk2.image);}
4507+
)
4508+
)
44994509
)
45004510

45014511
{

src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,16 @@ public void testAlterConstraintWithoutFKSourceColumnsIssue929() throws JSQLParse
402402
public void testAlterTableAlterColumnDropNotNullIssue918() throws JSQLParserException {
403403
assertSqlCanBeParsedAndDeparsed("ALTER TABLE \"user_table_t\" ALTER COLUMN name DROP NOT NULL");
404404
}
405+
406+
@Test
407+
public void testAlterTableRenameColumn() throws JSQLParserException {
408+
String sql = "ALTER TABLE \"test_table\" RENAME COLUMN \"test_column\" TO \"test_c\"";
409+
assertSqlCanBeParsedAndDeparsed(sql);
410+
411+
Alter alter= (Alter) CCJSqlParserUtil.parse(sql);
412+
AlterExpression expression = alter.getAlterExpressions().get(0);
413+
assertEquals(expression.getOperation(), AlterOperation.RENAME);
414+
assertEquals(expression.getColOldName(), "\"test_column\"");
415+
assertEquals(expression.getColumnName(), "\"test_c\"");
416+
}
405417
}

0 commit comments

Comments
 (0)