Skip to content
Merged
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 @@ -25,6 +25,29 @@

public class CreateTable implements Statement {

private boolean tableOptionsAfterPartition;

public boolean isTableOptionsAfterPartition() {
return tableOptionsAfterPartition;
}

public void setTableOptionsAfterPartition(boolean tableOptionsAfterPartition) {
this.tableOptionsAfterPartition = tableOptionsAfterPartition;
}

private net.sf.jsqlparser.statement.execute.Execute execute;

/** Prepared statement source of PostgreSQL CREATE TABLE AS EXECUTE. */
public net.sf.jsqlparser.statement.execute.Execute getExecute() {
return execute;
}

public void setExecute(net.sf.jsqlparser.statement.execute.Execute execute) {
this.execute = execute;
select = null;
selectParenthesis = false;
}

private Table table;
private boolean unlogged = false;
private List<String> createOptionsStrings;
Expand Down Expand Up @@ -231,15 +254,25 @@ public void setWithData(Boolean withData) {

public StringBuilder appendSelectTo(StringBuilder builder,
java.util.function.Consumer<Select> selectRenderer) {
if (select != null) {
return appendQueryTo(builder, selectRenderer, builder::append);
}

public StringBuilder appendQueryTo(StringBuilder builder,
java.util.function.Consumer<Select> selectRenderer,
java.util.function.Consumer<net.sf.jsqlparser.statement.execute.Execute> executeRenderer) {
if (select != null || execute != null) {
if (duplicateHandling != null) {
builder.append(' ').append(duplicateHandling);
}
builder.append(useAsKeyword ? " AS " : " ");
if (selectParenthesis) {
builder.append("(");
}
selectRenderer.accept(select);
if (execute != null) {
executeRenderer.accept(execute);
} else {
selectRenderer.accept(select);
}
if (selectParenthesis) {
builder.append(")");
}
Expand All @@ -252,6 +285,7 @@ public StringBuilder appendSelectTo(StringBuilder builder,

public void setSelect(Select select, boolean parenthesis) {
this.select = select;
this.execute = null;
this.selectParenthesis = parenthesis;
}

Expand Down Expand Up @@ -449,14 +483,27 @@ private void appendColumnDefinitions(StringBuilder b) {
}
}

public void appendTableOptionsTo(StringBuilder builder,
java.util.function.Consumer<net.sf.jsqlparser.expression.Expression> expressionPrinter) {
if (tableOptions != null) {
for (TableOption option : tableOptions) {
builder.append(' ');
option.appendTo(builder, expressionPrinter);
}
} else {
String options = PlainSelect.getStringList(tableOptionsStrings, false, false);
if (options != null && !options.isEmpty()) {
builder.append(' ').append(options);
}
}
}

private void appendTableOptions(StringBuilder b) {
String options = tableOptions != null
? PlainSelect.getStringList(tableOptions, false, false)
: PlainSelect.getStringList(tableOptionsStrings, false, false);
if (options != null && options.length() > 0) {
b.append(" ").append(options);
if (partitioning != null && tableOptionsAfterPartition) {
b.append(" ").append(partitioning);
}
if (partitioning != null) {
appendTableOptionsTo(b, b::append);
if (partitioning != null && !tableOptionsAfterPartition) {
b.append(" ").append(partitioning);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
public class TableOption implements Serializable {

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, ENGINE_ATTRIBUTE, SECONDARY_ENGINE_ATTRIBUTE, OTHER
ENGINE, CHARACTER_SET, COLLATE, COMMENT, AUTO_INCREMENT, STATS_AUTO_RECALC, STATS_PERSISTENT, STATS_SAMPLE_PAGES, UNION, ENCRYPTION, PASSWORD, DATA_DIRECTORY, INDEX_DIRECTORY,
STORAGE_PARAMETERS, WITHOUT_OIDS, ENGINE_ATTRIBUTE, SECONDARY_ENGINE_ATTRIBUTE, OTHER
}

private Kind kind = Kind.OTHER;
Expand All @@ -30,6 +31,31 @@ public enum Kind {
private boolean useEquals;
private List<String> tokens;
private List<Table> unionTables;
private List<Index.Option> storageParameters;

public List<Index.Option> getStorageParameters() {
return storageParameters;
}

public void setStorageParameters(List<Index.Option> storageParameters) {
this.storageParameters = storageParameters;
kind = Kind.STORAGE_PARAMETERS;
name = "WITH";
value = null;
tokens = null;
unionTables = null;
}

public void appendTo(StringBuilder builder,
java.util.function.Consumer<net.sf.jsqlparser.expression.Expression> expressionPrinter) {
if (storageParameters != null) {
builder.append("WITH ");
Index.Option.appendListTo(builder, storageParameters, expressionPrinter);
} else {
builder.append(toString());
}
}


public TableOption() {}

Expand All @@ -56,6 +82,9 @@ public Kind getKind() {

public void setKind(Kind kind) {
this.kind = kind;
if (kind != Kind.STORAGE_PARAMETERS) {
storageParameters = null;
}
if (kind != Kind.UNION) {
unionTables = null;
}
Expand All @@ -70,12 +99,16 @@ public void setName(String name) {
}

public String getValue() {
if (storageParameters != null) {
return PlainSelect.getStringList(storageParameters, true, true);
}
return unionTables == null ? value : PlainSelect.getStringList(unionTables, true, true);
}

public void setValue(String value) {
this.value = value;
unionTables = null;
storageParameters = null;
}

/** Returns the mutable MERGE table sources, including an empty UNION list. */
Expand All @@ -90,6 +123,7 @@ public void setUnionTables(List<Table> unionTables) {
name = "UNION";
value = null;
tokens = null;
storageParameters = null;
}

public TableOption withUnionTables(List<Table> unionTables) {
Expand Down Expand Up @@ -128,6 +162,7 @@ public void setTokens(List<String> tokens) {
this.tokens = tokens;
if (tokens != null) {
unionTables = null;
storageParameters = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public enum Type {
EXPRESSION
}

private List<Index.ColumnParams> keyColumns;

/** PostgreSQL keys carrying collation/operator-class attributes, otherwise null. */
public List<Index.ColumnParams> getKeyColumns() {
return keyColumns;
}

public void setKeyColumns(List<Index.ColumnParams> keyColumns) {
this.keyColumns = keyColumns;
expression = null;
expressionList = null;
columns = null;
}

private Type type;
private boolean linear;
private boolean columnsSyntax;
Expand Down Expand Up @@ -84,6 +98,7 @@ public Expression getExpression() {

public void setExpression(Expression expression) {
this.expression = expression;
keyColumns = null;
}

/**
Expand All @@ -97,6 +112,7 @@ public ExpressionList<Expression> getExpressionList() {

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

public ExpressionList<Column> getColumns() {
Expand All @@ -105,6 +121,7 @@ public ExpressionList<Column> getColumns() {

public void setColumns(ExpressionList<Column> columns) {
this.columns = columns;
keyColumns = null;
}

public Integer getAlgorithm() {
Expand Down Expand Up @@ -305,7 +322,16 @@ private void appendMethod(StringBuilder builder, Consumer<Expression> expression
if (columnsSyntax) {
builder.append(" COLUMNS");
}
if (expression != null) {
if (keyColumns != null) {
builder.append(" (");
for (int i = 0; i < keyColumns.size(); i++) {
if (i > 0) {
builder.append(", ");
}
keyColumns.get(i).appendTo(builder, expressionPrinter);
}
builder.append(')');
} else if (expression != null) {
builder.append(" (");
expressionPrinter.accept(expression);
builder.append(')');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
}
if (table.getTableOptions() != null) {
table.getTableOptions().forEach(option -> {
visitOptions(option.getStorageParameters(), expressions);
if (option.getUnionTables() != null) {
option.getUnionTables().forEach(source -> accept(source, tables));
}
Expand All @@ -112,6 +113,9 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
if (table.getInherits() != null) {
table.getInherits().forEach(parent -> accept(parent, tables));
}
if (table.getExecute() != null && table.getExecute().getExprList() != null) {
accept(table.getExecute().getExprList(), expressions);
}
accept(table.getTrailingLikeTable(), tables);
accept(table.getPartitionOf(), tables);
visit(table.getPartitioning(), expressions);
Expand All @@ -123,7 +127,12 @@ public static void visit(TablePartitioning partitioning, Consumer<Expression> ex
if (partitioning == null) {
return;
}
if (partitioning.getExpression() != null) {
if (partitioning.getKeyColumns() != null) {
partitioning.getKeyColumns().forEach(key -> {
accept(key.getExpression(), expressions);
visitOptions(key.getOperatorClassParameters(), expressions);
});
} else if (partitioning.getExpression() != null) {
accept(partitioning.getExpression(), expressions);
} else if (partitioning.getExpressionList() != null) {
accept(partitioning.getExpressionList(), expressions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,15 @@ public void deParse(CreateTable createTable) {
null));
}

params = PlainSelect.getStringList(createTable.getTableOptionsStrings(), false, false);
if (!"".equals(params)) {
builder.append(' ').append(params);
if (createTable.getPartitioning() != null && createTable.isTableOptionsAfterPartition()) {
builder.append(' ');
createTable.getPartitioning().appendTo(builder,
expression -> expression.accept(statementDeParser.getExpressionDeParser(),
null));
}
if (createTable.getPartitioning() != null) {
createTable.appendTableOptionsTo(builder,
expression -> expression.accept(statementDeParser.getExpressionDeParser(), null));
if (createTable.getPartitioning() != null && !createTable.isTableOptionsAfterPartition()) {
builder.append(' ');
createTable.getPartitioning().appendTo(builder,
expression -> expression.accept(statementDeParser.getExpressionDeParser(),
Expand All @@ -129,7 +133,8 @@ public void deParse(CreateTable createTable) {
builder.append(' ').append(createTable.getRowMovement().getMode().toString())
.append(" ROW MOVEMENT");
}
createTable.appendSelectTo(builder, select -> select.accept(this.statementDeParser, null));
createTable.appendQueryTo(builder, select -> select.accept(this.statementDeParser, null),
execute -> execute.accept(this.statementDeParser, null));
if (createTable.getTrailingLikeTable() != null) {
builder.append(" LIKE ");
if (createTable.isSelectParenthesis()) {
Expand Down
Loading
Loading