|
| 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 net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 14 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 15 | +import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
| 16 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 17 | +import net.sf.jsqlparser.statement.create.table.Index; |
| 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 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | + |
| 25 | +class SqlServerCreateTableSeparatorTest { |
| 26 | + private CreateTable parse(String sql) throws JSQLParserException { |
| 27 | + return (CreateTable) CCJSqlParserUtil.parse(sql, |
| 28 | + p -> p.withDialect(Dialect.SQLSERVER).withUnsupportedStatements(false)); |
| 29 | + } |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @ValueSource(strings = {"CREATE TABLE t (id int,)", |
| 33 | + "CREATE TABLE t (id int, name varchar(20),)", |
| 34 | + "CREATE TABLE t (id int, PRIMARY KEY (id),)", |
| 35 | + "CREATE TABLE t (id int, CHECK (id > 0),)", |
| 36 | + "CREATE TABLE t (id int, /* last element */ )"}) |
| 37 | + void acceptsOneTrailingSeparatorAndNormalizesBothRenderers(String sql) throws Exception { |
| 38 | + CreateTable table = parse(sql); |
| 39 | + String normalized = table.toString(); |
| 40 | + assertFalse(normalized.contains(",)")); |
| 41 | + assertEquals(normalized, parse(normalized).toString()); |
| 42 | + StringBuilder buffer = new StringBuilder(); |
| 43 | + table.accept(new StatementDeParser(buffer), null); |
| 44 | + assertEquals(normalized, buffer.toString()); |
| 45 | + assertEquals(table.getTableElements().size(), |
| 46 | + parse(buffer.toString()).getTableElements().size()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void preservesSakilaColumnsAndNonclusteredPrimaryKey() throws Exception { |
| 51 | + CreateTable table = parse("CREATE TABLE film_text (film_id INT NOT NULL, " |
| 52 | + + "title VARCHAR(255) NOT NULL, description TEXT, PRIMARY KEY NONCLUSTERED (film_id),)"); |
| 53 | + assertEquals(4, table.getTableElements().size()); |
| 54 | + assertInstanceOf(ColumnDefinition.class, table.getTableElements().get(0)); |
| 55 | + Index primaryKey = assertInstanceOf(Index.class, table.getTableElements().get(3)); |
| 56 | + assertEquals("PRIMARY KEY", primaryKey.getType()); |
| 57 | + assertEquals(Index.Clustering.NONCLUSTERED, primaryKey.getClustering()); |
| 58 | + assertEquals(3, table.getColumnDefinitions().size()); |
| 59 | + table.getColumnDefinitions().get(0).setColumnName("renamed_id"); |
| 60 | + assertTrue(table.toString().contains("renamed_id INT")); |
| 61 | + assertEquals(2, CCJSqlParserUtil.parseStatements(table + "; SELECT 1;", |
| 62 | + p -> p.withDialect(Dialect.SQLSERVER)).size()); |
| 63 | + } |
| 64 | + |
| 65 | + @ParameterizedTest |
| 66 | + @ValueSource(strings = {"CREATE TABLE t (,)", |
| 67 | + "CREATE TABLE t (, id int)", "CREATE TABLE t (id int,,)", |
| 68 | + "CREATE TABLE t (id int,, name int)", "CREATE TABLE t (id int, PRIMARY KEY,)", |
| 69 | + "CREATE TABLE t (id int, PRIMARY KEY (id,))", |
| 70 | + "CREATE FUNCTION f() RETURNS @r TABLE (id int,) AS BEGIN RETURN; END"}) |
| 71 | + void rejectsMissingElementsAndDoesNotWidenOtherDefinitionLists(String sql) { |
| 72 | + assertThrows(JSQLParserException.class, () -> parse(sql)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void retainsOtherDialectsAndCreateTableForms() throws Exception { |
| 77 | + String trailing = "CREATE TABLE t (id int,)"; |
| 78 | + assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(trailing)); |
| 79 | + for (Dialect dialect : new Dialect[] {Dialect.POSTGRESQL, Dialect.MYSQL, Dialect.ORACLE}) { |
| 80 | + assertThrows(JSQLParserException.class, |
| 81 | + () -> CCJSqlParserUtil.parse(trailing, p -> p.withDialect(dialect))); |
| 82 | + } |
| 83 | + for (String sql : new String[] {"CREATE TABLE t (id int, PRIMARY KEY (id))", |
| 84 | + "CREATE TABLE t (id) AS SELECT 1", "CREATE TABLE t AS SELECT 1", |
| 85 | + "CREATE TABLE t ()"}) { |
| 86 | + assertEquals(CCJSqlParserUtil.parse(sql).toString(), parse(sql).toString()); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments