Search in sources :

Example 11 with PrimaryKey

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;
}
Also used : HashMap(java.util.HashMap) PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) PrimaryKeyInfo(org.datanucleus.store.rdbms.schema.PrimaryKeyInfo) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator) RDBMSTablePKInfo(org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo) UnexpectedColumnException(org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)

Example 12 with PrimaryKey

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;
}
Also used : PrimaryKey(org.datanucleus.store.rdbms.key.PrimaryKey) Index(org.datanucleus.store.rdbms.key.Index) ForeignKey(org.datanucleus.store.rdbms.key.ForeignKey) HashSet(java.util.HashSet)

Aggregations

PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)12 Iterator (java.util.Iterator)4 ForeignKey (org.datanucleus.store.rdbms.key.ForeignKey)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Set (java.util.Set)2 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)2 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)2 Index (org.datanucleus.store.rdbms.key.Index)2 EmbeddedPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping)2 Column (org.datanucleus.store.rdbms.table.Column)2 List (java.util.List)1