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"));
}
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;
}
Aggregations