Package org.apache.derby.iapi.sql.compile

Examples of org.apache.derby.iapi.sql.compile.CompilerContext


    GenericDescriptorList     tdl,
    Dependent          dependent
  )
    throws StandardException
  {
    CompilerContext       compilerContext = getCompilerContext();

    Enumeration descs = tdl.elements();

    while (descs.hasMoreElements())
    {
      TriggerDescriptor td = (TriggerDescriptor)descs.nextElement();

      /*
      ** The dependent now depends on this trigger.
      ** the default dependent is the statement
      ** being compiled.
      */
      if (dependent == null)
      {
        compilerContext.createDependency(td);
      }
      else
      {
        compilerContext.createDependency(dependent, td);
      }
    }
  }
View Full Code Here


    ConstraintDescriptorList   cdl,
    Dependent          dependent
  )
    throws StandardException
  {
    CompilerContext       compilerContext = getCompilerContext();

    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++)
    {
      ConstraintDescriptor cd = cdl.elementAt(index);

      /*
      ** The dependent now depends on this constraint.
      ** the default dependent is the statement
      ** being compiled.
      */
      if (dependent == null)
      {
        compilerContext.createDependency(cd);
      }
      else
      {
        compilerContext.createDependency(dependent, cd);
      }

      /*
      ** We are also dependent on all referencing keys --
      ** if one of them is deleted, we'll have to recompile.
      ** Also, if there is a BULK_INSERT on the table
      ** we are going to scan to validate the constraint,
      ** the index number will change, so we'll add a
      ** dependency on all tables we will scan.
      */
      if (cd instanceof ReferencedKeyConstraintDescriptor)
      { 
        ConstraintDescriptorList fkcdl = dd.getActiveConstraintDescriptors
          ( ((ReferencedKeyConstraintDescriptor)cd).getForeignKeyConstraints(ConstraintDescriptor.ENABLED) );
 
        int fklSize = fkcdl.size();
        for (int inner = 0; inner < fklSize; inner++)
        {
          ConstraintDescriptor fkcd = fkcdl.elementAt(inner);
          if (dependent == null)
          {
            compilerContext.createDependency(fkcd);
            compilerContext.createDependency(fkcd.getTableDescriptor());
          }
          else
          {
            compilerContext.createDependency(dependent, fkcd);
            compilerContext.createDependency(dependent, fkcd.getTableDescriptor());
          }
        }
      }
      else if (cd instanceof ForeignKeyConstraintDescriptor)
      {
        ForeignKeyConstraintDescriptor fkcd = (ForeignKeyConstraintDescriptor) cd;
        if (dependent == null)
        {
          compilerContext.createDependency(fkcd.getReferencedConstraint().getTableDescriptor());
        }
        else
        {
          compilerContext.createDependency(dependent,
                  fkcd.getReferencedConstraint().getTableDescriptor());
        }
      }
    }
  }
View Full Code Here

    throws StandardException
  {
    Parser            p;
    ValueNode          checkTree;
    LanguageConnectionContext  lcc = getLanguageConnectionContext();
    CompilerContext       compilerContext = getCompilerContext();

    /* Get a Statement to pass to the parser */

    /* We're all set up to parse. We have to build a compile SQL statement
     * before we can parse - we just have a WHERE clause right now.
     * So, we goober up a SELECT * FROM table WHERE checkDefs.
     */
    String select = "SELECT * FROM " +
                  td.getQualifiedName() +
                  " WHERE " +
                  checkConstraintText;
   
    /*
    ** 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
    StatementNode qt = p.parseStatement(select);
View Full Code Here

    )
    throws StandardException
  {
    ConglomerateDescriptor  cd;
    int            indexCount = affectedConglomerates.size();
    CompilerContext      cc = getCompilerContext();

    indicesToMaintain = new IndexRowGenerator[ indexCount ];
    indexConglomerateNumbers = new long[ indexCount ];
    indexNames = new String[indexCount];

    for ( int ictr = 0; ictr < indexCount; ictr++ )
    {
      cd = (ConglomerateDescriptor) affectedConglomerates.get( ictr );

      indicesToMaintain[ ictr ] = cd.getIndexDescriptor();
      indexConglomerateNumbers[ ictr ] = cd.getConglomerateNumber();
      indexNames[ictr] =
        ((cd.isConstraint()) ? null : cd.getConglomerateName());

      cc.createDependency(cd);
    }

  }
View Full Code Here

   *
   * @exception StandardException    Thrown on error
   */
  public void bindStatement() throws StandardException
  {
    CompilerContext compilerContext = getCompilerContext();
    DataDictionary  dd = getDataDictionary();
    /*
    ** Grab the current schema.  We will use that for
    ** sps compilation
    */
    LanguageConnectionContext lcc = getLanguageConnectionContext();
    compSchemaDescriptor = lcc.getDefaultSchema();

    /*
    ** Get and check the schema descriptor for this
    ** trigger.  This check will throw the proper exception
    ** if someone tries to create a trigger in the SYS
    ** schema.
    */
    triggerSchemaDescriptor = getSchemaDescriptor();

    /*
    ** Get the trigger table.
    */
    triggerTableDescriptor = getTableDescriptor(tableName);

    //throw an exception if user is attempting to create a trigger on a temporary table
    if (isSessionSchema(triggerTableDescriptor.getSchemaDescriptor()))
    {
        throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES);
    }
    if (isPrivilegeCollectionRequired())
    {
      compilerContext.pushCurrentPrivType(Authorizer.TRIGGER_PRIV);
      compilerContext.addRequiredTablePriv(triggerTableDescriptor);
      compilerContext.popCurrentPrivType();     
    }

    /*
    ** Regenerates the actionText and actionNode if necessary.
    */
    boolean needInternalSQL = bindReferencesClause(dd);

    lcc.pushTriggerTable(triggerTableDescriptor);
    try
   
      /*
      ** Bind the trigger action and the trigger
      ** when clause to make sure that they are
      ** ok.  Note that we have already substituted
      ** in various replacements for OLD/NEW transition
      ** tables/variables and reparsed if necessary.
      */
      if (needInternalSQL)
        compilerContext.setReliability(CompilerContext.INTERNAL_SQL_LEGAL);
     
      // For before triggers, the action statement cannot contain calls
      // to procedures that modify SQL data. If the action statement
      // contains a procedure call, this reliability will be used during
      // bind of the call statement node.
      if(isBefore)
        compilerContext.setReliability(CompilerContext.MODIFIES_SQL_DATA_PROCEDURE_ILLEGAL);
         
      actionNode.bindStatement();

      /* when clause is always null
      if (whenClause != null)
      {
        whenClause.bind();
      }
      */
    }
    finally
    {
      lcc.popTriggerTable(triggerTableDescriptor);
    }

    /*
    ** Statement is dependent on the TableDescriptor
    */
    compilerContext.createDependency(triggerTableDescriptor);

    /*
    ** If there is a list of columns, then no duplicate columns,
    ** and all columns must be found.
    */
 
View Full Code Here

    baseTableRestrictionList = (PredicateList) getNodeFactory().getNode(
                      C_NodeTypes.PREDICATE_LIST,
                      getContextManager());


    CompilerContext compilerContext = getCompilerContext();

    /* Generate the ResultColumnList */
    resultColumns = genResultColList();
    templateColumns = resultColumns;

    /* Resolve the view, if this is a view */
    if (tableDescriptor.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(tableDescriptor);

      /*
      ** 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);
 
        if (SanityManager.DEBUG)
        {
          SanityManager.ASSERT(vd != null,
            "vd not expected to be null for " + tableName);
        }

        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())
        {
          resultColumns.setCountMismatchAllowed(true);
        }
        //Views execute with definer's privileges and if any one of
        //those privileges' are revoked from the definer, the view gets
        //dropped. So, a view can exist in Derby only if it's owner has
        //all the privileges needed to create one. In order to do a
        //select from a view, a user only needs select privilege on the
        //view and doesn't need any privilege for objects accessed by
        //the view. Hence, when collecting privilege requirement for a
        //sql accessing a view, we only need to look for select privilege
        //on the actual view and that is what the following code is
        //checking.
        for (int i = 0; i < resultColumns.size(); i++) {
          ResultColumn rc = (ResultColumn) resultColumns.elementAt(i);
          if (rc.isPrivilegeCollectionRequired())
            compilerContext.addRequiredColumnPriv( rc.getTableColumnDescriptor());
        }

        fsq = (FromSubquery) getNodeFactory().getNode(
          C_NodeTypes.FROM_SUBQUERY,
          rsn,
                    cvn.getOrderByList(),
                    cvn.getOffset(),
                    cvn.getFetchFirst(),
          (correlationName != null) ?
                        correlationName : getOrigTableName().getTableName(),
          resultColumns,
          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.
        fsq.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(tableDescriptor);

      /* Get the base conglomerate descriptor */
      baseConglomerateDescriptor =
        tableDescriptor.getConglomerateDescriptor(
          tableDescriptor.getHeapConglomerateId()
          );

      /* Build the 0-based array of base column names. */
      columnNames = resultColumns.getColumnNames();

      /* Do error checking on derived column list and update "exposed"
       * column names if valid.
       */
      if (derivedRCL != null)
      {
         resultColumns.propagateDCLInfo(derivedRCL,
                          origTableName.getFullTableName());
      }

      /* Assign the tableNumber */
      if (tableNumber == -1// allow re-bind, in which case use old number
        tableNumber = compilerContext.getNextTableNumber();
    }

    return this;
  }
View Full Code Here

      // Here we get the trigger action sql and use the parser to build
      // the parse tree for it.
      SchemaDescriptor compSchema;
      compSchema = dd.getSchemaDescriptor(trd.getSchemaDescriptor().getUUID(), null);
      CompilerContext newCC = lcc.pushCompilerContext(compSchema);
      Parser  pa = newCC.getParser();
      StatementNode stmtnode = (StatementNode)pa.parseStatement(trd.getTriggerDefinition());
      lcc.popCompilerContext(newCC);
      // Do not delete following. We use this in finally clause to
      // determine if the CompilerContext needs to be popped.
      newCC = null;
     
      try {
        // We are interested in ColumnReference classes in the parse tree
        CollectNodesVisitor visitor = new CollectNodesVisitor(ColumnReference.class);
        stmtnode.accept(visitor);
        Vector refs = visitor.getList();
       
        // Regenerate the internal representation for the trigger action
        // sql using the ColumnReference classes in the parse tree. It
        // will catch dropped column getting used in trigger action sql
        // through the REFERENCING clause(this can happen only for the
        // the triggers created prior to 10.7. Trigger created with
        // 10.7 and higher keep track of trigger action column used
        // through the REFERENCING clause in system table and hence
        // use of dropped column will be detected earlier in this
        // method for such triggers).
        //
        // We might catch errors like following during this step.
        // Say that following pre-10.7 trigger exists in the system and
        // user is dropping column c11. During the regeneration of the
        // internal trigger action sql format, we will catch that
        // column oldt.c11 does not exist anymore
        // CREATE TRIGGER DERBY4998_SOFT_UPGRADE_RESTRICT_tr1
        //    AFTER UPDATE OF c12
        //    ON DERBY4998_SOFT_UPGRADE_RESTRICT REFERENCING OLD AS oldt
        //    FOR EACH ROW
        //    SELECT oldt.c11 from DERBY4998_SOFT_UPGRADE_RESTRICT

        SPSDescriptor triggerActionSPSD = trd.getActionSPS(lcc);
        int[] referencedColsInTriggerAction = new int[td.getNumberOfColumns()];
        java.util.Arrays.fill(referencedColsInTriggerAction, -1);
        triggerActionSPSD.setText(dd.getTriggerActionString(stmtnode,
          trd.getOldReferencingName(),
          trd.getNewReferencingName(),
          trd.getTriggerDefinition(),
          trd.getReferencedCols(),
          referencedColsInTriggerAction,
          0,
          trd.getTableDescriptor(),
          trd.getTriggerEventMask(),
          true
          ));
       
        // Now that we have the internal format of the trigger action sql,
        // bind that sql to make sure that we are not using colunm being
        // dropped in the trigger action sql directly (ie not through
        // REFERENCING clause.
        // eg
        // create table atdc_12 (a integer, b integer);
        // create trigger atdc_12_trigger_1 after update of a
        //     on atdc_12 for each row select a,b from atdc_12
        // Drop one of the columns used in the trigger action
        //   alter table atdc_12 drop column b
        // Following rebinding of the trigger action sql will catch the use
        // of column b in trigger atdc_12_trigger_1
        compSchema = dd.getSchemaDescriptor(trd.getSchemaDescriptor().getUUID(), null);
        newCC = lcc.pushCompilerContext(compSchema);
          newCC.setReliability(CompilerContext.INTERNAL_SQL_LEGAL);
        pa = newCC.getParser();
        stmtnode = (StatementNode)pa.parseStatement(triggerActionSPSD.getText());
        // need a current dependent for bind
        newCC.setCurrentDependent(triggerActionSPSD.getPreparedStatement());
        stmtnode.bindStatement();
        //Register the dependency between trigger table and trigger
        // action SPS
        dm.addDependency(triggerActionSPSD, td, lcc.getContextManager());
      } catch (StandardException se)
View Full Code Here

            if (dep instanceof ViewDescriptor)
            {
              ViewDescriptor vd = (ViewDescriptor) dep;
              SchemaDescriptor compSchema;
              compSchema = dd.getSchemaDescriptor(vd.getCompSchemaId(), null);
              CompilerContext newCC = lcc.pushCompilerContext(compSchema);
              Parser  pa = newCC.getParser();

              // Since this is always nested inside another SQL
              // statement, so topLevel flag should be false
              CreateViewNode cvn = (CreateViewNode)pa.parseStatement(
                        vd.getViewText());

              // need a current dependent for bind
              newCC.setCurrentDependent(dep);
              cvn.bindStatement();
              ProviderInfo[] providerInfos = cvn.getProviderInfo();
              lcc.popCompilerContext(newCC);

              boolean    interferent = false;
View Full Code Here

    throws StandardException
  {
    Parser            p;
    ValueNode          defaultTree;
    LanguageConnectionContext  lcc = getLanguageConnectionContext();
    CompilerContext       compilerContext = getCompilerContext();

    /* 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
    StatementNode qt = p.parseStatement(values);
View Full Code Here

     * The compiler context's compilation schema will be set accordingly based
     * on the given input above.  
     */
    public  CompilerContext pushCompilerContext(SchemaDescriptor sd)
    {
        CompilerContext cc;
        boolean         firstCompilerContext = false;

        //  DEBUG   END

        cc = (CompilerContext) (getContextManager().getContext(CompilerContext.CONTEXT_ID));

        /*
        ** If there is no compiler context, this is the first one on the
        ** stack, so don't pop it when we're done (saves time).
        */
        if (cc == null) { firstCompilerContext = true; }

        if (cc == null || cc.getInUse())
        {
            cc = new CompilerContextImpl(getContextManager(), this, tcf);
            if (firstCompilerContext) { cc.firstOnStack(); }
        }
        else
        {
            /* Reset the next column,table, subquery and ResultSet numbers at
            * the beginning of each statement
            */
            cc.resetContext();
        }

        cc.setInUse(true);

        StatementContext sc = getStatementContext();
        if (sc.getSystemCode())
            cc.setReliability(CompilerContext.INTERNAL_SQL_LEGAL);

        /*
         * Set the compilation schema when its UUID is available.
         * i.e.:  Schema may not have been physically created yet, so
         *        its UUID will be null.
         *
         * o For trigger SPS recompilation, the system must use its
         *   compilation schema to recompile the statement.
         *
         * o For view recompilation, we set the compilation schema
         *   for this compiler context if its UUID is available.
         *   Otherwise, the compilation schema will be determined
         *   at execution time of view creation.
         */
        if (sd != null && sd.getUUID() != null)
        {
            cc.setCompilationSchema(sd);
        }
       
        return  cc;
    }
View Full Code Here

TOP

Related Classes of org.apache.derby.iapi.sql.compile.CompilerContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.