diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/MetaDataTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/MetaDataTests.java index 6144f40cf1..68c5f675dc 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/MetaDataTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/MetaDataTests.java @@ -460,9 +460,10 @@ extends BaseTest * For those databases that don't really support JDBC meta data catalogs, a * single empty catalog (named "") will be returned. In other @@ -77,7 +77,7 @@ public interface Meta { List getCatalogs() throws DataAccessException; /** - * Get all schema objects from the underlying {@link DatabaseMetaData} + * Get all schema objects from the underlying {@link DatabaseMetaData}. * * @throws DataAccessException If something went wrong fetching the meta * objects @@ -86,11 +86,20 @@ public interface Meta { List getSchemas() throws DataAccessException; /** - * Get all table objects from the underlying {@link DatabaseMetaData} + * Get all table objects from the underlying {@link DatabaseMetaData}. * * @throws DataAccessException If something went wrong fetching the meta * objects */ @Support List> getTables() throws DataAccessException; + + /** + * Get all primary keys from the underlying {@link DatabaseMetaData}. + * + * @throws DataAccessException If something went wrong fetching the meta + * objects + */ + @Support + List> getPrimaryKeys() throws DataAccessException; } diff --git a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java index 9874e12e1f..fb7328ba6d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/MetaImpl.java @@ -35,6 +35,8 @@ */ package org.jooq.impl; +import static java.util.Arrays.asList; +import static java.util.Collections.unmodifiableList; import static org.jooq.SQLDialect.SQLITE; import static org.jooq.impl.DSL.fieldByName; import static org.jooq.impl.DSL.name; @@ -61,6 +63,8 @@ import org.jooq.Record; import org.jooq.Result; import org.jooq.Schema; import org.jooq.Table; +import org.jooq.TableField; +import org.jooq.UniqueKey; import org.jooq.exception.DataAccessException; import org.jooq.exception.SQLDialectNotSupportedException; @@ -165,6 +169,21 @@ class MetaImpl implements Meta, Serializable { return result; } + @Override + public final List> getPrimaryKeys() { + List> result = new ArrayList>(); + + for (Table table : getTables()) { + UniqueKey pk = table.getPrimaryKey(); + + if (pk != null) { + result.add(pk); + } + } + + return result; + } + private class MetaCatalog extends CatalogImpl { /** @@ -287,8 +306,6 @@ class MetaImpl implements Meta, Serializable { tableName }); - System.out.println(getColumns0(schema, "%").format(1000)); - columnCache = new LinkedHashMap>(); for (Entry> entry : groups.entrySet()) { @@ -344,6 +361,49 @@ class MetaImpl implements Meta, Serializable { } } + @SuppressWarnings("unchecked") + @Override + public final List> getKeys() { + return unmodifiableList(asList(getPrimaryKey())); + } + + @SuppressWarnings("unchecked") + @Override + public final UniqueKey getPrimaryKey() { + String schema = getSchema() == null ? null : getSchema().getName(); + + try { + Result result = + create.fetch( + meta().getPrimaryKeys(null, schema, getName()), + String.class, // TABLE_CAT + String.class, // TABLE_SCHEM + String.class, // TABLE_NAME + String.class, // COLUMN_NAME + int.class, // KEY_SEQ + String.class // PK_NAME + ); + + // Sort by KEY_SEQ + result.sortAsc(4); + + if (result.size() > 0) { + TableField[] fields = new TableField[result.size()]; + for (int i = 0; i < fields.length; i++) { + fields[i] = (TableField) field(result.get(i).getValue(3, String.class)); + } + + return AbstractKeys.createUniqueKey(this, fields); + } + else { + return null; + } + } + catch (SQLException e) { + throw new DataAccessException("Error while accessing DatabaseMetaData", e); + } + } + private final void init(Result columns) { for (Record column : columns) { String columnName = column.getValue("COLUMN_NAME", String.class);