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;
}
}
use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.
the class BooleanConstantNode method eliminateNots.
/**
* Eliminate NotNodes in the current query block. We traverse the tree,
* inverting ANDs and ORs and eliminating NOTs as we go. We stop at
* ComparisonOperators and boolean expressions. We invert
* ComparisonOperators and replace boolean expressions with
* boolean expression = false.
* NOTE: Since we do not recurse under ComparisonOperators, there
* still could be NotNodes left in the tree.
*
* @param underNotNode Whether or not we are under a NotNode.
*
* @return The modified expression
*/
@Override
ValueNode eliminateNots(boolean underNotNode) {
if (!underNotNode) {
return this;
}
booleanValue = !booleanValue;
super.setValue(new SQLBoolean(booleanValue));
return this;
}
use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.
the class SYSALIASESRowFactory method makeRow.
// ///////////////////////////////////////////////////////////////////////////
//
// METHODS
//
// ///////////////////////////////////////////////////////////////////////////
/**
* Make a SYSALIASES row
*
* @return Row suitable for inserting into SYSALIASES.
*
* @exception StandardException thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
DataValueDescriptor col;
String schemaID = null;
String javaClassName = null;
String sAliasType = null;
String aliasID = null;
String aliasName = null;
String specificName = null;
char cAliasType = AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR;
char cNameSpace = AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR;
boolean systemAlias = false;
AliasInfo aliasInfo = null;
if (td != null) {
AliasDescriptor ad = (AliasDescriptor) td;
aliasID = ad.getUUID().toString();
aliasName = ad.getDescriptorName();
schemaID = ad.getSchemaUUID().toString();
javaClassName = ad.getJavaClassName();
cAliasType = ad.getAliasType();
cNameSpace = ad.getNameSpace();
systemAlias = ad.getSystemAlias();
aliasInfo = ad.getAliasInfo();
specificName = ad.getSpecificName();
char[] charArray = new char[1];
charArray[0] = cAliasType;
sAliasType = new String(charArray);
if (SanityManager.DEBUG) {
switch(cAliasType) {
case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
case AliasInfo.ALIAS_TYPE_UDT_AS_CHAR:
case AliasInfo.ALIAS_TYPE_AGGREGATE_AS_CHAR:
break;
default:
SanityManager.THROWASSERT("Unexpected value (" + cAliasType + ") for aliasType");
}
}
}
/* Insert info into sysaliases */
/* RESOLVE - It would be nice to require less knowledge about sysaliases
* and have this be more table driven.
*/
/* Build the row to insert */
ExecRow row = getExecutionFactory().getValueRow(SYSALIASES_COLUMN_COUNT);
/* 1st column is ALIASID (UUID - char(36)) */
row.setColumn(SYSALIASES_ALIASID, new SQLChar(aliasID));
/* 2nd column is ALIAS (varchar(128))) */
row.setColumn(SYSALIASES_ALIAS, new SQLVarchar(aliasName));
// System.out.println(" added row-- " + aliasName);
/* 3rd column is SCHEMAID (UUID - char(36)) */
row.setColumn(SYSALIASES_SCHEMAID, new SQLChar(schemaID));
/* 4th column is JAVACLASSNAME (longvarchar) */
row.setColumn(SYSALIASES_JAVACLASSNAME, dvf.getLongvarcharDataValue(javaClassName));
/* 5th column is ALIASTYPE (char(1)) */
row.setColumn(SYSALIASES_ALIASTYPE, new SQLChar(sAliasType));
/* 6th column is NAMESPACE (char(1)) */
String sNameSpace = new String(new char[] { cNameSpace });
row.setColumn(SYSALIASES_NAMESPACE, new SQLChar(sNameSpace));
/* 7th column is SYSTEMALIAS (boolean) */
row.setColumn(SYSALIASES_SYSTEMALIAS, new SQLBoolean(systemAlias));
/* 8th column is ALIASINFO (org.apache.derby.catalog.AliasInfo) */
row.setColumn(SYSALIASES_ALIASINFO, new UserType(aliasInfo));
/* 9th column is specific name */
row.setColumn(SYSALIASES_SPECIFIC_NAME, new SQLVarchar(specificName));
return row;
}
use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.
the class SYSCONGLOMERATESRowFactory method makeRow.
/**
* Make a SYSCONGLOMERATES row
*
* @return Row suitable for inserting into SYSCONGLOMERATES.
*
* @exception StandardException thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
ExecRow row;
DataValueDescriptor col;
String tabID = null;
Long conglomNumber = null;
String conglomName = null;
Boolean supportsIndex = null;
IndexRowGenerator indexRowGenerator = null;
Boolean supportsConstraint = null;
String conglomUUIDString = null;
String schemaID = null;
ConglomerateDescriptor conglomerate = (ConglomerateDescriptor) td;
if (td != null) {
/* Sometimes the SchemaDescriptor is non-null and sometimes it
* is null. (We can't just rely on getting the schema id from
* the ConglomerateDescriptor because it can be null when
* we are creating a new conglomerate.
*/
if (parent != null) {
SchemaDescriptor sd = (SchemaDescriptor) parent;
schemaID = sd.getUUID().toString();
} else {
schemaID = conglomerate.getSchemaID().toString();
}
tabID = conglomerate.getTableID().toString();
conglomNumber = conglomerate.getConglomerateNumber();
conglomName = conglomerate.getConglomerateName();
conglomUUIDString = conglomerate.getUUID().toString();
supportsIndex = conglomerate.isIndex();
indexRowGenerator = conglomerate.getIndexDescriptor();
supportsConstraint = conglomerate.isConstraint();
}
/* RESOLVE - It would be nice to require less knowledge about sysconglomerates
* and have this be more table driven.
*/
/* Build the row to insert */
row = getExecutionFactory().getValueRow(SYSCONGLOMERATES_COLUMN_COUNT);
/* 1st column is SCHEMAID (UUID - char(36)) */
row.setColumn(1, new SQLChar(schemaID));
/* 2nd column is TABLEID (UUID - char(36)) */
row.setColumn(2, new SQLChar(tabID));
/* 3rd column is CONGLOMERATENUMBER (long) */
row.setColumn(3, new SQLLongint(conglomNumber));
/* 4th column is CONGLOMERATENAME (varchar(128))
** If null, use the tableid so we always
** have a unique column
*/
row.setColumn(4, (conglomName == null) ? new SQLVarchar(tabID) : new SQLVarchar(conglomName));
/* 5th column is ISINDEX (boolean) */
row.setColumn(5, new SQLBoolean(supportsIndex));
/* 6th column is DESCRIPTOR
* (user type org.apache.derby.catalog.IndexDescriptor)
*/
row.setColumn(6, new UserType((indexRowGenerator == null ? (IndexDescriptor) null : indexRowGenerator.getIndexDescriptor())));
/* 7th column is ISCONSTRAINT (boolean) */
row.setColumn(7, new SQLBoolean(supportsConstraint));
/* 8th column is CONGLOMERATEID (UUID - char(36)) */
row.setColumn(8, new SQLChar(conglomUUIDString));
return row;
}
use of org.apache.derby.iapi.types.SQLBoolean in project derby by apache.
the class SYSSTATEMENTSRowFactory method makeSYSSTATEMENTSrow.
// ///////////////////////////////////////////////////////////////////////////
//
// METHODS
//
// ///////////////////////////////////////////////////////////////////////////
/**
* Make a SYSSTATEMENTS row.
* <p>
* <B>WARNING</B>: When empty row is true, this method takes
* a snapshot of the SPSD and creates a row. It is imperative
* that that row remain consistent with the descriptor (the
* valid and StorablePreparedStatement fields must be in sync).
* If this row is to be written out and valid is true, then
* this call and the insert should be synchronized on the
* SPSD. This method has <B>NO</B> synchronization.
*
* @param compileMe passed into SPSDescriptorImpl.getPreparedStatement().
* if true, we (re)compile the stmt
* @param spsDescriptor In-memory tuple to be converted to a disk row.
*
* @return Row suitable for inserting into SYSSTATEMENTS.
*
* @exception StandardException thrown on failure
*/
public ExecRow makeSYSSTATEMENTSrow(boolean compileMe, SPSDescriptor spsDescriptor) throws StandardException {
DataTypeDescriptor dtd;
ExecRow row;
DataValueDescriptor col;
String name = null;
UUID uuid = null;
String uuidStr = null;
// schema
String suuidStr = null;
// compilation schema
String compUuidStr = null;
String text = null;
String usingText = null;
ExecPreparedStatement preparedStatement = null;
String typeStr = null;
boolean valid = true;
Timestamp time = null;
boolean initiallyCompilable = true;
if (spsDescriptor != null) {
name = spsDescriptor.getName();
uuid = spsDescriptor.getUUID();
suuidStr = spsDescriptor.getSchemaDescriptor().getUUID().toString();
uuidStr = uuid.toString();
text = spsDescriptor.getText();
valid = spsDescriptor.isValid();
time = spsDescriptor.getCompileTime();
typeStr = spsDescriptor.getTypeAsString();
initiallyCompilable = spsDescriptor.initiallyCompilable();
preparedStatement = spsDescriptor.getPreparedStatement(compileMe);
compUuidStr = (spsDescriptor.getCompSchemaId() != null) ? spsDescriptor.getCompSchemaId().toString() : null;
usingText = spsDescriptor.getUsingText();
}
/* Build the row to insert */
row = getExecutionFactory().getValueRow(SYSSTATEMENTS_COLUMN_COUNT);
/* 1st column is STMTID */
row.setColumn(1, new SQLChar(uuidStr));
/* 2nd column is STMTNAME */
row.setColumn(2, new SQLVarchar(name));
/* 3rd column is SCHEMAID */
row.setColumn(3, new SQLChar(suuidStr));
/* 4th column is TYPE */
row.setColumn(4, new SQLChar(typeStr));
/* 5th column is VALID */
row.setColumn(5, new SQLBoolean(valid));
/* 6th column is TEXT */
row.setColumn(6, dvf.getLongvarcharDataValue(text));
/* 7th column is LASTCOMPILED */
row.setColumn(7, new SQLTimestamp(time));
/* 8th column is COMPILATIONSCHEMAID */
row.setColumn(8, new SQLChar(compUuidStr));
/* 9th column is USINGTEXT */
row.setColumn(9, dvf.getLongvarcharDataValue(usingText));
/*
** 10th column is CONSTANTSTATE
**
** CONSTANTSTATE is really a formatable StorablePreparedStatement.
*/
row.setColumn(10, new UserType(preparedStatement));
/* 11th column is INITIALLY_COMPILABLE */
row.setColumn(11, new SQLBoolean(initiallyCompilable));
return row;
}
Aggregations