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
50 changes: 37 additions & 13 deletions src/main/java/net/sf/jsqlparser/statement/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,46 @@
import net.sf.jsqlparser.statement.StatementVisitor;

public class Alter implements Statement {

private Table table;
private boolean useOnly = false;

private boolean useTableIfExists = false;

private List<AlterExpression> alterExpressions;
private boolean useDescendants;
private boolean foreignTable;



public boolean isUseDescendants() {
return useDescendants;
}

public void setUseDescendants(boolean useDescendants) {
this.useDescendants = useDescendants;
}

public StringBuilder appendTargetTo(StringBuilder builder) {
builder.append(foreignTable ? "ALTER FOREIGN TABLE " : "ALTER TABLE ");
if (useTableIfExists) {
builder.append("IF EXISTS ");
}
if (useOnly) {
builder.append("ONLY ");
}
builder.append(table.getFullyQualifiedName());
if (useDescendants) {
builder.append(" *");
}
return builder.append(' ');
}


public boolean isForeignTable() {
return foreignTable;
}

public void setForeignTable(boolean foreignTable) {
this.foreignTable = foreignTable;
}

public Table getTable() {
return table;
Expand Down Expand Up @@ -82,16 +115,7 @@ public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
public String toString() {

StringBuilder b = new StringBuilder();
b.append("ALTER TABLE ");
if (useOnly) {
b.append("ONLY ");
}

if (useTableIfExists) {
b.append("IF EXISTS ");
}

b.append(table.getFullyQualifiedName()).append(" ");
appendTargetTo(b);

Iterator<AlterExpression> altIter = alterExpressions.iterator();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.alter;

import java.util.List;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.statement.create.table.ForeignDataOption;

/** Foreign table OPTIONS, optionally applied to one column. */
public class AlterForeignDataOptions extends AlterExpression {
private List<ForeignDataOption> options;

public AlterForeignDataOptions() {
setOperation(AlterOperation.FOREIGN_OPTIONS);
}

public List<ForeignDataOption> getOptions() {
return options;
}

public void setOptions(List<ForeignDataOption> options) {
this.options = options;
}

public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
if (getColumnName() != null) {
builder.append(hasColumn() ? "ALTER COLUMN " : "ALTER ").append(getColumnName())
.append(' ');
}
ForeignDataOption.appendOptionsTo(builder, options, expressionPrinter);
return builder;
}

@Override
protected void appendBody(StringBuilder builder) {
appendTo(builder, builder::append);
}
}
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, DROP_CHECK, 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, ALTER_PRIMARY_KEY, ALTER_RELATION, ORDER_BY;
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, DROP_CHECK, 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, ALTER_PRIMARY_KEY, ALTER_RELATION, ORDER_BY, FOREIGN_OPTIONS;

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 @@ -23,14 +23,32 @@
public class ColumnOption implements Serializable {

public enum Kind {
SERIAL_DEFAULT_VALUE, REFERENCE, IDENTITY, CONSTRAINT, DEFAULT, NULLABILITY, COLLATE, COMMENT, ON_UPDATE, GENERATED, AUTO_INCREMENT, VISIBILITY, STORAGE, COMPRESSION, OTHER
SERIAL_DEFAULT_VALUE, REFERENCE, IDENTITY, CONSTRAINT, DEFAULT, NULLABILITY, COLLATE, COMMENT, ON_UPDATE, GENERATED, AUTO_INCREMENT, VISIBILITY, STORAGE, COMPRESSION, FOREIGN_OPTIONS, OTHER
}

/** PostgreSQL storage strategies and MySQL column storage locations. */
public enum Storage {
PLAIN, EXTERNAL, EXTENDED, MAIN, DEFAULT, DISK, MEMORY
}

private List<ForeignDataOption> foreignOptions;

public List<ForeignDataOption> getForeignOptions() {
return foreignOptions;
}

public void setForeignOptions(List<ForeignDataOption> options) {
foreignOptions = options;
kind = Kind.FOREIGN_OPTIONS;
tokens = null;
}

public static ColumnOption foreignOptions(List<ForeignDataOption> options) {
ColumnOption option = new ColumnOption();
option.setForeignOptions(options);
return option;
}

private Storage storage;
private String compression;

Expand Down Expand Up @@ -175,7 +193,9 @@ public void setGeneratedDefinition(GeneratedColumnDefinition definition) {

/** Visits expressions of the selected option kind, including comment literals. */
public void visitExpressions(Consumer<Expression> visitor) {
if (kind == Kind.DEFAULT) {
if (kind == Kind.FOREIGN_OPTIONS) {
ForeignDataOption.visitExpressions(foreignOptions, visitor);
} else if (kind == Kind.DEFAULT) {
visitor.accept(defaultExpression);
} else if (kind == Kind.COMMENT) {
visitor.accept(comment);
Expand Down Expand Up @@ -294,6 +314,9 @@ public String toString() {
/** Appends the option using the supplied printer for structured expressions. */
public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
switch (kind) {
case FOREIGN_OPTIONS:
ForeignDataOption.appendOptionsTo(builder, foreignOptions, expressionPrinter);
break;
case STORAGE:
builder.append("STORAGE ").append(storage);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public void setExecute(net.sf.jsqlparser.statement.execute.Execute execute) {

private Table table;
private boolean unlogged = false;

/** The structured SERVER clause, also present in the shared table-options list. */
public ForeignTableOptions getForeignTableOptions() {
if (tableOptions != null) {
for (TableOption option : tableOptions) {
if (option.getForeignTableOptions() != null) {
return option.getForeignTableOptions();
}
}
}
return null;
}

private List<String> createOptionsStrings;
private List<String> tableOptionsStrings;
private List<TableOption> tableOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*-
* #%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 java.util.List;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

/** A foreign table or column option; absent action means the implicit ADD form. */
public class ForeignDataOption implements Serializable {
public enum Action {
ADD, SET, DROP
}

private Action action;
private String name;
private StringValue value;

public ForeignDataOption(String name, StringValue value) {
this.name = name;
this.value = value;
}

public Action getAction() {
return action;
}

public void setAction(Action action) {
this.action = action;
if (action == Action.DROP) {
value = null;
}
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public StringValue getValue() {
return value;
}

public void setValue(StringValue value) {
this.value = value;
}

public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
if (action != null) {
builder.append(action).append(' ');
}
builder.append(name);
if (action != Action.DROP && value != null) {
builder.append(' ');
expressionPrinter.accept(value);
}
}

public static void appendOptionsTo(StringBuilder builder, List<ForeignDataOption> options,
Consumer<Expression> expressionPrinter) {
builder.append("OPTIONS (");
for (int i = 0; i < options.size(); i++) {
if (i > 0) {
builder.append(", ");
}
options.get(i).appendTo(builder, expressionPrinter);
}
builder.append(')');
}

public static void visitExpressions(List<ForeignDataOption> options,
Consumer<Expression> visitor) {
if (options != null) {
for (ForeignDataOption option : options) {
if (option.action != Action.DROP && option.value != null) {
visitor.accept(option.value);
}
}
}
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder, builder::append);
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*-
* #%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 java.util.List;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;

/** PostgreSQL foreign server and optional table-level FDW options. */
public class ForeignTableOptions implements Serializable {
private String server;
private List<ForeignDataOption> options;

public String getServer() {
return server;
}

public void setServer(String server) {
this.server = server;
}

public List<ForeignDataOption> getOptions() {
return options;
}

public void setOptions(List<ForeignDataOption> options) {
this.options = options;
}

public void appendTo(StringBuilder builder, Consumer<Expression> expressionPrinter) {
builder.append("SERVER ").append(server);
if (options != null && !options.isEmpty()) {
builder.append(' ');
ForeignDataOption.appendOptionsTo(builder, options, expressionPrinter);
}
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
appendTo(builder, builder::append);
return builder.toString();
}
}
Loading
Loading