Search in sources :

Example 1 with RDBMSTablePKInfo

use of org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo in project tests by datanucleus.

the class SchemaHandlerTest method testPrimaryKeyRetrieval.

/**
 * Test of the retrieval of PKs.
 */
public void testPrimaryKeyRetrieval() {
    addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    // Retrieve the table for SchemaClass1
    ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
    DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
    DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
    // Check for the FK using the schema handler
    StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
    Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
    RDBMSTablePKInfo pkInfo1 = (RDBMSTablePKInfo) handler.getSchemaData(con, "primary-keys", new Object[] { table1 });
    RDBMSTablePKInfo pkInfo2 = (RDBMSTablePKInfo) handler.getSchemaData(con, "primary-keys", new Object[] { table2 });
    // Expecting 2 PK columns for SchemaClass1
    // TODO Enable checks on the PK name (when JDBC drivers return it correctly)
    assertEquals("Number of PKs for table " + table1 + " is wrong", 2, pkInfo1.getNumberOfChildren());
    PrimaryKeyInfo pk = (PrimaryKeyInfo) pkInfo1.getChild(0);
    assertEquals("Column Name is wrong", "TABLE1_ID1", ((String) pk.getProperty("column_name")).toUpperCase());
    // assertEquals("PK Name is wrong", "TABLE1_PK", pk.getProperty("pk_name"));
    pk = (PrimaryKeyInfo) pkInfo1.getChild(1);
    assertEquals("Column Name is wrong", "TABLE1_ID2", ((String) pk.getProperty("column_name")).toUpperCase());
    // assertEquals("PK Name is wrong", "TABLE1_PK", pk.getProperty("pk_name"));
    // Expecting 1 PK column for SchemaClass
    assertEquals("Number of PKs for table " + table1 + " is wrong", 1, pkInfo2.getNumberOfChildren());
    pk = (PrimaryKeyInfo) pkInfo2.getChild(0);
    assertEquals("Column Name is wrong", "TABLE2_ID", ((String) pk.getProperty("column_name")).toUpperCase());
// assertEquals("PK Name is wrong", "TABLE2_PK", pk.getProperty("pk_name"));
}
Also used : PrimaryKeyInfo(org.datanucleus.store.rdbms.schema.PrimaryKeyInfo) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Connection(java.sql.Connection) SchemaClass1(org.jpox.samples.rdbms.schema.SchemaClass1) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) SchemaClass2(org.jpox.samples.rdbms.schema.SchemaClass2) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTablePKInfo(org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Example 2 with RDBMSTablePKInfo

use of org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo 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)

Aggregations

PrimaryKeyInfo (org.datanucleus.store.rdbms.schema.PrimaryKeyInfo)2 RDBMSTablePKInfo (org.datanucleus.store.rdbms.schema.RDBMSTablePKInfo)2 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)2 Connection (java.sql.Connection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 PersistenceManager (javax.jdo.PersistenceManager)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 UnexpectedColumnException (org.datanucleus.store.rdbms.exceptions.UnexpectedColumnException)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)1 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)1 SchemaClass1 (org.jpox.samples.rdbms.schema.SchemaClass1)1 SchemaClass2 (org.jpox.samples.rdbms.schema.SchemaClass2)1