use of org.datanucleus.store.rdbms.key.PrimaryKey in project datanucleus-rdbms by datanucleus.
the class TableImpl method getExistingPrimaryKeys.
/**
* Accessor for the primary keys for this table in the datastore.
* @param conn The JDBC Connection
* @return Map of primary keys
* @throws SQLException Thrown when an error occurs in the JDBC call.
*/
private Map<DatastoreIdentifier, PrimaryKey> getExistingPrimaryKeys(Connection conn) throws SQLException {
Map<DatastoreIdentifier, PrimaryKey> primaryKeysByName = new HashMap<>();
if (tableExistsInDatastore(conn)) {
StoreSchemaHandler handler = storeMgr.getSchemaHandler();
RDBMSTablePKInfo tablePkInfo = (RDBMSTablePKInfo) handler.getSchemaData(conn, "primary-keys", new Object[] { this });
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
Iterator pkColsIter = tablePkInfo.getChildren().iterator();
while (pkColsIter.hasNext()) {
PrimaryKeyInfo pkInfo = (PrimaryKeyInfo) pkColsIter.next();
String pkName = (String) pkInfo.getProperty("pk_name");
DatastoreIdentifier pkIdentifier;
if (pkName == null) {
pkIdentifier = idFactory.newPrimaryKeyIdentifier(this);
} else {
pkIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, pkName);
}
PrimaryKey pk = primaryKeysByName.get(pkIdentifier);
if (pk == null) {
pk = new PrimaryKey(this);
pk.setName(pkIdentifier.getName());
primaryKeysByName.put(pkIdentifier, pk);
}
int keySeq = (((Short) pkInfo.getProperty("key_seq")).shortValue()) - 1;
String colName = (String) pkInfo.getProperty("column_name");
DatastoreIdentifier colIdentifier = idFactory.newIdentifier(IdentifierType.COLUMN, colName);
Column col = columnsByIdentifier.get(colIdentifier);
if (col == null) {
throw new UnexpectedColumnException(this.toString(), colIdentifier.getName(), this.getSchemaName(), this.getCatalogName());
}
pk.setColumn(keySeq, col);
}
}
return primaryKeysByName;
}
use of org.datanucleus.store.rdbms.key.PrimaryKey in project datanucleus-rdbms by datanucleus.
the class TableImpl method getExpectedIndices.
/**
* Accessor for the indices for this table in the datastore.
* @param clr The ClassLoaderResolver
* @return Set of indices expected.
*/
protected Set<Index> getExpectedIndices(ClassLoaderResolver clr) {
assertIsInitialized();
/*
* For each foreign key, add to the list an index made up of the "from"
* column(s) of the key, *unless* those columns also happen to be
* equal to the primary key (then they are indexed anyway).
* Ensure that we have separate indices for foreign key columns
* if the primary key is the combination of foreign keys, e.g. in join tables.
* This greatly decreases deadlock probability e.g. on Oracle.
*/
Set<Index> indices = new HashSet<>();
PrimaryKey pk = getPrimaryKey();
Iterator<ForeignKey> i = getExpectedForeignKeys(clr).iterator();
while (i.hasNext()) {
ForeignKey fk = i.next();
if (!pk.getColumnList().equals(fk.getColumnList())) {
indices.add(new Index(fk));
}
}
return indices;
}
Aggregations