From 7522f7c5a80aaa970a85ab3a48787f82dcd7cb39 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Fri, 14 Feb 2014 10:47:20 +0100 Subject: [PATCH] [#3042] Work around flaws in H2's INFORMATION_SCHEMA.CONSTRAINTS.COLUMN_LIST --- .../java/org/jooq/util/h2/H2Database.java | 28 +++++++++---------- .../lukas/h2/information_schema.xml | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/jOOQ-meta/src/main/java/org/jooq/util/h2/H2Database.java b/jOOQ-meta/src/main/java/org/jooq/util/h2/H2Database.java index 229e4f700b..a40612df0e 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/h2/H2Database.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/h2/H2Database.java @@ -45,6 +45,7 @@ import static org.jooq.util.h2.information_schema.tables.Columns.COLUMNS; import static org.jooq.util.h2.information_schema.tables.Constraints.CONSTRAINTS; import static org.jooq.util.h2.information_schema.tables.CrossReferences.CROSS_REFERENCES; import static org.jooq.util.h2.information_schema.tables.FunctionAliases.FUNCTION_ALIASES; +import static org.jooq.util.h2.information_schema.tables.Indexes.INDEXES; import static org.jooq.util.h2.information_schema.tables.Schemata.SCHEMATA; import static org.jooq.util.h2.information_schema.tables.Sequences.SEQUENCES; import static org.jooq.util.h2.information_schema.tables.Tables.TABLES; @@ -78,6 +79,7 @@ import org.jooq.util.h2.information_schema.tables.Columns; import org.jooq.util.h2.information_schema.tables.Constraints; import org.jooq.util.h2.information_schema.tables.CrossReferences; import org.jooq.util.h2.information_schema.tables.FunctionAliases; +import org.jooq.util.h2.information_schema.tables.Indexes; import org.jooq.util.h2.information_schema.tables.Schemata; import org.jooq.util.h2.information_schema.tables.Sequences; import org.jooq.util.h2.information_schema.tables.Tables; @@ -100,16 +102,12 @@ public class H2Database extends AbstractDatabase { for (Record record : fetchKeys("PRIMARY KEY")) { SchemaDefinition schema = getSchema(record.getValue(Constraints.TABLE_SCHEMA)); String tableName = record.getValue(Constraints.TABLE_NAME); - String columnList = record.getValue(Constraints.COLUMN_LIST); String primaryKey = record.getValue(Constraints.CONSTRAINT_NAME); + String columnName = record.getValue(Indexes.COLUMN_NAME); TableDefinition table = getTable(schema, tableName); if (table != null) { - String[] columnNames = columnList.split("[,]+"); - - for (String columnName : columnNames) { - relations.addPrimaryKey(primaryKey, table.getColumn(columnName)); - } + relations.addPrimaryKey(primaryKey, table.getColumn(columnName)); } } } @@ -119,16 +117,12 @@ public class H2Database extends AbstractDatabase { for (Record record : fetchKeys("UNIQUE")) { SchemaDefinition schema = getSchema(record.getValue(Constraints.TABLE_SCHEMA)); String tableName = record.getValue(Constraints.TABLE_NAME); - String columnList = record.getValue(Constraints.COLUMN_LIST); String primaryKey = record.getValue(Constraints.CONSTRAINT_NAME); + String columnName = record.getValue(Indexes.COLUMN_NAME); TableDefinition table = getTable(schema, tableName); if (table != null) { - String[] columnNames = columnList.split("[,]+"); - - for (String columnName : columnNames) { - relations.addUniqueKey(primaryKey, table.getColumn(columnName)); - } + relations.addUniqueKey(primaryKey, table.getColumn(columnName)); } } } @@ -137,15 +131,19 @@ public class H2Database extends AbstractDatabase { return create().select( Constraints.TABLE_SCHEMA, Constraints.TABLE_NAME, - Constraints.COLUMN_LIST, - Constraints.CONSTRAINT_NAME) + Constraints.CONSTRAINT_NAME, + Indexes.COLUMN_NAME) .from(CONSTRAINTS) + .join(INDEXES) + .on(Constraints.TABLE_SCHEMA.eq(Indexes.TABLE_SCHEMA)) + .and(Constraints.TABLE_NAME.eq(Indexes.TABLE_NAME)) + .and(Constraints.UNIQUE_INDEX_NAME.eq(Indexes.INDEX_NAME)) .where(Constraints.TABLE_SCHEMA.in(getInputSchemata())) .and(Constraints.CONSTRAINT_TYPE.equal(constraintType)) .orderBy( Constraints.TABLE_SCHEMA, Constraints.CONSTRAINT_NAME, - Constraints.COLUMN_LIST) + Indexes.ORDINAL_POSITION) .fetch(); } diff --git a/jOOQ-test/configuration/org/jooq/configuration/lukas/h2/information_schema.xml b/jOOQ-test/configuration/org/jooq/configuration/lukas/h2/information_schema.xml index e9cda5e2b6..9b2e7cfc87 100644 --- a/jOOQ-test/configuration/org/jooq/configuration/lukas/h2/information_schema.xml +++ b/jOOQ-test/configuration/org/jooq/configuration/lukas/h2/information_schema.xml @@ -11,7 +11,7 @@ org.jooq.util.DefaultGenerator org.jooq.util.h2.H2Database - SCHEMATA|TABLES|COLUMNS|CONSTRAINTS|CROSS_REFERENCES|TYPE_INFO|FUNCTION_ALIASES|FUNCTION_COLUMNS|SEQUENCES + COLUMNS|CONSTRAINTS|CROSS_REFERENCES|FUNCTION_ALIASES|FUNCTION_COLUMNS|INDEXES|SCHEMATA|SEQUENCES|TABLES|TYPE_INFO