|
| 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.schema.Sequence; |
| 17 | +import net.sf.jsqlparser.statement.Statement; |
| 18 | +import net.sf.jsqlparser.statement.alter.Alter; |
| 19 | +import net.sf.jsqlparser.statement.create.table.CreateTable; |
| 20 | +import net.sf.jsqlparser.statement.create.table.IdentityDefinition; |
| 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 IdentitySequenceNameTest { |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(strings = {"ids", "public.ids", "\"schema.name\".\"seq.name\""}) |
| 29 | + void sharesIdentityOptionsAcrossCreateAndAlter(String name) throws JSQLParserException { |
| 30 | + String definition = "GENERATED BY DEFAULT AS IDENTITY (START 4 SEQUENCE NAME " + name |
| 31 | + + " CACHE 2 NO CYCLE)"; |
| 32 | + CreateTable create = (CreateTable) parse("CREATE TABLE t (id INT " + definition + ")"); |
| 33 | + IdentityDefinition identity = create.getColumnDefinitions().get(0).getColumnOptions().get(0) |
| 34 | + .getIdentityDefinition(); |
| 35 | + Alter alter = (Alter) parse("ALTER TABLE t ALTER COLUMN id ADD " + definition); |
| 36 | + IdentityDefinition altered = alter.getAlterExpressions().get(0).getColDataTypeList().get(0) |
| 37 | + .getIdentityAlterations().get(0).getIdentityDefinition(); |
| 38 | + for (IdentityDefinition current : new IdentityDefinition[] {identity, altered}) { |
| 39 | + assertEquals(4, current.getParameters().size()); |
| 40 | + Sequence.Parameter named = current.getParameters().get(1); |
| 41 | + assertEquals(Sequence.ParameterType.SEQUENCE_NAME, named.getOption()); |
| 42 | + assertEquals(name, named.getSequenceName().getFullyQualifiedName()); |
| 43 | + assertNull(named.getValue()); |
| 44 | + named.getSequenceName().setName("renamed"); |
| 45 | + } |
| 46 | + assertTrue(create.toString().contains("renamed")); |
| 47 | + assertTrue(alter.toString().contains("renamed")); |
| 48 | + roundTrip(create); |
| 49 | + roundTrip(alter); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void preservesNameOnlyAndDoesNotExtendStandaloneSequenceOptions() throws JSQLParserException { |
| 54 | + String sql = "CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY (SEQUENCE NAME ids))"; |
| 55 | + assertEquals(sql, parse(sql).toString()); |
| 56 | + roundTrip(parse(sql)); |
| 57 | + for (String invalid : new String[] { |
| 58 | + "CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY (SEQUENCE NAME))", |
| 59 | + "CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY ())", |
| 60 | + "CREATE SEQUENCE ids SEQUENCE NAME other_ids", |
| 61 | + "ALTER TABLE t ALTER COLUMN id SET SEQUENCE NAME other_ids"}) { |
| 62 | + assertThrows(JSQLParserException.class, () -> parse(invalid), invalid); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static Statement parse(String sql) throws JSQLParserException { |
| 67 | + return CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.POSTGRESQL)); |
| 68 | + } |
| 69 | + |
| 70 | + private static void roundTrip(Statement statement) throws JSQLParserException { |
| 71 | + StringBuilder out = new StringBuilder(); |
| 72 | + statement.accept(new StatementDeParser(out)); |
| 73 | + assertEquals(statement.toString(), out.toString()); |
| 74 | + assertEquals(out.toString(), parse(out.toString()).toString()); |
| 75 | + } |
| 76 | +} |
0 commit comments