|
| 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.create; |
| 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.expression.LongValue; |
| 16 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 17 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 18 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 19 | +import net.sf.jsqlparser.statement.create.view.CreateView; |
| 20 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 21 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 22 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.ValueSource; |
| 26 | + |
| 27 | +class CreateTableDataPopulationTest { |
| 28 | + @ParameterizedTest |
| 29 | + @ValueSource(strings = {"SELECT id FROM source", "TABLE source", "VALUES (1), (2)", |
| 30 | + "(SELECT id FROM source)", "WITH c AS (SELECT 1 AS id) SELECT id FROM c", |
| 31 | + "SELECT id FROM source UNION ALL SELECT id FROM other ORDER BY id LIMIT 4"}) |
| 32 | + void preservesDataChoiceForQuerySources(String query) throws JSQLParserException { |
| 33 | + for (String suffix : new String[] {"", " WITH DATA", " WITH NO DATA"}) { |
| 34 | + CreateTable table = parse("CREATE TABLE t AS " + query + suffix); |
| 35 | + assertEquals(suffix.isEmpty() ? null : !suffix.contains("NO"), table.getWithData()); |
| 36 | + roundTrip(table); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void sharesOutputAndKeepsCustomQueryVisitor() throws JSQLParserException { |
| 42 | + CreateTable table = |
| 43 | + parse("CREATE TEMP TABLE t (id) ON COMMIT DROP AS SELECT 7 WITH NO DATA"); |
| 44 | + table.setWithData(true); |
| 45 | + assertTrue(table.toString().endsWith("WITH DATA")); |
| 46 | + ExpressionDeParser expression = new ExpressionDeParser() { |
| 47 | + @Override |
| 48 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 49 | + return getBuilder().append(value.getValue() + 100); |
| 50 | + } |
| 51 | + }; |
| 52 | + StringBuilder out = new StringBuilder(); |
| 53 | + table.accept(new StatementDeParser(expression, new SelectDeParser(), out)); |
| 54 | + assertTrue(out.toString().endsWith("SELECT 107 WITH DATA")); |
| 55 | + assertTrue(table.toString().contains("SELECT 7")); |
| 56 | + table.setWithData(null); |
| 57 | + assertFalse(table.toString().contains("WITH DATA")); |
| 58 | + roundTrip(table); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void retainsMaterializedViewTailAndStatementBoundaries() throws JSQLParserException { |
| 63 | + CreateView view = (CreateView) CCJSqlParserUtil.parse( |
| 64 | + "CREATE MATERIALIZED VIEW v AS SELECT 1 WITH NO DATA"); |
| 65 | + assertEquals(Boolean.FALSE, view.getWithData()); |
| 66 | + assertEquals(2, CCJSqlParserUtil.parseStatements( |
| 67 | + "CREATE TABLE t AS SELECT 1 WITH NO DATA; SELECT 2").size()); |
| 68 | + for (String sql : List.of("CREATE TABLE t AS SELECT 1 WITH NO", |
| 69 | + "CREATE TABLE t AS SELECT 1 WITH DATA WITH NO DATA", |
| 70 | + "SELECT 1 WITH NO DATA")) { |
| 71 | + assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static CreateTable parse(String sql) throws JSQLParserException { |
| 76 | + return (CreateTable) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 77 | + } |
| 78 | + |
| 79 | + private static void roundTrip(CreateTable table) throws JSQLParserException { |
| 80 | + StringBuilder out = new StringBuilder(); |
| 81 | + table.accept(new StatementDeParser(out)); |
| 82 | + assertEquals(table.toString(), out.toString()); |
| 83 | + assertEquals(table.getWithData(), parse(out.toString()).getWithData()); |
| 84 | + assertEquals(out.toString(), parse(out.toString()).toString()); |
| 85 | + } |
| 86 | +} |
0 commit comments