|
| 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 net.sf.jsqlparser.JSQLParserException; |
| 14 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 17 | +import net.sf.jsqlparser.statement.create.table.CreateTable.DuplicateHandling; |
| 18 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.ValueSource; |
| 22 | + |
| 23 | +class MySqlCreateTableDuplicatesTest { |
| 24 | + @ParameterizedTest |
| 25 | + @ValueSource(strings = {"SELECT id FROM source", "TABLE source", "VALUES ROW(1), ROW(2)", |
| 26 | + "(SELECT id FROM source)", "WITH c AS (SELECT 1 AS id) SELECT id FROM c"}) |
| 27 | + void retainsModifierAndSourceAfterTableOptions(String query) throws JSQLParserException { |
| 28 | + for (DuplicateHandling handling : DuplicateHandling.values()) { |
| 29 | + for (boolean useAs : new boolean[] {false, true}) { |
| 30 | + CreateTable table = parse("CREATE TABLE t (id INT PRIMARY KEY) ENGINE=InnoDB " |
| 31 | + + handling + (useAs ? " AS " : " ") + query); |
| 32 | + assertEquals(handling, table.getDuplicateHandling()); |
| 33 | + assertEquals(useAs, table.isUseAsKeyword()); |
| 34 | + assertNotNull(table.getSelect()); |
| 35 | + roundTrip(table); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void preservesPartitionAndAllowsAstMutation() throws JSQLParserException { |
| 42 | + CreateTable table = parse("CREATE TABLE t (id INT PRIMARY KEY) PARTITION BY HASH(id) " |
| 43 | + + "PARTITIONS 2 IGNORE AS SELECT id FROM source"); |
| 44 | + assertNotNull(table.getPartitioning()); |
| 45 | + assertNull(table.getPartitioning().getPartitionOptions()); |
| 46 | + table.setDuplicateHandling(DuplicateHandling.REPLACE); |
| 47 | + assertTrue(table.toString().contains(" REPLACE AS SELECT")); |
| 48 | + roundTrip(table); |
| 49 | + table.setDuplicateHandling(null); |
| 50 | + assertFalse(table.toString().contains("REPLACE")); |
| 51 | + roundTrip(table); |
| 52 | + assertNull(parse("CREATE TABLE t AS SELECT 1").getDuplicateHandling()); |
| 53 | + } |
| 54 | + |
| 55 | + @ParameterizedTest |
| 56 | + @ValueSource(strings = {"CREATE TABLE t IGNORE", "CREATE TABLE t REPLACE AS", |
| 57 | + "CREATE TABLE t IGNORE REPLACE SELECT 1", |
| 58 | + "CREATE TABLE t ENGINE=InnoDB, IGNORE SELECT 1"}) |
| 59 | + void rejectsMissingQueriesAndConflictingModifiers(String sql) { |
| 60 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 61 | + } |
| 62 | + |
| 63 | + private static CreateTable parse(String sql) throws JSQLParserException { |
| 64 | + return (CreateTable) CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.MYSQL)); |
| 65 | + } |
| 66 | + |
| 67 | + private static void roundTrip(CreateTable table) throws JSQLParserException { |
| 68 | + StringBuilder out = new StringBuilder(); |
| 69 | + table.accept(new StatementDeParser(out)); |
| 70 | + assertEquals(table.toString(), out.toString()); |
| 71 | + CreateTable parsed = parse(out.toString()); |
| 72 | + assertEquals(table.getDuplicateHandling(), parsed.getDuplicateHandling()); |
| 73 | + assertEquals(table.isUseAsKeyword(), parsed.isUseAsKeyword()); |
| 74 | + assertEquals(out.toString(), parsed.toString()); |
| 75 | + } |
| 76 | +} |
0 commit comments