diff --git a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/Adapters.java b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/Adapters.java index 27689c106c..985a4fc382 100644 --- a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/Adapters.java +++ b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/Adapters.java @@ -49,10 +49,14 @@ import liquibase.database.core.SQLiteDatabase; import liquibase.database.core.SybaseASADatabase; import liquibase.database.core.SybaseDatabase; import liquibase.database.structure.Column; +import liquibase.database.structure.Sequence; import liquibase.database.structure.Table; import liquibase.database.structure.View; +import org.jooq.DataType; import org.jooq.SQLDialect; +import org.jooq.impl.DefaultDataType; +import org.jooq.impl.SQLDataType; /** * A set of adapters to wrap Liquibase types in jOOQ types. @@ -65,7 +69,29 @@ import org.jooq.SQLDialect; public class Adapters { /** - * Extract the jOOQ {@link SQLDialect} from a Liquibase column. + * Extract the jOOQ {@link DataType} from a Liquibase {@link Column}. + */ + public static DataType dataType(Column column) { + return DefaultDataType.getDataType( + dialect(column), + column.getTypeName(), + column.getColumnSize(), + column.getDecimalDigits()); + } + + /** + * Extract the jOOQ {@link DataType} from a Liquibase {@link Sequence}. + *

+ * Note: Liquibase sequences do not expose their data types. The best match + * for this in jOOQ is {@link SQLDataType#BIGINT} + */ + @SuppressWarnings("unused") + public static DataType dataType(Sequence sequence) { + return SQLDataType.BIGINT; + } + + /** + * Extract the jOOQ {@link SQLDialect} from a Liquibase {@link Column}. */ public static SQLDialect dialect(Column column) { View view = column.getView(); @@ -78,21 +104,21 @@ public class Adapters { } /** - * Extract the jOOQ {@link SQLDialect} from a Liquibase view. + * Extract the jOOQ {@link SQLDialect} from a Liquibase {@link View}. */ public static SQLDialect dialect(View view) { return dialect(view.getDatabase()); } /** - * Extract the jOOQ {@link SQLDialect} from a Liquibase table. + * Extract the jOOQ {@link SQLDialect} from a Liquibase {@link Table}. */ public static SQLDialect dialect(Table table) { return dialect(table.getDatabase()); } /** - * Extract the jOOQ {@link SQLDialect} from a Liquibase. + * Extract the jOOQ {@link SQLDialect} from a Liquibase {@link Database}. */ @SuppressWarnings("deprecation") public static SQLDialect dialect(Database database) { @@ -127,16 +153,23 @@ public class Adapters { } /** - * Extract the jOOQ {@link Table} from a Liquibase table. + * Extract the jOOQ {@link org.jooq.Table} from a Liquibase {@link Table}. */ public static org.jooq.Table table(Table table) { return new LiquibaseTable(table); } /** - * Extract the jOOQ {@link Table} from a Liquibase view. + * Extract the jOOQ {@link org.jooq.Table} from a Liquibase {@link View}. */ public static org.jooq.Table table(View view) { return new LiquibaseTable(view); } + + /** + * Extract the jOOQ {@link org.jooq.Sequence} from a Liquibase {@link Sequence}. + */ + public static org.jooq.Sequence sequence(Sequence sequence) { + return new LiquibaseSequence(sequence); + } } diff --git a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSchema.java b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSchema.java index 12040c9772..fd7b46ce5d 100644 --- a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSchema.java +++ b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSchema.java @@ -37,6 +37,9 @@ package org.jooq.liquibase; import org.jooq.impl.SchemaImpl; +/** + * @author Lukas Eder + */ class LiquibaseSchema extends SchemaImpl { /** @@ -47,4 +50,4 @@ class LiquibaseSchema extends SchemaImpl { LiquibaseSchema(String name) { super(name); } -} \ No newline at end of file +} diff --git a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSequence.java b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSequence.java new file mode 100644 index 0000000000..7c6a0a6c95 --- /dev/null +++ b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseSequence.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2009-2013, Lukas Eder, lukas.eder@gmail.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.liquibase; + +import liquibase.database.structure.Sequence; + +import org.jooq.impl.SequenceImpl; + +/** + * @author Lukas Eder + */ +@SuppressWarnings({ "rawtypes", "unchecked" }) +class LiquibaseSequence extends SequenceImpl { + + /** + * Generated UID + */ + private static final long serialVersionUID = -5000543581139890516L; + + LiquibaseSequence(Sequence sequence) { + super(sequence.getName(), new LiquibaseSchema(sequence.getSchema()), Adapters.dataType(sequence)); + } +} diff --git a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseTable.java b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseTable.java index cdd79cde27..f1fb1d1462 100644 --- a/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseTable.java +++ b/jOOQ-liquibase/src/main/java/org/jooq/liquibase/LiquibaseTable.java @@ -36,7 +36,7 @@ package org.jooq.liquibase; import static org.jooq.impl.Factory.tableByName; -import static org.jooq.liquibase.Adapters.dialect; +import static org.jooq.liquibase.Adapters.dataType; import java.util.List; @@ -44,9 +44,7 @@ import liquibase.database.structure.Column; import liquibase.database.structure.Table; import liquibase.database.structure.View; -import org.jooq.DataType; import org.jooq.impl.CustomTable; -import org.jooq.impl.DefaultDataType; /** * @author Lukas Eder @@ -71,14 +69,7 @@ class LiquibaseTable extends CustomTable { private void init(List columns) { for (Column column : columns) { - DataType type = DefaultDataType.getDataType( - dialect(column), - column.getTypeName(), - column.getColumnSize(), - column.getDecimalDigits()); - - column.getDataType(); - createField(column.getName(), type, this); + createField(column.getName(), dataType(column), this); } } diff --git a/jOOQ-liquibase/src/test/java/org/jooq/liquibase/test/AdaptersTest.java b/jOOQ-liquibase/src/test/java/org/jooq/liquibase/test/AdaptersTest.java index 2a44a8f46a..2f606761f4 100644 --- a/jOOQ-liquibase/src/test/java/org/jooq/liquibase/test/AdaptersTest.java +++ b/jOOQ-liquibase/src/test/java/org/jooq/liquibase/test/AdaptersTest.java @@ -37,6 +37,7 @@ package org.jooq.liquibase.test; import static java.util.Arrays.asList; import static junit.framework.Assert.assertEquals; +import static org.jooq.liquibase.Adapters.sequence; import static org.jooq.liquibase.Adapters.table; import static org.jooq.maven.example.h2.Tables.T_BOOK; import static org.jooq.maven.example.h2.Tables.V_LIBRARY; @@ -50,8 +51,10 @@ import liquibase.snapshot.DatabaseSnapshot; import liquibase.snapshot.DatabaseSnapshotGeneratorFactory; import org.jooq.SQLDialect; +import org.jooq.Sequence; import org.jooq.Table; import org.jooq.impl.Executor; +import org.jooq.maven.example.h2.Sequences; import org.junit.After; import org.junit.Before; @@ -95,6 +98,15 @@ public class AdaptersTest { testEqualTables(V_LIBRARY, table); } + @Test + public void testSequences() { + Sequence sequence = sequence(snapshot.getSequence("S_AUTHOR_ID")); + + assertEquals( + create.nextval(Sequences.S_AUTHOR_ID) + 1, + create.nextval(sequence)); + } + /** * A couple of tests, checking if tables are really equal */