[#2593] Add Meta.getPrimaryKeys()

This commit is contained in:
Lukas Eder 2013-07-02 21:52:00 +02:00
parent 8ef4103416
commit 088160e24d
3 changed files with 78 additions and 8 deletions

View File

@ -460,9 +460,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertTrue(metaFields.containsAll(asList(generatedTable.fields())));
// Check if relations are correctly loaded (and typed) as well
// [#1977] Fix this, once the "main key" concept has been removed
if (generatedTable.getPrimaryKey() != null && metaTable.getPrimaryKey() != null) {
// [#1977] TODO: Add key checks
if (generatedTable.getPrimaryKey() != null) {
assertNotNull(metaTable.getPrimaryKey());
assertEquals(generatedTable, metaTable.getPrimaryKey().getTable());
assertEquals(generatedTable.getPrimaryKey().getFields(), metaTable.getPrimaryKey().getFields());
}
// Only truly updatable tables should be "Updatable"

View File

@ -62,7 +62,7 @@ import org.jooq.exception.DataAccessException;
public interface Meta {
/**
* Get all catalog objects from the underlying {@link DatabaseMetaData}
* Get all catalog objects from the underlying {@link DatabaseMetaData}.
* <p>
* For those databases that don't really support JDBC meta data catalogs, a
* single empty catalog (named <code>""</code>) will be returned. In other
@ -77,7 +77,7 @@ public interface Meta {
List<Catalog> 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<Schema> 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<Table<?>> getTables() throws DataAccessException;
/**
* Get all primary keys from the underlying {@link DatabaseMetaData}.
*
* @throws DataAccessException If something went wrong fetching the meta
* objects
*/
@Support
List<UniqueKey<?>> getPrimaryKeys() throws DataAccessException;
}

View File

@ -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<UniqueKey<?>> getPrimaryKeys() {
List<UniqueKey<?>> result = new ArrayList<UniqueKey<?>>();
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<Name, Result<Record>>();
for (Entry<Record, Result<Record>> entry : groups.entrySet()) {
@ -344,6 +361,49 @@ class MetaImpl implements Meta, Serializable {
}
}
@SuppressWarnings("unchecked")
@Override
public final List<UniqueKey<Record>> getKeys() {
return unmodifiableList(asList(getPrimaryKey()));
}
@SuppressWarnings("unchecked")
@Override
public final UniqueKey<Record> getPrimaryKey() {
String schema = getSchema() == null ? null : getSchema().getName();
try {
Result<Record> 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<Record, ?>[] fields = new TableField[result.size()];
for (int i = 0; i < fields.length; i++) {
fields[i] = (TableField<Record, ?>) 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<Record> columns) {
for (Record column : columns) {
String columnName = column.getValue("COLUMN_NAME", String.class);