|
| 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.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | + |
| 15 | +import net.sf.jsqlparser.JSQLParserException; |
| 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.select.TableStatement; |
| 20 | +import net.sf.jsqlparser.util.TablesNamesFinder; |
| 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 MySqlCreateTableSourceTest { |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(strings = {"CREATE TABLE t AS TABLE db.s", "CREATE TABLE t TABLE db.s", |
| 29 | + "CREATE TABLE t (id BIGINT) TABLE db.s", |
| 30 | + "CREATE TEMPORARY TABLE IF NOT EXISTS t ENGINE=InnoDB TABLE `db`.`s`", |
| 31 | + "CREATE TABLE t (extra INT) ENGINE=InnoDB, COMMENT='source' AS TABLE s ORDER BY id DESC LIMIT 2 OFFSET 1", |
| 32 | + "CREATE TABLE t AS SELECT 1 AS id", "CREATE TABLE t SELECT 1 AS id", |
| 33 | + "CREATE TABLE t AS (SELECT 1 AS id)"}) |
| 34 | + void roundTripsQuerySources(String sql) throws Exception { |
| 35 | + CreateTable table = parse(sql); |
| 36 | + assertNotNull(table.getSelect()); |
| 37 | + if (sql.contains("TABLE db.") || sql.contains("TABLE `db`")) { |
| 38 | + assertInstanceOf(TableStatement.class, table.getSelect()); |
| 39 | + } |
| 40 | + assertRoundTrip(table); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void retainsSourceStructureAndAsChoiceAfterMutation() throws Exception { |
| 45 | + CreateTable table = parse("CREATE TABLE target TABLE old.source"); |
| 46 | + assertFalse(table.isUseAsKeyword()); |
| 47 | + TableStatement source = assertInstanceOf(TableStatement.class, table.getSelect()); |
| 48 | + source.getTable().setSchemaName("newdb"); |
| 49 | + assertThat(new TablesNamesFinder().getTables(table)) |
| 50 | + .containsExactlyInAnyOrder("target", "newdb.source"); |
| 51 | + assertEquals("CREATE TABLE target TABLE newdb.source", table.toString()); |
| 52 | + table.setUseAsKeyword(true); |
| 53 | + assertEquals("CREATE TABLE target AS TABLE newdb.source", table.toString()); |
| 54 | + assertRoundTrip(table); |
| 55 | + assertTrue(new CreateTable().isUseAsKeyword()); |
| 56 | + } |
| 57 | + |
| 58 | + @ParameterizedTest |
| 59 | + @ValueSource(strings = {"CREATE TABLE t AS TABLE", "CREATE TABLE t TABLE", |
| 60 | + "CREATE TABLE t ENGINE=InnoDB, TABLE s", "CREATE TABLE t AS"}) |
| 61 | + void rejectsMissingSourcesAndDanglingOptionSeparators(String sql) { |
| 62 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 63 | + } |
| 64 | + |
| 65 | + private static CreateTable parse(String sql) throws JSQLParserException { |
| 66 | + return (CreateTable) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.MYSQL)); |
| 67 | + } |
| 68 | + |
| 69 | + private static void assertRoundTrip(CreateTable table) throws Exception { |
| 70 | + StringBuilder output = new StringBuilder(); |
| 71 | + table.accept(new StatementDeParser(output), null); |
| 72 | + assertEquals(table.toString(), output.toString()); |
| 73 | + CreateTable reparsed = parse(output.toString()); |
| 74 | + assertEquals(table.toString(), reparsed.toString()); |
| 75 | + assertEquals(table.isUseAsKeyword(), reparsed.isUseAsKeyword()); |
| 76 | + } |
| 77 | +} |
0 commit comments