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 @@ -32,6 +32,76 @@ public void setStructuredTableOption(TableOption option) {
super.setUseEqual(false);
}

@Override
public AlterOperation getOperation() {
if (structuredTableOption != null) {
switch (structuredTableOption.getKind()) {
case ENGINE:
return AlterOperation.ENGINE;
case KEY_BLOCK_SIZE:
return AlterOperation.KEY_BLOCK_SIZE;
case COMMENT:
return structuredTableOption.isUseEquals()
? AlterOperation.COMMENT_WITH_EQUAL_SIGN
: AlterOperation.COMMENT;
default:
return AlterOperation.SET_TABLE_OPTION;
}
}
return super.getOperation();
}

@Override
public String getEngineOption() {
return structuredTableOption != null
&& structuredTableOption.getKind() == TableOption.Kind.ENGINE
? structuredTableOption.getValue()
: super.getEngineOption();
}

@Override
public void setEngineOption(String value) {
if (structuredTableOption != null
&& structuredTableOption.getKind() == TableOption.Kind.ENGINE) {
structuredTableOption.setValue(value);
}
super.setEngineOption(value);
}

@Override
public int getKeyBlockSize() {
return structuredTableOption != null
&& structuredTableOption.getKind() == TableOption.Kind.KEY_BLOCK_SIZE
? Integer.parseInt(structuredTableOption.getValue())
: super.getKeyBlockSize();
}

@Override
public void setKeyBlockSize(int value) {
if (structuredTableOption != null
&& structuredTableOption.getKind() == TableOption.Kind.KEY_BLOCK_SIZE) {
structuredTableOption.setValue(Integer.toString(value));
}
super.setKeyBlockSize(value);
}

@Override
public String getCommentText() {
return structuredTableOption != null
&& structuredTableOption.getKind() == TableOption.Kind.COMMENT
? structuredTableOption.getValue()
: super.getCommentText();
}

@Override
public void setCommentText(String value) {
if (structuredTableOption != null
&& structuredTableOption.getKind() == TableOption.Kind.COMMENT) {
structuredTableOption.setValue(value);
}
super.setCommentText(value);
}

@Override
public String getTableOption() {
return structuredTableOption == null ? super.getTableOption()
Expand Down Expand Up @@ -60,6 +130,10 @@ public void setUseEqual(boolean useEqual) {

@Override
protected void appendBody(StringBuilder b) {
if (structuredTableOption != null) {
b.append(structuredTableOption);
return;
}
switch (getOperation()) {
case SET_TABLE_OPTION:
b.append(getTableOption());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
/** A structured option following a {@code CREATE TABLE} definition. */
public class TableOption implements Serializable {

private ForeignTableOptions foreignTableOptions;
private Kind kind = Kind.OTHER;
private String name;
private String value;
private boolean useEquals;
private List<String> tokens;
private List<Table> unionTables;
private List<Index.Option> storageParameters;
private ColumnOption.Storage tablespaceStorage;

public enum Kind {
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, ENCRYPTION, PASSWORD, DATA_DIRECTORY, INDEX_DIRECTORY, SECONDARY_ENGINE, AUTOEXTEND_SIZE, INSERT_METHOD, PACK_KEYS, DELAY_KEY_WRITE, CHECKSUM, CONNECTION, COMPRESSION, STORAGE_PARAMETERS, WITHOUT_OIDS, ENGINE_ATTRIBUTE, SECONDARY_ENGINE_ATTRIBUTE, ROW_FORMAT, FOREIGN_SERVER, OTHER
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, ENCRYPTION, PASSWORD, DATA_DIRECTORY, INDEX_DIRECTORY, SECONDARY_ENGINE, AUTOEXTEND_SIZE, INSERT_METHOD, PACK_KEYS, DELAY_KEY_WRITE, CHECKSUM, CONNECTION, COMPRESSION, STORAGE_PARAMETERS, WITHOUT_OIDS, ENGINE_ATTRIBUTE, SECONDARY_ENGINE_ATTRIBUTE, ROW_FORMAT, AVG_ROW_LENGTH, MAX_ROWS, MIN_ROWS, KEY_BLOCK_SIZE, TABLESPACE, FOREIGN_SERVER, OTHER
}

private ForeignTableOptions foreignTableOptions;

public ForeignTableOptions getForeignTableOptions() {
return foreignTableOptions;
}
Expand All @@ -36,13 +44,17 @@ public static TableOption foreignServer(ForeignTableOptions options) {
return option;
}

private Kind kind = Kind.OTHER;
private String name;
private String value;
private boolean useEquals;
private List<String> tokens;
private List<Table> unionTables;
private List<Index.Option> storageParameters;
public ColumnOption.Storage getTablespaceStorage() {
return tablespaceStorage;
}

public void setTablespaceStorage(ColumnOption.Storage storage) {
if (storage != null && storage != ColumnOption.Storage.DISK
&& storage != ColumnOption.Storage.MEMORY) {
throw new IllegalArgumentException("TABLESPACE storage must be DISK or MEMORY");
}
tablespaceStorage = storage;
}

public List<Index.Option> getStorageParameters() {
return storageParameters;
Expand All @@ -51,6 +63,7 @@ public List<Index.Option> getStorageParameters() {
public void setStorageParameters(List<Index.Option> storageParameters) {
this.storageParameters = storageParameters;
foreignTableOptions = null;
tablespaceStorage = null;
kind = Kind.STORAGE_PARAMETERS;
name = "WITH";
value = null;
Expand Down Expand Up @@ -96,6 +109,9 @@ public Kind getKind() {

public void setKind(Kind kind) {
this.kind = kind;
if (kind != Kind.TABLESPACE) {
tablespaceStorage = null;
}
if (kind != Kind.FOREIGN_SERVER) {
foreignTableOptions = null;
}
Expand Down Expand Up @@ -141,6 +157,7 @@ public List<Table> getUnionTables() {
public void setUnionTables(List<Table> unionTables) {
this.unionTables = unionTables;
foreignTableOptions = null;
tablespaceStorage = null;
kind = Kind.UNION;
name = "UNION";
value = null;
Expand Down Expand Up @@ -177,12 +194,17 @@ public List<String> getTokens() {
if (renderedValue != null) {
result.add(renderedValue);
}
if (kind == Kind.TABLESPACE && tablespaceStorage != null) {
result.add("STORAGE");
result.add(tablespaceStorage.name());
}
return Collections.unmodifiableList(result);
}

public void setTokens(List<String> tokens) {
this.tokens = tokens;
if (tokens != null) {
tablespaceStorage = null;
foreignTableOptions = null;
unionTables = null;
storageParameters = null;
Expand Down Expand Up @@ -218,6 +240,9 @@ public String toString() {
return PlainSelect.getStringList(tokens, false, false);
}
String renderedValue = getValue();
return name + (renderedValue != null ? (useEquals ? " = " : " ") + renderedValue : "");
return name + (renderedValue != null ? (useEquals ? " = " : " ") + renderedValue : "")
+ (kind == Kind.TABLESPACE && tablespaceStorage != null
? " STORAGE " + tablespaceStorage
: "");
}
}
22 changes: 16 additions & 6 deletions src/main/java/net/sf/jsqlparser/util/TableDefinitionTraversal.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public static void visit(AlterExpression action, Consumer<Expression> expression
relation.visitExpressions(expressions);
relation.visitTables(tables);
}
if (action instanceof net.sf.jsqlparser.statement.alter.AlterExpressionTableOption) {
visit(((net.sf.jsqlparser.statement.alter.AlterExpressionTableOption) action)
.getStructuredTableOption(), expressions, tables);
}
if (action.getOperation() == AlterOperation.RENAME_TABLE) {
accept(action.getNewTable(), tables);
}
Expand Down Expand Up @@ -123,12 +127,7 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
expressions);
}
if (table.getTableOptions() != null) {
table.getTableOptions().forEach(option -> {
visitOptions(option.getStorageParameters(), expressions);
if (option.getUnionTables() != null) {
option.getUnionTables().forEach(source -> accept(source, tables));
}
});
table.getTableOptions().forEach(option -> visit(option, expressions, tables));
}
if (table.getInherits() != null) {
table.getInherits().forEach(parent -> accept(parent, tables));
Expand All @@ -142,6 +141,17 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
visit(table.getPartitionBound(), expressions);
}

/** Shared CREATE/ALTER table option traversal. */
public static void visit(net.sf.jsqlparser.statement.create.table.TableOption option,
Consumer<Expression> expressions, Consumer<Table> tables) {
if (option != null) {
visitOptions(option.getStorageParameters(), expressions);
if (option.getUnionTables() != null) {
option.getUnionTables().forEach(source -> accept(source, tables));
}
}
}

/** Visits the active partition key and any subpartition key. Raw bounds remain opaque. */
public static void visit(TablePartitioning partitioning, Consumer<Expression> expressions) {
if (partitioning == null) {
Expand Down
Loading
Loading