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

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

import java.util.Objects;
import net.sf.jsqlparser.statement.create.table.ConstraintAttributes;
import net.sf.jsqlparser.statement.create.table.ConstraintKind;
Expand All @@ -30,6 +33,11 @@ public void setAttributes(ConstraintAttributes attributes) {
this.attributes = Objects.requireNonNull(attributes, "attributes");
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,35 @@ public List<ColumnDefaultAction> getColumnDefaults() {
return Collections.unmodifiableList(columnSetDefaultList);
}

/**
* Returns active ALTER COLUMN identity actions without treating them as data-type changes. The
* list is an unmodifiable membership snapshot; its entries are the original mutable column
* nodes. Request a new list after adding, removing or changing actions. Subclasses that render
* a different body override this method to exclude inactive inherited column definitions.
*/
public List<ColumnIdentityAction> getColumnIdentityActions() {
if (hasExplicitConstraintHeader() || isRenameOperation() || hasAlterColumnValueActions()) {
return Collections.emptyList();
}
return getColumnIdentityActionsFromDefinitions();
}

/** Returns identity actions when this body's general column-definition branch is active. */
protected List<ColumnIdentityAction> getColumnIdentityActionsFromDefinitions() {
if (operation != AlterOperation.ALTER || colDataTypeList == null
|| columnName != null || commentText != null) {
return Collections.emptyList();
}
List<ColumnIdentityAction> actions = new ArrayList<>();
for (ColumnDataType column : colDataTypeList) {
if (column.getIdentityAlterations() != null
&& !column.getIdentityAlterations().isEmpty()) {
actions.add(column);
}
}
return Collections.unmodifiableList(actions);
}

public void addColSetVisibility(ColumnSetVisibility columnSetVisibility) {
if (columnSetVisibilityList == null) {
columnSetVisibilityList = new ArrayList<>();
Expand Down Expand Up @@ -994,10 +1023,7 @@ protected void appendBody(StringBuilder b) {
b.append(optionalSpecifier);
} else if (hasExplicitConstraintHeader()) {
toStringConstraintAlter(b);
} else if (operation == AlterOperation.ALTER
&& (columnDropDefaultList != null && !columnDropDefaultList.isEmpty()
|| columnSetDefaultList != null && !columnSetDefaultList.isEmpty()
|| columnSetVisibilityList != null && !columnSetVisibilityList.isEmpty())) {
} else if (hasAlterColumnValueActions()) {
toStringAlterColumn(b);
} else if (isSimpleKeywordOperation()) {
toStringSimpleKeyword(b);
Expand All @@ -1014,6 +1040,13 @@ protected void appendBody(StringBuilder b) {
}
}

private boolean hasAlterColumnValueActions() {
return operation == AlterOperation.ALTER
&& (columnDropDefaultList != null && !columnDropDefaultList.isEmpty()
|| columnSetDefaultList != null && !columnSetDefaultList.isEmpty()
|| columnSetVisibilityList != null && !columnSetVisibilityList.isEmpty());
}

/**
* Appends the common tail (parameters, index comment) shared by all ALTER expressions.
*/
Expand Down Expand Up @@ -1711,7 +1744,8 @@ public void setHasEqualForCollate(boolean hasEqualForCollate) {
this.hasEqualForCollate = hasEqualForCollate;
}

public static final class ColumnDataType extends ColumnDefinition {
public static final class ColumnDataType extends ColumnDefinition
implements ColumnIdentityAction {

private final boolean withType;
private boolean useSetData;
Expand Down Expand Up @@ -1739,10 +1773,12 @@ public void setUsingExpression(Expression usingExpression) {
this.usingExpression = usingExpression;
}

@Override
public List<IdentityAlteration> getIdentityAlterations() {
return identityAlterations;
}

@Override
public void setIdentityAlterations(List<IdentityAlteration> identityAlterations) {
this.identityAlterations =
identityAlterations == null ? null : new ArrayList<>(identityAlterations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package net.sf.jsqlparser.statement.alter;

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

import net.sf.jsqlparser.statement.create.table.ConstraintKind;

/**
Expand All @@ -17,6 +20,11 @@
*/
public class AlterExpressionCharset extends AlterExpression {

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.statement.alter;

import java.util.List;

import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.create.table.ConstraintKind;
Expand All @@ -18,6 +19,11 @@
* DROP INDEX/KEY, DROP PRIMARY KEY, DROP UNIQUE, DROP FOREIGN KEY, and DROP PARTITION.
*/
public class AlterExpressionDrop extends AlterExpression {
@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return getColumnIdentityActionsFromDefinitions();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package net.sf.jsqlparser.statement.alter;

import java.util.Collections;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -28,6 +30,11 @@ public List<OrderByElement> getOrderByElements() {
return orderByElements;
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.statement.alter;


import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -264,6 +265,11 @@ public void setCoalescePartitionCount(Integer coalescePartitionCount) {
}
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package net.sf.jsqlparser.statement.alter;

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

import net.sf.jsqlparser.statement.create.table.IndexOption;
import net.sf.jsqlparser.statement.create.table.KeyElement;
import java.util.Iterator;
Expand All @@ -31,6 +34,11 @@ public AlterExpressionPrimaryKey() {
setConstraint(new KeyConstraint().withType("PRIMARY KEY"));
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package net.sf.jsqlparser.statement.alter;

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

import net.sf.jsqlparser.statement.create.table.ConstraintKind;

/**
Expand Down Expand Up @@ -42,6 +45,11 @@ public void setTargetName(String name) {
setColumnName(name);
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package net.sf.jsqlparser.statement.alter;

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

import net.sf.jsqlparser.statement.create.table.TableOption;
import net.sf.jsqlparser.statement.create.table.ConstraintKind;

Expand All @@ -33,6 +36,11 @@ public void setStructuredTableOption(TableOption option) {
super.setUseEqual(false);
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package net.sf.jsqlparser.statement.alter;

import java.util.Collections;

import java.util.List;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.Expression;
Expand Down Expand Up @@ -40,6 +42,11 @@ public StringBuilder appendTo(StringBuilder builder, Consumer<Expression> expres
return builder;
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*-
* #%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.alter;

import java.util.List;

/**
* An ALTER COLUMN target and its ordered identity alterations. Obtain active column actions through
* {@link AlterExpression#getColumnIdentityActions()}; edits update the existing statement model.
*/
public interface ColumnIdentityAction {
String getColumnName();

void setColumnName(String columnName);

/** Returns the mutable, ordered alterations, or null when this column has none. */
List<IdentityAlteration> getIdentityAlterations();

/** Replaces the alterations; null restores the column's ordinary definition rendering. */
void setIdentityAlterations(List<IdentityAlteration> identityAlterations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,60 @@ public void setIdentityDefinition(IdentityDefinition identityDefinition) {
this.identityDefinition = identityDefinition;
}

/**
* Returns ADD's definition mode or SET GENERATED's mode. An ADD without a definition returns
* null; no definition is created by reading it.
*/
public IdentityDefinition.GenerationMode getGenerationMode() {
return generationMode;
return kind == Kind.ADD
? identityDefinition == null ? null : identityDefinition.getGenerationMode()
: generationMode;
}

/**
* Updates ADD's existing definition or SET GENERATED's mode. A non-null ADD mode creates a
* definition when absent; a null mode never creates one. Other kinds retain the value as
* inactive metadata and do not render it.
*/
public void setGenerationMode(IdentityDefinition.GenerationMode generationMode) {
this.generationMode = generationMode;
if (kind == Kind.ADD) {
if (identityDefinition != null) {
identityDefinition.setGenerationMode(generationMode);
} else if (generationMode != null) {
identityDefinition = new IdentityDefinition(generationMode);
}
} else {
this.generationMode = generationMode;
}
}

/**
* Returns ADD's definition parameters or SET_PARAMETER's parameters. The returned list belongs
* to the active payload and can be edited directly; an omitted list is null.
*/
public List<Sequence.Parameter> getParameters() {
return parameters;
return kind == Kind.ADD
? identityDefinition == null ? null : identityDefinition.getParameters()
: parameters;
}

/**
* Updates ADD's definition parameters or SET_PARAMETER's parameters. A non-null ADD list
* creates a BY DEFAULT definition when absent; a null list never creates one. Other kinds
* retain the value as inactive metadata and do not render it.
*/
public void setParameters(List<Sequence.Parameter> parameters) {
this.parameters = parameters == null ? null : new ArrayList<>(parameters);
if (kind == Kind.ADD) {
if (identityDefinition == null && parameters != null) {
identityDefinition =
new IdentityDefinition(IdentityDefinition.GenerationMode.BY_DEFAULT);
}
if (identityDefinition != null) {
identityDefinition.setParameters(parameters);
}
} else {
this.parameters = parameters == null ? null : new ArrayList<>(parameters);
}
}

/** Returns the restart value, or null for a bare RESTART using the sequence start value. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.statement.alter;


import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -240,6 +241,11 @@ public void setResetOptions(List<String> resetOptions) {
this.resetOptions = resetOptions;
}

@Override
public List<ColumnIdentityAction> getColumnIdentityActions() {
return Collections.emptyList();
}

@Override
public boolean hasActiveTableDefinition() {
return false;
Expand Down
Loading
Loading