Visitar URL original
Share access to table and view column defaults by minleejae · Pull Request #2742 · JSQLParser/JSqlParser · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: omit inactive column defaults from shared access
Signed-off-by: minleejae <mmj9808@gmail.com>
  • Loading branch information
minleejae committed Oct 1, 2026
commit b2614e408d133efeecf50e044b8779d8609f13b5
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public List<ColumnSetDefault> getColumnSetDefaultList() {
*/
public List<ColumnDefaultAction> getColumnDefaults() {
if (operation != AlterOperation.ALTER || columnSetDefaultList == null
|| constraintType != null && constraintSymbol != null
|| columnDropDefaultList != null && !columnDropDefaultList.isEmpty()) {
return Collections.emptyList();
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ void inactiveRelationPropertiesDoNotAppearAsDefaults() {
assertTrue(action.getColumnDefaults().isEmpty());
}

@Test
void constraintAlterationTakesPrecedenceOverPreviouslySetDefaults() throws JSQLParserException {
Alter statement =
(Alter) CCJSqlParserUtil.parse("ALTER TABLE t ALTER COLUMN a SET DEFAULT 1");
AlterExpression action = statement.getAlterExpressions().get(0);
ColumnDefaultAction previousDefault = action.getColumnDefaults().get(0);
action.setConstraintType("CHECK");
action.setConstraintSymbol("c");
action.setEnforced(true);

assertTrue(action.getColumnDefaults().isEmpty());
assertEquals("ALTER TABLE t ALTER CHECK c ENFORCED", statement.toString());
StringBuilder sql = new StringBuilder();
statement.accept(new StatementDeParser(sql), null);
assertEquals(statement.toString(), sql.toString());
assertEquals(statement.toString(), CCJSqlParserUtil.parse(sql.toString()).toString());

action.setConstraintSymbol(null);
assertSame(previousDefault, action.getColumnDefaults().get(0));
roundTrip(statement);
}

@Test
void opaqueLegacyConstructorIsNotParsedOrPassedToAnExpressionVisitor() {
ColumnSetDefault legacy = new ColumnSetDefault("a", "vendor_specific(?)::opaque");
Expand Down
Loading