|
| 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.drop; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | + |
| 16 | +import net.sf.jsqlparser.JSQLParserException; |
| 17 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 18 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 19 | +import net.sf.jsqlparser.schema.Table; |
| 20 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.ValueSource; |
| 24 | + |
| 25 | +class OracleDropSynonymTest { |
| 26 | + @ParameterizedTest |
| 27 | + @ValueSource(strings = {"sample_syn", "\"Sample.Synonym\""}) |
| 28 | + void preservesSynonymModifiers(String name) throws JSQLParserException { |
| 29 | + for (String scope : new String[] {"", "PUBLIC "}) { |
| 30 | + for (String existence : new String[] {"", "IF EXISTS "}) { |
| 31 | + for (String force : new String[] {"", " FORCE"}) { |
| 32 | + String sql = "DROP " + scope + "SYNONYM " + existence + name + force; |
| 33 | + for (Dialect dialect : new Dialect[] {null, Dialect.ORACLE}) { |
| 34 | + Drop drop = parse(sql, dialect); |
| 35 | + assertEquals(Drop.ObjectType.SYNONYM, drop.getObjectType()); |
| 36 | + assertEquals("SYNONYM", drop.getType()); |
| 37 | + assertEquals(!scope.isEmpty(), drop.isPublicSynonym()); |
| 38 | + assertEquals(!existence.isEmpty(), drop.isIfExists()); |
| 39 | + assertEquals(!force.isEmpty(), drop.isForce()); |
| 40 | + assertEquals(1, drop.getNames().size()); |
| 41 | + assertEquals(name, drop.getName().getName()); |
| 42 | + assertEquals(sql, drop.toString()); |
| 43 | + roundTrip(drop, dialect); |
| 44 | + assertEquals(2, |
| 45 | + CCJSqlParserUtil.parseStatements(sql + "; SELECT 1").size()); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void preservesQualifiedNamesAndSupportsModelChanges() throws JSQLParserException { |
| 54 | + Drop drop = parse("DROP SYNONYM IF EXISTS \"hr.schema\".\"sample.syn\" FORCE", |
| 55 | + Dialect.ORACLE); |
| 56 | + assertEquals("\"hr.schema\"", drop.getName().getSchemaName()); |
| 57 | + assertEquals("\"sample.syn\"", drop.getName().getName()); |
| 58 | + drop.getName().setName("replacement"); |
| 59 | + assertEquals("DROP SYNONYM IF EXISTS \"hr.schema\".replacement FORCE", drop.toString()); |
| 60 | + roundTrip(drop, Dialect.ORACLE); |
| 61 | + |
| 62 | + Drop created = new Drop().withObjectType(Drop.ObjectType.SYNONYM) |
| 63 | + .withPublicSynonym(true).withIfExists(true).withName(new Table("s")) |
| 64 | + .withForce(true); |
| 65 | + assertEquals("DROP PUBLIC SYNONYM IF EXISTS s FORCE", created.toString()); |
| 66 | + roundTrip(created, Dialect.ORACLE); |
| 67 | + created.setPublicSynonym(false); |
| 68 | + created.setForce(false); |
| 69 | + assertEquals("DROP SYNONYM IF EXISTS s", created.toString()); |
| 70 | + roundTrip(created, null); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void keepsPostgreSqlForceSyntax() throws JSQLParserException { |
| 75 | + Drop database = parse("DROP DATABASE d WITH (FORCE)", Dialect.POSTGRESQL); |
| 76 | + assertTrue(database.isForce()); |
| 77 | + assertEquals("DROP DATABASE d WITH (FORCE)", database.toString()); |
| 78 | + roundTrip(database, Dialect.POSTGRESQL); |
| 79 | + } |
| 80 | + |
| 81 | + @ParameterizedTest |
| 82 | + @ValueSource(strings = {"DROP PUBLIC TABLE t", "DROP SYNONYM", |
| 83 | + "DROP PUBLIC SYNONYM IF EXISTS", |
| 84 | + "DROP SYNONYM s FORCE FORCE", "DROP SYNONYM s WITH (FORCE)", |
| 85 | + "DROP TABLE t FORCE"}) |
| 86 | + void rejectsMalformedSynonymClauses(String sql) { |
| 87 | + assertThrows(JSQLParserException.class, () -> parse(sql, Dialect.ORACLE)); |
| 88 | + } |
| 89 | + |
| 90 | + private static Drop parse(String sql, Dialect dialect) throws JSQLParserException { |
| 91 | + return (Drop) CCJSqlParserUtil.parse(sql, parser -> { |
| 92 | + if (dialect != null) { |
| 93 | + parser.withDialect(dialect); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private static void roundTrip(Drop drop, Dialect dialect) throws JSQLParserException { |
| 99 | + StringBuilder output = new StringBuilder(); |
| 100 | + drop.accept(new StatementDeParser(output), null); |
| 101 | + assertEquals(drop.toString(), output.toString()); |
| 102 | + Drop reparsed = parse(output.toString(), dialect); |
| 103 | + assertEquals(drop.getObjectType(), reparsed.getObjectType()); |
| 104 | + assertEquals(drop.isPublicSynonym(), reparsed.isPublicSynonym()); |
| 105 | + assertEquals(drop.isIfExists(), reparsed.isIfExists()); |
| 106 | + assertEquals(drop.isForce(), reparsed.isForce()); |
| 107 | + assertEquals(output.toString(), reparsed.toString()); |
| 108 | + } |
| 109 | +} |
0 commit comments