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

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


  }

  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(
            StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED,
               fkcd.getConstraintName(),
              fkcd.getTableDescriptor().getName()));
        }
      }

      /*
      ** Now that we got rid of the fks (if we were cascading), it is
View Full Code Here


    /* 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,
            "RENAME",
            td.getName(),
            "CONSTRAINT",
            constraintDescriptor.getConstraintName());
      }
    }
  }
View Full Code Here

    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();
        checkConstraintCDLSize = checkConstraintCDL.size();

        for (int index2 = 0; index2 < checkConstraintCDLSize; index2++)
          if (checkConstraintCDL.elementAt( index2 ) == columnDescriptor)
            throw StandardException.newException(
            SQLState.LANG_RENAME_COLUMN_WILL_BREAK_CHECK_CONSTRAINT,
            oldObjectName,
            constraintDescriptor.getConstraintName());
      }
    }
  }
View Full Code Here

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

      innerTable.getTrulyTheBestAccessPath().getConglomerateDescriptor();
    if (cd.isConstraint())
    {
      DataDictionary dd = innerTable.getDataDictionary();
      TableDescriptor td = innerTable.getTableDescriptor();
      ConstraintDescriptor constraintDesc = dd.getConstraintDescriptor(
                            td, cd.getUUID());
      mb.push(constraintDesc.getConstraintName());
    } else if (cd.isIndex())  {
      mb.push(cd.getConglomerateName());
    } else {
      mb.pushNull("java.lang.String");
    }
View Full Code Here

   * @exception StandardException    Thrown on failure
   */
  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());
            fkcd.setEnabled();
            dd.updateConstraintDescriptor(fkcd,
                fkcd.getUUID(),
                enabledCol,
                tc);
          }
          else if (!enable && fkcd.isEnabled())
          {
            dm.invalidateFor(fkcd, DependencyManager.SET_CONSTRAINTS_DISABLE,
                     lcc);
            fkcd.setDisabled();
            dd.updateConstraintDescriptor(fkcd,
                fkcd.getUUID(),
                enabledCol,
                tc);
          }
        }
      }
 
      if (!enable && cd.isEnabled())
      {
        dm.invalidateFor(cd, DependencyManager.SET_CONSTRAINTS_DISABLE,
                 lcc);
        cd.setDisabled();
        dd.updateConstraintDescriptor(cd,
                        cd.getUUID(),
                        enabledCol,
                        tc);
      }
    }

View Full Code Here

  private void validateAllCheckConstraints(LanguageConnectionContext lcc, Hashtable ht)
    throws StandardException
  {
    ConstraintDescriptorList  cdl;
    ConstraintDescriptor    cd = null;
    TableDescriptor        td;
    StringBuffer        text;
    StringBuffer        constraintNames;

    if (ht == null)
    {
      return;
    }

    for (Enumeration e = ht.elements(); e.hasMoreElements(); )
    {
   
      cdl = (ConstraintDescriptorList) e.nextElement();
      text = null;
      constraintNames = null;

      /*
      ** Build up the text of all the constraints into one big
      ** predicate.  Also, we unfortunately have to build up a big
      ** comma separated list of constraint names in case
      ** there is an error (we are favoring speed over a very
      ** explicit check constraint xxxx failed error message).
      */
      int cdlSize = cdl.size();
      for (int index = 0; index < cdlSize; index++)
      {
        cd = (CheckConstraintDescriptor) cdl.elementAt(index);
        if (text == null)
        {
          text = new StringBuffer("(").append(cd.getConstraintText()).append(") ");
          constraintNames = new StringBuffer(cd.getConstraintName());
        }
        else
        {
          text.append(" AND (").append(cd.getConstraintText()).append(") ");
          constraintNames.append(", ").append(cd.getConstraintName());
        }
      }

      if (SanityManager.DEBUG)
      {
        SanityManager.ASSERT(text != null, "internal error, badly built hastable");
      }

      ConstraintConstantAction.validateConstraint(
                        constraintNames.toString(),
                        text.toString(),
                        cd.getTableDescriptor(),
                        lcc, true);
    }
  }
View Full Code Here

          SanityManager.THROWASSERT("couldn't locate table descriptor "+
            "in SET CONSTRAINTS for uuid "+tuuids[i]);
        }
      }

      ConstraintDescriptor cd = dd.getConstraintDescriptorById(td, cuuids[i]);

      if (SanityManager.DEBUG)
      {
        if (cd == null)
        {
View Full Code Here

    {
      //In case of alter table, get the already existing primary key and unique
      //key constraints for this table. And then we will compare them with  new
      //primary key/unique key constraint column lists.
      ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
      ConstraintDescriptor cd;

      if (cdl != null) //table does have some pre-existing constraints defined on it
      {
        for (int i=0; i<cdl.size();i++)
        {
          cd = cdl.elementAt(i);
          //if the constraint type is not primary key or unique key, ignore it.
          if (cd.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT ||
          cd.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT)
            constraintsVector.addElement(cd);
        }
      }
    }

    int tableType = TableDescriptor.BASE_TABLE_TYPE;
    if (ddlStmt instanceof CreateTableNode)
      tableType = ((CreateTableNode)ddlStmt).tableType;

    for (int index = 0; index < size; index++)
    {
      TableElementNode tableElement = (TableElementNode) elementAt(index);

      if (tableElement instanceof ColumnDefinitionNode)
      {
        ColumnDefinitionNode cdn = (ColumnDefinitionNode) elementAt(index);
        if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE &&
          (cdn.getDataTypeServices().getTypeId().isLongConcatableTypeId() ||
          cdn.getDataTypeServices().getTypeId().isUserDefinedTypeId()))
        {
          throw StandardException.newException(SQLState.LANG_LONG_DATA_TYPE_NOT_ALLOWED, cdn.getColumnName());
        }
        checkForDuplicateColumns(ddlStmt, columnHT, cdn.getColumnName());
        cdn.checkUserType(td);
        cdn.bindAndValidateDefault(dd, td);

        cdn.validateAutoincrement(dd, td, tableType);

        if (tableElement instanceof ModifyColumnNode)
        {
          ModifyColumnNode mcdn = (ModifyColumnNode)cdn;
          mcdn.checkExistingConstraints(td);
        } else if (cdn.isAutoincrementColumn())
          numAutoCols ++;
      }
      else if (tableElement.getElementType() == TableElementNode.AT_DROP_COLUMN)
      {
        String colName = tableElement.getName();
        if (td.getColumnDescriptor(colName) == null)
        {
          throw StandardException.newException(
                        SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE,
                        colName,
                        td.getQualifiedName());
        }
        break;
      }

      /* The rest of this method deals with validating constraints */
      if (! (tableElement.hasConstraint()))
      {
        continue;
      }

      ConstraintDefinitionNode cdn = (ConstraintDefinitionNode) tableElement;

      cdn.bind(ddlStmt, dd);

      //if constraint is primary key or unique key, add it to the vector
      if (cdn.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT ||
      cdn.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT)
      {
        /* In case of create table, the vector can have only ConstraintDefinitionNode
        * elements. In case of alter table, it can have both ConstraintDefinitionNode
        * (for new constraints) and ConstraintDescriptor(for pre-existing constraints).
        */

        Object destConstraint;
        String destName = null;
        String[] destColumnNames = null;

        for (int i=0; i<constraintsVector.size();i++)
        {

          destConstraint = constraintsVector.elementAt(i);
          if (destConstraint instanceof ConstraintDefinitionNode)
          {
            ConstraintDefinitionNode destCDN = (ConstraintDefinitionNode)destConstraint;
            destName = destCDN.getConstraintMoniker();
            destColumnNames = destCDN.getColumnList().getColumnNames();
          }
          else if (destConstraint instanceof ConstraintDescriptor)
          {
            //will come here only for pre-existing constraints in case of alter table
            ConstraintDescriptor destCD = (ConstraintDescriptor)destConstraint;
            destName = destCD.getConstraintName();
            destColumnNames = destCD.getColumnDescriptors().getColumnNames();
          }
          //check if there are multiple constraints with same set of columns
          if (columnsMatch(cdn.getColumnList().getColumnNames(), destColumnNames))
            throw StandardException.newException(SQLState.LANG_MULTIPLE_CONSTRAINTS_WITH_SAME_COLUMNS,
            cdn.getConstraintMoniker(), destName);
        }
        constraintsVector.addElement(cdn);
      }

      /* Make sure that there are no duplicate constraint names in the list */
      if (cdn instanceof ConstraintDefinitionNode)
        checkForDuplicateConstraintNames(ddlStmt, constraintHT, cdn.getConstraintMoniker());

      /* Make sure that the constraint we are trying to drop exists */
      if (cdn.getConstraintType() == DataDictionary.DROP_CONSTRAINT)
      {
        /*
        ** If no schema descriptor, then must be an invalid
        ** schema name.
        */

        String dropConstraintName = cdn.getConstraintMoniker();

        if (dropConstraintName != null) {

          String dropSchemaName = cdn.getDropSchemaName();

          SchemaDescriptor sd = dropSchemaName == null ? td.getSchemaDescriptor() :
                      getSchemaDescriptor(dropSchemaName);

          ConstraintDescriptor cd =
                dd.getConstraintDescriptorByName(
                    td, sd, dropConstraintName,
                    false);
          if (cd == null)
          {
View Full Code Here

    boolean          isEnabled = true;
    int            referenceCount = 0;

    if (td != null)
    {
      ConstraintDescriptor constraint = (ConstraintDescriptor)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.
      */
      oid = constraint.getUUID();
      constraintID = oid.toString();

      oid = constraint.getTableId();
      tableID = oid.toString();

      constraintName = constraint.getConstraintName();

      constraintIType = constraint.getConstraintType();
      switch (constraintIType)
      {
          case DataDictionary.PRIMARYKEY_CONSTRAINT:
            constraintSType = "P";
          break;

          case DataDictionary.UNIQUE_CONSTRAINT:
          constraintSType = "U";
          break;

          case DataDictionary.CHECK_CONSTRAINT:
          constraintSType = "C";
          break;

          case DataDictionary.FOREIGNKEY_CONSTRAINT:
          constraintSType = "F";
          break;

          default:
          if (SanityManager.DEBUG)
          {
            SanityManager.THROWASSERT("invalid constraint type");
          }
      }

      schemaID = constraint.getSchemaDescriptor().getUUID().toString();
      isEnabled = constraint.isEnabled();
      referenceCount = constraint.getReferenceCount();
    }

    /* Insert info into sysconstraints */

    /* RESOLVE - It would be nice to require less knowledge about sysconstraints
View Full Code Here

TOP

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

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.