Skip to content

Commit 98e7bf6

Browse files
authored
Add support for MySQL ALTER TABLE partition options in parser (#2211)
1 parent d60c762 commit 98e7bf6

File tree

5 files changed

+429
-32
lines changed

5 files changed

+429
-32
lines changed

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

+85-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package net.sf.jsqlparser.statement.alter;
1111

12+
import java.util.stream.Collectors;
1213
import net.sf.jsqlparser.expression.Expression;
1314
import net.sf.jsqlparser.statement.ReferentialAction;
1415
import net.sf.jsqlparser.statement.ReferentialAction.Action;
@@ -79,6 +80,11 @@ public class AlterExpression implements Serializable {
7980
private String partitionType;
8081
private Expression partitionExpression;
8182
private List<String> partitionColumns;
83+
private int coalescePartitionNumber;
84+
85+
private String exchangePartitionTableName;
86+
private boolean exchangePartitionWithValidation;
87+
private boolean exchangePartitionWithoutValidation;
8288

8389

8490
public Index getOldIndex() {
@@ -524,6 +530,38 @@ public List<String> getPartitionColumns() {
524530
return partitionColumns;
525531
}
526532

533+
public void setExchangePartitionTableName(String exchangePartitionTableName) {
534+
this.exchangePartitionTableName = exchangePartitionTableName;
535+
}
536+
537+
public String getExchangePartitionTableName() {
538+
return exchangePartitionTableName;
539+
}
540+
541+
public void setCoalescePartitionNumber(int coalescePartitionNumber) {
542+
this.coalescePartitionNumber = coalescePartitionNumber;
543+
}
544+
545+
public int getCoalescePartitionNumber() {
546+
return coalescePartitionNumber;
547+
}
548+
549+
public void setExchangePartitionWithValidation(boolean exchangePartitionWithValidation) {
550+
this.exchangePartitionWithValidation = exchangePartitionWithValidation;
551+
}
552+
553+
public boolean isExchangePartitionWithValidation() {
554+
return exchangePartitionWithValidation;
555+
}
556+
557+
public void setExchangePartitionWithoutValidation(boolean exchangePartitionWithoutValidation) {
558+
this.exchangePartitionWithoutValidation = exchangePartitionWithoutValidation;
559+
}
560+
561+
public boolean isExchangePartitionWithoutValidation() {
562+
return exchangePartitionWithoutValidation;
563+
}
564+
527565
@Override
528566
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
529567
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
@@ -628,31 +666,63 @@ public String toString() {
628666
&& !pkColumns.isEmpty()) {
629667
// Oracle Multi Column Drop
630668
b.append("DROP (").append(PlainSelect.getStringList(pkColumns)).append(')');
669+
} else if (operation == AlterOperation.DISCARD_PARTITION && partitions != null) {
670+
b.append("DISCARD PARTITION ").append(PlainSelect.getStringList(partitions));
671+
if (tableOption != null) {
672+
b.append(" ").append(tableOption);
673+
}
674+
} else if (operation == AlterOperation.IMPORT_PARTITION) {
675+
b.append("IMPORT PARTITION ").append(PlainSelect.getStringList(partitions));
676+
if (tableOption != null) {
677+
b.append(" ").append(tableOption);
678+
}
631679
} else if (operation == AlterOperation.TRUNCATE_PARTITION
632680
&& partitions != null) {
633681
b.append("TRUNCATE PARTITION ").append(PlainSelect.getStringList(partitions));
682+
} else if (operation == AlterOperation.COALESCE_PARTITION) {
683+
b.append("COALESCE PARTITION ").append(coalescePartitionNumber);
684+
} else if (operation == AlterOperation.REORGANIZE_PARTITION
685+
&& partitions != null
686+
&& partitionDefinitions != null) {
687+
b.append("REORGANIZE PARTITION ")
688+
.append(PlainSelect.getStringList(partitions))
689+
.append(" INTO (")
690+
.append(partitionDefinitions.stream()
691+
.map(PartitionDefinition::toString)
692+
.collect(Collectors.joining(", ")))
693+
.append(")");
694+
} else if (operation == AlterOperation.EXCHANGE_PARTITION) {
695+
b.append("EXCHANGE PARTITION ");
696+
b.append(partitions.get(0)).append(" WITH TABLE ").append(exchangePartitionTableName);
697+
if (exchangePartitionWithValidation) {
698+
b.append(" WITH VALIDATION ");
699+
} else if (exchangePartitionWithoutValidation) {
700+
b.append(" WITHOUT VALIDATION ");
701+
}
702+
} else if (operation == AlterOperation.ANALYZE_PARTITION && partitions != null) {
703+
b.append("ANALYZE PARTITION ").append(PlainSelect.getStringList(partitions));
704+
} else if (operation == AlterOperation.CHECK_PARTITION && partitions != null) {
705+
b.append("CHECK PARTITION ").append(PlainSelect.getStringList(partitions));
706+
} else if (operation == AlterOperation.OPTIMIZE_PARTITION && partitions != null) {
707+
b.append("OPTIMIZE PARTITION ").append(PlainSelect.getStringList(partitions));
708+
} else if (operation == AlterOperation.REBUILD_PARTITION && partitions != null) {
709+
b.append("REBUILD PARTITION ").append(PlainSelect.getStringList(partitions));
710+
} else if (operation == AlterOperation.REPAIR_PARTITION && partitions != null) {
711+
b.append("REPAIR PARTITION ").append(PlainSelect.getStringList(partitions));
712+
} else if (operation == AlterOperation.REMOVE_PARTITIONING) {
713+
b.append("REMOVE PARTITIONING");
634714
} else if (operation == AlterOperation.PARTITION_BY) {
635715
b.append("PARTITION BY ").append(partitionType).append(" ");
636716
if (partitionExpression != null) {
637717
b.append("(").append(partitionExpression).append(") ");
638718
} else if (partitionColumns != null && !partitionColumns.isEmpty()) {
639719
b.append("COLUMNS(").append(String.join(", ", partitionColumns)).append(") ");
640720
}
641-
b.append("(");
642-
for (int i = 0; i < partitionDefinitions.size(); i++) {
643-
PartitionDefinition partition = partitionDefinitions.get(i);
644-
b.append("PARTITION ").append(partition.getPartitionName())
645-
.append(" ").append(partition.getPartitionOperation())
646-
.append(" (").append(PlainSelect.getStringList(partition.getValues()))
647-
.append(")");
648-
if (partition.getStorageEngine() != null) {
649-
b.append(" ENGINE = ").append(partition.getStorageEngine());
650-
}
651-
if (i < partitionDefinitions.size() - 1) {
652-
b.append(", ");
653-
}
654-
}
655-
b.append(")");
721+
722+
b.append("(").append(partitionDefinitions.stream()
723+
.map(PartitionDefinition::toString)
724+
.collect(Collectors.joining(", ")))
725+
.append(")");
656726
} else {
657727
if (operation == AlterOperation.COMMENT_WITH_EQUAL_SIGN) {
658728
b.append("COMMENT =").append(" ");

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
package net.sf.jsqlparser.statement.alter;
1111

1212
public enum AlterOperation {
13-
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, COLLATE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, PARTITION_BY, TRUNCATE_PARTITION, SET_TABLE_OPTION, ENGINE, FORCE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS;
13+
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, COLLATE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, ADD_PARTITION, DROP_PARTITION, DISCARD_PARTITION, IMPORT_PARTITION, TRUNCATE_PARTITION, COALESCE_PARTITION, REORGANIZE_PARTITION, EXCHANGE_PARTITION, ANALYZE_PARTITION, CHECK_PARTITION, OPTIMIZE_PARTITION, REBUILD_PARTITION, REPAIR_PARTITION, REMOVE_PARTITIONING, PARTITION_BY, SET_TABLE_OPTION, ENGINE, FORCE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS;
1414

1515
public static AlterOperation from(String operation) {
1616
return Enum.valueOf(AlterOperation.class, operation.toUpperCase());

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

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.Serializable;
1313
import java.util.List;
14+
import net.sf.jsqlparser.statement.select.PlainSelect;
1415

1516
public class PartitionDefinition implements Serializable {
1617
private String partitionName;
@@ -57,4 +58,17 @@ public String getStorageEngine() {
5758
public void setStorageEngine(String storageEngine) {
5859
this.storageEngine = storageEngine;
5960
}
61+
62+
@Override
63+
public String toString() {
64+
StringBuilder b = new StringBuilder();
65+
b.append("PARTITION ").append(partitionName)
66+
.append(" ").append(partitionOperation)
67+
.append(" (").append(PlainSelect.getStringList(values))
68+
.append(")");
69+
if (storageEngine != null) {
70+
b.append(" ENGINE = ").append(storageEngine);
71+
}
72+
return b.toString();
73+
}
6074
}

0 commit comments

Comments
 (0)