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 @@ -419,6 +419,20 @@ public List<ColumnSetDefault> getColumnSetDefaultList() {
return columnSetDefaultList;
}

/**
* Returns this action's active SET DEFAULT entries through the same API used by ALTER VIEW. The
* list is read-only; its entries are the original mutable AST objects, not copies. Use the
* existing action-specific API to add or remove entries.
*/
public List<ColumnDefaultAction> getColumnDefaults() {
if (operation != AlterOperation.ALTER || columnSetDefaultList == null
|| constraintType != null && constraintSymbol != null
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
return Collections.emptyList();
}
return Collections.unmodifiableList(columnSetDefaultList);
}

public void addColSetVisibility(ColumnSetVisibility columnSetVisibility) {
if (columnSetVisibilityList == null) {
columnSetVisibilityList = new ArrayList<>();
Expand Down Expand Up @@ -1696,8 +1710,8 @@ public String toString() {
}
}

public static final class ColumnSetDefault implements Serializable {
private final String columnName;
public static final class ColumnSetDefault implements ColumnDefaultAction, Serializable {
private String columnName;
private String defaultValue;
private Expression defaultExpression;

Expand All @@ -1706,37 +1720,42 @@ public ColumnSetDefault(String columnName, String defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public String getColumnName() {
return columnName;
}

@Override
public void setColumnName(String columnName) {
this.columnName = columnName;
}

/** Constructs a structured default without overloading the legacy nullable String API. */
public static ColumnSetDefault fromExpression(String columnName, Expression expression) {
ColumnSetDefault result = new ColumnSetDefault(columnName, null);
result.setDefaultExpression(expression);
return result;
}

@Override
public Expression getDefaultExpression() {
return defaultExpression;
}

@Override
public void setDefaultExpression(Expression defaultExpression) {
this.defaultExpression = defaultExpression;
this.defaultValue = null;
}

@Override
public String getDefaultValue() {
return defaultExpression == null ? defaultValue : defaultExpression.toString();
}

public void appendTo(StringBuilder sql, Consumer<Expression> expressionPrinter) {
sql.append(columnName).append(" SET DEFAULT ");
if (defaultExpression == null) {
sql.append(defaultValue);
} else {
expressionPrinter.accept(defaultExpression);
}
appendDefaultValueTo(sql, expressionPrinter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*-
* #%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.function.Consumer;
import net.sf.jsqlparser.expression.Expression;

/**
* Shared access to a column SET DEFAULT action on a table or view. Obtain active actions through
* {@link AlterExpression#getColumnDefaults()}; edits update the existing statement model directly.
*/
public interface ColumnDefaultAction {
String getColumnName();

void setColumnName(String columnName);

/** Returns the parsed default, or null for a legacy opaque SQL value. */
Expression getDefaultExpression();

/** Replaces the default expression, discarding any legacy opaque SQL value. */
void setDefaultExpression(Expression expression);

/** Returns the default's SQL text, including legacy opaque values when present. */
default String getDefaultValue() {
Expression expression = getDefaultExpression();
return expression == null ? null : expression.toString();
}

/** Renders the shared value while preserving the caller's expression visitor. */
default void appendDefaultValueTo(StringBuilder sql, Consumer<Expression> expressionPrinter) {
Expression expression = getDefaultExpression();
if (expression == null) {
sql.append(getDefaultValue());
} else {
expressionPrinter.accept(expression);
}
}
}
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;
import net.sf.jsqlparser.expression.Expression;
Expand All @@ -17,7 +18,7 @@
import net.sf.jsqlparser.statement.select.PlainSelect;

/** Structured PostgreSQL property actions shared by tables, indexes and views. */
public class RelationAlterAction extends AlterExpression {
public class RelationAlterAction extends AlterExpression implements ColumnDefaultAction {
private EnableState enableState;
private TriggerTarget triggerTarget;
private Kind kind;
Expand Down Expand Up @@ -168,14 +169,23 @@ public void setStatisticsDefault(boolean statisticsDefault) {
}
}

@Override
public Expression getDefaultExpression() {
return defaultExpression;
}

@Override
public void setDefaultExpression(Expression defaultExpression) {
this.defaultExpression = defaultExpression;
}

@Override
public List<ColumnDefaultAction> getColumnDefaults() {
return kind == Kind.ALTER_COLUMN && columnAction == ColumnAction.SET_DEFAULT
? Collections.singletonList(this)
: Collections.emptyList();
}

public Table getRelation() {
return relation;
}
Expand Down Expand Up @@ -315,7 +325,7 @@ private void appendColumnChange(StringBuilder builder, Consumer<Expression> expr
switch (columnAction) {
case SET_DEFAULT:
builder.append(" SET DEFAULT ");
expressionPrinter.accept(defaultExpression);
appendDefaultValueTo(builder, expressionPrinter);
break;
case DROP_DEFAULT:
builder.append(" DROP DEFAULT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ private void deParseAction(AlterExpression action) {
deParseTail(action);
return;
}
if (action.getOperation() == net.sf.jsqlparser.statement.alter.AlterOperation.ALTER
&& action.getColumnSetDefaultList() != null
&& !action.getColumnSetDefaultList().isEmpty()) {
if (!action.getColumnDefaults().isEmpty()) {
action.appendColumnActionTo(builder,
expression -> expression.accept(expressionVisitor, null));
return;
Expand Down
31 changes: 31 additions & 0 deletions src/site/sphinx/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,37 @@ bare ``RESTART``, which uses the sequence's configured start value. Call
``new IdentityAlteration(Kind.RESTART).withRestartWith(20L)``.


Column defaults on tables and views
-----------------------------------

``AlterExpression.getColumnDefaults()`` exposes active ``SET DEFAULT`` actions
through ``ColumnDefaultAction`` for both tables and views. The returned list is
read-only, but its entries are the original mutable AST objects:

.. code-block:: java

for (String objectType : List.of("TABLE", "VIEW")) {
Statement statement = CCJSqlParserUtil.parse(
"ALTER " + objectType + " t ALTER COLUMN a SET DEFAULT 1",
parser -> parser.withDialect(Dialect.POSTGRESQL));
List<? extends AlterExpression> actions = statement instanceof Alter
? ((Alter) statement).getAlterExpressions()
: ((AlterRelation) statement).getActions();
for (AlterExpression action : actions) {
for (ColumnDefaultAction column : action.getColumnDefaults()) {
column.setDefaultExpression(new LongValue(42));
}
}
System.out.println(statement); // SET DEFAULT 42 for either object type
}

``getColumnName()`` and ``setColumnName()`` access the affected column.
``getDefaultExpression()`` exposes parsed expressions; ``getDefaultValue()``
returns their SQL text. Existing ``ColumnSetDefault`` constructors still accept
opaque strings, for which ``getDefaultExpression()`` returns ``null``.
Replacing an expression also replaces any opaque value. Unrelated actions
return an empty list.

Structured column attributes
============================

Expand Down
Loading
Loading