Search in sources :

Example 1 with RDBMSTableIndexInfo

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;
}
Also used : HashMap(java.util.HashMap) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) IndexInfo(org.datanucleus.store.rdbms.schema.IndexInfo) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) CandidateKey(org.datanucleus.store.rdbms.key.CandidateKey) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator)

Example 2 with RDBMSTableIndexInfo

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;
}
Also used : HashMap(java.util.HashMap) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) Index(org.datanucleus.store.rdbms.key.Index) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) IndexInfo(org.datanucleus.store.rdbms.schema.IndexInfo) IdentifierFactory(org.datanucleus.store.rdbms.identifier.IdentifierFactory) DatastoreIdentifier(org.datanucleus.store.rdbms.identifier.DatastoreIdentifier) Iterator(java.util.Iterator)

Example 3 with RDBMSTableIndexInfo

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);
        }
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Connection(java.sql.Connection) SchemaClass1(org.jpox.samples.rdbms.schema.SchemaClass1) SchemaClass2(org.jpox.samples.rdbms.schema.SchemaClass2) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSTableIndexInfo(org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo) IndexInfo(org.datanucleus.store.rdbms.schema.IndexInfo) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) Iterator(java.util.Iterator) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass)

Aggregations

Iterator (java.util.Iterator)3 IndexInfo (org.datanucleus.store.rdbms.schema.IndexInfo)3 RDBMSTableIndexInfo (org.datanucleus.store.rdbms.schema.RDBMSTableIndexInfo)3 StoreSchemaHandler (org.datanucleus.store.schema.StoreSchemaHandler)3 HashMap (java.util.HashMap)2 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)2 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)2 Connection (java.sql.Connection)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 CandidateKey (org.datanucleus.store.rdbms.key.CandidateKey)1 Index (org.datanucleus.store.rdbms.key.Index)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