|
| 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.junit.jupiter.api.Assertions.*; |
| 13 | +import java.util.List; |
| 14 | +import net.sf.jsqlparser.JSQLParserException; |
| 15 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 16 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 17 | +import net.sf.jsqlparser.parser.feature.Feature; |
| 18 | +import net.sf.jsqlparser.statement.StatementVisitorAdapter; |
| 19 | +import net.sf.jsqlparser.statement.Statements; |
| 20 | +import net.sf.jsqlparser.statement.alter.schema.AlterSchema; |
| 21 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 22 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 23 | +import net.sf.jsqlparser.util.validation.Validation; |
| 24 | +import net.sf.jsqlparser.util.validation.feature.FeaturesAllowed; |
| 25 | +import net.sf.jsqlparser.util.validation.feature.PostgresqlVersion; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.ValueSource; |
| 29 | + |
| 30 | +class AlterSchemaTest { |
| 31 | + @ParameterizedTest |
| 32 | + @ValueSource(strings = {"ALTER SCHEMA analytics RENAME TO reporting", |
| 33 | + "ALTER SCHEMA \"a.b\" RENAME TO \"next.schema\"", |
| 34 | + "ALTER SCHEMA s OWNER TO app", "ALTER SCHEMA s OWNER TO CURRENT_USER", |
| 35 | + "ALTER SCHEMA s OWNER TO CURRENT_ROLE", "ALTER SCHEMA s OWNER TO SESSION_USER", |
| 36 | + "ALTER SCHEMA \"Case\" OWNER TO \"role.with.dot\""}) |
| 37 | + void parsesAndRendersSchemaActions(String sql) throws Exception { |
| 38 | + AlterSchema schema = parse(sql); |
| 39 | + assertEquals(sql, schema.toString()); |
| 40 | + assertTrue(new TablesNamesFinder().getTables(schema).isEmpty()); |
| 41 | + roundTrip(schema); |
| 42 | + assertTrue(new Validation(List.of(PostgresqlVersion.V14), sql).validate().isEmpty()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void supportsAstMutationAndVisitorDispatch() throws Exception { |
| 47 | + AlterSchema schema = parse("ALTER SCHEMA s RENAME TO renamed"); |
| 48 | + assertEquals(AlterSchema.Action.RENAME, schema.getAction()); |
| 49 | + assertEquals("renamed", schema.getNewName()); |
| 50 | + schema.setSchemaName("\"schema.name\""); |
| 51 | + schema.setNewName("replacement"); |
| 52 | + roundTrip(schema); |
| 53 | + schema.setAction(AlterSchema.Action.OWNER); |
| 54 | + schema.setOwner("CURRENT_ROLE"); |
| 55 | + assertEquals("ALTER SCHEMA \"schema.name\" OWNER TO CURRENT_ROLE", schema.toString()); |
| 56 | + roundTrip(schema); |
| 57 | + Object context = new Object(); |
| 58 | + assertEquals("visited", schema.accept(new StatementVisitorAdapter<String>() { |
| 59 | + @Override |
| 60 | + public <S> String visit(AlterSchema statement, S supplied) { |
| 61 | + assertSame(schema, statement); |
| 62 | + assertSame(context, supplied); |
| 63 | + return "visited"; |
| 64 | + } |
| 65 | + }, context)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void respectsStatementBoundariesAndValidationCapabilities() throws Exception { |
| 70 | + Statements statements = CCJSqlParserUtil.parseStatements( |
| 71 | + "ALTER SCHEMA s OWNER TO CURRENT_USER; SELECT 1;"); |
| 72 | + assertEquals(2, statements.size()); |
| 73 | + assertInstanceOf(AlterSchema.class, statements.get(0)); |
| 74 | + assertFalse(new Validation(List.of(FeaturesAllowed.SELECT), |
| 75 | + "ALTER SCHEMA s RENAME TO r").validate().isEmpty()); |
| 76 | + assertTrue(new Validation(List.of(new FeaturesAllowed().add(Feature.alterSchema)), |
| 77 | + "ALTER SCHEMA s RENAME TO r").validate().isEmpty()); |
| 78 | + } |
| 79 | + |
| 80 | + @ParameterizedTest |
| 81 | + @ValueSource(strings = {"ALTER SCHEMA s RENAME", "ALTER SCHEMA s OWNER TO", |
| 82 | + "ALTER SCHEMA s RENAME TO 'not an identifier'", "ALTER SCHEMA s OWNER TO a.b", |
| 83 | + "ALTER SCHEMA s OWNER TO a, b", "ALTER SCHEMA a.b RENAME TO c"}) |
| 84 | + void rejectsIncompleteActionsAndQualifiedSchemaOrRoleNames(String sql) { |
| 85 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 86 | + } |
| 87 | + |
| 88 | + private static AlterSchema parse(String sql) throws JSQLParserException { |
| 89 | + return (AlterSchema) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 90 | + } |
| 91 | + |
| 92 | + private static void roundTrip(AlterSchema schema) throws Exception { |
| 93 | + StringBuilder out = new StringBuilder(); |
| 94 | + schema.accept(new StatementDeParser(out), null); |
| 95 | + assertEquals(schema.toString(), out.toString()); |
| 96 | + assertEquals(schema.toString(), parse(out.toString()).toString()); |
| 97 | + } |
| 98 | +} |
0 commit comments