Search in sources :

Example 1 with DeferModification

use of org.apache.derby.vti.DeferModification in project derby by apache.

the class VTIDeferModPolicy method deferIt.

/**
 * See if a VTI modification statement should be deferred.
 *
 * @param statementType DeferModification.INSERT_STATEMENT, UPDATE_STATEMENT, or DELETE_STATEMENT
 * @param targetVTI The target VTI
 * @param updateColumnNames The list of columns being updated, null if this is not an update statement
 * @param source
 */
public static boolean deferIt(int statementType, FromVTI targetVTI, String[] updateColumnNames, QueryTreeNode source) throws StandardException {
    try {
        DeferModification deferralControl;
        int resultSetType = targetVTI.getResultSetType();
        /* Deferred updates and deletes are implemented by scrolling the result set. So, if
             * the statement is an update or delete but the result set is not scrollable then do
             * not attempt to defer the statement.
             */
        if ((statementType == DeferModification.UPDATE_STATEMENT || statementType == DeferModification.DELETE_STATEMENT) && resultSetType == ResultSet.TYPE_FORWARD_ONLY)
            return false;
        deferralControl = targetVTI.getDeferralControl();
        if (deferralControl == null) {
            String VTIClassName = targetVTI.getMethodCall().getJavaClassName();
            deferralControl = new DefaultVTIModDeferPolicy(VTIClassName, ResultSet.TYPE_SCROLL_SENSITIVE == resultSetType);
        }
        if (deferralControl.alwaysDefer(statementType))
            return true;
        if (source == null && statementType != DeferModification.UPDATE_STATEMENT)
            return false;
        VTIDeferModPolicy deferralSearch = new VTIDeferModPolicy(targetVTI, updateColumnNames, deferralControl, statementType);
        if (source != null)
            source.accept(deferralSearch);
        if (statementType == DeferModification.UPDATE_STATEMENT) {
            // Apply the columnRequiresDefer method to updated columns not in the where clause.
            for (String s : deferralSearch.columns) {
                if (deferralControl.columnRequiresDefer(statementType, s, false)) {
                    return true;
                }
            }
        }
        return deferralSearch.deferred;
    } catch (SQLException sqle) {
        throw StandardException.unexpectedUserException(sqle);
    }
}
Also used : DeferModification(org.apache.derby.vti.DeferModification) SQLException(java.sql.SQLException)

Example 2 with DeferModification

use of org.apache.derby.vti.DeferModification in project derby by apache.

the class InsertVTIResultSet method openCore.

/**
 *		@exception StandardException Standard Derby error policy
 */
protected void openCore() throws StandardException {
    /* We must instantiate the VTI on each execution if any of the
		 * parameters contain a ?.
		 */
    if (ps == null) {
        ps = (PreparedStatement) vtiRS.getVTIConstructor().invoke(activation);
    }
    if (ps instanceof DeferModification) {
        try {
            ((DeferModification) ps).modificationNotify(DeferModification.INSERT_STATEMENT, constants.deferred);
        } catch (Throwable t) {
            throw StandardException.unexpectedUserException(t);
        }
    }
    ExecRow row = getNextRowCore(sourceResultSet);
    try {
        rs = ps.executeQuery();
    } catch (Throwable t) {
        throw StandardException.unexpectedUserException(t);
    }
    /* The source does not know whether or not we are doing a
		 * deferred mode insert.  If we are, then we must clear the
		 * index scan info from the activation so that the row changer
		 * does not re-use that information (which won't be valid for
		 * a deferred mode insert).
		 */
    if (constants.deferred) {
        activation.clearIndexScanInfo();
    }
    if (firstExecute && constants.deferred) {
        Properties properties = new Properties();
        /*
			** If deferred we save a copy of the entire row.
			*/
        rowHolder = new TemporaryRowHolderImpl(activation, properties, resultDescription);
    }
    while (row != null) {
        /*
			** If we're doing a deferred insert, insert into the temporary
			** conglomerate.  Otherwise, insert directly into the permanent
			** conglomerates using the rowChanger.
			*/
        if (constants.deferred) {
            rowHolder.insert(row);
        } else {
            insertIntoVTI(rs, row);
        }
        rowCount++;
        // No need to do a next on a single row source
        if (constants.singleRowSource) {
            row = null;
        } else {
            row = getNextRowCore(sourceResultSet);
        }
    }
    /*
		** If it's a deferred insert, scan the temporary conglomerate and
		** insert the rows into the permanent conglomerates using rowChanger.
		*/
    if (constants.deferred) {
        CursorResultSet tempRS = rowHolder.getResultSet();
        try {
            tempRS.open();
            while ((row = tempRS.getNextRow()) != null) {
                insertIntoVTI(rs, row);
            }
        } finally {
            sourceResultSet.clearCurrentRow();
            tempRS.close();
        }
    }
    if (rowHolder != null) {
        rowHolder.close();
    // rowHolder kept across opens
    }
}
Also used : DeferModification(org.apache.derby.vti.DeferModification) CursorResultSet(org.apache.derby.iapi.sql.execute.CursorResultSet) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) Properties(java.util.Properties)

Example 3 with DeferModification

use of org.apache.derby.vti.DeferModification in project derby by apache.

the class VTIResultSet method openCore.

// 
// ResultSet interface (leftover from NoPutResultSet)
// 
/**
 * Sets state to 'open'.
 *
 * @exception StandardException thrown if activation closed.
 */
public void openCore() throws StandardException {
    beginTime = getCurrentTimeMillis();
    if (SanityManager.DEBUG)
        SanityManager.ASSERT(!isOpen, "VTIResultSet already open");
    isOpen = true;
    numOpens++;
    /* We need to Instantiate the user's ResultSet on the each open since
		 * there is no way to close and then reopen a java.sql.ResultSet.
		 * For Version 2 VTIs, we may be able to skip instantiated their
		 * PreparedStatement here.
		 */
    try {
        if (version2) {
            userPS = (PreparedStatement) constructor.invoke(activation);
            if (userPS instanceof org.apache.derby.vti.Pushable) {
                org.apache.derby.vti.Pushable p = (org.apache.derby.vti.Pushable) userPS;
                if (referencedColumns != null) {
                    pushedProjection = p.pushProjection(this, getProjectedColList());
                }
            }
            if (userPS instanceof org.apache.derby.vti.IQualifyable) {
                org.apache.derby.vti.IQualifyable q = (org.apache.derby.vti.IQualifyable) userPS;
                q.setQualifiers(this, pushedQualifiers);
            }
            fastPath = userPS instanceof IFastPath ? (IFastPath) userPS : null;
            if (isTarget && userPS instanceof DeferModification && activation.getConstantAction() instanceof UpdatableVTIConstantAction) {
                UpdatableVTIConstantAction constants = (UpdatableVTIConstantAction) activation.getConstantAction();
                ((DeferModification) userPS).modificationNotify(constants.statementType, constants.deferred);
            }
            if ((fastPath != null) && fastPath.executeAsFastPath())
                ;
            else
                userVTI = userPS.executeQuery();
            /* Save off the target VTI */
            if (isTarget) {
                activation.setTargetVTI(userVTI);
            }
        } else {
            userVTI = (ResultSet) constructor.invoke(activation);
            if (userVTI instanceof RestrictedVTI) {
                RestrictedVTI restrictedVTI = (RestrictedVTI) userVTI;
                restrictedVTI.initScan(vtiProjection, cloneRestriction(activation));
            }
            if (userVTI instanceof AwareVTI) {
                AwareVTI awareVTI = (AwareVTI) userVTI;
                awareVTI.setContext(new VTIContext(vtiSchema, vtiName, activation.getLanguageConnectionContext().getStatementContext().getStatementText()));
            }
        }
        // Set up the nullablity of the runtime columns, may be delayed
        setNullableColumnList();
    } catch (Throwable t) {
        throw StandardException.unexpectedUserException(t);
    }
    openTime += getElapsedMillis(beginTime);
}
Also used : DeferModification(org.apache.derby.vti.DeferModification) AwareVTI(org.apache.derby.vti.AwareVTI) VTIContext(org.apache.derby.vti.VTIContext) RestrictedVTI(org.apache.derby.vti.RestrictedVTI) IFastPath(org.apache.derby.vti.IFastPath)

Example 4 with DeferModification

use of org.apache.derby.vti.DeferModification in project derby by apache.

the class FromVTI method getResultSetMetaData.

/**
 * Get the ResultSetMetaData for the class/object.  We first look for
 * the optional static method which has the same signature as the constructor.
 * If it doesn't exist, then we instantiate an object and get the ResultSetMetaData
 * from that object.
 *
 * @return The ResultSetMetaData from the class/object.
 *
 * @exception StandardException		Thrown on error
 */
ResultSetMetaData getResultSetMetaData() throws StandardException {
    // Get the actual
    ResultSetMetaData rsmd = null;
    try {
        if (version2) {
            ps = (PreparedStatement) getNewInstance();
            if (ps.getResultSetConcurrency() != ResultSet.CONCUR_UPDATABLE) {
                throw StandardException.newException(SQLState.LANG_UPDATABLE_VTI_NON_UPDATABLE_RS, getVTIName());
            }
            rsmd = ps.getMetaData();
            controlsDeferral = (ps instanceof DeferModification);
            /* See if the result set is known to be insensitive or not.
                 *
                 * Some older VTI implementations do not implement getResultSetType(). UpdatableVTITemplate
                 * does not implement it at all. UpdatableVTITemplate.getResultSetType throws an
                 * exception. In either of these cases make the conservative assumption that the result set is sensitive.
                 */
            try {
                resultSetType = ps.getResultSetType();
            } catch (SQLException sqle) {
            } catch (java.lang.AbstractMethodError ame) {
            } catch (java.lang.NoSuchMethodError nsme) {
            }
            if (!implementsVTICosting) {
                ps.close();
                ps = null;
            }
        } else {
            rs = (ResultSet) getNewInstance();
            rsmd = rs.getMetaData();
            if (!implementsVTICosting) {
                rs.close();
                rs = null;
            }
        }
    } catch (Throwable t) {
        throw StandardException.unexpectedUserException(t);
    }
    return rsmd;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DeferModification(org.apache.derby.vti.DeferModification) SQLException(java.sql.SQLException)

Aggregations

DeferModification (org.apache.derby.vti.DeferModification)4 SQLException (java.sql.SQLException)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 Properties (java.util.Properties)1 CursorResultSet (org.apache.derby.iapi.sql.execute.CursorResultSet)1 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)1 AwareVTI (org.apache.derby.vti.AwareVTI)1 IFastPath (org.apache.derby.vti.IFastPath)1 RestrictedVTI (org.apache.derby.vti.RestrictedVTI)1 VTIContext (org.apache.derby.vti.VTIContext)1