diff --git a/jOOQ/src/main/java/org/jooq/SQLDialect.java b/jOOQ/src/main/java/org/jooq/SQLDialect.java index d49fd7dd90..24525ba702 100644 --- a/jOOQ/src/main/java/org/jooq/SQLDialect.java +++ b/jOOQ/src/main/java/org/jooq/SQLDialect.java @@ -121,6 +121,16 @@ public enum SQLDialect { */ POSTGRES("Postgres", false), + /** + * The PostgreSQL dialect family. + */ + POSTGRES_9_3("Postgres", false, POSTGRES, null), + + /** + * The PostgreSQL dialect family. + */ + POSTGRES_9_4("Postgres", false, POSTGRES, POSTGRES_9_3), + /** * The SQLite dialect family. */ @@ -172,7 +182,7 @@ public enum SQLDialect { x xxx x xxxx xxxxxxx xx xxxxxxxxx xx xxxxxxxxxx xxxx xxxxxxxxxxxxxx xxxxx xx - xxxxxxxxxxxxx xxxxx xxxxx + xxxxxxxxxxxxx xxxxx xxxx xxxxxxx xxx x xxx xxx xxxx xxx xxxxxxxx @@ -214,14 +224,14 @@ public enum SQLDialect { x xxx x xxxx xxxxxxx xx xxxxxxxxx xx xxxxxxxxxx xxxx xxxxxxxxxxxxxx xxxxx xx - xxxxxxxxxxxxxxxxxxx xxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxx xxxxx xxxxxxx xxxxxxxxxxx xxx x xxx xxxxxx xxx xxxxxxxx x xxx x xxxx xxxxxxx xx xxxxxxxxx xx xxxxxxxxxx xxxx xxxxxxxxxxxxxx xxxxx xx - xxxxxxxxxxxxxxxxxxx xxxxx xxxxxxxx + xxxxxxxxxxxxxxxxxxx xxxxx xxxxxxx xxxxxxxxxxx xxx x xxx xxx xxxxxx xxxxxxx xxxxxxx @@ -242,7 +252,7 @@ public enum SQLDialect { x xxx x xxxx xxxxxxx xx xxxxxxxxx xx xxxxxxxxxx xxxx xxxxxxxxxxxxxx xxxxx xx - xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxx xxxxxxxxxxx + xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxx xxxxxxxxxx xxxxxxxxxxxxxxx xxx x xxx xxxxxx xxx xxxxxxxx xxxxxxx xxxxxxx @@ -267,18 +277,27 @@ public enum SQLDialect { FAMILIES = set.toArray(new SQLDialect[set.size()]); } - private final String name; - private final boolean commercial; - private final SQLDialect family; + private final String name; + private final boolean commercial; + private final SQLDialect family; + private SQLDialect predecessor; private SQLDialect(String name, boolean commercial) { - this(name, commercial, null); + this(name, commercial, null, null); } private SQLDialect(String name, boolean commercial, SQLDialect family) { + this(name, commercial, family, null); + } + + private SQLDialect(String name, boolean commercial, SQLDialect family, SQLDialect predecessor) { this.name = name; this.commercial = commercial; - this.family = family; + this.family = family == null ? this : family; + this.predecessor = predecessor == null ? this : predecessor; + + if (family != null) + family.predecessor = this; } /** @@ -299,7 +318,68 @@ public enum SQLDialect { * */ public final SQLDialect family() { - return family == null ? this : family; + return family; + } + + /** + * The predecessor dialect. + *
+ * If this is a dialect version (e.g. {@link #POSTGRES_9_4}) within a family + * (e.g. {@link #POSTGRES}), then the predecessor will point to the + * historically previous dialect version (e.g. {@link #POSTGRES_9_3}) within + * the same family, or to the dialect itself if there was no predecessor + * explicitly supported by jOOQ. + */ + public final SQLDialect predecessor() { + return predecessor; + } + + /** + * Whether this dialect precedes an other dialect from the same family. + *
+ * This returns: + *
true if this dialect is the same as the other dialecttrue if this dialect precedes the other dialect via any
+ * number of calls to {@link #predecessor()}false if the two dialects do not belong to the same
+ * family
+ * This is useful to see if some feature is supported by "at least"
+ * a given dialect version. Example:
+ */
+ public final boolean precedes(SQLDialect other) {
+ if (family != other.family)
+ return false;
+
+ SQLDialect candidate = other;
+ while (candidate != null) {
+ if (this == candidate)
+ return true;
+
+ if (candidate == candidate.predecessor())
+ return false;
+
+ candidate = candidate.predecessor();
+ }
+
+ return false;
}
/**
diff --git a/jOOQ/src/test/java/org/jooq/test/DialectTest.java b/jOOQ/src/test/java/org/jooq/test/DialectTest.java
new file mode 100644
index 0000000000..2565823976
--- /dev/null
+++ b/jOOQ/src/test/java/org/jooq/test/DialectTest.java
@@ -0,0 +1,101 @@
+/**
+ * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
+ * All rights reserved.
+ *
+ * This work is dual-licensed
+ * - under the Apache Software License 2.0 (the "ASL")
+ * - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
+ * =============================================================================
+ * You may choose which license applies to you:
+ *
+ * - If you're using this work with Open Source databases, you may choose
+ * either ASL or jOOQ License.
+ * - If you're using this work with at least one commercial database, you must
+ * choose jOOQ License
+ *
+ * For more information, please visit http://www.jooq.org/licenses
+ *
+ * Apache Software License 2.0:
+ * -----------------------------------------------------------------------------
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * jOOQ License and Maintenance Agreement:
+ * -----------------------------------------------------------------------------
+ * Data Geekery grants the Customer the non-exclusive, timely limited and
+ * non-transferable license to install and use the Software under the terms of
+ * the jOOQ License and Maintenance Agreement.
+ *
+ * This library is distributed with a LIMITED WARRANTY. See the jOOQ License
+ * and Maintenance Agreement for more details: http://www.jooq.org/licensing
+ */
+
+package org.jooq.test;
+
+import static org.jooq.SQLDialect.DEFAULT;
+import static org.jooq.SQLDialect.H2;
+// ...
+// ...
+// ...
+// ...
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.jooq.SQLDialect;
+
+import org.junit.Test;
+
+
+/**
+ * Tests related to {@link SQLDialect}
+ *
+ * @author Lukas Eder
+ */
+public class DialectTest extends AbstractTest {
+
+ @Test
+ public void testPredecessor() throws Exception {
+ assertEquals(DEFAULT, DEFAULT.predecessor());
+ assertEquals(H2, H2.predecessor());
+
+ assertEquals(ORACLE10G, ORACLE10G.predecessor());
+ assertEquals(ORACLE10G, ORACLE11G.predecessor());
+ assertEquals(ORACLE11G, ORACLE12C.predecessor());
+ assertEquals(ORACLE12C, ORACLE.predecessor());
+ }
+
+ @Test
+ public void testPrecedes() throws Exception {
+ assertTrue(DEFAULT.precedes(DEFAULT));
+ assertFalse(DEFAULT.precedes(H2));
+
+ assertTrue(ORACLE10G.precedes(ORACLE10G));
+ assertTrue(ORACLE10G.precedes(ORACLE11G));
+ assertTrue(ORACLE10G.precedes(ORACLE12C));
+ assertTrue(ORACLE10G.precedes(ORACLE));
+
+ assertFalse(ORACLE11G.precedes(ORACLE10G));
+ assertTrue(ORACLE11G.precedes(ORACLE11G));
+ assertTrue(ORACLE11G.precedes(ORACLE12C));
+ assertTrue(ORACLE11G.precedes(ORACLE));
+
+ assertFalse(ORACLE12C.precedes(ORACLE10G));
+ assertFalse(ORACLE12C.precedes(ORACLE11G));
+ assertTrue(ORACLE12C.precedes(ORACLE12C));
+ assertTrue(ORACLE12C.precedes(ORACLE));
+
+ assertFalse(ORACLE.precedes(ORACLE10G));
+ assertFalse(ORACLE.precedes(ORACLE11G));
+ assertFalse(ORACLE.precedes(ORACLE12C));
+ assertTrue(ORACLE.precedes(ORACLE));
+ }
+}
+ * // Do this block only if the chosen dialect supports PostgreSQL 9.4+ features
+ * if (POSTGRES_9_4.precedes(dialect)) {
+ * }
+ *
+ * // Do this block only if the chosen dialect supports PostgreSQL 9.3+ features
+ * else if (POSTGRES_9_3.precedes(dialect)) {
+ * }
+ *
+ * // Fall back to pre-PostgreSQL 9.3 behaviour
+ * else {
+ * }
+ *