Search in sources :

Example 1 with SecondaryDatastoreClass

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

the class RDBMSPersistenceHandler method insertObjectInTable.

/**
 * Convenience method to handle the insert into the various tables that this object is persisted into.
 * @param table The table to process
 * @param op ObjectProvider for the object being inserted
 * @param clr ClassLoader resolver
 */
private void insertObjectInTable(DatastoreClass table, ObjectProvider op, ClassLoaderResolver clr) {
    if (table instanceof ClassView) {
        throw new NucleusUserException("Cannot perform InsertRequest on RDBMS view " + table);
    }
    DatastoreClass supertable = table.getSuperDatastoreClass();
    if (supertable != null) {
        // Process the superclass table first
        insertObjectInTable(supertable, op, clr);
    }
    // Do the actual insert of this table
    getInsertRequest(table, op.getClassMetaData(), clr).execute(op);
    // Process any secondary tables
    Collection<SecondaryDatastoreClass> secondaryTables = table.getSecondaryDatastoreClasses();
    if (secondaryTables != null) {
        for (SecondaryDatastoreClass secTable : secondaryTables) {
            // Process the secondary table
            insertObjectInTable(secTable, op, clr);
        }
    }
}
Also used : ClassView(org.datanucleus.store.rdbms.table.ClassView) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass)

Example 2 with SecondaryDatastoreClass

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

the class RDBMSPersistenceHandler method deleteObjectFromTable.

/**
 * Convenience method to handle the delete from the various tables that this object is persisted into.
 * @param table The table to process
 * @param sm ObjectProvider for the object being deleted
 * @param clr ClassLoader resolver
 */
private void deleteObjectFromTable(DatastoreClass table, ObjectProvider sm, ClassLoaderResolver clr) {
    if (table instanceof ClassView) {
        throw new NucleusUserException("Cannot perform DeleteRequest on RDBMS view " + table);
    }
    // Delete any secondary tables
    Collection<SecondaryDatastoreClass> secondaryTables = table.getSecondaryDatastoreClasses();
    if (secondaryTables != null) {
        for (SecondaryDatastoreClass secTable : secondaryTables) {
            // Process the secondary table
            deleteObjectFromTable(secTable, sm, clr);
        }
    }
    // Do the actual delete of this table
    getDeleteRequest(table, sm.getClassMetaData(), clr).execute(sm);
    DatastoreClass supertable = table.getSuperDatastoreClass();
    if (supertable != null) {
        // Process the superclass table last
        deleteObjectFromTable(supertable, sm, clr);
    }
}
Also used : ClassView(org.datanucleus.store.rdbms.table.ClassView) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass)

Example 3 with SecondaryDatastoreClass

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

the class RDBMSPersistenceHandler method updateObjectInTable.

/**
 * Convenience method to handle the update into the various tables that this object is persisted into.
 * @param table The table to process
 * @param op ObjectProvider for the object being updated
 * @param clr ClassLoader resolver
 * @param mmds MetaData for the fields being updated
 */
private void updateObjectInTable(DatastoreClass table, ObjectProvider op, ClassLoaderResolver clr, AbstractMemberMetaData[] mmds) {
    if (table instanceof ClassView) {
        throw new NucleusUserException("Cannot perform UpdateRequest on RDBMS view " + table);
    }
    DatastoreClass supertable = table.getSuperDatastoreClass();
    if (supertable != null) {
        // Process the superclass table first
        updateObjectInTable(supertable, op, clr, mmds);
    }
    // Do the actual update of this table
    getUpdateRequest(table, mmds, op.getClassMetaData(), clr).execute(op);
    // Update any secondary tables
    Collection<SecondaryDatastoreClass> secondaryTables = table.getSecondaryDatastoreClasses();
    if (secondaryTables != null) {
        for (SecondaryDatastoreClass secTable : secondaryTables) {
            // Process the secondary table
            updateObjectInTable(secTable, op, clr, mmds);
        }
    }
}
Also used : ClassView(org.datanucleus.store.rdbms.table.ClassView) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass)

Example 4 with SecondaryDatastoreClass

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

the class SQLStatementHelper method getSQLTableForMappingOfTable.

/**
 * Method to return the SQLTable where the specified mapping (in the same table group as the provided
 * SQLTable) is defined. If the statement doesn't currently join to the required table then a join will
 * be added. If the required table is a superclass table then the join will be INNER. If the required
 * table is a secondary table then the join will be defined by the meta-data for the secondary table.
 * If this table group is NOT the candidate table group then LEFT OUTER JOIN will be used.
 * @param stmt The statement
 * @param sqlTbl SQLTable to start from for the supplied mapping (may be in super-table, or secondary-table of this)
 * @param mapping The mapping
 * @return The SQLTable for this mapping (may have been added to the statement during this method)
 */
public static SQLTable getSQLTableForMappingOfTable(SQLStatement stmt, SQLTable sqlTbl, JavaTypeMapping mapping) {
    Table table = sqlTbl.getTable();
    if (table instanceof SecondaryDatastoreClass || table instanceof JoinTable) {
        // Secondary/join tables have no inheritance so ought to be correct
        if (mapping.getTable() != null) {
            // Check there is no better table already present in the TableGroup for this mapping
            // This can happen when we do a select of a join table and the element table is in the
            // same table group, so hence already is present
            SQLTable mappingSqlTbl = stmt.getTable(mapping.getTable(), sqlTbl.getGroupName());
            if (mappingSqlTbl != null) {
                return mappingSqlTbl;
            }
        }
        return sqlTbl;
    }
    DatastoreClass sourceTbl = (DatastoreClass) sqlTbl.getTable();
    DatastoreClass mappingTbl = null;
    if (mapping.getTable() != null) {
        mappingTbl = (DatastoreClass) mapping.getTable();
    } else {
        mappingTbl = sourceTbl.getBaseDatastoreClassWithMember(mapping.getMemberMetaData());
    }
    if (mappingTbl == sourceTbl) {
        return sqlTbl;
    }
    // Try to find this datastore table in the same table group
    SQLTable mappingSqlTbl = stmt.getTable(mappingTbl, sqlTbl.getGroupName());
    if (mappingSqlTbl == null) {
        boolean forceLeftOuter = false;
        SQLTableGroup tableGrp = stmt.getTableGroup(sqlTbl.getGroupName());
        if (tableGrp.getJoinType() == JoinType.LEFT_OUTER_JOIN) {
            // This group isn't the candidate group, and we joined to the candidate group using
            // a left outer join originally, so use the same type for this table
            forceLeftOuter = true;
        }
        if (mappingTbl instanceof SecondaryDatastoreClass) {
            // Secondary table, so add inner/outer based on metadata
            boolean innerJoin = true;
            JoinMetaData joinmd = ((SecondaryDatastoreClass) mappingTbl).getJoinMetaData();
            if (joinmd != null && joinmd.isOuter() && !forceLeftOuter) {
                innerJoin = false;
            }
            if (innerJoin && !forceLeftOuter) {
                // Add join from {sourceTbl}.ID to {secondaryTbl}.ID
                mappingSqlTbl = stmt.join(JoinType.INNER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
            } else {
                // Add join from {sourceTbl}.ID to {secondaryTbl}.ID
                mappingSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
            }
        } else {
            if (forceLeftOuter) {
                // Add join from {sourceTbl}.ID to {superclassTbl}.ID
                mappingSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
            } else {
                // Add join from {sourceTbl}.ID to {superclassTbl}.ID
                mappingSqlTbl = stmt.join(JoinType.INNER_JOIN, sqlTbl, sqlTbl.getTable().getIdMapping(), mappingTbl, null, mappingTbl.getIdMapping(), null, sqlTbl.getGroupName());
            }
        }
    }
    return mappingSqlTbl;
}
Also used : PersistableJoinTable(org.datanucleus.store.rdbms.table.PersistableJoinTable) Table(org.datanucleus.store.rdbms.table.Table) JoinTable(org.datanucleus.store.rdbms.table.JoinTable) JoinMetaData(org.datanucleus.metadata.JoinMetaData) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) PersistableJoinTable(org.datanucleus.store.rdbms.table.PersistableJoinTable) JoinTable(org.datanucleus.store.rdbms.table.JoinTable)

Aggregations

DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)4 SecondaryDatastoreClass (org.datanucleus.store.rdbms.table.SecondaryDatastoreClass)4 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 ClassView (org.datanucleus.store.rdbms.table.ClassView)3 JoinMetaData (org.datanucleus.metadata.JoinMetaData)1 JoinTable (org.datanucleus.store.rdbms.table.JoinTable)1 PersistableJoinTable (org.datanucleus.store.rdbms.table.PersistableJoinTable)1 Table (org.datanucleus.store.rdbms.table.Table)1