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

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


    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


    ValueNode          checkTree = null;

    // Get the text of all the check constraints
    for (int index = 0; index < ccCDLSize; index++)
    {
      ConstraintDescriptor cd = ccCDL.elementAt(index);

      String constraintText = cd.getConstraintText();

      // Get the query tree for this constraint
      ValueNode oneConstraint =
        parseCheckConstraint(constraintText, td);

      // Put a TestConstraintNode above the constraint tree
      TestConstraintNode tcn =
        (TestConstraintNode) getNodeFactory().getNode(
          C_NodeTypes.TEST_CONSTRAINT_NODE,
          oneConstraint,
          SQLState.LANG_CHECK_CONSTRAINT_VIOLATED,
          td.getQualifiedName(),
          cd.getConstraintName(),
          getContextManager());
         
      // Link consecutive TestConstraintNodes with AND nodes
      if (checkTree == null)
      {
View Full Code Here

    Vector                              refColDescriptors = new Vector(1);
    Vector                              fkColMap = new Vector(1);
    int activeSize = activeList.size();
    for (int index = 0; index < activeSize; index++)
    {
      ConstraintDescriptor cd = activeList.elementAt(index);

      if (cd instanceof ForeignKeyConstraintDescriptor)
      {
        /*
        ** We are saving information for checking the
        ** primary/unique key that is referenced by this
        ** foreign key, so type is FOREIGN KEY.
        */ 
        type = FKInfo.FOREIGN_KEY;
        refcd = ((ForeignKeyConstraintDescriptor)cd).getReferencedConstraint();
        uuids = new UUID[1];
        conglomNumbers = new long[1];
        fkNames = new String[1];
        isSelfReferencingFK = new boolean[1];
        raRules = new int[1];
        fkSetupArrays(dd, (ForeignKeyConstraintDescriptor)cd,
            0, uuids, conglomNumbers,
            fkNames, isSelfReferencingFK, raRules);

        // oops, get the right constraint name -- for error
        // handling we want the FK name, not refcd name
        fkNames[0] = cd.getConstraintName();
      }
      else if (cd instanceof ReferencedKeyConstraintDescriptor)
      { 
        refcd = (ReferencedKeyConstraintDescriptor)cd;

        /*
        ** We are saving information for checking the
        ** foreign key(s) that is dependent on this referenced
        ** key, so type is REFERENCED KEY.
        */ 
        type = FKInfo.REFERENCED_KEY;
        fkcdl = dd.getActiveConstraintDescriptors
          ( ((ReferencedKeyConstraintDescriptor)cd).getForeignKeyConstraints(ConstraintDescriptor.ENABLED) );
 
        int size = fkcdl.size();
        if (size == 0)
        {
          continue;
        }

        uuids = new UUID[size];
        fkNames = new String[size];
        conglomNumbers = new long[size];
        isSelfReferencingFK = new boolean[size];
        raRules = new int[size];
        ForeignKeyConstraintDescriptor fkcd = null;
        TableDescriptor fktd;
        ColumnDescriptorList coldl;
        int[] refColumns;
        ColumnDescriptor cold;
        int[] colArray = remapReferencedColumns(cd, rowMap);
        for (int inner = 0; inner < size; inner++)
        {
          fkcd = (ForeignKeyConstraintDescriptor) fkcdl.elementAt(inner);
          fkSetupArrays(dd, fkcd,
                inner, uuids, conglomNumbers, fkNames,
                isSelfReferencingFK, raRules);
          if((raRules[inner] == StatementType.RA_CASCADE) ||
             (raRules[inner] ==StatementType.RA_SETNULL))
          {
            //find  the referencing  table Name
            fktd = fkcd.getTableDescriptor();
            refTableNames.addElement(fktd.getSchemaName() + "." + fktd.getName());
            refActions.addElement(new Integer(raRules[inner]));
            //find the referencing column name required for update null.
            refColumns = fkcd.getReferencedColumns();
            coldl = fktd.getColumnDescriptorList();
            ColumnDescriptorList releventColDes = new ColumnDescriptorList();
            for(int i = 0 ; i < refColumns.length; i++)
            {
              cold =(ColumnDescriptor)coldl.elementAt(refColumns[i]-1);
              releventColDes.add(cold);
            }
            refColDescriptors.addElement(releventColDes);
            refIndexConglomNum.addElement(new Long(conglomNumbers[inner]));
            fkColMap.addElement(colArray);
          }
        }
      }
      else
      {
        continue;
      }

      TableDescriptor  pktd = refcd.getTableDescriptor();
      UUID pkuuid = refcd.getIndexId();
      ConglomerateDescriptor pkIndexConglom = pktd.getConglomerateDescriptor(pkuuid);

      TableDescriptor refTd = cd.getTableDescriptor();
      fkVector.addElement(new FKInfo(
                  fkNames,              // foreign key names
                  refTd.getName(),        // table being modified
                  statementType,            // INSERT|UPDATE|DELETE
                  type,                // FOREIGN_KEY|REFERENCED_KEY
View Full Code Here

    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

      ( StatementType.UPDATE, false, changedColumnIds, needsDeferredProcessing, relevantConstraints );

    int rclSize = relevantConstraints.size();
    for (int index = 0; index < rclSize; index++)
    {
      ConstraintDescriptor cd = relevantConstraints.elementAt(index);
      if (cd.getConstraintType() != DataDictionary.CHECK_CONSTRAINT)
      {
        continue;
      }

      int[] refColumns = ((CheckConstraintDescriptor)cd).getReferencedColumns();
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);
          mcdn.useExistingCollation(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

      ( StatementType.UPDATE, false, changedColumnIds, needsDeferredProcessing, relevantConstraints );

    int rclSize = relevantConstraints.size();
    for (int index = 0; index < rclSize; index++)
    {
      ConstraintDescriptor cd = relevantConstraints.elementAt(index);
      if (cd.getConstraintType() != DataDictionary.CHECK_CONSTRAINT)
      {
        continue;
      }

      int[] refColumns = ((CheckConstraintDescriptor)cd).getReferencedColumns();
View Full Code Here

     *    invalid value for hashLoadFactor
     *    invalid value for hashMaxCapacity
     */
    boolean indexSpecified = false;
    boolean constraintSpecified = false;
    ConstraintDescriptor consDesc = null;
    Enumeration e = tableProperties.keys();

      StringUtil.SQLEqualsIgnoreCase(tableDescriptor.getSchemaName(),
                       "SYS");
    while (e.hasMoreElements())
    {
      String key = (String) e.nextElement();
      String value = (String) tableProperties.get(key);

      if (key.equals("index"))
      {
        // User only allowed to specify 1 of index and constraint, not both
        if (constraintSpecified)
        {
          throw StandardException.newException(SQLState.LANG_BOTH_FORCE_INDEX_AND_CONSTRAINT_SPECIFIED,
                getBaseTableName());
        }
        indexSpecified = true;

        /* Validate index name - NULL means table scan */
        if (! StringUtil.SQLToUpperCase(value).equals("NULL"))
        {
          ConglomerateDescriptor cd = null;
          ConglomerateDescriptor[] cds = tableDescriptor.getConglomerateDescriptors();

          for (int index = 0; index < cds.length; index++)
          {
            cd = cds[index];
            String conglomerateName = cd.getConglomerateName();
            if (conglomerateName != null)
            {
              if (conglomerateName.equals(value))
              {
                break;
              }
            }
            // Not a match, clear cd
            cd = null;
          }

          // Throw exception if user specified index not found
          if (cd == null)
          {
            throw StandardException.newException(SQLState.LANG_INVALID_FORCED_INDEX1,
                    value, getBaseTableName());
          }
          /* Query is dependent on the ConglomerateDescriptor */
          getCompilerContext().createDependency(cd);
        }
      }
      else if (key.equals("constraint"))
      {
        // User only allowed to specify 1 of index and constraint, not both
        if (indexSpecified)
        {
          throw StandardException.newException(SQLState.LANG_BOTH_FORCE_INDEX_AND_CONSTRAINT_SPECIFIED,
                getBaseTableName());
        }
        constraintSpecified = true;

        if (! StringUtil.SQLToUpperCase(value).equals("NULL"))
        {
          consDesc =
            dDictionary.getConstraintDescriptorByName(
                  tableDescriptor, (SchemaDescriptor)null, value,
                  false);

          /* Throw exception if user specified constraint not found
           * or if it does not have a backing index.
           */
          if ((consDesc == null) || ! consDesc.hasBackingIndex())
          {
            throw StandardException.newException(SQLState.LANG_INVALID_FORCED_INDEX2,
                    value, getBaseTableName());
          }

          /* Query is dependent on the ConstraintDescriptor */
          getCompilerContext().createDependency(consDesc);
        }
      }
      else if (key.equals("joinStrategy"))
      {
        userSpecifiedJoinStrategy = StringUtil.SQLToUpperCase(value);
      }
      else if (key.equals("hashInitialCapacity"))
      {
        initialCapacity = getIntProperty(value, key);

        // verify that the specified value is valid
        if (initialCapacity <= 0)
        {
          throw StandardException.newException(SQLState.LANG_INVALID_HASH_INITIAL_CAPACITY,
              String.valueOf(initialCapacity));
        }
      }
      else if (key.equals("hashLoadFactor"))
      {
        try
        {
          loadFactor = Float.valueOf(value).floatValue();
        }
        catch (NumberFormatException nfe)
        {
          throw StandardException.newException(SQLState.LANG_INVALID_NUMBER_FORMAT_FOR_OVERRIDE,
              value, key);
        }

        // verify that the specified value is valid
        if (loadFactor <= 0.0 || loadFactor > 1.0)
        {
          throw StandardException.newException(SQLState.LANG_INVALID_HASH_LOAD_FACTOR,
              value);
        }
      }
      else if (key.equals("hashMaxCapacity"))
      {
        maxCapacity = getIntProperty(value, key);

        // verify that the specified value is valid
        if (maxCapacity <= 0)
        {
          throw StandardException.newException(SQLState.LANG_INVALID_HASH_MAX_CAPACITY,
              String.valueOf(maxCapacity));
        }
      }
      else if (key.equals("bulkFetch"))
      {
        bulkFetch = getIntProperty(value, key);

        // verify that the specified value is valid
        if (bulkFetch <= 0)
        {
          throw StandardException.newException(SQLState.LANG_INVALID_BULK_FETCH_VALUE,
              String.valueOf(bulkFetch));
        }
     
        // no bulk fetch on updatable scans
        if (forUpdate())
        {
          throw StandardException.newException(SQLState.LANG_INVALID_BULK_FETCH_UPDATEABLE);
        }
      }
      else
      {
        // No other "legal" values at this time
        throw StandardException.newException(SQLState.LANG_INVALID_FROM_TABLE_PROPERTY, key,
          "index, constraint, joinStrategy");
      }
    }

    /* If user specified a non-null constraint name(DERBY-1707), then 
     * replace it in the properties list with the underlying index name to
     * simplify the code in the optimizer.
     * NOTE: The code to get from the constraint name, for a constraint
     * with a backing index, to the index name is convoluted.  Given
     * the constraint name, we can get the conglomerate id from the
     * ConstraintDescriptor.  We then use the conglomerate id to get
     * the ConglomerateDescriptor from the DataDictionary and, finally,
     * we get the index name (conglomerate name) from the ConglomerateDescriptor.
     */
    if (constraintSpecified && consDesc != null)
    {
      ConglomerateDescriptor cd =
        dDictionary.getConglomerateDescriptor(
          consDesc.getConglomerateId());
      String indexName = cd.getConglomerateName();

      tableProperties.remove("constraint");
      tableProperties.put("index", indexName);
    }
View Full Code Here

        private void pushIndexName(ConglomerateDescriptor cd, MethodBuilder mb)
          throws StandardException
        {
            if (cd.isConstraint()) {
                DataDictionary dd = getDataDictionary();
                ConstraintDescriptor constraintDesc =
                    dd.getConstraintDescriptor(tableDescriptor, cd.getUUID());
                mb.push(constraintDesc.getConstraintName());
            } else if (cd.isIndex())  {
                mb.push(cd.getConglomerateName());
            } else {
             // If the conglomerate is the base table itself, make sure we push null.
             //  Before the fix for DERBY-578, we would push the base table name
View Full Code Here

            boolean forUpdate)
      throws StandardException
  {
    SYSCONSTRAINTSRowFactory rf = (SYSCONSTRAINTSRowFactory) ti.getCatalogRowFactory();
    ConglomerateController  heapCC;
    ConstraintDescriptor  cd = null;
    ExecIndexRow        indexRow1;
    ExecIndexRow      indexTemplateRow;
    ExecRow         outRow;
    RowLocation        baseRowLocation;
    ScanController      scanController;
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.