Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ protected boolean isPartitionOperation() {
switch (operation) {
case DISCARD_PARTITION:
case IMPORT_PARTITION:
case ATTACH_PARTITION:
case DETACH_PARTITION:
case TRUNCATE_PARTITION:
case COALESCE_PARTITION:
case REORGANIZE_PARTITION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,75 @@
*/
package net.sf.jsqlparser.statement.alter;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.create.table.PartitionBound;

/**
* Internal subclass for partition maintenance operations within ALTER TABLE. Handles TRUNCATE,
* COALESCE, REORGANIZE, EXCHANGE, ANALYZE, CHECK, OPTIMIZE, REBUILD, REPAIR PARTITION, PARTITION
* BY, and REMOVE PARTITIONING.
*/
public class AlterExpressionPartition extends AlterExpression {

public enum DetachMode {
CONCURRENTLY, FINALIZE
}

private Table partitionTable;
private PartitionBound partitionBound;
private DetachMode detachMode;

public Table getPartitionTable() {
return partitionTable;
}

public void setPartitionTable(Table partitionTable) {
this.partitionTable = partitionTable;
}

public PartitionBound getPartitionBound() {
return partitionBound;
}

public void setPartitionBound(PartitionBound partitionBound) {
this.partitionBound = partitionBound;
}

public DetachMode getDetachMode() {
return detachMode;
}

public void setDetachMode(DetachMode detachMode) {
this.detachMode = detachMode;
}

public AlterExpressionPartition withPartitionTable(Table partitionTable) {
setPartitionTable(partitionTable);
return this;
}

public AlterExpressionPartition withPartitionBound(PartitionBound partitionBound) {
setPartitionBound(partitionBound);
return this;
}

public AlterExpressionPartition withDetachMode(DetachMode detachMode) {
setDetachMode(detachMode);
return this;
}

@Override
protected void appendBody(StringBuilder b) {
toStringPartition(b);
if (getOperation() == AlterOperation.ATTACH_PARTITION) {
b.append("ATTACH PARTITION ").append(partitionTable).append(" ")
.append(partitionBound);
} else if (getOperation() == AlterOperation.DETACH_PARTITION) {
b.append("DETACH PARTITION ").append(partitionTable);
if (detachMode != null) {
b.append(" ").append(detachMode);
}
} else {
toStringPartition(b);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Locale;

public enum AlterOperation {
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, KEY_BLOCK_SIZE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS, ENABLE_ROW_LEVEL_SECURITY, DISABLE_ROW_LEVEL_SECURITY, FORCE_ROW_LEVEL_SECURITY, NO_FORCE_ROW_LEVEL_SECURITY;
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, ATTACH_PARTITION, DETACH_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, KEY_BLOCK_SIZE, LOCK, DISCARD_TABLESPACE, IMPORT_TABLESPACE, DISABLE_KEYS, ENABLE_KEYS, ENABLE_ROW_LEVEL_SECURITY, DISABLE_ROW_LEVEL_SECURITY, FORCE_ROW_LEVEL_SECURITY, NO_FORCE_ROW_LEVEL_SECURITY;

public static AlterOperation from(String operation) {
return Enum.valueOf(AlterOperation.class, operation.toUpperCase(Locale.ROOT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class CreateTable implements Statement {
private boolean ifNotExists = false;
private boolean orReplace = false;
private TablePartitioning partitioning;
private Table partitionOf;
private PartitionBound partitionBound;

private RowMovement rowMovement;

Expand Down Expand Up @@ -157,6 +159,22 @@ public void setPartitioning(TablePartitioning partitioning) {
this.partitioning = partitioning;
}

public Table getPartitionOf() {
return partitionOf;
}

public void setPartitionOf(Table partitionOf) {
this.partitionOf = partitionOf;
}

public PartitionBound getPartitionBound() {
return partitionBound;
}

public void setPartitionBound(PartitionBound partitionBound) {
this.partitionBound = partitionBound;
}

public boolean isSelectParenthesis() {
return selectParenthesis;
}
Expand All @@ -179,6 +197,9 @@ public String toString() {
StringBuilder b = new StringBuilder();
appendCreateClause(b);
appendColumnDefinitions(b);
if (partitionBound != null) {
b.append(" ").append(partitionBound);
}
appendTableOptions(b);
appendTableProperties(b);
return b.toString();
Expand All @@ -202,6 +223,9 @@ private void appendCreateClause(StringBuilder b) {
b.append("IF NOT EXISTS ");
}
b.append(table);
if (partitionOf != null) {
b.append(" PARTITION OF ").append(partitionOf);
}
}

private void appendColumnDefinitions(StringBuilder b) {
Expand Down Expand Up @@ -289,6 +313,16 @@ public CreateTable withPartitioning(TablePartitioning partitioning) {
return this;
}

public CreateTable withPartitionOf(Table partitionOf) {
setPartitionOf(partitionOf);
return this;
}

public CreateTable withPartitionBound(PartitionBound partitionBound) {
setPartitionBound(partitionBound);
return this;
}

public CreateTable withRowMovement(RowMovement rowMovement) {
this.setRowMovement(rowMovement);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create.table;

import java.io.Serializable;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;

/** A PostgreSQL declarative-partition bound. */
public class PartitionBound implements Serializable {

public enum Type {
RANGE, LIST, HASH, DEFAULT
}

private Type type;
private ExpressionList<Expression> fromExpressions;
private ExpressionList<Expression> toExpressions;
private ExpressionList<Expression> inExpressions;
private Expression modulus;
private Expression remainder;

public PartitionBound() {}

public PartitionBound(Type type) {
this.type = type;
}

public Type getType() {
return type;
}

public void setType(Type type) {
this.type = type;
}

public ExpressionList<Expression> getFromExpressions() {
return fromExpressions;
}

public void setFromExpressions(ExpressionList<Expression> fromExpressions) {
this.fromExpressions = fromExpressions;
}

public ExpressionList<Expression> getToExpressions() {
return toExpressions;
}

public void setToExpressions(ExpressionList<Expression> toExpressions) {
this.toExpressions = toExpressions;
}

public ExpressionList<Expression> getInExpressions() {
return inExpressions;
}

public void setInExpressions(ExpressionList<Expression> inExpressions) {
this.inExpressions = inExpressions;
}

public Expression getModulus() {
return modulus;
}

public void setModulus(Expression modulus) {
this.modulus = modulus;
}

public Expression getRemainder() {
return remainder;
}

public void setRemainder(Expression remainder) {
this.remainder = remainder;
}

public PartitionBound withType(Type type) {
setType(type);
return this;
}

public PartitionBound withFromExpressions(ExpressionList<Expression> fromExpressions) {
setFromExpressions(fromExpressions);
return this;
}

public PartitionBound withToExpressions(ExpressionList<Expression> toExpressions) {
setToExpressions(toExpressions);
return this;
}

public PartitionBound withInExpressions(ExpressionList<Expression> inExpressions) {
setInExpressions(inExpressions);
return this;
}

public PartitionBound withModulus(Expression modulus) {
setModulus(modulus);
return this;
}

public PartitionBound withRemainder(Expression remainder) {
setRemainder(remainder);
return this;
}

@Override
public String toString() {
switch (type) {
case RANGE:
return "FOR VALUES FROM (" + fromExpressions + ") TO (" + toExpressions + ")";
case LIST:
return "FOR VALUES IN (" + inExpressions + ")";
case HASH:
return "FOR VALUES WITH (MODULUS " + modulus + ", REMAINDER " + remainder + ")";
case DEFAULT:
return "DEFAULT";
default:
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum Type {
private boolean linear;
private boolean columnsSyntax;
private Expression expression;
private ExpressionList<Expression> expressionList;
private ExpressionList<Column> columns;
private Integer algorithm;
private boolean algorithmUseEquals;
Expand Down Expand Up @@ -79,6 +80,19 @@ public void setExpression(Expression expression) {
this.expression = expression;
}

/**
* Returns the ordered partition key expressions when more than one key was declared.
*
* @return partition key expressions, or {@code null} for the legacy single-expression form
*/
public ExpressionList<Expression> getExpressionList() {
return expressionList;
}

public void setExpressionList(ExpressionList<Expression> expressionList) {
this.expressionList = expressionList;
}

public ExpressionList<Column> getColumns() {
return columns;
}
Expand Down Expand Up @@ -155,6 +169,11 @@ public TablePartitioning withExpression(Expression expression) {
return this;
}

public TablePartitioning withExpressionList(ExpressionList<Expression> expressionList) {
setExpressionList(expressionList);
return this;
}

public TablePartitioning withColumns(ExpressionList<Column> columns) {
setColumns(columns);
return this;
Expand Down Expand Up @@ -273,6 +292,8 @@ private void appendMethod(StringBuilder builder) {
}
if (expression != null) {
builder.append(" (").append(expression).append(")");
} else if (expressionList != null) {
builder.append(" (").append(expressionList).append(")");
} else if (columns != null) {
builder.append(" ").append(PlainSelect.getStringList(columns, true, true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void deParse(CreateTable createTable) {
builder.append("IF NOT EXISTS ");
}
builder.append(createTable.getTable().getFullyQualifiedName());
if (createTable.getPartitionOf() != null) {
builder.append(" PARTITION OF ")
.append(createTable.getPartitionOf().getFullyQualifiedName());
}

if (createTable.getColumns() != null && !createTable.getColumns().isEmpty()) {
builder.append(" (");
Expand Down Expand Up @@ -92,6 +96,10 @@ public void deParse(CreateTable createTable) {
builder.append(")");
}

if (createTable.getPartitionBound() != null) {
builder.append(' ').append(createTable.getPartitionBound());
}

params = PlainSelect.getStringList(createTable.getTableOptionsStrings(), false, false);
if (!"".equals(params)) {
builder.append(' ').append(params);
Expand Down
Loading
Loading