use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.
the class DefaultNode method parseDefault.
/**
* Parse a default and turn it into a query tree.
*
* @param defaultText Text of Default.
* @param lcc LanguageConnectionContext
* @param cc CompilerContext
*
* @return The parsed default as a query tree.
*
* @exception StandardException Thrown on failure
*/
public static ValueNode parseDefault(String defaultText, LanguageConnectionContext lcc, CompilerContext cc) throws StandardException {
Parser p;
ValueNode defaultTree;
/* Get a Statement to pass to the parser */
/* We're all set up to parse. We have to build a compilable SQL statement
* before we can parse - So, we goober up a VALUES defaultText.
*/
String values = "VALUES " + defaultText;
/*
** Get a new compiler context, so the parsing of the select statement
** doesn't mess up anything in the current context (it could clobber
** the ParameterValueSet, for example).
*/
CompilerContext newCC = lcc.pushCompilerContext();
p = newCC.getParser();
/* Finally, we can call the parser */
// Since this is always nested inside another SQL statement, so topLevel flag
// should be false
Visitable qt = p.parseStatement(values);
if (SanityManager.DEBUG) {
if (!(qt instanceof CursorNode)) {
SanityManager.THROWASSERT("qt expected to be instanceof CursorNode, not " + qt.getClass().getName());
}
CursorNode cn = (CursorNode) qt;
if (!(cn.getResultSetNode() instanceof RowResultSetNode)) {
SanityManager.THROWASSERT("cn.getResultSetNode() expected to be instanceof RowResultSetNode, not " + cn.getResultSetNode().getClass().getName());
}
}
defaultTree = ((CursorNode) qt).getResultSetNode().getResultColumns().elementAt(0).getExpression();
lcc.popCompilerContext(newCC);
return defaultTree;
}
use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.
the class DropIndexNode method bindStatement.
/**
* Bind this DropIndexNode. This means looking up the index,
* verifying it exists and getting the conglomerate number.
*
* @exception StandardException Thrown on error
*/
@Override
public void bindStatement() throws StandardException {
CompilerContext cc = getCompilerContext();
DataDictionary dd = getDataDictionary();
SchemaDescriptor sd;
sd = getSchemaDescriptor();
if (sd.getUUID() != null)
cd = dd.getConglomerateDescriptor(getRelativeName(), sd, false);
if (cd == null) {
throw StandardException.newException(SQLState.LANG_INDEX_NOT_FOUND, getFullName());
}
/* Get the table descriptor */
td = getTableDescriptor(cd.getTableID());
/* Drop index is not allowed on an index backing a constraint -
* user must drop the constraint, which will drop the index.
* Drop constraint drops the constraint before the index,
* so it's okay to drop a backing index if we can't find its
* ConstraintDescriptor.
*/
if (cd.isConstraint()) {
ConstraintDescriptor conDesc;
String constraintName;
conDesc = dd.getConstraintDescriptor(td, cd.getUUID());
if (conDesc != null) {
constraintName = conDesc.getConstraintName();
throw StandardException.newException(SQLState.LANG_CANT_DROP_BACKING_INDEX, getFullName(), constraintName);
}
}
/* Statement is dependent on the TableDescriptor and ConglomerateDescriptor */
cc.createDependency(td);
cc.createDependency(cd);
}
use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.
the class FromList method bindTables.
void bindTables(DataDictionary dataDictionary, FromList fromListParam) throws StandardException {
//
// Don't add USAGE privilege on user-defined types just because we're
// binding tables.
//
boolean wasSkippingTypePrivileges = getCompilerContext().skipTypePrivileges(true);
FromTable fromTable;
/* Now we bind the tables - this is a 2 step process.
* We first bind all of the non-VTIs, then we bind the VTIs.
* This enables us to handle the passing of correlation
* columns in VTI parameters.
* NOTE: We set the table numbers for all of the VTIs in the
* first step, when we find them, in order to avoid an ordering
* problem with join columns in parameters.
*/
int size = size();
for (int index = 0; index < size; index++) {
fromTable = (FromTable) elementAt(index);
FromTable newNode = (FromTable) fromTable.bindNonVTITables(dataDictionary, fromListParam);
// was referencing a SESSION schema object.
if (fromTable.referencesSessionSchema()) {
referencesSessionSchema = true;
}
newNode.setMergeTableID(fromTable.getMergeTableID());
setElementAt(newNode, index);
}
for (int index = 0; index < size; index++) {
fromTable = (FromTable) elementAt(index);
FromTable newNode = (FromTable) fromTable.bindVTITables(fromListParam);
if (fromTable.referencesSessionSchema()) {
referencesSessionSchema = true;
}
newNode.setMergeTableID(fromTable.getMergeTableID());
setElementAt(newNode, index);
}
// DERBY-4191: We must have some SELECT privilege on every table
// that we read from, even if we don't actually read any column
// values from it (for example if we do SELECT COUNT(*) FROM T).
// We ask for MIN_SELECT_PRIV requirement of the first column in
// the table. The first column is just a place holder. What we
// really do at execution time when we see we are looking for
// MIN_SELECT_PRIV privilege is as follows:
//
// 1) We will look for SELECT privilege at table level.
// 2) If not found, we will look for SELECT privilege on
// ANY column, not necessarily the first column. But since
// the constructor for column privilege requires us to pass
// a column descriptor, we just choose the first column for
// MIN_SELECT_PRIV requirement.
final CompilerContext cc = getCompilerContext();
cc.pushCurrentPrivType(Authorizer.MIN_SELECT_PRIV);
for (int index = 0; index < size; index++) {
fromTable = (FromTable) elementAt(index);
if (fromTable.isPrivilegeCollectionRequired() && fromTable.isBaseTable() && !fromTable.forUpdate()) {
// This is a base table in the FROM list of a SELECT statement.
// Make sure we check for minimum SELECT privilege on it.
cc.addRequiredColumnPriv(fromTable.getTableDescriptor().getColumnDescriptor(1));
}
}
cc.popCurrentPrivType();
getCompilerContext().skipTypePrivileges(wasSkippingTypePrivileges);
}
use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.
the class FromSubquery method bindExpressions.
/**
* Bind the expressions in this FromSubquery. This means
* binding the sub-expressions, as well as figuring out what the return
* type is for each expression.
*
* @exception StandardException Thrown on error
*/
@Override
void bindExpressions(FromList fromListParam) throws StandardException {
FromList emptyFromList = new FromList(getOptimizerFactory().doJoinOrderOptimization(), getContextManager());
ResultColumnList derivedRCL = getResultColumns();
ResultColumnList subqueryRCL;
FromList nestedFromList;
/* From subqueries cannot be correlated, so we pass an empty FromList
* to subquery.bindExpressions() and .bindResultColumns()
*/
if (orderByList != null) {
orderByList.pullUpOrderByColumns(subquery);
}
nestedFromList = emptyFromList;
CompilerContext compilerContext = getCompilerContext();
if (origCompilationSchema != null) {
// View expansion needs the definition time schema
compilerContext.pushCompilationSchema(origCompilationSchema);
}
// Nested VTI/tableFunctions will want to know whether their arguments
// reference tables in the FROM list which contains this subquery. Those
// references are illegal. See DERBY-5554 and DERBY-5779.
CollectNodesVisitor<FromVTI> nestedVTIs = new CollectNodesVisitor<FromVTI>(FromVTI.class);
subquery.accept(nestedVTIs);
for (FromVTI ref : nestedVTIs.getList()) {
ref.addOuterFromList(fromListParam);
}
try {
subquery.bindExpressions(nestedFromList);
subquery.bindResultColumns(nestedFromList);
} finally {
if (origCompilationSchema != null) {
compilerContext.popCompilationSchema();
}
}
if (orderByList != null) {
orderByList.bindOrderByColumns(subquery);
}
bindOffsetFetch(offset, fetchFirst);
/* NOTE: If the size of the derived column list is less than
* the size of the subquery's RCL and the derived column list is marked
* for allowing a size mismatch, then we have a select * view
* on top of a table that has had columns added to it via alter table.
* In this case, we trim out the columns that have been added to
* the table since the view was created.
*/
subqueryRCL = subquery.getResultColumns();
if (getResultColumns() != null && getResultColumns().getCountMismatchAllowed() && getResultColumns().size() < subqueryRCL.size()) {
for (int index = subqueryRCL.size() - 1; index >= getResultColumns().size(); index--) {
subqueryRCL.removeElementAt(index);
}
}
/*
* Create RCL based on subquery, adding a level of VCNs.
*/
ResultColumnList newRcl = subqueryRCL.copyListAndObjects();
newRcl.genVirtualColumnNodes(subquery, subquery.getResultColumns());
setResultColumns(newRcl);
/* Propagate the name info from the derived column list */
if (derivedRCL != null) {
getResultColumns().propagateDCLInfo(derivedRCL, correlationName);
}
}
use of org.apache.derby.iapi.sql.compile.CompilerContext in project derby by apache.
the class LockTableNode method bindStatement.
/**
* Bind this LockTableNode. This means looking up the table,
* verifying it exists and getting the heap conglomerate number.
*
* @exception StandardException Thrown on error
*/
@Override
public void bindStatement() throws StandardException {
CompilerContext cc = getCompilerContext();
ConglomerateDescriptor cd;
SchemaDescriptor sd;
String schemaName = tableName.getSchemaName();
sd = getSchemaDescriptor(schemaName);
// Users are not allowed to lock system tables
if (sd.isSystemSchema()) {
throw StandardException.newException(SQLState.LANG_NO_USER_DDL_IN_SYSTEM_SCHEMA, statementToString(), schemaName);
}
lockTableDescriptor = getTableDescriptor(tableName.getTableName(), sd);
if (lockTableDescriptor == null) {
// Check if the reference is for a synonym.
TableName synonymTab = resolveTableToSynonym(tableName);
if (synonymTab == null)
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, tableName);
tableName = synonymTab;
sd = getSchemaDescriptor(tableName.getSchemaName());
lockTableDescriptor = getTableDescriptor(synonymTab.getTableName(), sd);
if (lockTableDescriptor == null)
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, tableName);
}
// throw an exception if user is attempting to lock a temporary table
if (lockTableDescriptor.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE);
}
conglomerateNumber = lockTableDescriptor.getHeapConglomerateId();
/* Get the base conglomerate descriptor */
cd = lockTableDescriptor.getConglomerateDescriptor(conglomerateNumber);
/* Statement is dependent on the TableDescriptor and ConglomerateDescriptor */
cc.createDependency(lockTableDescriptor);
cc.createDependency(cd);
if (isPrivilegeCollectionRequired()) {
// need SELECT privilege to perform lock table statement.
cc.pushCurrentPrivType(Authorizer.SELECT_PRIV);
cc.addRequiredTablePriv(lockTableDescriptor);
cc.popCurrentPrivType();
}
}
Aggregations