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

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


            {
              ViewDescriptor vd = (ViewDescriptor) dep;
              DataDictionary dd = getDataDictionary();
              SchemaDescriptor compSchema;
              compSchema = dd.getSchemaDescriptor(vd.getCompSchemaId(), null);
              CompilerContext newCC = lcc.pushCompilerContext(compSchema);
              Parser  pa = newCC.getParser();
              LanguageConnectionFactory  lcf = lcc.getLanguageConnectionFactory();

              // 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 = (CreateViewNode) cvn.bind();
              ProviderInfo[] providerInfos = cvn.getProviderInfo();
              lcc.popCompilerContext(newCC);

              boolean    interferent = false;
View Full Code Here


   * @return the compiler context
   *
   */
  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);

    // Save off the current isolation level on entry so that it gets restored
    cc.setEntryIsolationLevel( getCurrentIsolationLevel());

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

    return  cc;
  }
View Full Code Here

      ** RESOLVE: we may ultimately wish to pass in
      ** whether we are a jdbc metadata query or not to
      ** get the CompilerContext to make the createDependency()
      ** call a noop.
      */
      CompilerContext cc = lcc.pushCompilerContext(compilationSchema);
     
      if (prepareIsolationLevel !=
        ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL)
      {
        cc.setScanIsolationLevel(prepareIsolationLevel);
      }


      // Look for stored statements that are in a system schema
      // and with a match compilation schema. If so, allow them
      // to compile using internal SQL constructs.

      if (internalSQL ||
        (spsSchema != null) && (spsSchema.isSystemSchema()) &&
          (spsSchema.equals(compilationSchema))) {
            cc.setReliability(CompilerContext.INTERNAL_SQL_LEGAL);
      }

      try
      {
        // Statement logging if lcc.getLogStatementText() is true
        if (istream != null)
        {
          String xactId = lcc.getTransactionExecute().getActiveStateTxIdString();
          istream.printlnWithHeader(LanguageConnectionContext.xidStr +
                        xactId +
                        "), " +
                        LanguageConnectionContext.lccStr +
                          lcc.getInstanceNumber() +
                        "), " +
                        LanguageConnectionContext.dbnameStr +
                          lcc.getDbname() +
                        "), " +
                        LanguageConnectionContext.drdaStr +
                          lcc.getDrdaID() +
                        "), Begin compiling prepared statement: " +
                        getSource() +
                        " :End prepared statement");
        }

        Parser p = cc.getParser();

        cc.setCurrentDependent(preparedStmt);

        //Only top level statements go through here, nested statement
        //will invoke this method from other places
        QueryTreeNode qt = p.parseStatement(statementText, paramDefaults);

        parseTime = getCurrentTimeMillis(lcc);

        if (SanityManager.DEBUG)
        {
          if (SanityManager.DEBUG_ON("DumpParseTree"))
          {
            qt.treePrint();
          }

          if (SanityManager.DEBUG_ON("StopAfterParsing"))
          {
            throw StandardException.newException(SQLState.LANG_STOP_AFTER_PARSING);
          }
        }

        /*
        ** Tell the data dictionary that we are about to do
        ** a bunch of "get" operations that must be consistent with
        ** each other.
        */
       
        DataDictionary dataDictionary = lcc.getDataDictionary();

        int ddMode = dataDictionary == null ? 0 : dataDictionary.startReading(lcc);

        try
        {
          // start a nested transaction -- all locks acquired by bind
          // and optimize will be released when we end the nested
          // transaction.
          lcc.beginNestedTransaction(true);

          qt = qt.bind();
          bindTime = getCurrentTimeMillis(lcc);

          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON("DumpBindTree"))
            {
              qt.treePrint();
            }

            if (SanityManager.DEBUG_ON("StopAfterBinding")) {
              throw StandardException.newException(SQLState.LANG_STOP_AFTER_BINDING);
            }
          }

          //Derby424 - In order to avoid caching select statements referencing
          // any SESSION schema objects (including statements referencing views
          // in SESSION schema), we need to do the SESSION schema object check
          // here. 
          //a specific eg for statement referencing a view in SESSION schema
          //CREATE TABLE t28A (c28 int)
          //INSERT INTO t28A VALUES (280),(281)
          //CREATE VIEW SESSION.t28v1 as select * from t28A
          //SELECT * from SESSION.t28v1 should show contents of view and we
          // should not cache this statement because a user can later define
          // a global temporary table with the same name as the view name.
          //Following demonstrates that
          //DECLARE GLOBAL TEMPORARY TABLE SESSION.t28v1(c21 int, c22 int) not
          //     logged
          //INSERT INTO SESSION.t28v1 VALUES (280,1),(281,2)
          //SELECT * from SESSION.t28v1 should show contents of global temporary
          //table and not the view.  Since this select statement was not cached
          // earlier, it will be compiled again and will go to global temporary
          // table to fetch data. This plan will not be cached either because
          // select statement is using SESSION schema object.
          //
          //Following if statement makes sure that if the statement is
          // referencing SESSION schema objects, then we do not want to cache it.
          // We will remove the entry that was made into the cache for
          //this statement at the beginning of the compile phase.
          //The reason we do this check here rather than later in the compile
          // phase is because for a view, later on, we loose the information that
          // it was referencing SESSION schema because the reference
          //view gets replaced with the actual view definition. Right after
          // binding, we still have the information on the view and that is why
          // we do the check here.
          if (preparedStmt.referencesSessionSchema(qt)) {
            if (foundInCache)
              ((GenericLanguageConnectionContext)lcc).removeStatement(this);
          }
         
          qt = qt.optimize();

          optimizeTime = getCurrentTimeMillis(lcc);

          // Statement logging if lcc.getLogStatementText() is true
          if (istream != null)
          {
            String xactId = lcc.getTransactionExecute().getActiveStateTxIdString();
            istream.printlnWithHeader(LanguageConnectionContext.xidStr +
                          xactId +
                          "), " +
                          LanguageConnectionContext.lccStr +
                          lcc.getInstanceNumber() +
                          "), " +
                          LanguageConnectionContext.dbnameStr +
                          lcc.getDbname() +
                          "), " +
                          LanguageConnectionContext.drdaStr +
                          lcc.getDrdaID() +
                          "), End compiling prepared statement: " +
                          getSource() +
                          " :End prepared statement");
          }
        }

        catch (StandardException se)
        {
          lcc.commitNestedTransaction();

          // Statement logging if lcc.getLogStatementText() is true
          if (istream != null)
          {
            String xactId = lcc.getTransactionExecute().getActiveStateTxIdString();
            istream.printlnWithHeader(LanguageConnectionContext.xidStr +
                          xactId +
                          "), " +
                          LanguageConnectionContext.lccStr +
                          lcc.getInstanceNumber() +
                          "), " +
                          LanguageConnectionContext.dbnameStr +
                          lcc.getDbname() +
                          "), " +
                          LanguageConnectionContext.drdaStr +
                          lcc.getDrdaID() +
                          "), Error compiling prepared statement: " +
                          getSource() +
                          " :End prepared statement");
          }
          throw se;
        }

        finally
        {
          /* Tell the data dictionary that we are done reading */
          if (dataDictionary != null)
          dataDictionary.doneReading(ddMode, lcc);
        }

        /* we need to move the commit of nested sub-transaction
         * after we mark PS valid, during compilation, we might need
         * to get some lock to synchronize with another thread's DDL
         * execution, in particular, the compilation of insert/update/
         * delete vs. create index/constraint (see Beetle 3976).  We
         * can't release such lock until after we mark the PS valid.
         * Otherwise we would just erase the DDL's invalidation when
         * we mark it valid.
         */
        try    // put in try block, commit sub-transaction if bad
        {
          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON("DumpOptimizedTree"))
            {
              qt.treePrint();
            }

            if (SanityManager.DEBUG_ON("StopAfterOptimizing"))
            {
              throw StandardException.newException(SQLState.LANG_STOP_AFTER_OPTIMIZING);
            }
          }

          GeneratedClass ac = qt.generate(preparedStmt.getByteCodeSaver());

          generateTime = getCurrentTimeMillis(lcc);
          /* endTimestamp only meaningful if generateTime is meaningful.
           * generateTime is meaningful if STATISTICS TIMING is ON.
           */
          if (generateTime != 0)
          {
            endTimestamp = new Timestamp(generateTime);
          }

          if (SanityManager.DEBUG)
          {
            if (SanityManager.DEBUG_ON("StopAfterGenerating"))
            {
              throw StandardException.newException(SQLState.LANG_STOP_AFTER_GENERATING);
            }
          }

          /*
            copy over the compile-time created objects
            to the prepared statement.  This always happens
            at the end of a compile, so there is no need
            to erase the previous entries on a re-compile --
            this erases as it replaces.  Set the activation
            class in case it came from a StorablePreparedStatement
          */
          preparedStmt.setConstantAction( qt.makeConstantAction() );
          preparedStmt.setSavedObjects( cc.getSavedObjects() );
          preparedStmt.setRequiredPermissionsList(cc.getRequiredPermissionsList());
          preparedStmt.setActivationClass(ac);
          preparedStmt.setNeedsSavepoint(qt.needsSavepoint());
          preparedStmt.setCursorInfo((CursorInfo)cc.getCursorInfo());
          preparedStmt.setIsAtomic(qt.isAtomic());
          preparedStmt.setExecuteStatementNameAndSchema(
                        qt.executeStatementName(),
                        qt.executeSchemaName()
                        );
          preparedStmt.setSPSName(qt.getSPSName());
          preparedStmt.completeCompile(qt);
          preparedStmt.setCompileTimeWarnings(cc.getWarnings());
        }
        catch (StandardException e)   // hold it, throw it
        {
          lcc.commitNestedTransaction();
          throw e;
View Full Code Here

    /*
    ** Get a new compiler context, so the parsing of the text
    ** doesn't mess up anything in the current context
    */
    LanguageConnectionContext lcc = getLanguageConnectionContext();
    CompilerContext newCC = lcc.pushCompilerContext();
    newCC.setReliability(CompilerContext.INTERNAL_SQL_LEGAL);

    try
    {
      return QueryTreeNode.parseQueryText(newCC, actionText, (Object[])null, lcc);
    }
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
    QueryTreeNode qt = p.parseStatement(values);
View Full Code Here

   * @exception StandardException    Thrown on error
   */

  public QueryTreeNode bind() throws StandardException
  {
    CompilerContext      cc = getCompilerContext();
    DataDictionary      dd = getDataDictionary();
    ConglomerateDescriptor  cd;
    SchemaDescriptor sd;

    /* in case of rename index, the only thing we get from parser is
     * current and new index names with no information about the
     * table it belongs to. This is because index names are unique
     * within a schema and hence then is no need to qualify an index
     * name with a table name which we have to do for rename column.
     * But from the index name, using the data dictionary, you can
     * find the table it belongs to. Since most of the checking
     * in bind is done using table descriptor, in the following if
     * statement, we are trying to get the table information from the
     * index name so it is available for the rest of he bind code.
     */
    TableName baseTable;

    if (renamingWhat == StatementType.RENAME_INDEX) {

      sd = getSchemaDescriptor((String)null);

      ConglomerateDescriptor indexDescriptor =
        dd.getConglomerateDescriptor(oldObjectName, sd, false);
      if (indexDescriptor == null)
        throw StandardException.newException(
                   SQLState.LANG_INDEX_NOT_FOUND, oldObjectName);
      /* Get the table descriptor */
      td = dd.getTableDescriptor(indexDescriptor.getTableID());
      initAndCheck(makeTableName(td.getSchemaName(),
                     td.getName()));
    } else
      sd = getSchemaDescriptor();

    td = getTableDescriptor();

    //throw an exception if user is attempting a rename on temporary table
    if (td.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE)
    {
        throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE);
    }

    switch (this.renamingWhat)
    {
      case StatementType.RENAME_TABLE:
        /* Verify that new table name does not exist in the database */
        TableDescriptor td = getTableDescriptor(newObjectName, sd);
          if (td != null)
          throw descriptorExistsException(td, sd);
        renameTableBind(dd);
        break;

      case StatementType.RENAME_COLUMN:
        renameColumnBind(dd);
        break;

      case StatementType.RENAME_INDEX:
        ConglomerateDescriptor conglomDesc = dd.getConglomerateDescriptor(newObjectName, sd, false);
          if (conglomDesc != null)
          throw descriptorExistsException(conglomDesc, sd);
          break;

      default:
        if (SanityManager.DEBUG)
          SanityManager.THROWASSERT(
              "Unexpected rename action in RenameNode");
        break;
    }

    conglomerateNumber = td.getHeapConglomerateId();

    /* Get the base conglomerate descriptor */
    cd = td.getConglomerateDescriptor(conglomerateNumber);

    /* Statement is dependent on the TableDescriptor and ConglomerateDescriptor */
    cc.createDependency(td);
    cc.createDependency(cd);

    return this;
  }
View Full Code Here

  {
    String schemaName = objectName.getSchemaName();
    //boolean needError = !(implicitCreateSchema || (schemaName == null));
    boolean needError = !implicitCreateSchema;
    SchemaDescriptor sd = getSchemaDescriptor(schemaName, needError);
    CompilerContext cc = getCompilerContext();

    if (sd == null) {
      /* Disable creating schemas starting with SYS */
      if (schemaName.startsWith("SYS"))
        throw StandardException.newException(
          SQLState.LANG_NO_USER_DDL_IN_SYSTEM_SCHEMA,
          statementToString(),
          schemaName);

      sd  = new SchemaDescriptor(getDataDictionary(), schemaName,
        (String) null, (UUID)null, false);

      if (isPrivilegeCollectionRequired())
        cc.addRequiredSchemaPriv(schemaName, null, Authorizer.CREATE_SCHEMA_PRIV);
    }

    if (ownerCheck && isPrivilegeCollectionRequired())
      cc.addRequiredSchemaPriv(sd.getSchemaName(), null,
            Authorizer.MODIFY_SCHEMA_PRIV);

    /*
    ** Catch the system schema here.
    */  
View Full Code Here

   * @exception StandardException    Thrown on error
   */

  public QueryTreeNode bind() throws StandardException
  {
    CompilerContext      cc = getCompilerContext();
    ConglomerateDescriptor  cd;
    DataDictionary      dd = getDataDictionary();
    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();
    }

    return this;
  }
View Full Code Here

    SchemaDescriptor sd = null;
    boolean isCurrent = false;
    boolean isCompilation = false;
    if (schemaName == null) {

      CompilerContext cc = getCompilerContext();
      sd = cc.getCompilationSchema();

      if (sd == null) {
        // Set the compilation schema to be the default,
        // notes that this query has schema dependencies.
        sd = getLanguageConnectionContext().getDefaultSchema();

        isCurrent = true;

        cc.setCompilationSchema(sd);
      }
      else
      {
        isCompilation = true;
      }
View Full Code Here

  {
    DataDictionary dd = getDataDictionary();
    String nextSynonymTable = tabName.getTableName();
    String nextSynonymSchema = tabName.getSchemaName();
    boolean found = false;
    CompilerContext cc = getCompilerContext();

    // Circular synonym references should have been detected at the DDL time, so
    // the following loop shouldn't loop forever.
    for (;;)
    {
      SchemaDescriptor nextSD = getSchemaDescriptor(nextSynonymSchema, false);
      if (nextSD == null || nextSD.getUUID() == null)
        break;
 
      AliasDescriptor nextAD = dd.getAliasDescriptor(nextSD.getUUID().toString(),
             nextSynonymTable, AliasInfo.ALIAS_NAME_SPACE_SYNONYM_AS_CHAR);
      if (nextAD == null)
        break;

      /* Query is dependent on the AliasDescriptor */
      cc.createDependency(nextAD);

      found = true;
      SynonymAliasInfo info = ((SynonymAliasInfo)nextAD.getAliasInfo());
      nextSynonymTable = info.getSynonymTable();
      nextSynonymSchema = info.getSynonymSchema();
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.