Skip to content

Commit a88e921

Browse files
Add some flexibility to the Alter Statement (#1293)
in order to allow: ALTER TABLE ... MOVE TABLESPACE ... ALTER TABLE ... COMPRESS NOLOGGING ALTER TABLE ... ROWFORMAT=DYNAMIC Fixes #1033
1 parent 4ac5ab4 commit a88e921

4 files changed

Lines changed: 124 additions & 89 deletions

File tree

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

Lines changed: 92 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -360,101 +360,105 @@ public void setUk(boolean uk) {
360360
}
361361

362362
@Override
363-
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
363+
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", "PMD.ExcessiveMethodLength"})
364364
public String toString() {
365365

366366
StringBuilder b = new StringBuilder();
367-
368-
b.append(operation).append(" ");
369-
370-
if (commentText != null) {
371-
if (columnName != null) {
372-
b.append(columnName).append(" COMMENT ");
373-
}
374-
b.append(commentText);
375-
} else if (columnName != null) {
376-
if (hasColumn) {
377-
b.append("COLUMN ");
378-
}
379-
if (operation == AlterOperation.RENAME) {
380-
b.append(columnOldName).append(" TO ");
381-
}
382-
b.append(columnName);
383-
} else if (getColDataTypeList() != null) {
384-
if (operation == AlterOperation.CHANGE) {
385-
if (optionalSpecifier != null) {
386-
b.append(optionalSpecifier).append(" ");
367+
368+
if (operation== AlterOperation.UNSPECIFIC) {
369+
b.append(optionalSpecifier);
370+
} else {
371+
b.append(operation).append(" ");
372+
373+
if (commentText != null) {
374+
if (columnName != null) {
375+
b.append(columnName).append(" COMMENT ");
376+
}
377+
b.append(commentText);
378+
} else if (columnName != null) {
379+
if (hasColumn) {
380+
b.append("COLUMN ");
381+
}
382+
if (operation == AlterOperation.RENAME) {
383+
b.append(columnOldName).append(" TO ");
384+
}
385+
b.append(columnName);
386+
} else if (getColDataTypeList() != null) {
387+
if (operation == AlterOperation.CHANGE) {
388+
if (optionalSpecifier != null) {
389+
b.append(optionalSpecifier).append(" ");
390+
}
391+
b.append(columnOldName).append(" ");
392+
} else if (colDataTypeList.size() > 1) {
393+
b.append("(");
394+
} else {
395+
if (hasColumn) {
396+
b.append("COLUMN ");
397+
}
398+
}
399+
b.append(PlainSelect.getStringList(colDataTypeList));
400+
if (colDataTypeList.size() > 1) {
401+
b.append(")");
402+
}
403+
} else if (getColumnDropNotNullList() != null) {
404+
if (operation == AlterOperation.CHANGE) {
405+
if (optionalSpecifier != null) {
406+
b.append(optionalSpecifier).append(" ");
407+
}
408+
b.append(columnOldName).append(" ");
409+
} else if (columnDropNotNullList.size() > 1) {
410+
b.append("(");
411+
} else {
412+
b.append("COLUMN ");
413+
}
414+
b.append(PlainSelect.getStringList(columnDropNotNullList));
415+
if (columnDropNotNullList.size() > 1) {
416+
b.append(")");
417+
}
418+
} else if (constraintName != null) {
419+
b.append("CONSTRAINT ");
420+
if (constraintIfExists) {
421+
b.append("IF EXISTS ");
422+
}
423+
b.append(constraintName);
424+
} else if (pkColumns != null) {
425+
b.append("PRIMARY KEY (").append(PlainSelect.getStringList(pkColumns)).append(')');
426+
} else if (ukColumns != null) {
427+
b.append("UNIQUE");
428+
if (ukName != null) {
429+
if (getUk()) {
430+
b.append(" KEY ");
431+
} else {
432+
b.append(" INDEX ");
433+
}
434+
b.append(ukName);
435+
}
436+
b.append(" (").append(PlainSelect.getStringList(ukColumns)).append(")");
437+
} else if (fkColumns != null) {
438+
b.append("FOREIGN KEY (")
439+
.append(PlainSelect.getStringList(fkColumns))
440+
.append(") REFERENCES ")
441+
.append(
442+
fkSourceSchema != null && fkSourceSchema.trim().length() > 0
443+
? fkSourceSchema + "."
444+
: "")
445+
.append(fkSourceTable)
446+
.append(" (")
447+
.append(PlainSelect.getStringList(fkSourceColumns))
448+
.append(")");
449+
referentialActions.forEach(b::append);
450+
} else if (index != null) {
451+
b.append(index);
387452
}
388-
b.append(columnOldName).append(" ");
389-
} else if (colDataTypeList.size() > 1) {
390-
b.append("(");
391-
} else {
392-
if (hasColumn) {
393-
b.append("COLUMN ");
453+
if (getConstraints() != null && !getConstraints().isEmpty()) {
454+
b.append(' ').append(PlainSelect.getStringList(constraints, false, false));
394455
}
395-
}
396-
b.append(PlainSelect.getStringList(colDataTypeList));
397-
if (colDataTypeList.size() > 1) {
398-
b.append(")");
399-
}
400-
} else if (getColumnDropNotNullList() != null) {
401-
if (operation == AlterOperation.CHANGE) {
402-
if (optionalSpecifier != null) {
403-
b.append(optionalSpecifier).append(" ");
456+
if (getUseEqual()) {
457+
b.append('=');
404458
}
405-
b.append(columnOldName).append(" ");
406-
} else if (columnDropNotNullList.size() > 1) {
407-
b.append("(");
408-
} else {
409-
b.append("COLUMN ");
410-
}
411-
b.append(PlainSelect.getStringList(columnDropNotNullList));
412-
if (columnDropNotNullList.size() > 1) {
413-
b.append(")");
414-
}
415-
} else if (constraintName != null) {
416-
b.append("CONSTRAINT ");
417-
if (constraintIfExists) {
418-
b.append("IF EXISTS ");
419-
}
420-
b.append(constraintName);
421-
} else if (pkColumns != null) {
422-
b.append("PRIMARY KEY (").append(PlainSelect.getStringList(pkColumns)).append(')');
423-
} else if (ukColumns != null) {
424-
b.append("UNIQUE");
425-
if (ukName != null) {
426-
if (getUk()) {
427-
b.append(" KEY ");
428-
} else {
429-
b.append(" INDEX ");
459+
if (parameters != null && !parameters.isEmpty()) {
460+
b.append(' ').append(PlainSelect.getStringList(parameters, false, false));
430461
}
431-
b.append(ukName);
432-
}
433-
b.append(" (").append(PlainSelect.getStringList(ukColumns)).append(")");
434-
} else if (fkColumns != null) {
435-
b.append("FOREIGN KEY (")
436-
.append(PlainSelect.getStringList(fkColumns))
437-
.append(") REFERENCES ")
438-
.append(
439-
fkSourceSchema != null && fkSourceSchema.trim().length() > 0
440-
? fkSourceSchema + "."
441-
: "")
442-
.append(fkSourceTable)
443-
.append(" (")
444-
.append(PlainSelect.getStringList(fkSourceColumns))
445-
.append(")");
446-
referentialActions.forEach(b::append);
447-
} else if (index != null) {
448-
b.append(index);
449-
}
450-
if (getConstraints() != null && !getConstraints().isEmpty()) {
451-
b.append(' ').append(PlainSelect.getStringList(constraints, false, false));
452-
}
453-
if (getUseEqual()) {
454-
b.append('=');
455-
}
456-
if (parameters != null && !parameters.isEmpty()) {
457-
b.append(' ').append(PlainSelect.getStringList(parameters, false, false));
458462
}
459463

460464
return b.toString();

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, RENAME, COMMENT;
13+
ADD, ALTER, DROP, MODIFY, CHANGE, ALGORITHM, RENAME, COMMENT, UNSPECIFIC;
1414
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5306,6 +5306,9 @@ AlterExpression AlterExpression():
53065306
AlterExpression.ColumnDataType alterExpressionColumnDataType = null;
53075307
AlterExpression.ColumnDropNotNull alterExpressionColumnDropNotNull = null;
53085308
ReferentialAction.Action action = null;
5309+
5310+
// for captureRest()
5311+
List<String> tokens = new LinkedList<String>();
53095312
}
53105313
{
53115314

@@ -5515,6 +5518,22 @@ AlterExpression AlterExpression():
55155518
(<K_COMMENT> {alterExp.setOperation(AlterOperation.COMMENT);}
55165519
tk=<S_CHAR_LITERAL> { alterExp.setCommentText(tk.image); }
55175520
)
5521+
|
5522+
tokens = captureRest() {
5523+
alterExp.setOperation(AlterOperation.UNSPECIFIC);
5524+
StringBuilder optionalSpecifier = new StringBuilder();
5525+
int i=0;
5526+
5527+
for (String s: tokens)
5528+
if (!s.equals(";")) {
5529+
if (i>0)
5530+
optionalSpecifier.append( " " );
5531+
optionalSpecifier.append( s );
5532+
i++;
5533+
}
5534+
5535+
alterExp.setOptionalSpecifier( optionalSpecifier.toString() );
5536+
}
55185537
)
55195538

55205539
{

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,5 +747,17 @@ private void assertReferentialAction(Alter parsed, Action onUpdate, Action onDel
747747
assertNull(alterExpression.getReferentialAction(Type.UPDATE));
748748
}
749749
}
750+
751+
@Test
752+
public void testRowFormatKeywordIssue1033() throws JSQLParserException {
753+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE basic_test_case "
754+
+ "ADD COLUMN display_name varchar(512) NOT NULL DEFAULT '' AFTER name"
755+
+ ", ADD KEY test_case_status (test_case_status)"
756+
+ ", add KEY display_name (display_name), ROW_FORMAT=DYNAMIC", true);
757+
758+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE t1 MOVE TABLESPACE users", true);
759+
760+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE test_tab MOVE PARTITION test_tab_q2 COMPRESS", true);
761+
}
750762

751763
}

0 commit comments

Comments
 (0)