Skip to content

Commit 3b89cd2

Browse files
committed
fixes #918
1 parent 8de0fd9 commit 3b89cd2

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class AlterExpression {
2626
//private ColDataType dataType;
2727

2828
private List<ColumnDataType> colDataTypeList;
29+
private List<ColumnDropNotNull> columnDropNotNullList;
2930

3031
private List<String> pkColumns;
3132
private List<String> ukColumns;
@@ -115,6 +116,13 @@ public void addColDataType(ColumnDataType columnDataType) {
115116
}
116117
colDataTypeList.add(columnDataType);
117118
}
119+
120+
public void addColDropNotNull(ColumnDropNotNull columnDropNotNull) {
121+
if (columnDropNotNullList == null) {
122+
columnDropNotNullList = new ArrayList<ColumnDropNotNull>();
123+
}
124+
columnDropNotNullList.add(columnDropNotNull);
125+
}
118126

119127
public List<String> getFkSourceColumns() {
120128
return fkSourceColumns;
@@ -196,6 +204,10 @@ public void setConstraints(List<ConstraintState> constraints) {
196204
this.constraints = constraints;
197205
}
198206

207+
public List<ColumnDropNotNull> getColumnDropNotNullList() {
208+
return columnDropNotNullList;
209+
}
210+
199211
public void addParameters(String... params) {
200212
if (parameters == null) {
201213
parameters = new ArrayList<String>();
@@ -247,6 +259,21 @@ public String toString() {
247259
if (colDataTypeList.size() > 1) {
248260
b.append(")");
249261
}
262+
} else if ( getColumnDropNotNullList() != null) {
263+
if (operation == AlterOperation.CHANGE) {
264+
if (optionalSpecifier != null) {
265+
b.append(optionalSpecifier).append(" ");
266+
}
267+
b.append(columnOldName).append(" ");
268+
} else if (columnDropNotNullList.size() > 1) {
269+
b.append("(");
270+
} else {
271+
b.append("COLUMN ");
272+
}
273+
b.append(PlainSelect.getStringList(columnDropNotNullList));
274+
if (columnDropNotNullList.size() > 1) {
275+
b.append(")");
276+
}
250277
} else if (constraintName != null) {
251278
b.append("CONSTRAINT ");
252279
if (constraintIfExists) {
@@ -335,4 +362,28 @@ private String parametersToString() {
335362
}
336363
}
337364

365+
public static class ColumnDropNotNull {
366+
367+
private final String columnName;
368+
private final boolean withNot;
369+
370+
public ColumnDropNotNull(String columnName, boolean withNot) {
371+
this.columnName = columnName;
372+
this.withNot = withNot;
373+
}
374+
375+
public String getColumnName() {
376+
return columnName;
377+
}
378+
379+
public boolean isWithNot() {
380+
return withNot;
381+
}
382+
383+
@Override
384+
public String toString() {
385+
return columnName + " DROP" +
386+
(withNot ? " NOT " : " ") + "NULL";
387+
}
388+
}
338389
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4133,6 +4133,24 @@ AlterExpression.ColumnDataType AlterExpressionColumnDataType():
41334133
}
41344134
}
41354135

4136+
AlterExpression.ColumnDropNotNull AlterExpressionColumnDropNotNull():
4137+
{
4138+
String columnName = null;
4139+
boolean withNot = false;
4140+
ColDataType dataType = null;
4141+
List<String> columnSpecs = null;
4142+
List<String> parameter = null;
4143+
}
4144+
{
4145+
columnName = RelObjectName()
4146+
<K_DROP>
4147+
(<K_NOT> { withNot = true; } )?
4148+
<K_NULL>
4149+
{
4150+
return new AlterExpression.ColumnDropNotNull(columnName, withNot);
4151+
}
4152+
}
4153+
41364154
List<ConstraintState> AlterExpressionConstraintState():
41374155
{
41384156
List<ConstraintState> retval = new ArrayList<ConstraintState>();
@@ -4184,6 +4202,7 @@ AlterExpression AlterExpression():
41844202
Index index = null;
41854203
Table fkTable = null;
41864204
AlterExpression.ColumnDataType alterExpressionColumnDataType = null;
4205+
AlterExpression.ColumnDropNotNull alterExpressionColumnDropNotNull = null;
41874206
}
41884207
{
41894208

@@ -4211,7 +4230,9 @@ AlterExpression AlterExpression():
42114230
)
42124231
|
42134232
( (LOOKAHEAD(2) <K_COLUMN>)?
4214-
alterExpressionColumnDataType = AlterExpressionColumnDataType() { alterExp.addColDataType(alterExpressionColumnDataType); }
4233+
(LOOKAHEAD(2) alterExpressionColumnDataType = AlterExpressionColumnDataType() { alterExp.addColDataType(alterExpressionColumnDataType); }
4234+
|
4235+
alterExpressionColumnDropNotNull = AlterExpressionColumnDropNotNull() { alterExp.addColDropNotNull( alterExpressionColumnDropNotNull); })
42154236
)
42164237
|
42174238
(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,9 @@ public void testIssue259() throws JSQLParserException {
388388
public void testIssue633_2() throws JSQLParserException {
389389
assertSqlCanBeParsedAndDeparsed("CREATE INDEX idx_american_football_action_plays_1 ON american_football_action_plays USING btree (play_type)");
390390
}
391+
392+
@Test
393+
public void testAlterTableAlterColumnDropNotNullIssue918() throws JSQLParserException {
394+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE \"user_table_t\" ALTER COLUMN name DROP NOT NULL");
395+
}
391396
}

0 commit comments

Comments
 (0)