|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement.alter; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import net.sf.jsqlparser.JSQLParserException; |
| 20 | +import net.sf.jsqlparser.expression.ExpressionVisitorAdapter; |
| 21 | +import net.sf.jsqlparser.expression.LongValue; |
| 22 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 23 | +import net.sf.jsqlparser.statement.StatementVisitorAdapter; |
| 24 | +import net.sf.jsqlparser.statement.alter.AlterExpression.ColumnSetDefault; |
| 25 | +import net.sf.jsqlparser.statement.select.SelectVisitorAdapter; |
| 26 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 27 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 28 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.ValueSource; |
| 32 | + |
| 33 | +class AlterColumnDefaultExpressionTest { |
| 34 | + @ParameterizedTest |
| 35 | + @ValueSource(strings = {"(1 + 2)", "NULL", "CURRENT_TIMESTAMP", |
| 36 | + "nextval('app.counter'::regclass)", "'value'::text", "-1"}) |
| 37 | + void parsedDefaultRetainsExpressionAndLegacyText(String value) throws JSQLParserException { |
| 38 | + Alter alter = parse(value); |
| 39 | + ColumnSetDefault column = |
| 40 | + alter.getAlterExpressions().get(0).getColumnSetDefaultList().get(0); |
| 41 | + assertNotNull(column.getDefaultExpression()); |
| 42 | + assertEquals(column.getDefaultExpression().toString(), column.getDefaultValue()); |
| 43 | + StringBuilder buffer = new StringBuilder(); |
| 44 | + alter.accept(new StatementDeParser(buffer), null); |
| 45 | + assertEquals(alter.toString(), buffer.toString()); |
| 46 | + assertEquals(alter.toString(), CCJSqlParserUtil.parse(buffer.toString()).toString()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void visitorReachesDefaultsInEachAlterActionWithContext() throws JSQLParserException { |
| 51 | + List<Long> values = new ArrayList<>(); |
| 52 | + ExpressionVisitorAdapter<Void> expressions = new ExpressionVisitorAdapter<Void>() { |
| 53 | + @Override |
| 54 | + public <S> Void visit(LongValue value, S context) { |
| 55 | + assertEquals("context", context); |
| 56 | + values.add(value.getValue()); |
| 57 | + return null; |
| 58 | + } |
| 59 | + }; |
| 60 | + CCJSqlParserUtil.parse("ALTER TABLE t ALTER COLUMN a SET DEFAULT (1 + 2), " |
| 61 | + + "ALTER COLUMN b SET DEFAULT 3") |
| 62 | + .accept(new StatementVisitorAdapter<>(new SelectVisitorAdapter<>(expressions)), |
| 63 | + "context"); |
| 64 | + assertThat(values).containsExactly(1L, 2L, 3L); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void customDeparserAndAstEditsUseTheStructuredDefault() throws JSQLParserException { |
| 69 | + Alter alter = parse("(1 + 2)"); |
| 70 | + StringBuilder output = new StringBuilder(); |
| 71 | + ExpressionDeParser expressions = new ExpressionDeParser() { |
| 72 | + @Override |
| 73 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 74 | + return getBuilder().append(value.getValue() + 100); |
| 75 | + } |
| 76 | + }; |
| 77 | + alter.accept(new StatementDeParser(expressions, new SelectDeParser(), output), null); |
| 78 | + assertEquals("ALTER TABLE t ALTER COLUMN a SET DEFAULT (101 + 102)", output.toString()); |
| 79 | + ColumnSetDefault column = |
| 80 | + alter.getAlterExpressions().get(0).getColumnSetDefaultList().get(0); |
| 81 | + column.setDefaultExpression(new LongValue(42)); |
| 82 | + assertEquals("42", column.getDefaultValue()); |
| 83 | + assertEquals("ALTER TABLE t ALTER COLUMN a SET DEFAULT 42", alter.toString()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void legacyStringConstructorRemainsOpaqueAndAcceptsNull() { |
| 88 | + ColumnSetDefault column = new ColumnSetDefault("a", "vendor_default()"); |
| 89 | + assertNull(column.getDefaultExpression()); |
| 90 | + assertEquals("a SET DEFAULT vendor_default()", column.toString()); |
| 91 | + assertEquals("a SET DEFAULT null", new ColumnSetDefault("a", null).toString()); |
| 92 | + column.setDefaultExpression(new LongValue(1)); |
| 93 | + assertEquals("1", column.getDefaultValue()); |
| 94 | + column.setDefaultExpression(null); |
| 95 | + assertNull(column.getDefaultValue()); |
| 96 | + } |
| 97 | + |
| 98 | + private static Alter parse(String value) throws JSQLParserException { |
| 99 | + return (Alter) CCJSqlParserUtil.parse("ALTER TABLE t ALTER COLUMN a SET DEFAULT " + value); |
| 100 | + } |
| 101 | +} |
0 commit comments