Skip to content

Commit 029fd42

Browse files
authored
fix: Fixes parsing failing for ALTER MODIFY queries not containing datatype (#1961)
* fix: Fixes parsing failing for ALTER MODIFY queries not containing datatype * fixed spotcheck formatting issues
1 parent 182f484 commit 029fd42

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,8 @@ public ColumnDataType(
773773

774774
@Override
775775
public String toString() {
776-
return getColumnName() + (withType ? " TYPE " : " ") + toStringDataTypeAndSpec();
776+
return getColumnName() + (withType ? " TYPE " : getColDataType() == null ? "" : " ")
777+
+ toStringDataTypeAndSpec();
777778
}
778779

779780
@Override

src/main/java/net/sf/jsqlparser/statement/create/table/ColumnDefinition.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public String toString() {
6969
}
7070

7171
public String toStringDataTypeAndSpec() {
72-
return colDataType + (columnSpecs != null && !columnSpecs.isEmpty()
73-
? " " + PlainSelect.getStringList(columnSpecs, false, false)
74-
: "");
72+
return (colDataType == null ? "" : colDataType)
73+
+ (columnSpecs != null && !columnSpecs.isEmpty()
74+
? " " + PlainSelect.getStringList(columnSpecs, false, false)
75+
: "");
7576
}
7677

7778
public ColumnDefinition withColumnName(String columnName) {

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -6144,9 +6144,9 @@ AlterExpression.ColumnDataType AlterExpressionColumnDataType():
61446144
List<String> parameter = null;
61456145
}
61466146
{
6147-
columnName = RelObjectName()
6147+
columnName = RelObjectName() { columnSpecs = new ArrayList<String>(); }
61486148
( LOOKAHEAD(2) <K_TYPE> { withType = true; } )?
6149-
dataType = ColDataType() { columnSpecs = new ArrayList<String>(); }
6149+
( LOOKAHEAD(2) dataType = ColDataType() )?
61506150
( parameter = CreateParameter() { columnSpecs.addAll(parameter); } )*
61516151
{
61526152
return new AlterExpression.ColumnDataType(columnName, withType, dataType, columnSpecs);
@@ -6291,6 +6291,10 @@ AlterExpression AlterExpression():
62916291
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
62926292
)
62936293
|
6294+
LOOKAHEAD(2) (
6295+
sk3=RelObjectName() <K_COMMENT> tk=<S_CHAR_LITERAL> { alterExp.withColumnName(sk3).withCommentText(tk.image); }
6296+
)
6297+
|
62946298
LOOKAHEAD(3) (
62956299
( LOOKAHEAD(2) <K_COLUMN> { alterExp.hasColumn(true); } )?
62966300
[ <K_IF> <K_NOT> <K_EXISTS> { alterExp.setUseIfNotExists(true); } ]
@@ -6433,8 +6437,6 @@ AlterExpression AlterExpression():
64336437
)
64346438
)
64356439
)
6436-
|
6437-
( sk3=RelObjectName() <K_COMMENT> tk=<S_CHAR_LITERAL> { alterExp.withColumnName(sk3).withCommentText(tk.image); } )
64386440
)
64396441
)
64406442
|

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

+28
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,34 @@ public void testAlterTableModifyColumn2() throws JSQLParserException {
360360
assertFalse(alterExpression.hasColumn());
361361
}
362362

363+
@Test
364+
public void testAlterTableModifyColumn3() throws JSQLParserException {
365+
Alter alter =
366+
(Alter) CCJSqlParserUtil.parse("ALTER TABLE mytable modify col1 NULL");
367+
AlterExpression alterExpression = alter.getAlterExpressions().get(0);
368+
369+
// COLUMN keyword DOES NOT appear in deparsed statement, modify becomes all caps
370+
assertStatementCanBeDeparsedAs(alter, "ALTER TABLE mytable MODIFY col1 NULL");
371+
372+
assertEquals(AlterOperation.MODIFY, alterExpression.getOperation());
373+
374+
assertFalse(alterExpression.hasColumn());
375+
}
376+
377+
@Test
378+
public void testAlterTableModifyColumn4() throws JSQLParserException {
379+
Alter alter =
380+
(Alter) CCJSqlParserUtil.parse("ALTER TABLE mytable modify col1 DEFAULT 0");
381+
AlterExpression alterExpression = alter.getAlterExpressions().get(0);
382+
383+
// COLUMN keyword DOES NOT appear in deparsed statement, modify becomes all caps
384+
assertStatementCanBeDeparsedAs(alter, "ALTER TABLE mytable MODIFY col1 DEFAULT 0");
385+
386+
assertEquals(AlterOperation.MODIFY, alterExpression.getOperation());
387+
388+
assertFalse(alterExpression.hasColumn());
389+
}
390+
363391
@Test
364392
public void testAlterTableAlterColumn() throws JSQLParserException {
365393
// http://www.postgresqltutorial.com/postgresql-change-column-type/

0 commit comments

Comments
 (0)