|
| 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.*; |
| 13 | +import net.sf.jsqlparser.JSQLParserException; |
| 14 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.schema.Table; |
| 17 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
| 21 | + |
| 22 | +class PostgreSqlDropIndexTest { |
| 23 | + @ParameterizedTest |
| 24 | + @ValueSource(strings = {"DROP INDEX CONCURRENTLY ix", |
| 25 | + "DROP INDEX CONCURRENTLY IF EXISTS ix", |
| 26 | + "DROP INDEX CONCURRENTLY IF EXISTS public.ix RESTRICT"}) |
| 27 | + void concurrencyIsIndependentOfTarget(String sql) throws JSQLParserException { |
| 28 | + Drop drop = (Drop) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 29 | + assertTrue(drop.isConcurrently()); |
| 30 | + assertEquals(sql.contains("IF EXISTS"), drop.isIfExists()); |
| 31 | + assertEquals("ix", drop.getName().getName()); |
| 32 | + assertEquals(sql, drop.toString()); |
| 33 | + drop.setName(new Table("new_ix")); |
| 34 | + String expected = sql.replace("public.ix", "ix").replace("ix", "new_ix"); |
| 35 | + assertEquals(expected, drop.toString()); |
| 36 | + StringBuilder buffer = new StringBuilder(); |
| 37 | + drop.accept(new StatementDeParser(buffer), null); |
| 38 | + assertEquals(expected, buffer.toString()); |
| 39 | + Drop reparsed = (Drop) CCJSqlParserUtil.parse(expected); |
| 40 | + assertEquals("new_ix", reparsed.getName().getName()); |
| 41 | + assertTrue(reparsed.isConcurrently()); |
| 42 | + assertEquals(2, CCJSqlParserUtil.parseStatements(expected + "; SELECT 1").size()); |
| 43 | + drop.setConcurrently(false); |
| 44 | + assertEquals(expected.replace("CONCURRENTLY ", ""), drop.toString()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void ordinaryAndQuotedConcurrentNamesArePreserved() throws JSQLParserException { |
| 49 | + for (String sql : new String[] {"DROP TABLE concurrently", "DROP INDEX \"concurrently\"", |
| 50 | + "DROP INDEX ix1, ix2 CASCADE", "DROP INDEX ix ON t ALGORITHM = INPLACE"}) { |
| 51 | + Drop drop = (Drop) CCJSqlParserUtil.parse(sql); |
| 52 | + assertFalse(drop.isConcurrently()); |
| 53 | + assertEquals(sql, drop.toString()); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments