Package org.apache.derby.iapi.sql.depend

Examples of org.apache.derby.iapi.sql.depend.DependencyManager


  {
    SchemaDescriptor  sd;

    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);

    sd = dd.getSchemaDescriptor(schemaName, null, true);

    //If user is attempting to drop SESSION schema and there is no physical SESSION schema, then throw an exception
    //Need to handle it this special way is because SESSION schema is also used for temporary tables. If there is no
    //physical SESSION schema, we internally generate an in-memory SESSION schema in order to support temporary tables
    //But there is no way for the user to access that in-memory SESSION schema. Following if will be true if there is
    //no physical SESSION schema and hence getSchemaDescriptor has returned an in-memory SESSION schema
    if (schemaName.equals(SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME) && (sd != null) && (sd.getUUID() == null))
      throw StandardException.newException(SQLState.LANG_SCHEMA_DOES_NOT_EXIST, schemaName);

    /*
    ** Make sure the schema is empty.
    ** In the future we want to drop everything
    ** in the schema if it is CASCADE.
    */
    if (!dd.isSchemaEmpty(sd))
    {
      throw StandardException.newException(SQLState.LANG_SCHEMA_NOT_EMPTY, schemaName);
    }

    /* Prepare all dependents to invalidate.  (This is there chance
     * to say that they can't be invalidated.  For example, an open
     * cursor referencing a table/view that the user is attempting to
     * drop.) If no one objects, then invalidate any dependent objects.
     * We check for invalidation before we drop the table descriptor
     * since the table descriptor may be looked up as part of
     * decoding tuples in SYSDEPENDS.
     */
    dm.invalidateFor(sd, DependencyManager.DROP_SCHEMA, lcc);

    dd.dropSchemaDescriptor(schemaName, tc);

    /*
    ** If we have dropped the current default schema,
View Full Code Here


  public void  executeConstantAction( Activation activation )
            throws StandardException
  {
    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);

    // now do the real work

    // get an exclusive lock of the heap, to avoid deadlock on rows of
    // SYSCOLUMNS etc datadictionary tables (track 879) and phantom table
    // descriptor, in which case table shape could be changed by a
    // concurrent thread doing add/drop column (track 3804 and 3825)

    // older version (or at target) has to get td first, potential deadlock
    if (tableConglomerateId == 0)
    {
      td = dd.getTableDescriptor(tableId);
      if (td == null)
      {
        throw StandardException.newException(
          SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
      }
      tableConglomerateId = td.getHeapConglomerateId();
    }

    lockTableForDDL(tc, tableConglomerateId, true);

    td = dd.getTableDescriptor(tableId);
    if (td == null)
    {
      throw StandardException.newException(
        SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
    }

    if(truncateTable)
      dm.invalidateFor(td, DependencyManager.TRUNCATE_TABLE, lcc);
    else
      dm.invalidateFor(td, DependencyManager.ALTER_TABLE, lcc);
    execGuts( activation );
  }
View Full Code Here

    boolean            tableNeedsScanning = false;
    boolean            tableScanned = false;

    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();

    // Save the TableDescriptor off in the Activation
    activation.setDDLTableDescriptor(td);

    /*
    ** If the schema descriptor is null, then
    ** we must have just read ourselves in. 
    ** So we will get the corresponding schema
    ** descriptor from the data dictionary.
    */
    if (sd == null)
    {
      sd = getAndCheckSchemaDescriptor(dd, schemaId, "ALTER TABLE");
    }
   

    /* Prepare all dependents to invalidate.  (This is there chance
     * to say that they can't be invalidated.  For example, an open
     * cursor referencing a table/view that the user is attempting to
     * alter.) If no one objects, then invalidate any dependent objects.
     */
    if(truncateTable)
      dm.invalidateFor(td, DependencyManager.TRUNCATE_TABLE, lcc);
    else
      dm.invalidateFor(td, DependencyManager.ALTER_TABLE, lcc);

    // Are we working on columns?
    if (columnInfo != null)
    {
      /* NOTE: We only allow a single column to be added within
View Full Code Here

                   int ix)
          throws StandardException
  {
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();

    ColumnDescriptor columnDescriptor =
      td.getColumnDescriptor(columnInfo[ix].name);
    DataValueDescriptor storableDV;
View Full Code Here

                   int ix)
          throws StandardException
  {
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();


    ColumnDescriptor columnDescriptor =
      td.getColumnDescriptor(columnInfo[ix].name);

    // We already verified this in bind, but do it again
    if (columnDescriptor == null)
    {
      throw
        StandardException.newException(
                      SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE,
                    columnInfo[ix].name,
                    td.getQualifiedName());
    }

    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    ColumnDescriptorList tab_cdl = td.getColumnDescriptorList();
    int size = tab_cdl.size();

    // can NOT drop a column if it is the only one in the table
    if (size == 1)
    {
      throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT,
              dm.getActionString(DependencyManager.DROP_COLUMN),
              "THE *LAST* COLUMN " + columnInfo[ix].name,
              "TABLE",
              td.getQualifiedName() );
    }

    columnPosition = columnDescriptor.getPosition();
    boolean cascade = (behavior == StatementType.DROP_CASCADE);

    FormatableBitSet toDrop = new FormatableBitSet(size + 1);
    toDrop.set(columnPosition);
    td.setReferencedColumnMap(toDrop);

    dm.invalidateFor(td, DependencyManager.DROP_COLUMN, lcc);
         
    // If column has a default we drop the default and any dependencies
    if (columnDescriptor.getDefaultInfo() != null)
    {
      DefaultDescriptor defaultDesc = columnDescriptor.getDefaultDescriptor(dd);
      dm.clearDependencies(lcc, defaultDesc);
    }

    // need to deal with triggers if has referencedColumns
    GenericDescriptorList tdl = dd.getTriggerDescriptors(td);
    Enumeration descs = tdl.elements();
    while (descs.hasMoreElements())
    {
      TriggerDescriptor trd = (TriggerDescriptor) descs.nextElement();
      int[] referencedCols = trd.getReferencedCols();
      if (referencedCols == null)
        continue;
      int refColLen = referencedCols.length, j;
      boolean changed = false;
      for (j = 0; j < refColLen; j++)
      {
        if (referencedCols[j] > columnPosition)
          changed = true;
        else if (referencedCols[j] == columnPosition)
        {
          if (cascade)
          {
            DropTriggerConstantAction.dropTriggerDescriptor(lcc, dm, dd, tc, trd, activation);
            activation.addWarning(
              StandardException.newWarning(SQLState.LANG_TRIGGER_DROPPED,
                trd.getName(), td.getName()));
          }
          else
          // we'd better give an error if don't drop it,
            // otherwsie there would be unexpected behaviors
            throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT,
                    dm.getActionString(DependencyManager.DROP_COLUMN),
                    columnInfo[ix].name, "TRIGGER",
                    trd.getName() );
          }
          break;
        }
      }

      // change triggers to refer to columns in new positions
      if (j == refColLen && changed)
      {
        dd.dropTriggerDescriptor(trd, tc);
        for (j = 0; j < refColLen; j++)
        {
          if (referencedCols[j] > columnPosition)
            referencedCols[j]--;
        }
        dd.addDescriptor(trd, sd,
                 DataDictionary.SYSTRIGGERS_CATALOG_NUM,
                 false, tc);
      }
    }

    ConstraintDescriptorList csdl = dd.getConstraintDescriptors(td);
    int csdl_size = csdl.size();

    // we want to remove referenced primary/unique keys in the second
    // round.  This will ensure that self-referential constraints will
    // work OK.
    int tbr_size = 0;
    ConstraintDescriptor[] toBeRemoved = new ConstraintDescriptor[csdl_size];

    // let's go downwards, don't want to get messed up while removing
    for (int i = csdl_size - 1; i >= 0; i--)
    {
      ConstraintDescriptor cd = csdl.elementAt(i);
      int[] referencedColumns = cd.getReferencedColumns();
      int numRefCols = referencedColumns.length, j;
      boolean changed = false;
      for (j = 0; j < numRefCols; j++)
      {
        if (referencedColumns[j] > columnPosition)
          changed = true;
        if (referencedColumns[j] == columnPosition)
          break;
      }
      if (j == numRefCols)      // column not referenced
      {
        if ((cd instanceof CheckConstraintDescriptor) && changed)
        {
          dd.dropConstraintDescriptor(td, cd, tc);
          for (j = 0; j < numRefCols; j++)
          {
            if (referencedColumns[j] > columnPosition)
              referencedColumns[j]--;
          }
          ((CheckConstraintDescriptor) cd).setReferencedColumnsDescriptor(new ReferencedColumnsDescriptorImpl(referencedColumns));
          dd.addConstraintDescriptor(cd, tc);
        }
        continue;
      }

      if (! cascade)
      {
        if (numRefCols > 1 || cd.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT)
        {
          throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT,
                    dm.getActionString(DependencyManager.DROP_COLUMN),
                    columnInfo[ix].name, "CONSTRAINT",
                    cd.getConstraintName() );
        }
      }

      if (cd instanceof ReferencedKeyConstraintDescriptor)
      {
        // restrict will raise an error in invalidate if really referenced
        toBeRemoved[tbr_size++] = cd;
        continue;
      }

      // drop now in all other cases
      dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT,
                  lcc);
      DropConstraintConstantAction.dropConstraintAndIndex(dm, td, dd,
               cd, tc, lcc, true);
      activation.addWarning(StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED,
        cd.getConstraintName(), td.getName()));
    }

    for (int i = tbr_size - 1; i >= 0; i--)
    {
      ConstraintDescriptor cd = toBeRemoved[i];
      DropConstraintConstantAction.dropConstraintAndIndex(dm, td, dd, cd,
            tc, lcc, false);
      activation.addWarning(StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED,
          cd.getConstraintName(), td.getName()));

      if (cascade)
      {
        ConstraintDescriptorList fkcdl = dd.getForeignKeys(cd.getUUID());
        for (int j = 0; j < fkcdl.size(); j++)
        {
          ConstraintDescriptor fkcd = (ConstraintDescriptor) fkcdl.elementAt(j);
          dm.invalidateFor(fkcd,
                  DependencyManager.DROP_CONSTRAINT,
                  lcc);

          DropConstraintConstantAction.dropConstraintAndIndex(
            dm, fkcd.getTableDescriptor(), dd, fkcd, tc, lcc, true);
          activation.addWarning(StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED,
            fkcd.getConstraintName(), fkcd.getTableDescriptor().getName()));
        }
      }

      dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc);
      dm.clearDependencies(lcc, cd);
    }

    compressTable(activation);

    // drop the column from syscolumns
View Full Code Here

                   int ix)
      throws StandardException            
  {
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();

    ColumnDescriptor columnDescriptor =
      td.getColumnDescriptor(columnInfo[ix].name);
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    int columnPosition = columnDescriptor.getPosition();

    // Clean up after the old default, if non-null
    if (columnDescriptor.hasNonNullDefault())
    {
      // Invalidate off of the old default
      DefaultDescriptor defaultDescriptor = new DefaultDescriptor(dd, columnInfo[ix].oldDefaultUUID,
                     td.getUUID(), columnPosition);

   
      dm.invalidateFor(defaultDescriptor, DependencyManager.MODIFY_COLUMN_DEFAULT, lcc);
   
      // Drop any dependencies
      dm.clearDependencies(lcc, defaultDescriptor);
    }

    UUID defaultUUID = columnInfo[ix].newDefaultUUID;

    /* Generate a UUID for the default, if one exists
View Full Code Here

    try {
   
      notifyLoader(false);
      dd.invalidateAllSPSPlans();
      DependencyManager dm = dd.getDependencyManager();
      dm.invalidateFor(fid, DependencyManager.DROP_JAR, lcc);

      dd.dropFileInfoDescriptor(fid);

      fr.remove(JarDDL.mkExternalName(schemaName, sqlName, fr.getSeparatorChar()),
        fid.getGenerationId(), true /*purgeOnCommit*/);
 
View Full Code Here

      Activation activation, Dependent dependent, UUID refTableUUID)
  throws StandardException
  {
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
   
    //If the Database Owner is creating this constraint, then no need to
    //collect any privilege dependencies because the Database Owner can  
    //access any objects without any restrictions
    if (!(lcc.getAuthorizationId().equals(dd.getAuthorizationDatabaseOwner())))
    {
      PermissionsDescriptor permDesc;
      //Now, it is time to add into dependency system, constraint's
      //dependency on REFERENCES privilege. If the REFERENCES privilege is
      //revoked from the constraint owner, the constraint will get
      //dropped automatically.
      List requiredPermissionsList = activation.getPreparedStatement().getRequiredPermissionsList();
      if (requiredPermissionsList != null && ! requiredPermissionsList.isEmpty())
      {
        for(Iterator iter = requiredPermissionsList.iterator();iter.hasNext();)
        {
          StatementPermission statPerm = (StatementPermission) iter.next();
          //First check if we are dealing with a Table or
          //Column level privilege. All the other privileges
          //are not required for a foreign key constraint.
          if (statPerm instanceof StatementTablePermission)
          {//It is a table/column level privilege
            StatementTablePermission statementTablePermission =
              (StatementTablePermission) statPerm;
            //Check if we are dealing with REFERENCES privilege.
            //If not, move on to the next privilege in the
            //required privileges list
            if (statementTablePermission.getPrivType() != Authorizer.REFERENCES_PRIV)
              continue;
            //Next check is this REFERENCES privilege is
            //on the same table as referenced by the foreign
            //key constraint? If not, move on to the next
            //privilege in the required privileges list
            if (!statementTablePermission.getTableUUID().equals(refTableUUID))
              continue;
          } else if (statPerm instanceof StatementSchemaPermission
              || statPerm instanceof StatementRoutinePermission)
            continue;

          //We know that we are working with a REFERENCES
          //privilege. Find all the PermissionDescriptors for
          //this privilege and make constraint depend on it
          //through dependency manager.
          //The REFERENCES privilege could be defined at the
          //table level or it could be defined at individual
          //column levels. In addition, individual column
          //REFERENCES privilege could be available at the
          //user level or PUBLIC level.
          permDesc = statPerm.getPermissionDescriptor(lcc.getAuthorizationId(), dd);       
          if (permDesc == null)
          {
            //No REFERENCES privilege exists for given
            //authorizer at table or column level.
            //REFERENCES privilege has to exist at at PUBLIC level
            permDesc = statPerm.getPermissionDescriptor(Authorizer.PUBLIC_AUTHORIZATION_ID, dd);
            if (!(permDesc.checkOwner(lcc.getAuthorizationId())))
              dm.addDependency(dependent, permDesc, lcc.getContextManager());
          } else
            //if the object on which permission is required is owned by the
            //same user as the current user, then no need to keep that
            //object's privilege dependency in the dependency system
          if (!(permDesc.checkOwner(lcc.getAuthorizationId())))
          {
            dm.addDependency(dependent, permDesc, lcc.getContextManager());
            if (permDesc instanceof ColPermsDescriptor)
            {
              //The if statement above means we found a
              //REFERENCES privilege at column level for
              //the given authorizer. If this privilege
              //doesn't cover all the column , then there
              //has to exisit REFERENCES for the remaining
              //columns at PUBLIC level. Get that permission
              //descriptor and save it in dependency system
              StatementColumnPermission statementColumnPermission = (StatementColumnPermission) statPerm;
              permDesc = statementColumnPermission.getPUBLIClevelColPermsDescriptor(lcc.getAuthorizationId(), dd);
              //Following if checks if some column level privileges
              //exist only at public level. If so, then the public
              //level column privilege dependency is added
              //into the dependency system
              if (permDesc != null)
                dm.addDependency(dependent, permDesc, lcc.getContextManager());                                            
            }
          }
          //We have found the REFERENCES privilege for all the
          //columns in foreign key constraint and we don't
          //need to go through the rest of the privileges
View Full Code Here

    TransactionController       tc
  )
    throws StandardException
  {
    ContextManager cm = lcc.getContextManager();
    DependencyManager dm;
    ProviderInfo[] providerInfo;

    LanguageConnectionFactory  lcf = lcc.getLanguageConnectionFactory();

    DataDictionary dd = getDataDictionary();


    /*
    ** If we are a trigger, then we have to go ahead
    ** and locate the trigger's table descriptor and
    ** push it on the lcc.  This is expensive, but
    ** pretty atypical since trigger actions aren't
    ** likely to be invalidated too often.  Also, when
    ** possible, we already have the triggerTable.
    */
    if (type == SPS_TYPE_TRIGGER && triggerTable == null)
    {
      String uuidStr = name.substring(49);
      triggerTable = dd.getTableDescriptor(recreateUUID(uuidStr));
      if (SanityManager.DEBUG)
      {
        if (triggerTable == null)
        {
          SanityManager.THROWASSERT("couldn't find trigger table for trigger sps "+name);
        }
      }
    }

    if (triggerTable != null)
    {
      lcc.pushTriggerTable(triggerTable);
    }

    // stored statements always stored as unicode.
    Statement       stmt = lcf.getStatement(dd.getSchemaDescriptor(compSchemaId, null), text, true);

    try
    {
      preparedStatement = (ExecPreparedStatement) stmt.prepareStorable(
                lcc,
                preparedStatement,
                getParameterDefaults(),
                getSchemaDescriptor(),
                type == SPS_TYPE_TRIGGER);
    }
    finally
    {
      if (triggerTable != null)
      {
        lcc.popTriggerTable(triggerTable);
      }
    }

    //If this references a SESSION schema table (temporary or permanent), then throw an exception
    //This is if EXECUTE STATEMENT executing a statement that was created with NOCOMPILE. Because
    //of NOCOMPILE, we could not catch SESSION schema table reference by the statement at
    //CREATE STATEMENT time. And hence need to catch such statements at EXECUTE STATEMENT time
    //when the query is getting compiled.
    if (preparedStatement.referencesSessionSchema())
      throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES);
     
    setCompileTime();
    setParams(preparedStatement.getParameterTypes());

    if (!((org.apache.derby.impl.sql.catalog.DataDictionaryImpl) dd).readOnlyUpgrade) {

      /*
      ** Indicate that we are going to write the data
      ** dictionary.  We have probably already done this
      ** but it is ok to call startWriting more than once.
      */
      dd.startWriting(lcc);

      dm = dd.getDependencyManager();
      /*
      ** Clear out all the dependencies that exist
      ** before we recreate them so we don't grow
      ** SYS.SYSDEPENDS forever.
      */
      dm.clearDependencies(lcc, this, tc);

      /*
      ** Copy over all the dependencies to me
      */
      dm.copyDependencies(preparedStatement,   // from
                      this,   // to
                      false,  // persistent only
                      cm,
                      tc);
    }
View Full Code Here

      /*
      ** The rest are errors
      */
        default:

        DependencyManager dm;

        dm = getDataDictionary().getDependencyManager();
        throw StandardException.newException(SQLState.LANG_PROVIDER_HAS_DEPENDENT_S_P_S,
          dm.getActionString(action),
          p.getObjectName(), name);

    }
  }
View Full Code Here

TOP

Related Classes of org.apache.derby.iapi.sql.depend.DependencyManager

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.