Search in sources :

Example 1 with SQLBoolean

use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.

the class DataDictionaryImpl method clearSPSPlans.

/**
 * Mark all SPS plans in the data dictionary invalid. This does
 * not invalidate cached plans. This function is for use by
 * the boot-up code.
 * @exception StandardException		Thrown on error
 */
void clearSPSPlans() throws StandardException {
    TabInfoImpl ti = getNonCoreTI(SYSSTATEMENTS_CATALOG_NUM);
    faultInTabInfo(ti);
    TransactionController tc = getTransactionExecute();
    FormatableBitSet columnToReadSet = new FormatableBitSet(SYSSTATEMENTSRowFactory.SYSSTATEMENTS_COLUMN_COUNT);
    FormatableBitSet columnToUpdateSet = new FormatableBitSet(SYSSTATEMENTSRowFactory.SYSSTATEMENTS_COLUMN_COUNT);
    columnToUpdateSet.set(SYSSTATEMENTSRowFactory.SYSSTATEMENTS_VALID - 1);
    columnToUpdateSet.set(SYSSTATEMENTSRowFactory.SYSSTATEMENTS_CONSTANTSTATE - 1);
    DataValueDescriptor[] replaceRow = new DataValueDescriptor[SYSSTATEMENTSRowFactory.SYSSTATEMENTS_COLUMN_COUNT];
    /* Set up a couple of row templates for fetching CHARS */
    replaceRow[SYSSTATEMENTSRowFactory.SYSSTATEMENTS_VALID - 1] = new SQLBoolean(false);
    replaceRow[SYSSTATEMENTSRowFactory.SYSSTATEMENTS_CONSTANTSTATE - 1] = new UserType((Object) null);
    /* Scan the entire heap */
    ScanController sc = tc.openScan(ti.getHeapConglomerate(), false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_REPEATABLE_READ, columnToReadSet, (DataValueDescriptor[]) null, ScanController.NA, (Qualifier[][]) null, (DataValueDescriptor[]) null, ScanController.NA);
    while (sc.fetchNext((DataValueDescriptor[]) null)) {
        /* Replace the column in the table */
        sc.replace(replaceRow, columnToUpdateSet);
    }
    sc.close();
}
Also used : ScanController(org.apache.derby.iapi.store.access.ScanController) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UserType(org.apache.derby.iapi.types.UserType) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean)

Example 2 with SQLBoolean

use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.

the class SYSTRIGGERSRowFactory method makeRow.

/**
 * Helper method that contains common logic for {@code makeRow()} and
 * {@code makeEmptyRowForCurrentVersion()}. Creates a row for the
 * SYSTRIGGERS conglomerate.
 *
 * @param td the {@code TriggerDescriptor} to create a row from (can be
 *   {@code null} if the returned row should be empty)
 * @param columnCount the number of columns in the returned row (used for
 *   trimming off columns in soft upgrade mode to match the format in
 *   the old dictionary version)
 * @return a row for the SYSTRIGGERS conglomerate
 * @throws StandardException if an error happens when creating the row
 */
private ExecRow makeRow(TupleDescriptor td, int columnCount) throws StandardException {
    String name = null;
    UUID uuid = null;
    // schema
    UUID suuid = null;
    // referenced table
    UUID tuuid = null;
    // action sps uuid string
    UUID actionSPSID = null;
    // when clause sps uuid string
    UUID whenSPSID = null;
    Timestamp createTime = null;
    String event = null;
    String time = null;
    String type = null;
    String enabled = null;
    String triggerDefinition = null;
    String oldReferencingName = null;
    String newReferencingName = null;
    ReferencedColumns rcd = null;
    boolean referencingOld = false;
    boolean referencingNew = false;
    String whenClauseText = null;
    if (td != null) {
        TriggerDescriptor triggerDescriptor = (TriggerDescriptor) td;
        name = triggerDescriptor.getName();
        uuid = triggerDescriptor.getUUID();
        suuid = triggerDescriptor.getSchemaDescriptor().getUUID();
        createTime = triggerDescriptor.getCreationTimestamp();
        // for now we are assuming that a trigger can only listen to a single event
        event = triggerDescriptor.listensForEvent(TriggerDescriptor.TRIGGER_EVENT_UPDATE) ? "U" : triggerDescriptor.listensForEvent(TriggerDescriptor.TRIGGER_EVENT_DELETE) ? "D" : "I";
        time = triggerDescriptor.isBeforeTrigger() ? "B" : "A";
        type = triggerDescriptor.isRowTrigger() ? "R" : "S";
        enabled = triggerDescriptor.isEnabled() ? "E" : "D";
        tuuid = triggerDescriptor.getTableDescriptor().getUUID();
        int[] refCols = triggerDescriptor.getReferencedCols();
        int[] refColsInTriggerAction = triggerDescriptor.getReferencedColsInTriggerAction();
        rcd = (refCols != null || refColsInTriggerAction != null) ? new ReferencedColumnsDescriptorImpl(refCols, refColsInTriggerAction) : null;
        actionSPSID = triggerDescriptor.getActionId();
        whenSPSID = triggerDescriptor.getWhenClauseId();
        triggerDefinition = triggerDescriptor.getTriggerDefinition();
        referencingOld = triggerDescriptor.getReferencingOld();
        referencingNew = triggerDescriptor.getReferencingNew();
        oldReferencingName = triggerDescriptor.getOldReferencingName();
        newReferencingName = triggerDescriptor.getNewReferencingName();
        whenClauseText = triggerDescriptor.getWhenClauseText();
    }
    /* Build the row to insert */
    ExecRow row = getExecutionFactory().getValueRow(columnCount);
    /* 1st column is TRIGGERID */
    row.setColumn(1, new SQLChar((uuid == null) ? null : uuid.toString()));
    /* 2nd column is TRIGGERNAME */
    row.setColumn(2, new SQLVarchar(name));
    /* 3rd column is SCHEMAID */
    row.setColumn(3, new SQLChar((suuid == null) ? null : suuid.toString()));
    /* 4th column is CREATIONTIMESTAMP */
    SQLTimestamp creationTimestamp = (createTime == null) ? new SQLTimestamp(null) : new SQLTimestamp(createTime, getCalendarForCreationTimestamp());
    row.setColumn(4, creationTimestamp);
    /* 5th column is EVENT */
    row.setColumn(5, new SQLChar(event));
    /* 6th column is FIRINGTIME */
    row.setColumn(6, new SQLChar(time));
    /* 7th column is TYPE */
    row.setColumn(7, new SQLChar(type));
    /* 8th column is STATE */
    row.setColumn(8, new SQLChar(enabled));
    /* 9th column is TABLEID */
    row.setColumn(9, new SQLChar((tuuid == null) ? null : tuuid.toString()));
    /* 10th column is WHENSTMTID */
    row.setColumn(10, new SQLChar((whenSPSID == null) ? null : whenSPSID.toString()));
    /* 11th column is ACTIONSTMTID */
    row.setColumn(11, new SQLChar((actionSPSID == null) ? null : actionSPSID.toString()));
    /* 12th column is REFERENCEDCOLUMNS 
		 *  (user type org.apache.derby.catalog.ReferencedColumns)
		 */
    row.setColumn(12, new UserType(rcd));
    /* 13th column is TRIGGERDEFINITION */
    row.setColumn(13, dvf.getLongvarcharDataValue(triggerDefinition));
    /* 14th column is REFERENCINGOLD */
    row.setColumn(14, new SQLBoolean(referencingOld));
    /* 15th column is REFERENCINGNEW */
    row.setColumn(15, new SQLBoolean(referencingNew));
    /* 16th column is OLDREFERENCINGNAME */
    row.setColumn(16, new SQLVarchar(oldReferencingName));
    /* 17th column is NEWREFERENCINGNAME */
    row.setColumn(17, new SQLVarchar(newReferencingName));
    /* 18th column is WHENCLAUSETEXT */
    if (row.nColumns() >= 18) {
        // This column is present only if the data dictionary version is
        // 10.11 or higher.
        row.setColumn(18, dvf.getLongvarcharDataValue(whenClauseText));
    }
    return row;
}
Also used : ReferencedColumns(org.apache.derby.catalog.ReferencedColumns) SQLChar(org.apache.derby.iapi.types.SQLChar) ReferencedColumnsDescriptorImpl(org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl) SQLVarchar(org.apache.derby.iapi.types.SQLVarchar) SQLTimestamp(org.apache.derby.iapi.types.SQLTimestamp) Timestamp(java.sql.Timestamp) TriggerDescriptor(org.apache.derby.iapi.sql.dictionary.TriggerDescriptor) SQLTimestamp(org.apache.derby.iapi.types.SQLTimestamp) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) UUID(org.apache.derby.catalog.UUID) UserType(org.apache.derby.iapi.types.UserType) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean)

Example 3 with SQLBoolean

use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.

the class ScrollInsensitiveResultSet method addRowToHashTable.

// 
// class implementation
// 
/**
 * Add a row to the backing hash table, keyed on position.
 * When a row gets updated when using scrollable insensitive updatable
 * result sets, the old entry for the row will be deleted from the hash
 * table and this method will be called to add the new values for the row
 * to the hash table, with the parameter rowUpdated = true so as to mark
 * the row as updated. The latter is done in order to implement
 * detectability of own changes for result sets of this type.
 *
 * @param sourceRow	The row to add.
 * @param position The key
 * @param rowLoc The rowLocation of the row to add.
 * @param rowUpdated Indicates whether the row has been updated.
 */
private void addRowToHashTable(ExecRow sourceRow, int position, RowLocation rowLoc, boolean rowUpdated) throws StandardException {
    DataValueDescriptor[] hashRowArray = new DataValueDescriptor[sourceRowWidth + extraColumns];
    // 1st element is the key
    hashRowArray[0] = new SQLInteger(position);
    if (isForUpdate()) {
        hashRowArray[POS_ROWLOCATION] = rowLoc.cloneValue(false);
        hashRowArray[POS_ROWDELETED] = new SQLBoolean(false);
        hashRowArray[POS_ROWUPDATED] = new SQLBoolean(rowUpdated);
    }
    /* Copy rest of elements from sourceRow.
		 * NOTE: We need to clone the source row
		 * and we do our own cloning since the 1st column
		 * is not a wrapper.
		 */
    DataValueDescriptor[] sourceRowArray = sourceRow.getRowArray();
    System.arraycopy(sourceRowArray, 0, hashRowArray, extraColumns, sourceRowArray.length);
    ht.putRow(true, hashRowArray, null);
    numToHashTable++;
}
Also used : DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) SQLInteger(org.apache.derby.iapi.types.SQLInteger) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean)

Example 4 with SQLBoolean

use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.

the class ScrollInsensitiveResultSet method markRowAsDeleted.

/**
 * @see NoPutResultSet#markRowAsDeleted
 *
 * Sets the deleted column of the hash table to true in the current row.
 */
public void markRowAsDeleted() throws StandardException {
    positionInHashTable.setValue(currentPosition);
    DataValueDescriptor[] hashRowArray = getCurrentRowFromHashtable();
    RowLocation rowLoc = (RowLocation) hashRowArray[POS_ROWLOCATION];
    ht.remove(new SQLInteger(currentPosition));
    ((SQLBoolean) hashRowArray[POS_ROWDELETED]).setValue(true);
    // Set all columns to NULL, the row is now a placeholder
    for (int i = extraColumns; i < hashRowArray.length; i++) {
        hashRowArray[i].setToNull();
    }
    ht.putRow(true, hashRowArray, null);
}
Also used : DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation) SQLInteger(org.apache.derby.iapi.types.SQLInteger) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean)

Example 5 with SQLBoolean

use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.

the class GenericTriggerExecutor method executeSPS.

/**
 * Execute the given stored prepared statement.  We
 * just grab the prepared statement from the spsd,
 * get a new activation holder and let er rip.
 *
 * @param sps the SPS to execute
 * @param isWhen {@code true} if the SPS is for the WHEN clause,
 *               {@code false} otherwise
 * @return {@code true} if the SPS is for a WHEN clause and it evaluated
 *         to {@code TRUE}, {@code false} otherwise
 * @exception StandardException on error
 */
private boolean executeSPS(SPSDescriptor sps, boolean isWhen) throws StandardException {
    boolean recompile = false;
    boolean whenClauseWasTrue = false;
    // The prepared statement and the activation may already be available
    // if the trigger has been fired before in the same statement. (Only
    // happens with row triggers that are triggered by a statement that
    // touched multiple rows.) The WHEN clause and the trigger action have
    // their own prepared statement and activation. Fetch the correct set.
    ExecPreparedStatement ps = isWhen ? whenPS : actionPS;
    Activation spsActivation = isWhen ? spsWhenActivation : spsActionActivation;
    while (true) {
        /*
			** Only grab the ps the 1st time through.  This
			** way a row trigger doesn't do any unnecessary
			** setup work.
			*/
        if (ps == null || recompile) {
            // The SPS activation will set its parent activation from
            // the statement context. Reset it to the original parent
            // activation first so that it doesn't use the activation of
            // the previously executed SPS as parent. DERBY-6348.
            lcc.getStatementContext().setActivation(activation);
            /*
				** We need to clone the prepared statement so we don't
				** wind up marking that ps that is tied to sps as finished
				** during the course of execution.
				*/
            ps = sps.getPreparedStatement();
            ps = ps.getClone();
            // it should be valid since we've just prepared for it
            ps.setValid();
            spsActivation = ps.getActivation(lcc, false);
            /*
				** Normally, we want getSource() for an sps invocation
				** to be EXEC STATEMENT xxx, but in this case, since
				** we are executing the SPS in our own fashion, we want
				** the text to be the trigger action.  So set it accordingly.
				*/
            ps.setSource(sps.getText());
            ps.setSPSAction();
            // trigger fires multiple times.
            if (isWhen) {
                whenPS = ps;
                spsWhenActivation = spsActivation;
            } else {
                actionPS = ps;
                spsActionActivation = spsActivation;
            }
        }
        // save the active statement context for exception handling purpose
        StatementContext active_sc = lcc.getStatementContext();
        /*
			** Execute the activation.  If we have an error, we
			** are going to go to some extra work to pop off
			** our statement context.  This is because we are
			** a nested statement (we have 2 activations), but
			** we aren't a nested connection, so we have to
			** pop off our statementcontext to get error handling	
			** to work correctly.  This is normally a no-no, but
			** we are an unusual case.
			*/
        try {
            // This is a substatement; for now, we do not set any timeout
            // for it. We might change this behaviour later, by linking
            // timeout to its parent statement's timeout settings.
            ResultSet rs = ps.executeSubStatement(activation, spsActivation, false, 0L);
            if (isWhen) {
                // This is a WHEN clause. Expect a single BOOLEAN value
                // to be returned.
                ExecRow row = rs.getNextRow();
                if (SanityManager.DEBUG && row.nColumns() != 1) {
                    SanityManager.THROWASSERT("Expected WHEN clause to have exactly " + "one column, found: " + row.nColumns());
                }
                DataValueDescriptor value = row.getColumn(1);
                if (SanityManager.DEBUG) {
                    SanityManager.ASSERT(value instanceof SQLBoolean);
                }
                whenClauseWasTrue = !value.isNull() && value.getBoolean();
                if (SanityManager.DEBUG) {
                    SanityManager.ASSERT(rs.getNextRow() == null, "WHEN clause returned more than one row");
                }
            } else if (rs.returnsRows()) {
                // The result set was opened in ps.execute()
                while (rs.getNextRow() != null) {
                }
            }
            rs.close();
        } catch (StandardException e) {
            /* 
				** When a trigger SPS action is executed and results in 
				** an exception, the system needs to clean up the active 
				** statement context(SC) and the trigger execution context
				** (TEC) in language connection context(LCC) properly (e.g.:  
				** "Maximum depth triggers exceeded" exception); otherwise, 
				** this will leave old TECs lingering and may result in 
				** subsequent statements within the same connection to throw 
				** the same exception again prematurely.  
				**    
				** A new statement context will be created for the SPS before
				** it is executed.  However, it is possible for some 
				** StandardException to be thrown before a new statement 
				** context is pushed down to the context stack; hence, the 
				** trigger executor needs to ensure that the current active SC 
				** is associated with the SPS, so that it is cleaning up the 
				** right statement context in LCC. 
                **
                ** It is also possible that the error has already been handled
                ** on a lower level, especially if the trigger re-enters the
                ** JDBC layer. In that case, the current SC will be null.
				**    
				** When the active SC is cleaned up, the TEC will be removed
				** from LCC and the SC object will be popped off from the LCC 
				** as part of cleanupOnError logic.  
				 */
            /* retrieve the current active SC */
            StatementContext sc = lcc.getStatementContext();
            /* make sure that the cleanup is on the new SC */
            if (sc != null && active_sc != sc) {
                sc.cleanupOnError(e);
            }
            /* Handle dynamic recompiles */
            if (e.getMessageId().equals(SQLState.LANG_STATEMENT_NEEDS_RECOMPILE)) {
                recompile = true;
                sps.revalidate(lcc);
                continue;
            }
            spsActivation.close();
            throw e;
        }
        /* Done with execution without any recompiles */
        return whenClauseWasTrue;
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ExecPreparedStatement(org.apache.derby.iapi.sql.execute.ExecPreparedStatement) ResultSet(org.apache.derby.iapi.sql.ResultSet) CursorResultSet(org.apache.derby.iapi.sql.execute.CursorResultSet) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) Activation(org.apache.derby.iapi.sql.Activation) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) SQLBoolean(org.apache.derby.iapi.types.SQLBoolean) StatementContext(org.apache.derby.iapi.sql.conn.StatementContext)

Aggregations

SQLBoolean (org.apache.derby.iapi.types.SQLBoolean)11 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)7 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)5 UserType (org.apache.derby.iapi.types.UserType)5 SQLVarchar (org.apache.derby.iapi.types.SQLVarchar)4 SQLChar (org.apache.derby.iapi.types.SQLChar)3 Timestamp (java.sql.Timestamp)2 UUID (org.apache.derby.catalog.UUID)2 ExecPreparedStatement (org.apache.derby.iapi.sql.execute.ExecPreparedStatement)2 SQLInteger (org.apache.derby.iapi.types.SQLInteger)2 AliasInfo (org.apache.derby.catalog.AliasInfo)1 IndexDescriptor (org.apache.derby.catalog.IndexDescriptor)1 ReferencedColumns (org.apache.derby.catalog.ReferencedColumns)1 ReferencedColumnsDescriptorImpl (org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl)1 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)1 Activation (org.apache.derby.iapi.sql.Activation)1 ResultSet (org.apache.derby.iapi.sql.ResultSet)1 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)1 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)1 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)1