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 @@ -9,8 +9,11 @@
*/
package net.sf.jsqlparser.statement.alter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.jsqlparser.statement.CascadeBehavior;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.create.table.ConstraintKind;

Expand All @@ -19,6 +22,47 @@
* DROP INDEX/KEY, DROP PRIMARY KEY, DROP UNIQUE, DROP FOREIGN KEY, and DROP PARTITION.
*/
public class AlterExpressionDrop extends AlterExpression {
private CascadeBehavior behavior;

/** The explicit CASCADE or RESTRICT clause; null when omitted. */
public CascadeBehavior getBehavior() {
return behavior;
}

public void setBehavior(CascadeBehavior behavior) {
this.behavior = behavior;
}

public AlterExpressionDrop withBehavior(CascadeBehavior behavior) {
setBehavior(behavior);
return this;
}

/**
* Returns the raw parameters, such as Oracle's {@code INVALIDATE} or
* {@code CASCADE CONSTRAINTS}. With a {@link #getBehavior() behavior}, this is an unmodifiable
* snapshot that ends with its keyword; use {@link #addParameters(String...)} to append raw
* parameters.
*/
@Override
public List<String> getParameters() {
List<String> parameters = super.getParameters();
if (behavior == null) {
return parameters;
}
List<String> tokens = parameters == null ? new ArrayList<>() : new ArrayList<>(parameters);
tokens.add(behavior.name());
return Collections.unmodifiableList(tokens);
}

@Override
protected void appendCommonTail(StringBuilder b) {
super.appendCommonTail(b);
if (behavior != null) {
b.append(' ').append(behavior);
}
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return getColumnIdentityActionsFromDefinitions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.Serializable;
import java.util.List;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.statement.select.PlainSelect;

public class PartitionDefinition implements Serializable {
Expand Down Expand Up @@ -39,7 +40,7 @@ public String toString() {
private boolean subPartition;
private boolean storageKeyword;
private boolean storageEngineUseEquals = true;
private String comment;
private StringValue commentValue;
private boolean commentUseEquals;
private String dataDirectory;
private boolean dataDirectoryUseEquals;
Expand Down Expand Up @@ -143,12 +144,26 @@ public void setStorageEngineUseEquals(boolean storageEngineUseEquals) {
this.storageEngineUseEquals = storageEngineUseEquals;
}

/** Returns the COMMENT literal's SQL text, for example {@code 'it''s'}, or null. */
public String getComment() {
return comment;
return commentValue != null ? commentValue.toString() : null;
}

/**
* Sets the COMMENT from a string literal's SQL text, read with standard escaping. Use
* {@link #setCommentValue(StringValue)} for another escaping. Null removes the comment.
*/
public void setComment(String comment) {
this.comment = comment;
commentValue = comment != null ? new StringValue(comment) : null;
}

/** Returns the COMMENT string literal; {@link StringValue#getNotExcapedValue()} decodes it. */
public StringValue getCommentValue() {
return commentValue;
}

public void setCommentValue(StringValue commentValue) {
this.commentValue = commentValue;
}

public boolean isCommentUseEquals() {
Expand Down Expand Up @@ -266,7 +281,7 @@ public String toString() {
}
appendOption(b, "ENGINE", storageEngine, storageEngineUseEquals);
}
appendOption(b, "COMMENT", comment, commentUseEquals);
appendOption(b, "COMMENT", getComment(), commentUseEquals);
appendOption(b, "DATA DIRECTORY", dataDirectory, dataDirectoryUseEquals);
appendOption(b, "INDEX DIRECTORY", indexDirectory, indexDirectoryUseEquals);
appendOption(b, "MAX_ROWS", maxRows, maxRowsUseEquals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.schema.Table;

Expand All @@ -33,9 +34,25 @@ public class TableOption implements Serializable {
private List<Table> unionTables;
private List<IndexOption> storageParameters;
private ColumnOption.Storage tablespaceStorage;
private OnCommitAction onCommitAction;
private StringValue comment;

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, AVG_ROW_LENGTH, MAX_ROWS, MIN_ROWS, KEY_BLOCK_SIZE, TABLESPACE, 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, ACCESS_METHOD, ON_COMMIT, OTHER
}

/**
* The action of a temporary table's {@code ON COMMIT} clause. The DEFINITION actions belong to
* Oracle private temporary tables.
*/
public enum OnCommitAction {
PRESERVE_ROWS, DELETE_ROWS, DROP, PRESERVE_DEFINITION, DROP_DEFINITION;

/** Returns the SQL keywords, for example {@code DELETE ROWS}. */
@Override
public String toString() {
return name().replace('_', ' ');
}
}

public ForeignTableOptions getForeignTableOptions() {
Expand Down Expand Up @@ -78,6 +95,31 @@ public void setTablespaceStorage(ColumnOption.Storage storage) {
tablespaceStorage = storage;
}

/** Creates a table access method option such as PostgreSQL's {@code USING heap}. */
public static TableOption accessMethod(String method) {
return new TableOption(Kind.ACCESS_METHOD, "USING", method, false);
}

/** Returns the {@code ON COMMIT} action, or null if this option has none. */
public OnCommitAction getOnCommitAction() {
return onCommitAction;
}

/** Replaces the current contents with an {@code ON COMMIT} clause. */
public void setOnCommitAction(OnCommitAction action) {
clearContents();
kind = Kind.ON_COMMIT;
name = "ON COMMIT";
onCommitAction = action;
}

/** Creates an {@code ON COMMIT} option, for example {@code ON COMMIT DROP}. */
public static TableOption onCommit(OnCommitAction action) {
TableOption option = new TableOption();
option.setOnCommitAction(action);
return option;
}

public List<IndexOption> getStorageParameters() {
return storageParameters;
}
Expand All @@ -89,13 +131,49 @@ public void setStorageParameters(List<IndexOption> storageParameters) {
name = "WITH";
}

/**
* Returns the COMMENT string literal, or null for another kind, opaque tokens or a plain value
* set with {@link #setValue(String)}. {@link StringValue#getNotExcapedValue()} decodes it.
*/
public StringValue getComment() {
return comment;
}

/**
* Replaces the current contents with a COMMENT string literal and keeps the equals spelling; an
* existing COMMENT also keeps its name. A null literal omits the value.
*/
public void setComment(StringValue comment) {
String commentName = kind == Kind.COMMENT && name != null ? name : "COMMENT";
boolean equals = useEquals;
clearContents();
kind = Kind.COMMENT;
name = commentName;
useEquals = equals;
this.comment = comment;
}

public TableOption withComment(StringValue comment) {
setComment(comment);
return this;
}

public static TableOption comment(StringValue comment) {
TableOption option = new TableOption();
option.setComment(comment);
return option;
}

public void appendTo(StringBuilder builder,
java.util.function.Consumer<net.sf.jsqlparser.expression.Expression> expressionPrinter) {
if (foreignTableOptions != null) {
foreignTableOptions.appendTo(builder, expressionPrinter);
} else if (storageParameters != null) {
builder.append(name).append(useEquals ? " = " : " ");
IndexOption.appendListTo(builder, storageParameters, expressionPrinter);
} else if (comment != null) {
builder.append(name).append(useEquals ? " = " : " ");
expressionPrinter.accept(comment);
} else {
builder.append(toString());
}
Expand Down Expand Up @@ -140,6 +218,14 @@ public void setKind(Kind kind) {
if (kind != Kind.UNION) {
unionTables = null;
}
if (kind != Kind.ON_COMMIT) {
onCommitAction = null;
}
if (kind != Kind.COMMENT && comment != null) {
// keep the literal's SQL text, as for other scalar values
value = comment.toString();
comment = null;
}
}

public String getName() {
Expand All @@ -158,15 +244,24 @@ public String getValue() {
if (storageParameters != null) {
return PlainSelect.getStringList(storageParameters, true, true);
}
if (onCommitAction != null) {
return onCommitAction.toString();
}
if (comment != null) {
return comment.toString();
}
return unionTables == null ? value : PlainSelect.getStringList(unionTables, true, true);
}

/** Sets a plain value; it replaces a COMMENT literal without being parsed. */
public void setValue(String value) {
tokens = null;
this.value = value;
foreignTableOptions = null;
unionTables = null;
storageParameters = null;
onCommitAction = null;
comment = null;
}

/** Returns the mutable MERGE table sources, including an empty UNION list. */
Expand Down Expand Up @@ -214,7 +309,10 @@ public List<String> getTokens() {
result.add("=");
}
String renderedValue = getValue();
if (renderedValue != null) {
if (onCommitAction != null) {
// one token per keyword, as in the legacy raw options
Collections.addAll(result, renderedValue.split(" "));
} else if (renderedValue != null) {
result.add(renderedValue);
}
if (kind == Kind.TABLESPACE && tablespaceStorage != null) {
Expand Down Expand Up @@ -245,6 +343,8 @@ private void clearContents() {
foreignTableOptions = null;
unionTables = null;
storageParameters = null;
onCommitAction = null;
comment = null;
}

public TableOption withKind(Kind kind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ public static void visit(CreateTable table, Consumer<Expression> expressions,
visit(table.getPartitionBound(), expressions);
}

/** Shared CREATE/ALTER table option traversal. */
/** Shared CREATE/ALTER table option traversal, including COMMENT literals. */
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);
accept(option.getComment(), expressions);
if (option.getUnionTables() != null) {
option.getUnionTables().forEach(source -> accept(source, tables));
}
Expand Down
Loading
Loading