Package org.apache.derby.iapi.sql.dictionary

Examples of org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList


            fkList,
            false);

    SubKeyConstraintDescriptor cd;
    TableDescriptor td;
    ConstraintDescriptorList cdl = new ConstraintDescriptorList();
    ConstraintDescriptorList tmpCdl;

    for (Iterator iterator = fkList.iterator(); iterator.hasNext(); )
    {
      cd = (SubKeyConstraintDescriptor) iterator.next();
      td = getConstraintTableDescriptor(cd.getUUID());
View Full Code Here


   */
  public void  dropAllConstraintDescriptors(TableDescriptor table,
                       TransactionController tc)
    throws StandardException
  {
    ConstraintDescriptorList cdl = getConstraintDescriptors(table);

    // Walk the table's CDL and drop each ConstraintDescriptor.
    for (Iterator iterator = cdl.iterator(); iterator.hasNext(); )
    {
      ConstraintDescriptor cd = (ConstraintDescriptor) iterator.next();
      dropConstraintDescriptor(table, cd, tc);
    }

View Full Code Here

           */
          switch (constraintType)
          {
            case DataDictionary.PRIMARYKEY_CONSTRAINT:
              // Check to see if a constraint of the same type already exists
              ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
              if (cdl.getPrimaryKey() != null)
              {
                throw StandardException.newException(SQLState.LANG_ADD_PRIMARY_KEY_FAILED1,
                      td.getQualifiedName());
              }
              if (! tableScanned)
View Full Code Here

                 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(
View Full Code Here

    }


    //truncate table is not allowed if there are any tables referencing it.
    //except if it is self referencing.
    ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
    for(int index = 0; index < cdl.size(); index++)
    {
      ConstraintDescriptor cd = cdl.elementAt(index);
      if (cd instanceof ReferencedKeyConstraintDescriptor)
      {
        ReferencedKeyConstraintDescriptor rfcd = (ReferencedKeyConstraintDescriptor) cd;
        if(rfcd.hasNonSelfReferencingFK(ConstraintDescriptor.ENABLED))
        {
View Full Code Here

  private void dropAllConstraintDescriptors(TableDescriptor td, Activation activation)
    throws StandardException
  {
    ConstraintDescriptor        cd;
    ConstraintDescriptorList       cdl;
    ConstraintDescriptor         fkcd;
    ConstraintDescriptorList       fkcdl;
    LanguageConnectionContext      lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();

    cdl = dd.getConstraintDescriptors(td);
   
    /*
    ** First go, don't drop unique or primary keys.
    ** This will ensure that self-referential constraints
    ** will work ok, even if not cascading.
     */
    /* The current element will be deleted underneath
     * the loop, so we only increment the counter when
     * skipping an element. (HACK!)
     */
    for(int index = 0; index < cdl.size(); )
    {
      cd = cdl.elementAt(index);
      if (cd instanceof ReferencedKeyConstraintDescriptor)
      {
        index++;
        continue;
      }

      dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc);
      DropConstraintConstantAction.dropConstraintAndIndex(dm, td, dd, cd,
            tc, lcc, true);
    }

    /*
     ** Referenced keys (unique or pk) constraints only
    */
    /* The current element will be deleted underneath
     * the loop. (HACK!)
     */
    while (cdl.size() > 0)
    {
      cd = cdl.elementAt(0);
      if (SanityManager.DEBUG)
      {
        if (!(cd instanceof ReferencedKeyConstraintDescriptor))
        {
          SanityManager.THROWASSERT("Constraint descriptor not an instance of " +
          "ReferencedKeyConstraintDescriptor as expected.  Is a "+ cd.getClass().getName());
        }
      }

      /*
      ** Drop the referenced constraint (after we got
      ** the primary keys) now.  Do this prior to
      ** droping the referenced keys to avoid performing
      ** a lot of extra work updating the referencedcount
      ** field of sys.sysconstraints.
      **
      ** Pass in false to dropConstraintsAndIndex so it
      ** doesn't clear dependencies, we'll do that ourselves.
      */
      DropConstraintConstantAction.dropConstraintAndIndex(dm, td, dd, cd,
            tc, lcc, false);

      /*
      ** If we are going to cascade, get all the
      ** referencing foreign keys and zap them first.
      */
      if (cascade)
      {
        /*
        ** Go to the system tables to get the foreign keys
        ** to be safe
        */

        fkcdl = dd.getForeignKeys(cd.getUUID());

        /*
        ** For each FK that references this key, drop
        ** it.
        */
        for(int inner = 0; inner < fkcdl.size(); inner++)
        {
          fkcd = (ConstraintDescriptor) fkcdl.elementAt(inner);
          dm.invalidateFor(fkcd, DependencyManager.DROP_CONSTRAINT, lcc);
          DropConstraintConstantAction.dropConstraintAndIndex(
              dm, fkcd.getTableDescriptor(), dd, fkcd,
              tc, lcc, true);
          activation.addWarning(
View Full Code Here

  //do any checking needs to be done at bind time for rename table
  private void renameTableBind(DataDictionary dd)
    throws StandardException
  {
    /* Verify that there are no check constraints on the table */
    ConstraintDescriptorList constraintDescriptorList = dd.getConstraintDescriptors(td);
    int size =
      constraintDescriptorList == null ? 0 : constraintDescriptorList.size();

    ConstraintDescriptor constraintDescriptor;

    // go through all the constraints defined on the table
    for (int index = 0; index < size; index++)
    {
      constraintDescriptor = constraintDescriptorList.elementAt(index);
      // if it is a check constraint, error
      if (constraintDescriptor.getConstraintType() == DataDictionary.CHECK_CONSTRAINT)
      {
        throw StandardException.newException(
            SQLState.LANG_PROVIDER_HAS_DEPENDENT_OBJECT,
View Full Code Here

    ColumnDescriptor cd = td.getColumnDescriptor(newObjectName);
      if (cd != null)
        throw descriptorExistsException(cd, td);

    /* Verify that there are no check constraints using the column being renamed */
    ConstraintDescriptorList constraintDescriptorList =
      dd.getConstraintDescriptors(td);
    int size =
      constraintDescriptorList == null ? 0 : constraintDescriptorList.size();

    ConstraintDescriptor constraintDescriptor;
    ColumnDescriptorList checkConstraintCDL;
    int  checkConstraintCDLSize;

    // go through all the constraints defined on the table
    for (int index = 0; index < size; index++)
    {
      constraintDescriptor = constraintDescriptorList.elementAt(index);
      // if it is a check constraint, verify that column being
      // renamed is not used in it's sql
      if (constraintDescriptor.getConstraintType() == DataDictionary.CHECK_CONSTRAINT)
      {
        checkConstraintCDL = constraintDescriptor.getColumnDescriptors();
View Full Code Here

      (getNodeType() != C_NodeTypes.MODIFY_COLUMN_CONSTRAINT_NODE) &&
      (getNodeType() != C_NodeTypes.MODIFY_COLUMN_CONSTRAINT_NOT_NULL_NODE))
      return;

    DataDictionary dd = getDataDictionary();
    ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
    int intArray[] = new int[1];
    intArray[0] = columnPosition;

    for (int index = 0; index < cdl.size(); index++)
    {
      ConstraintDescriptor existingConstraint =
                                        cdl.elementAt(index);
      if (!(existingConstraint instanceof KeyConstraintDescriptor))
        continue;

      if (!existingConstraint.columnIntersects(intArray))
        continue;
                              
      int constraintType = existingConstraint.getConstraintType();

      // cannot change the length of a column that is part of a
      // foreign key constraint. Must be an exact match between pkey
      // and fkey columns.
      if ((constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT)
        &&
        (getNodeType() == C_NodeTypes.MODIFY_COLUMN_TYPE_NODE))
      {
        throw StandardException.newException(
           SQLState.LANG_MODIFY_COLUMN_FKEY_CONSTRAINT, name, existingConstraint.getConstraintName());
     
     
      else
      {
        // a column that is part of a primary key or unique constraint
                // is being made nullable; can't be done.
        if ((getNodeType() ==
           C_NodeTypes.MODIFY_COLUMN_CONSTRAINT_NODE) &&
          ((existingConstraint.getConstraintType() ==
           DataDictionary.PRIMARYKEY_CONSTRAINT) ||
           (existingConstraint.getConstraintType() ==
           DataDictionary.UNIQUE_CONSTRAINT)))
        {
        throw StandardException.newException(
           SQLState.LANG_MODIFY_COLUMN_EXISTING_CONSTRAINT, name);
        }
        // unique key or primary key.
        ConstraintDescriptorList
          refcdl = dd.getForeignKeys(existingConstraint.getUUID());
        
        if (refcdl.size() > 0)
        {
          throw StandardException.newException(
             SQLState.LANG_MODIFY_COLUMN_REFERENCED, name, refcdl.elementAt(0).getConstraintName());
        }
       
        // Make the statement dependent on the primary key constraint.
        getCompilerContext().createDependency(existingConstraint);
      }
View Full Code Here

  public void  executeConstantAction( Activation activation )
            throws StandardException
  {
    ConstraintDescriptor    cd;
    TableDescriptor        td;
    ConstraintDescriptorList  tmpCdl;
    boolean            enforceThisConstraint;

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

    tmpCdl = getConstraintDescriptorList(dd);

    int[] enabledCol = new int[1];
    enabledCol[0] = ConstraintDescriptor.SYSCONSTRAINTS_STATE_FIELD;
    /*
    ** 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);

    /*
    ** Callback to rep subclass
    */
    publishToTargets(activation);

    boolean skipFKs = false;

    /*
    ** If the constraint list is empty, then we are getting
    ** all constraints.  In this case, don't bother going
    ** after referencing keys (foreign keys) when we are
    ** disabling a referenced key (pk or unique key) since
    ** we know we'll hit it eventually. 
    */
    if (tmpCdl == null)
    {
      skipFKs = true;
      tmpCdl = dd.getConstraintDescriptors((TableDescriptor)null);
    }
 
    Hashtable checkConstraintTables = null;
    int cdlSize = tmpCdl.size();
    for (int index = 0; index < cdlSize; index++)
    {
      cd = tmpCdl.elementAt(index);

      /* 
      ** We are careful to enable this constraint before trying
      ** to enable constraints that reference it.  Similarly,
      ** we disabled constraints that reference us before we
      ** disable ourselves, to make sure everything works ok.
      */
      if (unconditionallyEnforce)
      {
        enforceThisConstraint = true;
      }
      else
      {
        enforceThisConstraint = (enable && !cd.isEnabled());
      }

      if (enforceThisConstraint)
      {
        if (cd instanceof ForeignKeyConstraintDescriptor)
        {
          validateFKConstraint((ForeignKeyConstraintDescriptor)cd, dd, tc, lcc.getContextManager());
        }
        /*
        ** For check constraints, we build up a list of check constriants
        ** by table descriptor.  Once we have collected them all, we
        ** execute them in a single query per table descriptor.
        */
        else if (cd instanceof CheckConstraintDescriptor)
        {
          td = cd.getTableDescriptor();

          if (checkConstraintTables == null)
          {
            checkConstraintTables = new Hashtable(10);
          }

          ConstraintDescriptorList tabCdl = (ConstraintDescriptorList)
                        checkConstraintTables.get(td.getUUID());
          if (tabCdl == null)
          {
            tabCdl = new ConstraintDescriptorList();
            checkConstraintTables.put(td.getUUID(), tabCdl);
          }
          tabCdl.add(cd);
        }
        /*
        ** If we are enabling a constraint, we need to issue
        ** the invalidation on the underlying table rather than
        ** the constraint we are enabling.  This is because
        ** stmts that were compiled against a disabled constraint
        ** have no depedency on that disabled constriant.
        */
        dm.invalidateFor(cd.getTableDescriptor(),
                  DependencyManager.SET_CONSTRAINTS_ENABLE, lcc);
        cd.setEnabled();
        dd.updateConstraintDescriptor(cd,
                      cd.getUUID(),
                      enabledCol,
                      tc);
      }
 
      /*
      ** If we are dealing with a referenced constraint, then
      ** we find all of the constraints that reference this constraint.
      ** Turn them on/off based on what we are doing to this
      ** constraint.
      */
      if (!skipFKs &&
        (cd instanceof ReferencedKeyConstraintDescriptor))
      {
        ForeignKeyConstraintDescriptor fkcd;
        ReferencedKeyConstraintDescriptor refcd;
        ConstraintDescriptorList fkcdl;
 
        refcd = (ReferencedKeyConstraintDescriptor)cd;
        fkcdl = refcd.getForeignKeyConstraints(ReferencedKeyConstraintDescriptor.ALL);

        int fkcdlSize = fkcdl.size();
        for (int inner = 0; inner < fkcdlSize; inner++)
        {
          fkcd = (ForeignKeyConstraintDescriptor) fkcdl.elementAt(inner)
          if (enable && !fkcd.isEnabled())
          {
            dm.invalidateFor(fkcd.getTableDescriptor(),
                  DependencyManager.SET_CONSTRAINTS_ENABLE, lcc);
            validateFKConstraint(fkcd, dd, tc, lcc.getContextManager());
View Full Code Here

TOP

Related Classes of org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList

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.