📄 constraint.java
字号:
*/ int[] getMainColumns() { return core.iColMain; } /** * Method declaration * * @return */ int[] getRefColumns() { return core.iColRef; } /** * See if an index is part this constraint and the constraint is set for * a foreign key. Used for tests before dropping an index. (fredt@users) * * @return */ boolean isIndexFK(Index index) { if (iType == FOREIGN_KEY || iType == MAIN) { if (core.iMain == index || core.iRef == index) { return true; } } return false; } /** * See if an index is part this constraint and the constraint is set for * a unique constraint. Used for tests before dropping an index. * (fredt@users) * * @return */ boolean isIndexUnique(Index index) { if (iType == UNIQUE && core.iMain == index) { return true; } return false; }// fredt@users 20020225 - patch 1.7.0 by fredt - duplicate constraints /** * Compares this with another constraint column set. This implementation * only checks UNIQUE constraints. */ boolean isEquivalent(int col[], int type) { if (type == iType && iType == UNIQUE && core.iLen == col.length) { if (ArrayUtil.haveEqualSets(core.iColMain, col, core.iLen)) { return true; } } return false; } /** * Used to update constrains to reflect structural changes in a table. * * @param oldt reference to the old version of the table * @param newt referenct to the new version of the table * @param colindex index at which table column is added or removed * @param adjust -1, 0, +1 to indicate if column is added or removed * @throws SQLException */ void replaceTable(Table oldt, Table newt, int colindex, int adjust) throws SQLException { if (oldt == core.tMain) { core.tMain = newt; setTableRows(); core.iMain = core.tMain.getIndex(core.iMain.getName().name); core.iColMain = ArrayUtil.getAdjustedColumnArray(core.iColMain, core.iLen, colindex, adjust); } if (oldt == core.tRef) { core.tRef = newt; setTableRows(); if (core.iRef != null) { core.iRef = core.tRef.getIndex(core.iRef.getName().name); if (core.iRef != core.iMain) { core.iColRef = ArrayUtil.getAdjustedColumnArray(core.iColRef, core.iLen, colindex, adjust); } } } } /** * Checks for foreign key violation when inserting a row in the child * table. * * @param row * @throws SQLException */ void checkInsert(Object row[]) throws SQLException { if ((iType == MAIN) || (iType == UNIQUE)) { // inserts in the main table are never a problem // unique constraints are checked by the unique index return; } // must be called synchronized because of oMain for (int i = 0; i < core.iLen; i++) { Object o = row[core.iColRef[i]]; if (o == null) { // if one column is null then integrity is not checked return; } core.oMain[core.iColMain[i]] = o; } // a record must exist in the main table Trace.check(core.iMain.find(core.oMain) != null, Trace.INTEGRITY_CONSTRAINT_VIOLATION, core.fkName.name + " table: " + core.tMain.getName().name); } /** * Check if a row in the referenced (parent) table can be deleted. Used * only for UPDATE table statements. Checks for DELETE FROM table * statements are now handled by findFkRef() to support ON DELETE * CASCADE. * * @param row * @throws SQLException */ private void checkDelete(Object row[]) throws SQLException { // must be called synchronized because of oRef for (int i = 0; i < core.iLen; i++) { Object o = row[core.iColMain[i]]; if (o == null) { // if one column is null then integrity is not checked return; } core.oRef[core.iColRef[i]] = o; } // there must be no record in the 'slave' table Node node = core.iRef.find(core.oRef); // tony_lai@users 20020820 - patch 595156 Trace.check(node == null, Trace.INTEGRITY_CONSTRAINT_VIOLATION, core.fkName.name + " table: " + core.tRef.getName().name); }// fredt@users 20020225 - patch 1.7.0 - cascading deletes /** * New method to find any referencing node (containing the row) for a * foreign key (finds row in child table). If ON DELETE CASCADE is * supported by this constraint, then the method finds the first row * among the rows of the table ordered by the index and doesn't throw. * Without ON DELETE CASCADE, the method attempts to finds any row that * exists, in which case it throws an exception. If no row is found, * null is returned. * (fredt@users) * * @param array of objects for a database row * @return Node object or null * @throws SQLException */ Node findFkRef(Object row[]) throws SQLException { // must be called synchronized because of oRef for (int i = 0; i < core.iLen; i++) { Object o = row[core.iColMain[i]]; if (o == null) { // if one column is null then integrity is not checked return null; } core.oColRef[i] = o; } // there must be no record in the 'slave' table Node node = core.iRef.findSimple(core.oColRef, core.bCascade); // tony_lai@users 20020820 - patch 595156 Trace.check((node == null) || core.bCascade, Trace.INTEGRITY_CONSTRAINT_VIOLATION, core.fkName.name + " table: " + core.tRef.getName().name); return node; } /** * Checks if updating a set of columns in a table row breaks the * referential integrity constraint. * * @param col array of column indexes for columns to check * @param deleted rows to delete * @param inserted rows to insert * @throws SQLException */ void checkUpdate(int col[], Result deleted, Result inserted) throws SQLException { if (iType == UNIQUE) { // unique constraints are checked by the unique index return; } if (iType == MAIN) { if (!ArrayUtil.haveCommonElement(col, core.iColMain, core.iLen)) { return; } // check deleted records Record r = deleted.rRoot; while (r != null) { // if an identical record exists we don't have to test if (core.iMain.find(r.data) == null) { checkDelete(r.data); } r = r.next; } } else if (iType == FOREIGN_KEY) { if (!ArrayUtil.haveCommonElement(col, core.iColMain, core.iLen)) { return; } // check inserted records Record r = inserted.rRoot; while (r != null) { checkInsert(r.data); r = r.next; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -