use of org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo in project datanucleus-rdbms by datanucleus.
the class TableImpl method getExistingCandidateKeys.
/**
* Accessor for the candidate keys for this table.
* @param conn The JDBC Connection
* @return Map of candidate keys
* @throws SQLException Thrown when an error occurs in the JDBC call.
*/
private Map<DatastoreIdentifier, CandidateKey> getExistingCandidateKeys(Connection conn) throws SQLException {
Map<DatastoreIdentifier, CandidateKey> candidateKeysByName = new HashMap<>();
if (tableExistsInDatastore(conn)) {
StoreSchemaHandler handler = storeMgr.getSchemaHandler();
RDBMSTableIndexInfo tableIndexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(conn, "indices", new Object[] { this });
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
Iterator indexIter = tableIndexInfo.getChildren().iterator();
while (indexIter.hasNext()) {
IndexInfo indexInfo = (IndexInfo) indexIter.next();
boolean isUnique = !((Boolean) indexInfo.getProperty("non_unique")).booleanValue();
if (isUnique) {
// Only utilise unique indexes
short idxType = ((Short) indexInfo.getProperty("type")).shortValue();
if (idxType == DatabaseMetaData.tableIndexStatistic) {
// Ignore
continue;
}
String keyName = (String) indexInfo.getProperty("index_name");
DatastoreIdentifier idxName = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, keyName);
CandidateKey key = candidateKeysByName.get(idxName);
if (key == null) {
key = new CandidateKey(this, null);
key.setName(keyName);
candidateKeysByName.put(idxName, key);
}
// Set the column
int colSeq = ((Short) indexInfo.getProperty("ordinal_position")).shortValue() - 1;
DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierType.COLUMN, (String) indexInfo.getProperty("column_name"));
Column col = columnsByIdentifier.get(colName);
if (col != null) {
key.setColumn(colSeq, col);
}
}
}
}
return candidateKeysByName;
}
use of org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo in project datanucleus-rdbms by datanucleus.
the class TableImpl method getExistingIndices.
/**
* Accessor for indices on the actual table.
* @param conn The JDBC Connection
* @return Map of indices (keyed by the index name)
* @throws SQLException Thrown when an error occurs in the JDBC call.
*/
private Map<DatastoreIdentifier, Index> getExistingIndices(Connection conn) throws SQLException {
Map<DatastoreIdentifier, Index> indicesByName = new HashMap<>();
if (tableExistsInDatastore(conn)) {
StoreSchemaHandler handler = storeMgr.getSchemaHandler();
RDBMSTableIndexInfo tableIndexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(conn, "indices", new Object[] { this });
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
Iterator indexIter = tableIndexInfo.getChildren().iterator();
while (indexIter.hasNext()) {
IndexInfo indexInfo = (IndexInfo) indexIter.next();
short idxType = ((Short) indexInfo.getProperty("type")).shortValue();
if (idxType == DatabaseMetaData.tableIndexStatistic) {
// Ignore
continue;
}
String indexName = (String) indexInfo.getProperty("index_name");
DatastoreIdentifier indexIdentifier = idFactory.newIdentifier(IdentifierType.CANDIDATE_KEY, indexName);
Index idx = indicesByName.get(indexIdentifier);
if (idx == null) {
boolean isUnique = !((Boolean) indexInfo.getProperty("non_unique")).booleanValue();
idx = new Index(this, isUnique, null);
idx.setName(indexName);
indicesByName.put(indexIdentifier, idx);
}
// Set the column
int colSeq = ((Short) indexInfo.getProperty("ordinal_position")).shortValue() - 1;
DatastoreIdentifier colName = idFactory.newIdentifier(IdentifierType.COLUMN, (String) indexInfo.getProperty("column_name"));
Column col = columnsByIdentifier.get(colName);
if (col != null) {
idx.setColumn(colSeq, col);
}
}
}
return indicesByName;
}
use of org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo in project tests by datanucleus.
the class SchemaHandlerTest method testIndexRetrieval.
/**
* Test of the retrieval of indices.
*/
public void testIndexRetrieval() {
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 indices using the schema handler
StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
RDBMSTableIndexInfo indexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(con, "indices", new Object[] { table1 });
int numIndices = 3;
if (vendorID.equals("hsql")) {
// HSQL will create an index for the FK without asking, and we can't replace it with our own so end up with two
numIndices = 4;
}
assertEquals("Number of Indices for table " + table1 + " is wrong", numIndices, indexInfo.getNumberOfChildren());
Iterator indexIter = indexInfo.getChildren().iterator();
while (indexIter.hasNext()) {
IndexInfo index = (IndexInfo) indexIter.next();
String columnName = ((String) index.getProperty("column_name")).toUpperCase();
boolean unique = !((Boolean) index.getProperty("non_unique")).booleanValue();
if (columnName.equals("OTHER_ID")) {
assertFalse("Index for column " + columnName + " is unique!", unique);
} else if (columnName.equals("TABLE1_ID1")) {
assertTrue("Index for column " + columnName + " is not unique!", unique);
} else if (columnName.equals("TABLE1_ID2")) {
assertTrue("Index for column " + columnName + " is not unique!", unique);
} else {
fail("Unexpected index " + columnName + " for table " + table1);
}
}
indexInfo = (RDBMSTableIndexInfo) handler.getSchemaData(con, "indices", new Object[] { table2 });
assertEquals("Number of Indices for table " + table2 + " is wrong", 2, indexInfo.getNumberOfChildren());
indexIter = indexInfo.getChildren().iterator();
while (indexIter.hasNext()) {
IndexInfo index = (IndexInfo) indexIter.next();
String columnName = ((String) index.getProperty("column_name")).toUpperCase();
String indexName = ((String) index.getProperty("index_name")).toUpperCase();
boolean unique = !((Boolean) index.getProperty("non_unique")).booleanValue();
if (columnName.equals("VALUE")) {
assertFalse("Index for column " + columnName + " is unique!", unique);
assertEquals("Index name for column " + columnName + " is wrong!", "VALUE_IDX", indexName);
} else if (columnName.equals("TABLE2_ID")) {
assertTrue("Index for column " + columnName + " is not unique!", unique);
} else {
fail("Unexpected index " + columnName + " for table " + table1);
}
}
}
Aggregations