FileDocCategorySizeDatePackage
StatementQueryMechanism.javaAPI DocGlassfish v2 API15366Tue May 22 16:54:40 BST 2007oracle.toplink.essentials.internal.queryframework

StatementQueryMechanism

public class StatementQueryMechanism extends CallQueryMechanism

Purpose: Mechanism used for all statement objects.

Responsibilities: Executes the appropriate statement.

author
Yvon Lavoie
since
TOPLink/Java 1.0

Fields Summary
protected SQLStatement
sqlStatement
protected Vector
sqlStatements
Normally only a single statement is used, however multiple table may require multiple statements on write.
Constructors Summary
public StatementQueryMechanism(DatabaseQuery query)
INTERNAL: Return a new mechanism for the query

param
query - owner of mechanism

        super(query);
    
public StatementQueryMechanism(DatabaseQuery query, SQLStatement statement)
Return a new mechanism for the query

param
query - owner of mechanism
param
statement - sql statement

        super(query);
        this.sqlStatement = statement;
    
Methods Summary
public voidclearStatement()
The statement is no longer require after prepare so can be released.

        // Only clear the statement if it is an expression query, otherwise the statement may still be needed.
        if (isExpressionQueryMechanism()) {
            setSQLStatement(null);
            setSQLStatements(null);
        }
    
public DatabaseQueryMechanismclone(DatabaseQuery queryClone)
Clone the mechanism for the specified query clone.

        StatementQueryMechanism clone = (StatementQueryMechanism)super.clone(queryClone);
        if ((!hasMultipleStatements()) && (getSQLStatement() != null)) {
            clone.setSQLStatement((SQLStatement)sqlStatement.clone());
        } else {
            Vector currentStatements = getSQLStatements();
            if (currentStatements != null) {
                Vector statementClone = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(currentStatements.size());
                Enumeration enumtr = currentStatements.elements();
                while (enumtr.hasMoreElements()) {
                    statementClone.addElement(((SQLStatement)enumtr.nextElement()).clone());
                }
                clone.setSQLStatements(statementClone);
            }
        }
        return clone;
    
public java.lang.IntegerdeleteObject()
INTERNAL: delete the object

exception
DatabaseException - an error has occurred on the database.
return
the row count.

        // Prepare the calls if not already set (prepare may not have had the modify row).
        if ((!hasMultipleCalls()) && (getCall() == null)) {
            prepareDeleteObject();
            if ((!hasMultipleCalls()) && (getCall() == null)) {
                return new Integer(1);// Must be 1 otherwise locking error will occur.
            }
        }

        return super.deleteObject();
    
public java.lang.IntegerexecuteNoSelect()
Update the object

exception
DatabaseException - an error has occurred on the database.
return
the row count.

        // Prepare the calls if not already set (prepare may not have had the modify row).
        if ((!hasMultipleCalls()) && (getCall() == null)) {
            prepareExecuteNoSelect();
        }

        return super.executeNoSelect();
    
public SQLStatementgetSQLStatement()
INTERNAL: Return the sqlStatement

        return sqlStatement;
    
public java.util.VectorgetSQLStatements()
Normally only a single statement is used, however multiple table may require multiple statements on write. This is lazy initialied to conserv space.

        if (sqlStatements == null) {
            sqlStatements = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3);
        }
        return sqlStatements;
    
public ExpressiongetSelectionCriteria()
Return the selection criteria for the statement.

        return getSQLStatement().getWhereClause();
    
public booleanhasMultipleStatements()
Normally only a single statement is used, however multiple table may require multiple statements on write. This is lazy initialied to conserv space.

        return (sqlStatements != null) && (!sqlStatements.isEmpty());
    
public voidinsertObject()
Insert the object

exception
DatabaseException - an error has occurred on the database.

        // Prepare the calls if not already set (prepare may not have had the modify row).
        if ((!hasMultipleCalls()) && (getCall() == null)) {
            prepareInsertObject();
        }

        super.insertObject();
    
public voidinsertObject(boolean reprepare)
Insert the object if the reprepare flag is set, first reprepare the query. Added for CR#3237

param
boolean reprepare - whether to reprepare the query.

        if (reprepare) {
            // Clear old calls, and reprepare. 
            setCalls(null);
            prepareInsertObject();
        }
        insertObject();
    
public booleanisCallQueryMechanism()
Return true if this is a call query mechanism

        return false;
    
public booleanisStatementQueryMechanism()
Return true if this is a statement query mechanism

        return true;
    
public voidprepare()
INTERNAL: This is different from 'prepareForExecution' in that this is called on the original query, and the other is called on the copy of the query. This query is copied for concurrency so this prepare can only setup things that will apply to any future execution of this query.

        if ((!hasMultipleStatements()) && (getSQLStatement() == null)) {
            throw QueryException.sqlStatementNotSetProperly(getQuery());
        }

        // Cannot call super yet as the call is not built.
    
public voidprepareCursorSelectAllRows()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareCursorSelectAllRows();
    
public voidprepareDeleteAll()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareDeleteAll();
    
public voidprepareDeleteObject()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareDeleteObject();
    
public voidprepareDoesExist(DatabaseField field)
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        getCall().returnOneRow();
        prepareCall();
    
public voidprepareExecuteNoSelect()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareExecuteNoSelect();
    
public voidprepareExecuteSelect()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareExecuteSelect();
    
public voidprepareInsertObject()
Pre-build the SQL call from the statement.

        // Require modify row to prepare.
        if (getModifyRow() == null) {
            return;
        }

        if (hasMultipleStatements()) {
            for (Enumeration statementEnum = getSQLStatements().elements();
                     statementEnum.hasMoreElements();) {
                ((SQLModifyStatement)statementEnum.nextElement()).setModifyRow(getModifyRow());
            }
        } else if (getSQLStatement() != null) {
            ((SQLModifyStatement)getSQLStatement()).setModifyRow(getModifyRow());
        }
        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareInsertObject();
    
public voidprepareSelectAllRows()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareSelectAllRows();
    
public voidprepareSelectOneRow()
Pre-build the SQL call from the statement.

        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareSelectOneRow();
    
public voidprepareUpdateAll()
Pre-build the SQL call from the statement.

        setCallFromStatement();// Will build an SQLUpdateAllStatement
        clearStatement();// The statement is no longer require so can be released.
        super.prepareUpdateAll();
    
public voidprepareUpdateObject()
Pre-build the SQL call from the statement.

        // Require modify row to prepare.
        if (getModifyRow() == null) {
            return;
        }

        if (hasMultipleStatements()) {
            for (Enumeration statementEnum = getSQLStatements().elements();
                     statementEnum.hasMoreElements();) {
                ((SQLModifyStatement)statementEnum.nextElement()).setModifyRow(getModifyRow());
            }
        } else if (getSQLStatement() != null) {
            ((SQLModifyStatement)getSQLStatement()).setModifyRow(getModifyRow());
        }
        setCallFromStatement();
        // The statement is no longer require so can be released.
        clearStatement();

        super.prepareUpdateObject();
    
protected voidsetCallFromStatement()
Pre-build the SQL call from the statement.

        // Profile SQL generation.
        getSession().startOperationProfile(SessionProfiler.SQL_GENERATION);
        if (hasMultipleStatements()) {
            for (Enumeration statementEnum = getSQLStatements().elements();
                     statementEnum.hasMoreElements();) {
                //			DatabaseCall call = ((SQLStatement) statementEnum.nextElement()).buildCall(getSession());
                DatabaseCall call = null;
                if (getDescriptor() != null) {
                    call = getDescriptor().buildCallFromStatement((SQLStatement)statementEnum.nextElement(), getSession());
                } else {
                    call = ((SQLStatement)statementEnum.nextElement()).buildCall(getSession());
                }

                // In case of update call may be null if no update required.
                if (call != null) {
                    addCall(call);
                }
            }
        } else {
            DatabaseCall call = null;
            if (getDescriptor() != null) {
                call = getDescriptor().buildCallFromStatement(getSQLStatement(), getSession());
            } else {
                call = getSQLStatement().buildCall(getSession());
            }

            // In case of update call may be null if no update required.
            if (call != null) {
                setCall(call);
            }
        }

        // Profile SQL generation.
        getSession().endOperationProfile(SessionProfiler.SQL_GENERATION);
    
public voidsetSQLStatement(SQLStatement statement)
Set the sqlStatement

        this.sqlStatement = statement;
    
protected voidsetSQLStatements(java.util.Vector sqlStatements)
Normally only a single statement is used, however multiple table may require multiple statements on write. This is lazy initialied to conserv space.

        this.sqlStatements = sqlStatements;
    
public java.lang.IntegerupdateObject()
Update the object

exception
DatabaseException - an error has occurred on the database.
return
the row count.

        // Prepare the calls if not already set (prepare may not have had the modify row).
        if ((!hasMultipleCalls()) && (getCall() == null)) {
            prepareUpdateObject();
            if ((!hasMultipleCalls()) && (getCall() == null)) {
                return new Integer(1);// Must be 1 otherwise locking error will occur.
            }
        }

        return super.updateObject();