Search in sources :

Example 86 with DatastoreClass

use of org.datanucleus.store.rdbms.table.DatastoreClass in project datanucleus-rdbms by datanucleus.

the class ExpressionUtils method populatePrimaryKeyMappingsValuesForPCMapping.

/**
 * Convenience method to populate PK mappings/values allowing for recursion where a PK field is itself
 * a PCMapping, that itself has PK mappings, which in turn may include PCMappings.
 * The pkMappings/pkFieldValues arrays are already created and we populate from "position".
 * @param pkMappings The array of pk mappings to be populated
 * @param pkFieldValues The array of pk field values to be populated
 * @param position The current position needing populating
 * @param pcMapping The PC mapping we are processing
 * @param cmd ClassMetaData for the owning class with this PCMapping field
 * @param mmd Field metadata for the field that this PCMapping represents
 * @param fieldValue The value for the PCMapping field in the owning object
 * @param storeMgr Store Manager
 * @param clr ClassLoader resolver
 * @return The current position (after our processing)
 */
public static int populatePrimaryKeyMappingsValuesForPCMapping(JavaTypeMapping[] pkMappings, Object[] pkFieldValues, int position, PersistableMapping pcMapping, AbstractClassMetaData cmd, AbstractMemberMetaData mmd, Object fieldValue, RDBMSStoreManager storeMgr, ClassLoaderResolver clr) {
    ExecutionContext ec = storeMgr.getApiAdapter().getExecutionContext(fieldValue);
    JavaTypeMapping[] subMappings = pcMapping.getJavaTypeMapping();
    if (subMappings.length == 0) {
        // Embedded PC has no PK so must be embedded-only so use mapping from owner table
        DatastoreClass table = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
        JavaTypeMapping ownerMapping = table.getMemberMapping(mmd);
        EmbeddedMapping embMapping = (EmbeddedMapping) ownerMapping;
        for (int k = 0; k < embMapping.getNumberOfJavaTypeMappings(); k++) {
            JavaTypeMapping subMapping = embMapping.getJavaTypeMapping(k);
            pkMappings[position] = subMapping;
            pkFieldValues[position] = getValueForMemberOfObject(ec, subMapping.getMemberMetaData(), fieldValue);
            position++;
        }
    } else {
        AbstractClassMetaData pcCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(pcMapping.getType(), clr);
        int[] pcPkPositions = pcCmd.getPKMemberPositions();
        for (int k = 0; k < subMappings.length; k++) {
            AbstractMemberMetaData pcMmd = pcCmd.getMetaDataForManagedMemberAtAbsolutePosition(pcPkPositions[k]);
            if (subMappings[k] instanceof PersistableMapping) {
                Object val = getValueForMemberOfObject(ec, pcMmd, fieldValue);
                position = populatePrimaryKeyMappingsValuesForPCMapping(pkMappings, pkFieldValues, position, (PersistableMapping) subMappings[k], pcCmd, pcMmd, val, storeMgr, clr);
            } else {
                Object val = getValueForMemberOfObject(ec, pcMmd, fieldValue);
                pkMappings[position] = subMappings[k];
                pkFieldValues[position] = val;
                position++;
            }
        }
    }
    return position;
}
Also used : PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) ExecutionContext(org.datanucleus.ExecutionContext) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) EmbeddedMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedMapping) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 87 with DatastoreClass

use of org.datanucleus.store.rdbms.table.DatastoreClass 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

DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)87 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)60 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)49 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)48 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)44 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)41 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)35 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)32 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)28 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)26 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)21 MapTable (org.datanucleus.store.rdbms.table.MapTable)19 NucleusException (org.datanucleus.exceptions.NucleusException)18 SecondaryDatastoreClass (org.datanucleus.store.rdbms.table.SecondaryDatastoreClass)15 ArrayList (java.util.ArrayList)14 ExecutionContext (org.datanucleus.ExecutionContext)13 JoinTable (org.datanucleus.store.rdbms.table.JoinTable)13 Table (org.datanucleus.store.rdbms.table.Table)13 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)11 UnboundExpression (org.datanucleus.store.rdbms.sql.expression.UnboundExpression)11