📄 constraint.java
字号:
/* Copyright (c) 1995-2000, The Hypersonic SQL Group.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the Hypersonic SQL Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Hypersonic SQL Group.
*
*
* For work added by the HSQL Development Group:
*
* Copyright (c) 2001-2005, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb;
import org.hsqldb.HsqlNameManager.HsqlName;
import org.hsqldb.index.RowIterator;
import org.hsqldb.lib.ArrayUtil;
import org.hsqldb.lib.Iterator;
// fredt@users 20020225 - patch 1.7.0 by boucherb@users - named constraints
// fredt@users 20020320 - doc 1.7.0 - update
// tony_lai@users 20020820 - patch 595156 - violation of Integrity constraint name
/**
* Implementation of a table constraint with references to the indexes used
* by the constraint.<p>
*
* @author Thomas Mueller (Hypersonic SQL Group)
* @version 1.8.0
* @since Hypersonic SQL
*/
class Constraint {
/*
SQL CLI codes
Referential Constraint 0 CASCADE
Referential Constraint 1 RESTRICT
Referential Constraint 2 SET NULL
Referential Constraint 3 NO ACTION
Referential Constraint 4 SET DEFAULT
*/
static final int CASCADE = 0,
SET_NULL = 2,
NO_ACTION = 3,
SET_DEFAULT = 4,
INIT_DEFERRED = 5,
INIT_IMMEDIATE = 6,
NOT_DEFERRABLE = 7;
static final int FOREIGN_KEY = 0,
MAIN = 1,
UNIQUE = 2,
CHECK = 3,
PRIMARY_KEY = 4;
ConstraintCore core;
HsqlName constName;
int constType;
/**
* Constructor declaration for PK and UNIQUE */
Constraint(HsqlName name, Table t, Index index, int type) {
core = new ConstraintCore();
constName = name;
constType = type; core.mainTable = t;
core.mainIndex = index;
/* fredt - in unique constraints column list for iColMain is the
visible columns of iMain
*/
core.mainColArray = ArrayUtil.arraySlice(index.getColumns(), 0,
index.getVisibleColumns());
core.colLen = core.mainColArray.length;
}
/**
* Constructor for main constraints (foreign key references in PK table)
*/
Constraint(HsqlName name, Constraint fkconstraint) {
constName = name;
constType = MAIN;
core = fkconstraint.core;
}
/**
* Constructor for foreign key constraints.
*
* @param pkname name in the main (referenced) table, used internally
* @param name name in the referencing table, public name of the constraint
* @param mainTable referenced table
* @param refTable referencing talbe
* @param mainCols array of column indexes in main table
* @param refCols array of column indexes in referencing table
* @param mainIndex index on the main table
* @param refIndex index on the referencing table
* @param deleteAction triggered action on delete
* @param updateAction triggered action on update
* @exception HsqlException
*/
Constraint(HsqlName pkname, HsqlName name, Table mainTable,
Table refTable, int[] mainCols, int[] refCols,
Index mainIndex, Index refIndex, int deleteAction,
int updateAction) throws HsqlException {
core = new ConstraintCore();
core.pkName = pkname;
core.fkName = name;
constName = name;
constType = FOREIGN_KEY;
core.mainTable = mainTable;
core.refTable = refTable;
/* fredt - in FK constraints column lists for iColMain and iColRef have
identical sets to visible columns of iMain and iRef respectively
but the order of columns can be different and must be preserved
*/
core.mainColArray = mainCols;
core.colLen = core.mainColArray.length;
core.refColArray = refCols;
core.mainIndex = mainIndex;
core.refIndex = refIndex;
core.deleteAction = deleteAction;
core.updateAction = updateAction;
}
/**
* temp constraint constructor
*/
Constraint(HsqlName name, int[] mainCols, Table refTable, int[] refCols,
int type, int deleteAction, int updateAction) {
core = new ConstraintCore();
constName = name;
constType = type;
core.mainColArray = mainCols;
core.refTable = refTable;
core.refColArray = refCols;
core.deleteAction = deleteAction;
core.updateAction = updateAction;
}
private Constraint() {}
/**
* Returns the HsqlName.
*/
HsqlName getName() {
return constName;
}
/**
* Changes constraint name.
*/
private void setName(String name, boolean isquoted) throws HsqlException {
constName.rename(name, isquoted);
}
/**
* probably a misnomer, but DatabaseMetaData.getCrossReference specifies
* it this way (I suppose because most FKs are declared against the PK of
* another table)
*
* @return name of the index refereneced by a foreign key
*/
String getPkName() {
return core.pkName == null ? null
: core.pkName.name;
}
/**
* probably a misnomer, but DatabaseMetaData.getCrossReference specifies
* it this way (I suppose because most FKs are declared against the PK of
* another table)
*
* @return name of the index for the referencing foreign key
*/
String getFkName() {
return core.fkName == null ? null
: core.fkName.name;
}
/**
* Returns the type of constraint
*/
int getType() {
return constType;
}
/**
* Returns the main table
*/
Table getMain() {
return core.mainTable;
}
/**
* Returns the main index
*/
Index getMainIndex() {
return core.mainIndex;
}
/**
* Returns the reference table
*/
Table getRef() {
return core.refTable;
}
/**
* Returns the reference index
*/
Index getRefIndex() {
return core.refIndex;
}
/**
* The ON DELETE triggered action of (foreign key) constraint
*/
int getDeleteAction() {
return core.deleteAction;
}
/**
* The ON UPDATE triggered action of (foreign key) constraint
*/
int getUpdateAction() {
return core.updateAction;
}
/**
* Returns the main table column index array
*/
int[] getMainColumns() {
return core.mainColArray;
}
/**
* Returns the reference table column index array
*/
int[] getRefColumns() {
return core.refColArray;
}
/**
* Returns true if an index is part this constraint and the constraint is set for
* a foreign key. Used for tests before dropping an index.
*/
boolean isIndexFK(Index index) {
if (constType == FOREIGN_KEY || constType == MAIN) {
if (core.mainIndex == index || core.refIndex == index) {
return true;
}
}
return false;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -