예제 #1
0
  /**
   * Checks for foreign key or check constraint violation when inserting a row into the child table.
   */
  void checkInsert(Session session, Table table, Object[] row) {

    switch (constType) {
      case CHECK:
        if (!isNotNull) {
          checkCheckConstraint(session, table, row);
        }

        return;

      case FOREIGN_KEY:
        PersistentStore store = session.sessionData.getRowStore(core.mainTable);

        if (ArrayUtil.hasNull(row, core.refCols)) {
          if (core.matchType == OpTypes.MATCH_SIMPLE) {
            return;
          }

          if (core.refCols.length == 1) {
            return;
          }

          if (ArrayUtil.hasAllNull(row, core.refCols)) {
            return;
          }

          // core.matchType == OpTypes.MATCH_FULL
        } else if (core.mainIndex.exists(session, store, row, core.refCols)) {
          return;
        } else if (core.mainTable == core.refTable) {

          // special case: self referencing table and self referencing row
          int compare = core.mainIndex.compareRowNonUnique(row, core.refCols, row);

          if (compare == 0) {
            return;
          }
        }

        String[] info = new String[] {core.refName.name, core.mainTable.getName().name};

        throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info);
    }
  }
예제 #2
0
 void checkInsert(
     Session paramSession, Table paramTable, Object[] paramArrayOfObject, boolean paramBoolean) {
   switch (this.constType) {
     case 3:
       if (!this.isNotNull) checkCheckConstraint(paramSession, paramTable, paramArrayOfObject);
       return;
     case 0:
       PersistentStore localPersistentStore = this.core.mainTable.getRowStore(paramSession);
       if (ArrayUtil.hasNull(paramArrayOfObject, this.core.refCols)) {
         if (this.core.matchType == 59) return;
         if (this.core.refCols.length == 1) return;
         if (!ArrayUtil.hasAllNull(paramArrayOfObject, this.core.refCols)) ;
       } else if (this.core.mainIndex.existsParent(
           paramSession, localPersistentStore, paramArrayOfObject, this.core.refCols)) {
         return;
       }
       throw getException(paramArrayOfObject);
   }
 }
예제 #3
0
  /**
   * Check used before creating a new foreign key cosntraint, this method checks all rows of a table
   * to ensure they all have a corresponding row in the main table.
   */
  void checkReferencedRows(Session session, Table table, int[] rowColArray) {

    Index mainIndex = getMainIndex();
    PersistentStore store = session.sessionData.getRowStore(table);
    RowIterator it = table.rowIterator(session);

    while (true) {
      Row row = it.getNextRow();

      if (row == null) {
        break;
      }

      Object[] rowData = row.getData();

      if (ArrayUtil.hasNull(rowData, rowColArray)) {
        if (core.matchType == OpTypes.MATCH_SIMPLE) {
          continue;
        }
      } else if (mainIndex.exists(session, store, rowData, rowColArray)) {
        continue;
      }

      if (ArrayUtil.hasAllNull(rowData, rowColArray)) {
        continue;
      }

      String colValues = "";

      for (int i = 0; i < rowColArray.length; i++) {
        Object o = rowData[rowColArray[i]];

        colValues += table.getColumnTypes()[i].convertToString(o);
        colValues += ",";
      }

      String[] info = new String[] {getName().name, getMain().getName().name};

      throw Error.error(ErrorCode.X_23502, ErrorCode.CONSTRAINT, info);
    }
  }