Skip to content

Commit bfb8023

Browse files
Gikkmanwumpz
authored andcommitted
Added support for 'ALTER TABLE CHANGE COLUMN' (#741)
* Added support for 'ALTER TABLE CHANGE COLUMN oldName newName columnDefinition'. Please see https://dev.mysql.com/doc/refman/8.0/en/alter-table.html for reference. * Returned import ordering to avoid conflicts * Improved the tests somewhat Now also test the getOptionalSpecifier() for both cases (null and not-null) * Expanded tests for ALTER TABLE ... CHANGE
1 parent 07b8676 commit bfb8023

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
public class AlterExpression {
3838

3939
private AlterOperation operation;
40+
private String optionalSpecifier;
4041
private String columnName;
42+
private String columnOldName;
4143
//private ColDataType dataType;
4244

4345
private List<ColumnDataType> colDataTypeList;
@@ -66,6 +68,14 @@ public void setOperation(AlterOperation operation) {
6668
this.operation = operation;
6769
}
6870

71+
public String getOptionalSpecifier() {
72+
return optionalSpecifier;
73+
}
74+
75+
public void setOptionalSpecifier(String optionalSpecifier) {
76+
this.optionalSpecifier = optionalSpecifier;
77+
}
78+
6979
public boolean isOnDeleteCascade() {
7080
return onDeleteCascade;
7181
}
@@ -137,6 +147,14 @@ public void setColumnName(String columnName) {
137147
this.columnName = columnName;
138148
}
139149

150+
public String getColOldName() {
151+
return columnOldName;
152+
}
153+
154+
public void setColOldName(String columnOldName) {
155+
this.columnOldName = columnOldName;
156+
}
157+
140158
public String getConstraintName() {
141159
return this.constraintName;
142160
}
@@ -214,7 +232,12 @@ public String toString() {
214232
if (columnName != null) {
215233
b.append("COLUMN ").append(columnName);
216234
} else if (getColDataTypeList() != null) {
217-
if (colDataTypeList.size() > 1) {
235+
if(operation == AlterOperation.CHANGE) {
236+
if(optionalSpecifier != null) {
237+
b.append(optionalSpecifier).append(" ");
238+
}
239+
b.append(columnOldName).append(" ");
240+
} else if (colDataTypeList.size() > 1) {
218241
b.append("(");
219242
} else {
220243
b.append("COLUMN ");

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
158158
| <K_CASE:"CASE">
159159
| <K_CAST:"CAST">
160160
| <K_CHARACTER:"CHARACTER">
161+
| <K_CHANGE:"CHANGE">
161162
| <K_CHECK:"CHECK">
162163
| <K_CHAR:"CHAR">
163164
| <K_COLUMN:"COLUMN">
@@ -3866,6 +3867,22 @@ AlterExpression AlterExpression():
38663867
)
38673868
)
38683869
|
3870+
(<K_CHANGE>
3871+
{
3872+
alterExp.setOperation(AlterOperation.CHANGE);
3873+
}
3874+
(
3875+
<K_COLUMN> { alterExp.setOptionalSpecifier("COLUMN"); } | {}
3876+
)
3877+
(
3878+
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>)
3879+
alterExpressionColumnDataType = AlterExpressionColumnDataType() {
3880+
alterExp.setColOldName(tk.image);
3881+
alterExp.addColDataType(alterExpressionColumnDataType);
3882+
}
3883+
)
3884+
)
3885+
|
38693886
(<K_DROP>
38703887
{
38713888
alterExp.setOperation(AlterOperation.DROP);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ public void testAlterTableAlterColumn() throws JSQLParserException {
262262
assertSqlCanBeParsedAndDeparsed("ALTER TABLE table_name ALTER COLUMN column_name_1 TYPE TIMESTAMP, ALTER COLUMN column_name_2 TYPE BOOLEAN");
263263
}
264264

265+
@Test
266+
public void testAlterTableChangeColumn1() throws JSQLParserException {
267+
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE tb_test CHANGE COLUMN c1 c2 INT (10)");
268+
Alter alter = (Alter) stmt;
269+
assertEquals(AlterOperation.CHANGE, alter.getAlterExpressions().get(0).getOperation());
270+
assertEquals("c1", alter.getAlterExpressions().get(0).getColOldName());
271+
assertEquals("COLUMN", alter.getAlterExpressions().get(0).getOptionalSpecifier());
272+
}
273+
274+
@Test
275+
public void testAlterTableChangeColumn2() throws JSQLParserException {
276+
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE tb_test CHANGE c1 c2 INT (10)");
277+
Alter alter = (Alter) stmt;
278+
assertEquals(AlterOperation.CHANGE, alter.getAlterExpressions().get(0).getOperation());
279+
assertEquals("c1", alter.getAlterExpressions().get(0).getColOldName());
280+
assertNull(alter.getAlterExpressions().get(0).getOptionalSpecifier());
281+
}
282+
283+
@Test
284+
public void testAlterTableChangeColumn3() throws JSQLParserException {
285+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE tb_test CHANGE COLUMN c1 c2 INT (10)");
286+
}
287+
288+
@Test
289+
public void testAlterTableChangeColumn4() throws JSQLParserException {
290+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE tb_test CHANGE c1 c2 INT (10)");
291+
}
292+
265293
@Test
266294
public void testAlterTableAddColumnWithZone() throws JSQLParserException {
267295
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");

0 commit comments

Comments
 (0)