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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ and missing syntax gets added on demand — [open an issue](https://github.com/J
| **MySQL table maintenance** | `ANALYZE` · `CHECK` · `CHECKSUM` · `OPTIMIZE` · `REPAIR TABLE` |
| **MySQL locks** | `LOCK TABLES` · `UNLOCK TABLES` · `LOCK INSTANCE FOR BACKUP` · `UNLOCK INSTANCE` |
| **MySQL resource groups** | `CREATE`/`ALTER`/`DROP RESOURCE GROUP` · `SET RESOURCE GROUP` |
| **MySQL mixed ADD groups** | `ALTER TABLE … ADD [COLUMN] (columns, indexes, constraints)` |
| **MySQL loadable functions** | `CREATE [AGGREGATE] FUNCTION … RETURNS … SONAME` |
| **MySQL servers, plugins and components** | `CREATE SERVER` · `ALTER SERVER` with `OPTIONS (… PORT 3306)` · `INSTALL`/`UNINSTALL PLUGIN` · `INSTALL`/`UNINSTALL COMPONENT` |
| **MySQL server administration** | `FLUSH` · `KILL` · `RESET PERSIST` · `HANDLER … OPEN`/`READ`/`CLOSE` |
Expand Down
114 changes: 109 additions & 5 deletions src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.sf.jsqlparser.statement.create.table.NamedConstraint;
import net.sf.jsqlparser.statement.create.table.KeyConstraint;
import net.sf.jsqlparser.statement.create.table.TableElement;
import net.sf.jsqlparser.statement.create.table.TableElementList;
import net.sf.jsqlparser.statement.create.table.IndexOptionSource;
import net.sf.jsqlparser.statement.create.table.ConstraintKind;
import net.sf.jsqlparser.statement.create.table.PartitionDefinition;
Expand All @@ -50,6 +51,7 @@ public enum TableRenameKeyword {
// private ColDataType dataType;
private String columnOldName;
private List<ColumnDataType> colDataTypeList;
private List<TableElement> addGroupElements;
private List<ColumnDropNotNull> columnDropNotNullList;
private List<ColumnSetNotNull> columnSetNotNullList;
private List<ColumnDropDefault> columnDropDefaultList;
Expand Down Expand Up @@ -434,15 +436,73 @@ private void setForeignKeyTablePart(String value, int partIndex) {
table.setNameParts(parts);
}

/**
* Returns the mutable column list, filtering out the other declarations of a mixed ADD group.
*/
public List<ColumnDataType> getColDataTypeList() {
if (hasAddGroupElements()) {
return new TableElementList<>(addGroupElements, ColumnDataType.class);
}
return colDataTypeList;
}

/**
* Returns the mutable declarations of a parenthesized mixed ADD group in source order. Parsed
* columns are {@link ColumnDataType} nodes shared with {@link #getColDataTypeList()}.
*/
public List<TableElement> getAddGroupElements() {
return addGroupElements;
}

/** Copies the group membership, retaining the mutable column, index and constraint nodes. */
public void setAddGroupElements(List<TableElement> elements) {
addGroupElements = elements == null ? null : new ArrayList<>(elements);
if (elements != null) {
colDataTypeList = null;
constraint = null;
index = null;
useBrackets = true;
}
}

public boolean hasAddGroupElements() {
return operation == AlterOperation.ADD && addGroupElements != null;
}

private <E extends TableElement> E firstAddGroupElement(Class<E> type) {
for (TableElement element : addGroupElements) {
if (type.isInstance(element)) {
return type.cast(element);
}
}
return null;
}

private <E extends TableElement> void replaceFirstAddGroupElement(Class<E> type, E element) {
for (int i = 0; i < addGroupElements.size(); i++) {
if (type.isInstance(addGroupElements.get(i))) {
if (element == null) {
addGroupElements.remove(i);
} else {
addGroupElements.set(i, element);
}
return;
}
}
if (element != null) {
addGroupElements.add(element);
}
}

public void addColDataType(String columnName, ColDataType colDataType) {
addColDataType(new ColumnDataType(columnName, false, colDataType, null));
}

public void addColDataType(ColumnDataType columnDataType) {
if (hasAddGroupElements()) {
addGroupElements.add(columnDataType);
return;
}
if (colDataTypeList == null) {
colDataTypeList = new ArrayList<>();
}
Expand Down Expand Up @@ -782,7 +842,11 @@ public void setUkName(String ukName) {
}
}

/** Returns the ordinary index, or the first ordinary index in a mixed ADD group. */
public Index getIndex() {
if (hasAddGroupElements()) {
return firstAddGroupElement(Index.class);
}
return index;
}

Expand Down Expand Up @@ -878,22 +942,39 @@ public boolean hasActiveConstraintHeader() {
return hasExplicitConstraintHeader();
}

/** Sets an index definition or target; a non-null value replaces any constraint definition. */
/**
* Sets an index definition or target; a non-null value replaces any constraint definition. In a
* mixed ADD group, replaces or removes only its first ordinary index.
*/
public void setIndex(Index index) {
if (hasAddGroupElements()) {
replaceFirstAddGroupElement(Index.class, index);
return;
}
this.index = index;
if (index != null) {
clearReplacedConstraintAliases(null);
constraint = null;
}
}

/** Returns the constraint definition, distinct from an ordinary index or a named target. */
/** Returns the constraint definition, or the first constraint in a mixed ADD group. */
public NamedConstraint getConstraint() {
if (hasAddGroupElements()) {
return firstAddGroupElement(NamedConstraint.class);
}
return constraint;
}

/** Sets a constraint definition; a non-null value replaces any ordinary index definition. */
/**
* Sets a constraint definition; a non-null value replaces any ordinary index definition. In a
* mixed ADD group, replaces or removes only its first constraint.
*/
public void setConstraint(NamedConstraint constraint) {
if (hasAddGroupElements()) {
replaceFirstAddGroupElement(NamedConstraint.class, constraint);
return;
}
clearReplacedConstraintAliases(constraint);
this.constraint = constraint;
if (constraint != null) {
Expand Down Expand Up @@ -1208,7 +1289,9 @@ public final String toString() {
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity",
"PMD.ExcessiveMethodLength", "PMD.SwitchStmtsShouldHaveDefault"})
protected void appendBody(StringBuilder b) {
if (operation == AlterOperation.UNSPECIFIC) {
if (hasAddGroupElements()) {
appendAddGroupBody(b, b::append);
} else if (operation == AlterOperation.UNSPECIFIC) {
b.append(optionalSpecifier);
} else if (hasActiveConstraintHeader()) {
toStringConstraintAlter(b);
Expand Down Expand Up @@ -1250,7 +1333,28 @@ protected boolean usesColumnActionBody() {
* deparsing. Subclasses with their own body dispatch override this method accordingly.
*/
public boolean hasActiveColumnDefinitions() {
return hasColumnDefinitionPayload() && usesGeneralBody();
return hasAddGroupElements() || hasColumnDefinitionPayload() && usesGeneralBody();
}

/** Appends a mixed ADD group using the supplied renderer for every declaration. */
public void appendAddGroupTo(StringBuilder b, Consumer<TableElement> elementPrinter) {
appendAddGroupBody(b, elementPrinter);
appendCommonTail(b);
}

private void appendAddGroupBody(StringBuilder b, Consumer<TableElement> elementPrinter) {
b.append("ADD ");
if (hasColumn) {
b.append("COLUMN ");
}
b.append('(');
for (int i = 0; i < addGroupElements.size(); i++) {
if (i > 0) {
b.append(", ");
}
elementPrinter.accept(addGroupElements.get(i));
}
b.append(')');
}

/** Whether this action selects the general body, before choosing its active payload. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import java.util.Objects;

/** A mutable, filtered view preserving the other declarations in a table definition. */
final class TableElementList<E extends TableElement> extends AbstractList<E>
public final class TableElementList<E extends TableElement> extends AbstractList<E>
implements Serializable {
private final List<TableElement> elements;
private final Class<E> type;

TableElementList(List<TableElement> elements, Class<E> type) {
public TableElementList(List<TableElement> elements, Class<E> type) {
this.elements = elements;
this.type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public static void visit(CreateIndex createIndex, Consumer<Expression> expressio
*/
public static void visit(AlterExpression action, Consumer<Expression> expressions,
Consumer<Table> tables) {
if (action.hasAddGroupElements()) {
action.getAddGroupElements().forEach(element -> visit(element, expressions, tables));
return;
}
if (visitSpecialBody(action, expressions, tables)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void deParse(Alter alter) {
}

private void deParseAction(AlterExpression action) {
if (action.hasAddGroupElements()) {
TableElementDeParser elements = new TableElementDeParser(builder, expressionVisitor);
action.appendAddGroupTo(builder, elements::deParse);
return;
}
if (action instanceof AlterForeignDataOptions) {
((AlterForeignDataOptions) action).appendTo(builder,
expression -> expression.accept(expressionVisitor, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ public void validate(Alter alter, AlterExpression e) {
validateOptionalColumnNames(c, e.getDropColumns());
}

validateConstraint(c, definition);
validateIndex(c, e, definition);
if (e.hasAddGroupElements()) {
for (TableElement element : e.getAddGroupElements()) {
validateConstraint(c, element);
validateIndex(c, e, element);
}
} else {
validateConstraint(c, definition);
validateIndex(c, e, definition);
}
}
}

Expand Down Expand Up @@ -137,7 +144,9 @@ private boolean hasActiveDropColumns(AlterExpression expression) {

private void validateIndex(ValidationCapability capability, AlterExpression expression,
TableElement definition) {
Index index = expression.getIndex();
Index index = expression.hasAddGroupElements() && definition instanceof Index
? (Index) definition
: expression.getIndex();
boolean rename = expression.getOperation() == AlterOperation.RENAME_INDEX
|| expression.getOperation() == AlterOperation.RENAME_KEY;
if (index == null || definition != index && !rename) {
Expand Down
48 changes: 48 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -25361,6 +25361,47 @@ Truncate Truncate():
}
}


/** MySQL ADD (table_element_list), preserving interleaved columns, indexes and constraints. */
void MySqlAlterAddGroup(AlterExpression alterExp):
{
List<TableElement> elements = new ArrayList<TableElement>();
TableElement element;
boolean mixed = false;
}
{
[ <K_COLUMN> { alterExp.hasColumn(true); } ]
"(" element=MySqlAlterAddGroupElement() { elements.add(element); }
( "," element=MySqlAlterAddGroupElement() { elements.add(element); } )*
")"
{
for (TableElement item : elements) {
if (!(item instanceof AlterExpression.ColumnDataType)) {
mixed = true;
}
}
if (mixed) {
alterExp.setAddGroupElements(elements);
} else {
// Keep the existing representation and accessors for column-only groups.
alterExp.useBrackets(true);
for (TableElement item : elements) {
alterExp.addColDataType((AlterExpression.ColumnDataType) item);
}
}
}
}

TableElement MySqlAlterAddGroupElement():
{
TableElement element;
}
{
( LOOKAHEAD(3) element=CreateTableConstraint()
| element=AlterExpressionColumnDataType() )
{ return element; }
}

/**
* Parses common column-level changes shared between the COLUMN-prefixed and bare forms:
* DROP DEFAULT, SET DEFAULT, SET NOT NULL, SET VISIBLE/INVISIBLE, and bracketed multi-column
Expand Down Expand Up @@ -26547,6 +26588,13 @@ AlterExpression AlterExpressionAddAlterModify():
{ alterExp.getOperation() == AlterOperation.ADD })
alterExp=AddPartition()
|
LOOKAHEAD({ alterExp.getOperation() == AlterOperation.ADD
&& (getAsString(Feature.dialect) == null
|| Dialect.MYSQL.name().equals(getAsString(Feature.dialect)))
&& (getToken(1).kind == OPENING_BRACKET
|| getToken(1).kind == K_COLUMN && getToken(2).kind == OPENING_BRACKET) })
MySqlAlterAddGroup(alterExp)
|
LOOKAHEAD(3) (
( LOOKAHEAD(2)
(
Expand Down
Loading
Loading