Search in sources :

Example 1 with ViewDescriptor

use of org.apache.derby.iapi.sql.dictionary.ViewDescriptor in project derby by apache.

the class DataDictionaryImpl method getViewDescriptorScan.

/**
 * Get the information for the view from sys.sysviews.
 *
 * @param tdi					The TableDescriptor for the view.
 *
 * @return ViewDescriptor	The ViewDescriptor for the view.
 *
 * @exception StandardException		Thrown on error
 */
private ViewDescriptor getViewDescriptorScan(TableDescriptor tdi) throws StandardException {
    ViewDescriptor vd;
    DataValueDescriptor viewIdOrderable;
    TabInfoImpl ti = getNonCoreTI(SYSVIEWS_CATALOG_NUM);
    UUID viewID = tdi.getUUID();
    /* Use viewIdOrderable in both start 
		 * and stop position for scan. 
		 */
    viewIdOrderable = getIDValueAsCHAR(viewID);
    /* Set up the start/stop position for the scan */
    ExecIndexRow keyRow = exFactory.getIndexableRow(1);
    keyRow.setColumn(1, viewIdOrderable);
    vd = getDescriptorViaIndex(SYSVIEWSRowFactory.SYSVIEWS_INDEX1_ID, keyRow, (ScanQualifier[][]) null, ti, (TupleDescriptor) null, (List<TupleDescriptor>) null, ViewDescriptor.class, false);
    if (vd != null) {
        vd.setViewName(tdi.getName());
    }
    return vd;
}
Also used : TupleDescriptor(org.apache.derby.iapi.sql.dictionary.TupleDescriptor) ScanQualifier(org.apache.derby.iapi.sql.execute.ScanQualifier) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ConglomerateDescriptorList(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptorList) ArrayList(java.util.ArrayList) TriggerDescriptorList(org.apache.derby.iapi.sql.dictionary.TriggerDescriptorList) List(java.util.List) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) LinkedList(java.util.LinkedList) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) UUID(org.apache.derby.catalog.UUID) ExecIndexRow(org.apache.derby.iapi.sql.execute.ExecIndexRow) ViewDescriptor(org.apache.derby.iapi.sql.dictionary.ViewDescriptor)

Example 2 with ViewDescriptor

use of org.apache.derby.iapi.sql.dictionary.ViewDescriptor in project derby by apache.

the class SYSVIEWSRowFactory method buildDescriptor.

// /////////////////////////////////////////////////////////////////////////
// 
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
// 
// /////////////////////////////////////////////////////////////////////////
/**
 * Make a ViewDescriptor out of a SYSVIEWS row
 *
 * @param row a SYSVIEWS row
 * @param parentTupleDescriptor	Null for this kind of descriptor.
 * @param dd dataDictionary
 *
 * @exception   StandardException thrown on failure
 */
public TupleDescriptor buildDescriptor(ExecRow row, TupleDescriptor parentTupleDescriptor, DataDictionary dd) throws StandardException {
    ViewDescriptor vd = null;
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(row.nColumns() == SYSVIEWS_COLUMN_COUNT, "Wrong number of columns for a SYSVIEWS row");
    }
    DataValueDescriptor col;
    DataDescriptorGenerator ddg;
    int checkIType;
    String checkSType;
    String tableID;
    String compSchemaId;
    String viewDefinition;
    UUID tableUUID;
    UUID compSchemaUUID = null;
    ddg = dd.getDataDescriptorGenerator();
    /* 1st column is TABLEID (UUID - char(36)) */
    col = row.getColumn(SYSVIEWS_TABLEID);
    tableID = col.getString();
    tableUUID = getUUIDFactory().recreateUUID(tableID);
    /* 2nd column is VIEWDEFINITION */
    col = row.getColumn(SYSVIEWS_VIEWDEFINITION);
    viewDefinition = col.getString();
    /* 3rd column is CHECKOPTION (char(1)) */
    col = row.getColumn(SYSVIEWS_CHECKOPTION);
    checkSType = col.getString();
    if (SanityManager.DEBUG) {
        if (!checkSType.equals("N")) {
            SanityManager.THROWASSERT("checkSType expected to be 'N', not " + checkSType);
        }
    }
    /* RESOLVE - no check options for now */
    checkIType = ViewDescriptor.NO_CHECK_OPTION;
    /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */
    col = row.getColumn(SYSVIEWS_COMPILATION_SCHEMAID);
    compSchemaId = col.getString();
    if (compSchemaId != null) {
        compSchemaUUID = getUUIDFactory().recreateUUID(compSchemaId);
    }
    /* now build and return the descriptor */
    vd = ddg.newViewDescriptor(tableUUID, null, viewDefinition, checkIType, compSchemaUUID);
    return vd;
}
Also used : DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) UUID(org.apache.derby.catalog.UUID) ViewDescriptor(org.apache.derby.iapi.sql.dictionary.ViewDescriptor)

Example 3 with ViewDescriptor

use of org.apache.derby.iapi.sql.dictionary.ViewDescriptor in project derby by apache.

the class SYSVIEWSRowFactory method makeRow.

// ///////////////////////////////////////////////////////////////////////////
// 
// METHODS
// 
// ///////////////////////////////////////////////////////////////////////////
/**
 * Make a SYSVIEWS row
 *
 * @return	Row suitable for inserting into SYSVIEWS.
 *
 * @exception   StandardException thrown on failure
 */
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
    DataValueDescriptor col;
    ExecRow row;
    String tableID = null;
    String compSchemaId = null;
    String viewText = null;
    String checkSType = null;
    int checkIType;
    if (td != null) {
        UUID tableUUID;
        ViewDescriptor vd = (ViewDescriptor) td;
        /*
			** We only allocate a new UUID if the descriptor doesn't already have one.
			** For descriptors replicated from a Source system, we already have an UUID.
			*/
        tableUUID = vd.getUUID();
        if (tableUUID == null) {
            tableUUID = getUUIDFactory().createUUID();
            vd.setUUID(tableUUID);
        }
        tableID = tableUUID.toString();
        viewText = vd.getViewText();
        /* RESOLVE - check constraints not supported yet */
        checkIType = vd.getCheckOptionType();
        if (SanityManager.DEBUG) {
            if (checkIType != ViewDescriptor.NO_CHECK_OPTION) {
                SanityManager.THROWASSERT("checkIType expected to be " + ViewDescriptor.NO_CHECK_OPTION + ", not " + checkIType);
            }
        }
        checkSType = "N";
        UUID tmpId = vd.getCompSchemaId();
        compSchemaId = (tmpId == null) ? null : tmpId.toString();
    }
    /* Insert info into sysviews */
    /* RESOLVE - It would be nice to require less knowledge about sysviews
		 * and have this be more table driven.
		 */
    /* Build the row to insert  */
    row = getExecutionFactory().getValueRow(SYSVIEWS_COLUMN_COUNT);
    /* 1st column is TABLEID (UUID - char(36)) */
    row.setColumn(SYSVIEWS_TABLEID, new SQLChar(tableID));
    /* 2nd column is VIEWDEFINITION */
    row.setColumn(SYSVIEWS_VIEWDEFINITION, dvf.getLongvarcharDataValue(viewText));
    /* 3rd column is CHECKOPTION (char(1)) */
    row.setColumn(SYSVIEWS_CHECKOPTION, new SQLChar(checkSType));
    /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */
    row.setColumn(SYSVIEWS_COMPILATION_SCHEMAID, new SQLChar(compSchemaId));
    return row;
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) SQLChar(org.apache.derby.iapi.types.SQLChar) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) UUID(org.apache.derby.catalog.UUID) ViewDescriptor(org.apache.derby.iapi.sql.dictionary.ViewDescriptor)

Example 4 with ViewDescriptor

use of org.apache.derby.iapi.sql.dictionary.ViewDescriptor in project derby by apache.

the class FromBaseTable method bindNonVTITables.

/**
 * Bind the table in this FromBaseTable.
 * This is where view resolution occurs
 *
 * @param dataDictionary	The DataDictionary to use for binding
 * @param fromListParam		FromList to use/append to.
 *
 * @return	ResultSetNode	The FromTable for the table or resolved view.
 *
 * @exception StandardException		Thrown on error
 */
@Override
ResultSetNode bindNonVTITables(DataDictionary dataDictionary, FromList fromListParam) throws StandardException {
    tableName.bind();
    TableDescriptor tabDescr = bindTableDescriptor();
    if (tabDescr.getTableType() == TableDescriptor.VTI_TYPE) {
        ResultSetNode vtiNode = mapTableAsVTI(tabDescr, getCorrelationName(), getResultColumns(), getProperties(), getContextManager());
        return vtiNode.bindNonVTITables(dataDictionary, fromListParam);
    }
    ResultColumnList derivedRCL = getResultColumns();
    // make sure there's a restriction list
    restrictionList = new PredicateList(getContextManager());
    baseTableRestrictionList = new PredicateList(getContextManager());
    CompilerContext compilerContext = getCompilerContext();
    /* Generate the ResultColumnList */
    setResultColumns(genResultColList());
    templateColumns = getResultColumns();
    /* Resolve the view, if this is a view */
    if (tabDescr.getTableType() == TableDescriptor.VIEW_TYPE) {
        FromSubquery fsq;
        ResultSetNode rsn;
        ViewDescriptor vd;
        CreateViewNode cvn;
        SchemaDescriptor compSchema;
        /* Get the associated ViewDescriptor so that we can get 
			 * the view definition text.
			 */
        vd = dataDictionary.getViewDescriptor(tabDescr);
        /*
			** Set the default compilation schema to be whatever
			** this schema this view was originally compiled against.
			** That way we pick up the same tables no matter what
			** schema we are running against.
			*/
        compSchema = dataDictionary.getSchemaDescriptor(vd.getCompSchemaId(), null);
        compilerContext.pushCompilationSchema(compSchema);
        try {
            /* This represents a view - query is dependent on the ViewDescriptor */
            compilerContext.createDependency(vd);
            cvn = (CreateViewNode) parseStatement(vd.getViewText(), false);
            rsn = cvn.getParsedQueryExpression();
            /* If the view contains a '*' then we mark the views derived column list
				 * so that the view will still work, and return the expected results,
				 * if any of the tables referenced in the view have columns added to
				 * them via ALTER TABLE.  The expected results means that the view
				 * will always return the same # of columns.
				 */
            if (rsn.getResultColumns().containsAllResultColumn()) {
                getResultColumns().setCountMismatchAllowed(true);
            }
            // checking.
            for (ResultColumn rc : getResultColumns()) {
                if (isPrivilegeCollectionRequired()) {
                    compilerContext.addRequiredColumnPriv(rc.getTableColumnDescriptor());
                }
            }
            fsq = new FromSubquery(rsn, cvn.getOrderByList(), cvn.getOffset(), cvn.getFetchFirst(), cvn.hasJDBClimitClause(), (correlationName != null) ? correlationName : getOrigTableName().getTableName(), getResultColumns(), tableProperties, getContextManager());
            // Transfer the nesting level to the new FromSubquery
            fsq.setLevel(level);
            // We are getting ready to bind the query underneath the view. Since
            // that query is going to run with definer's privileges, we do not
            // need to collect any privilege requirement for that query.
            // Following call is marking the query to run with definer
            // privileges. This marking will make sure that we do not collect
            // any privilege requirement for it.
            CollectNodesVisitor<QueryTreeNode> cnv = new CollectNodesVisitor<QueryTreeNode>(QueryTreeNode.class);
            fsq.accept(cnv);
            for (QueryTreeNode node : cnv.getList()) {
                node.disablePrivilegeCollection();
            }
            fsq.setOrigTableName(this.getOrigTableName());
            // since we reset the compilation schema when we return, we
            // need to save it for use when we bind expressions:
            fsq.setOrigCompilationSchema(compSchema);
            ResultSetNode fsqBound = fsq.bindNonVTITables(dataDictionary, fromListParam);
            /* Do error checking on derived column list and update "exposed"
				 * column names if valid.
				 */
            if (derivedRCL != null) {
                fsqBound.getResultColumns().propagateDCLInfo(derivedRCL, origTableName.getFullTableName());
            }
            return fsqBound;
        } finally {
            compilerContext.popCompilationSchema();
        }
    } else {
        /* This represents a table - query is dependent on the TableDescriptor */
        compilerContext.createDependency(tabDescr);
        /* Get the base conglomerate descriptor */
        baseConglomerateDescriptor = tabDescr.getConglomerateDescriptor(tabDescr.getHeapConglomerateId());
        // probably doesn't exist anymore.
        if (baseConglomerateDescriptor == null) {
            throw StandardException.newException(SQLState.STORE_CONGLOMERATE_DOES_NOT_EXIST, Long.valueOf(tabDescr.getHeapConglomerateId()));
        }
        /* Build the 0-based array of base column names. */
        columnNames = getResultColumns().getColumnNames();
        /* Do error checking on derived column list and update "exposed"
			 * column names if valid.
			 */
        if (derivedRCL != null) {
            getResultColumns().propagateDCLInfo(derivedRCL, origTableName.getFullTableName());
        }
        /* Assign the tableNumber */
        if (// allow re-bind, in which case use old number
        tableNumber == -1)
            tableNumber = compilerContext.getNextTableNumber();
    }
    // 
    // Only the DBO can select from SYS.SYSUSERS.
    // 
    authorizeSYSUSERS = dataDictionary.usesSqlAuthorization() && tabDescr.getUUID().toString().equals(SYSUSERSRowFactory.SYSUSERS_UUID);
    if (authorizeSYSUSERS) {
        String databaseOwner = dataDictionary.getAuthorizationDatabaseOwner();
        String currentUser = getLanguageConnectionContext().getStatementContext().getSQLSessionContext().getCurrentUser();
        if (!databaseOwner.equals(currentUser)) {
            throw StandardException.newException(SQLState.DBO_ONLY);
        }
    }
    return this;
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) ViewDescriptor(org.apache.derby.iapi.sql.dictionary.ViewDescriptor) OptimizablePredicateList(org.apache.derby.iapi.sql.compile.OptimizablePredicateList)

Example 5 with ViewDescriptor

use of org.apache.derby.iapi.sql.dictionary.ViewDescriptor in project derby by apache.

the class CreateViewConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for CREATE VIEW.
 *
 *	@see ConstantAction#executeConstantAction
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    TableDescriptor td;
    UUID toid;
    ColumnDescriptor columnDescriptor;
    ViewDescriptor vd;
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    /*
		** Inform the data dictionary that we are about to write to it.
		** There are several calls to data dictionary "get" methods here
		** that might be done in "read" mode in the data dictionary, but
		** it seemed safer to do this whole operation in "write" mode.
		**
		** We tell the data dictionary we're done writing at the end of
		** the transaction.
		*/
    dd.startWriting(lcc);
    SchemaDescriptor sd = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, schemaName);
    /* Create a new table descriptor.
		 * (Pass in row locking, even though meaningless for views.)
		 */
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    td = ddg.newTableDescriptor(tableName, sd, tableType, TableDescriptor.ROW_LOCK_GRANULARITY);
    dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc);
    toid = td.getUUID();
    // for each column, stuff system.column
    ColumnDescriptor[] cdlArray = new ColumnDescriptor[columnInfo.length];
    int index = 1;
    for (int ix = 0; ix < columnInfo.length; ix++) {
        columnDescriptor = new ColumnDescriptor(columnInfo[ix].name, index++, columnInfo[ix].dataType, columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, (UUID) null, columnInfo[ix].autoincStart, columnInfo[ix].autoincInc, columnInfo[ix].autoincCycle);
        cdlArray[ix] = columnDescriptor;
    }
    dd.addDescriptorArray(cdlArray, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
    // add columns to the column descriptor list.
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    for (int i = 0; i < cdlArray.length; i++) cdl.add(cdlArray[i]);
    /* Get and add a view descriptor */
    vd = ddg.newViewDescriptor(toid, tableName, viewText, checkOption, (compSchemaId == null) ? lcc.getDefaultSchema().getUUID() : compSchemaId);
    for (int ix = 0; ix < providerInfo.length; ix++) {
        /* We should always be able to find the Provider */
        Provider provider = (Provider) providerInfo[ix].getDependableFinder().getDependable(dd, providerInfo[ix].getObjectId());
        dm.addDependency(vd, provider, lcc.getContextManager());
    }
    // store view's dependency on various privileges in the dependeny system
    storeViewTriggerDependenciesOnPrivileges(activation, vd);
    dd.addDescriptor(vd, sd, DataDictionary.SYSVIEWS_CATALOG_NUM, true, tc);
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) ViewDescriptor(org.apache.derby.iapi.sql.dictionary.ViewDescriptor) Provider(org.apache.derby.iapi.sql.depend.Provider) DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) UUID(org.apache.derby.catalog.UUID) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Aggregations

ViewDescriptor (org.apache.derby.iapi.sql.dictionary.ViewDescriptor)9 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)6 UUID (org.apache.derby.catalog.UUID)4 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)3 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)3 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)3 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)2 Provider (org.apache.derby.iapi.sql.depend.Provider)2 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)2 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)2 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)2 TupleDescriptor (org.apache.derby.iapi.sql.dictionary.TupleDescriptor)2 TransactionController (org.apache.derby.iapi.store.access.TransactionController)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)1 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)1 OptimizablePredicateList (org.apache.derby.iapi.sql.compile.OptimizablePredicateList)1