FileDocCategorySizeDatePackage
LOBValueWriter.javaAPI DocGlassfish v2 API9930Tue May 22 16:54:34 BST 2007oracle.toplink.essentials.internal.helper

LOBValueWriter

public class LOBValueWriter extends Object
INTERNAL:

Purpose:LOBValueWriter is used to write a large size of object into an Oracle CLOB/BLOB column through Oracle LOB Locator. It's a work-around object for the well-known 4k write limits on an Oracle thin driver.

Responsibilities:

  • Build the Oracle empty lob method call string for the insert call.
  • Build the minimial SELECT call to retrieve the locator.
  • Write the lob value through the locator.
  • Resolve the multiple table INSERT/SELECT orders.
  • Resolve the nested unit of work commit issue.
author:
King Wang
since
TopLink/Java 5.0. July 2002.

Fields Summary
private Collection
calls
private Accessor
accessor
Constructors Summary
public LOBValueWriter(Accessor accessor)
This is the default constructor for the class. Bug 2804663 - Each DatabaseAccessor will now hold on to its own instance of this class, hence a singleton pattern is not applicable.


                                        
       
        this.accessor = accessor;
    
Methods Summary
public voidaddCall(oracle.toplink.essentials.queryframework.Call call)
Add original (insert or update) call to the collection

        if (calls == null) {
            //use lazy initialization
            calls = new ArrayList(2);
        }
        calls.add(call);
    
protected voidbuildAndExecuteCall(oracle.toplink.essentials.internal.databaseaccess.DatabaseCall dbCall, oracle.toplink.essentials.internal.sessions.AbstractSession session)

        DatabaseQuery query = dbCall.getQuery();
        if (!query.isWriteObjectQuery()) {
            //if not writequery, should not go through the locator writing..
            return;
        }
        WriteObjectQuery writeQuery = (WriteObjectQuery)query;
        writeQuery.setAccessor(accessor);
        //build a select statement form the query
        SQLSelectStatement selectStatement = buildSelectStatementForLocator(writeQuery, dbCall, session);

        //then build a call from the statement
        DatabaseCall call = buildCallFromSelectStatementForLocator(selectStatement, writeQuery, dbCall, session);

        accessor.executeCall(call, call.getQuery().getTranslationRow(), session);
    
public voidbuildAndExecuteSelectCalls(oracle.toplink.essentials.internal.sessions.AbstractSession session)
Build and execute the deferred select calls.

        if ((calls == null) || calls.isEmpty()) {
            //no deferred select calls (it means no locator is required)
            return;
        }

        //all INSERTs have been executed, time to execute the SELECTs
        try {
            for (Iterator callIt = calls.iterator(); callIt.hasNext();) {
                DatabaseCall dbCall = (DatabaseCall)callIt.next();
                buildAndExecuteCall(dbCall, session);
            }
        } finally {
            //after executing all select calls, need to empty the collection.
            //this is neccessary in the nested unit of work cases.
            calls.clear();
        }
    
private oracle.toplink.essentials.internal.databaseaccess.DatabaseCallbuildCallFromSelectStatementForLocator(oracle.toplink.essentials.internal.expressions.SQLSelectStatement selectStatement, oracle.toplink.essentials.queryframework.WriteObjectQuery writeQuery, oracle.toplink.essentials.internal.databaseaccess.DatabaseCall dbCall, oracle.toplink.essentials.internal.sessions.AbstractSession session)
Build the sql call from the select statement for selecting the locator

        DatabaseCall call = selectStatement.buildCall(session);

        //the LOB context must be passed into the new call object
        call.setContexts(dbCall.getContexts());
        //need to explictly define one rwo return, otherwise, TL assumes multiple rows return and confuses the accessor
        call.returnOneRow();
        //the query object has to be set in order to access to the platform and login objects
        call.setQuery(writeQuery);
        // prepare it
        call.prepare(session);
        //finally do the translation
        call.translate(writeQuery.getTranslationRow(), writeQuery.getModifyRow(), session);
        return call;
    
private oracle.toplink.essentials.internal.expressions.SQLSelectStatementbuildSelectStatementForLocator(oracle.toplink.essentials.queryframework.WriteObjectQuery writeQuery, oracle.toplink.essentials.internal.databaseaccess.DatabaseCall call, oracle.toplink.essentials.internal.sessions.AbstractSession session)
Build the select statement for selecting the locator

        SQLSelectStatement selectStatement = new SQLSelectStatement();
        Vector tables = writeQuery.getDescriptor().getTables();
        selectStatement.setTables(tables);
        //rather than get ALL fields from the descriptor, only use the LOB-related fields to build the minimal SELECT statement.
        selectStatement.setFields(call.getContexts().getFields());
        //the where clause setting here is sufficient if the object does not map to multiple tables.
        selectStatement.setWhereClause(writeQuery.getDescriptor().getObjectBuilder().buildPrimaryKeyExpressionFromObject(writeQuery.getObject(), session));
        //need pessimistic locking for the locator select
        selectStatement.setLockingClause(ForUpdateClause.newInstance(ObjectBuildingQuery.LOCK));

        if (tables.size() > 1) {
            //the primary key expression from the primary table
            Expression expression = selectStatement.getWhereClause();

            //additioanl join from the non-primary tables
            Expression additionalJoin = (Expression)writeQuery.getDescriptor().getQueryManager().getAdditionalJoinExpression();
            if (additionalJoin != null) {
                expression = expression.and(additionalJoin);
            }

            //where clause now contains extra joins across all tables
            selectStatement.setWhereClause(expression);
        }

        //normalize the statement at the end, such as assign alias to all tables, and build sorting statement
        selectStatement.normalize(session, writeQuery.getDescriptor());
        return selectStatement;
    
public voidfetchLocatorAndWriteValue(oracle.toplink.essentials.internal.databaseaccess.DatabaseCall dbCall, java.lang.Object resultSet)
Fetch the locator(s) from the result set and write LOB value to the table

        Enumeration enumFields = dbCall.getContexts().getFields().elements();
        Enumeration enumValues = dbCall.getContexts().getValues().elements();
        AbstractSession executionSession = dbCall.getQuery().getSession().getExecutionSession(dbCall.getQuery());
        while (enumFields.hasMoreElements()) {
            DatabaseField field = (DatabaseField)enumFields.nextElement();
            Object value = enumValues.nextElement();

            //write the value through the locator
            executionSession.getPlatform().writeLOB(field, value, (ResultSet)resultSet, executionSession);
        }