[jOOQ/jOOQ#7639] Forward compatible H2 check constraint generation code

See also [h2database/h2database#2286]
This commit is contained in:
Lukas Eder 2019-12-02 11:00:42 +01:00
parent fb07821ced
commit 8e1cfbcb4b

View File

@ -37,6 +37,7 @@
*/
package org.jooq.meta.h2;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
@ -66,6 +67,7 @@ import org.jooq.Record;
import org.jooq.Record4;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.Select;
import org.jooq.SortOrder;
import org.jooq.Table;
import org.jooq.TableField;
@ -316,7 +318,20 @@ public class H2Database extends AbstractDatabase {
@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
// TODO: Should we really UNION INFORMATION_SCHEMA.COLUMNS.CHECK_CONSTRAINT?
// TODO [https://github.com/h2database/h2database/issues/2286]
// Starting from H2 1.4.201, we should no longer produce the below UNION
Select<Record4<String, String, String, String>> inlineChecks = is1_4_201()
? select(inline(""), inline(""), inline(""), inline("")).where(falseCondition())
: select(
Columns.TABLE_SCHEMA,
Columns.TABLE_NAME,
Columns.CHECK_CONSTRAINT,
Columns.CHECK_CONSTRAINT
)
.from(COLUMNS)
.where(Columns.CHECK_CONSTRAINT.nvl("").ne(""))
.and(Columns.TABLE_SCHEMA.in(getInputSchemata()));
for (Record record : create()
.select(
Constraints.TABLE_SCHEMA,
@ -327,17 +342,7 @@ public class H2Database extends AbstractDatabase {
.from(CONSTRAINTS)
.where(Constraints.CONSTRAINT_TYPE.eq("CHECK"))
.and(Constraints.TABLE_SCHEMA.in(getInputSchemata()))
.union(
select(
Columns.TABLE_SCHEMA,
Columns.TABLE_NAME,
Columns.CHECK_CONSTRAINT,
Columns.CHECK_CONSTRAINT
)
.from(COLUMNS)
.where(Columns.CHECK_CONSTRAINT.nvl("").ne(""))
.and(Columns.TABLE_SCHEMA.in(getInputSchemata())))
.fetch()) {
.union(inlineChecks)) {
SchemaDefinition schema = getSchema(record.get(Constraints.TABLE_SCHEMA));
@ -646,6 +651,7 @@ public class H2Database extends AbstractDatabase {
private static Boolean is1_4_197;
private static Boolean is1_4_198;
private static Boolean is1_4_201;
boolean is1_4_197() {
@ -664,4 +670,14 @@ public class H2Database extends AbstractDatabase {
return is1_4_198;
}
boolean is1_4_201() {
// [https://github.com/h2database/h2database/issues/2286]
// The COLUMNS.CHECK_CONSTRAINT column was removed backwards incompatibly in H2 1.4.201
if (is1_4_201 == null)
is1_4_201 = !exists(Columns.CHECK_CONSTRAINT);
return is1_4_201;
}
}