|
| 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.truncate; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 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.statement.CascadeBehavior; |
| 21 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
| 25 | + |
| 26 | +class OracleTruncateTest { |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(strings = {"samples", "hr.\"Sample.Table\""}) |
| 29 | + void preservesLogStorageAndCascadeOptions(String table) throws JSQLParserException { |
| 30 | + for (Truncate.MaterializedViewLogOption log : new Truncate.MaterializedViewLogOption[] { |
| 31 | + null, Truncate.MaterializedViewLogOption.PRESERVE, |
| 32 | + Truncate.MaterializedViewLogOption.PURGE}) { |
| 33 | + for (Truncate.StorageOption storage : new Truncate.StorageOption[] {null, |
| 34 | + Truncate.StorageOption.DROP, Truncate.StorageOption.DROP_ALL, |
| 35 | + Truncate.StorageOption.REUSE}) { |
| 36 | + for (String cascade : new String[] {"", " CASCADE"}) { |
| 37 | + String sql = "TRUNCATE TABLE " + table |
| 38 | + + (log == null ? "" : " " + log + " MATERIALIZED VIEW LOG") |
| 39 | + + (storage == null ? "" |
| 40 | + : storage == Truncate.StorageOption.DROP_ALL |
| 41 | + ? " DROP ALL STORAGE" |
| 42 | + : " " + storage + " STORAGE") |
| 43 | + + cascade; |
| 44 | + for (Dialect dialect : new Dialect[] {null, Dialect.ORACLE}) { |
| 45 | + Truncate truncate = parse(sql, dialect); |
| 46 | + assertEquals(table, truncate.getTable().getFullyQualifiedName()); |
| 47 | + assertEquals(log, truncate.getMaterializedViewLogOption()); |
| 48 | + assertEquals(storage, truncate.getStorageOption()); |
| 49 | + assertEquals(cascade.isEmpty() ? null : CascadeBehavior.CASCADE, |
| 50 | + truncate.getDropBehavior()); |
| 51 | + assertNull(truncate.getIdentityOption()); |
| 52 | + assertEquals(sql, truncate.toString()); |
| 53 | + roundTrip(truncate, dialect); |
| 54 | + assertEquals(2, |
| 55 | + CCJSqlParserUtil.parseStatements(sql + "; SELECT 1").size()); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void supportsModelConstructionAndClearingOptions() throws JSQLParserException { |
| 64 | + Truncate truncate = new Truncate().withTableToken(true).withTable(new Table("samples")) |
| 65 | + .withMaterializedViewLogOption(Truncate.MaterializedViewLogOption.PURGE) |
| 66 | + .withStorageOption(Truncate.StorageOption.DROP_ALL).withCascade(true); |
| 67 | + assertEquals("TRUNCATE TABLE samples PURGE MATERIALIZED VIEW LOG DROP ALL STORAGE CASCADE", |
| 68 | + truncate.toString()); |
| 69 | + roundTrip(truncate, Dialect.ORACLE); |
| 70 | + truncate.setMaterializedViewLogOption(Truncate.MaterializedViewLogOption.PRESERVE); |
| 71 | + truncate.setStorageOption(Truncate.StorageOption.REUSE); |
| 72 | + assertEquals("TRUNCATE TABLE samples PRESERVE MATERIALIZED VIEW LOG REUSE STORAGE CASCADE", |
| 73 | + truncate.toString()); |
| 74 | + roundTrip(truncate, null); |
| 75 | + truncate.setMaterializedViewLogOption(null); |
| 76 | + truncate.setStorageOption(null); |
| 77 | + assertEquals("TRUNCATE TABLE samples CASCADE", truncate.toString()); |
| 78 | + roundTrip(truncate, null); |
| 79 | + } |
| 80 | + |
| 81 | + @ParameterizedTest |
| 82 | + @ValueSource(strings = {"DROP", "DROP ALL", "REUSE", "PRESERVE MATERIALIZED VIEW", |
| 83 | + "PURGE LOG", "DROP STORAGE REUSE STORAGE", |
| 84 | + "REUSE STORAGE PURGE MATERIALIZED VIEW LOG", "CASCADE REUSE STORAGE"}) |
| 85 | + void rejectsIncompleteRepeatedAndOutOfOrderOptions(String options) { |
| 86 | + assertThrows(JSQLParserException.class, |
| 87 | + () -> parse("TRUNCATE TABLE samples " + options, Dialect.ORACLE)); |
| 88 | + } |
| 89 | + |
| 90 | + private static Truncate parse(String sql, Dialect dialect) throws JSQLParserException { |
| 91 | + return (Truncate) CCJSqlParserUtil.parse(sql, parser -> { |
| 92 | + if (dialect != null) { |
| 93 | + parser.withDialect(dialect); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + private static void roundTrip(Truncate truncate, Dialect dialect) throws JSQLParserException { |
| 99 | + StringBuilder output = new StringBuilder(); |
| 100 | + truncate.accept(new StatementDeParser(output), null); |
| 101 | + assertEquals(truncate.toString(), output.toString()); |
| 102 | + Truncate reparsed = parse(output.toString(), dialect); |
| 103 | + assertEquals(truncate.getMaterializedViewLogOption(), |
| 104 | + reparsed.getMaterializedViewLogOption()); |
| 105 | + assertEquals(truncate.getStorageOption(), reparsed.getStorageOption()); |
| 106 | + assertEquals(output.toString(), reparsed.toString()); |
| 107 | + } |
| 108 | +} |
0 commit comments