?? constraintreferential.java
字號:
/*
* Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.constraint;
import java.sql.SQLException;
import org.h2.command.Parser;
import org.h2.command.Prepared;
import org.h2.constant.ErrorCode;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.expression.Parameter;
import org.h2.index.Cursor;
import org.h2.index.Index;
import org.h2.message.Message;
import org.h2.result.LocalResult;
import org.h2.result.Row;
import org.h2.result.SearchRow;
import org.h2.schema.Schema;
import org.h2.table.Column;
import org.h2.table.IndexColumn;
import org.h2.table.Table;
import org.h2.util.ObjectArray;
import org.h2.util.StringUtils;
import org.h2.value.Value;
import org.h2.value.ValueNull;
/**
* A referential constraint.
*/
public class ConstraintReferential extends Constraint {
public static final int RESTRICT = 0, CASCADE = 1, SET_DEFAULT = 2, SET_NULL = 3;
private int deleteAction;
private int updateAction;
private Table refTable;
private Index index;
private Index refIndex;
private boolean indexOwner;
private boolean refIndexOwner;
protected IndexColumn[] columns;
protected IndexColumn[] refColumns;
private String deleteSQL, updateSQL;
private boolean skipOwnTable;
public ConstraintReferential(Schema schema, int id, String name, Table table) {
super(schema, id, name, table);
}
public String getConstraintType() {
return Constraint.REFERENTIAL;
}
private void appendAction(StringBuffer buff, int action) {
switch (action) {
case CASCADE:
buff.append("CASCADE");
break;
case SET_DEFAULT:
buff.append("SET DEFAULT");
break;
case SET_NULL:
buff.append("SET NULL");
break;
default:
throw Message.getInternalError("action=" + action);
}
}
/**
* Create the SQL statement of this object so a copy of the table can be made.
*
* @param table the table to create the object for
* @param quotedName the name of this object (quoted if necessary)
* @return the SQL statement
*/
public String getCreateSQLForCopy(Table table, String quotedName) {
return getCreateSQLForCopy(table, refTable, quotedName, true);
}
/**
* Create the SQL statement of this object so a copy of the table can be made.
*
* @param table the table to create the object for
* @param refTable the referenced table
* @param quotedName the name of this object (quoted if necessary)
* @param internalIndex add the index name to the statement
* @return the SQL statement
*/
public String getCreateSQLForCopy(Table table, Table refTable, String quotedName, boolean internalIndex) {
StringBuffer buff = new StringBuffer();
buff.append("ALTER TABLE ");
String mainTable = table.getSQL();
buff.append(mainTable);
buff.append(" ADD CONSTRAINT ");
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ");
buff.append(StringUtils.quoteStringSQL(comment));
}
IndexColumn[] cols = columns;
IndexColumn[] refCols = refColumns;
buff.append(" FOREIGN KEY(");
for (int i = 0; i < cols.length; i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(cols[i].getSQL());
}
buff.append(")");
if (internalIndex && indexOwner && table == this.table) {
buff.append(" INDEX ");
buff.append(index.getSQL());
}
buff.append(" REFERENCES ");
String quotedRefTable;
if (this.table == this.refTable) {
// self-referencing constraints: need to use new table
quotedRefTable = table.getSQL();
} else {
quotedRefTable = refTable.getSQL();
}
buff.append(quotedRefTable);
buff.append("(");
for (int i = 0; i < refCols.length; i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(refCols[i].getSQL());
}
buff.append(")");
if (internalIndex && refIndexOwner && table == this.table) {
buff.append(" INDEX ");
buff.append(refIndex.getSQL());
}
if (deleteAction != RESTRICT) {
buff.append(" ON DELETE ");
appendAction(buff, deleteAction);
}
if (updateAction != RESTRICT) {
buff.append(" ON UPDATE ");
appendAction(buff, updateAction);
}
buff.append(" NOCHECK");
return buff.toString();
}
public String getShortDescription() {
StringBuffer buff = new StringBuffer();
buff.append(getName());
buff.append(": ");
buff.append(table.getSQL());
buff.append(" FOREIGN KEY(");
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(columns[i].getSQL());
}
buff.append(")");
buff.append(" REFERENCES ");
buff.append(refTable.getSQL());
buff.append("(");
for (int i = 0; i < refColumns.length; i++) {
if (i > 0) {
buff.append(", ");
}
buff.append(refColumns[i].getSQL());
}
buff.append(")");
return buff.toString();
}
public String getCreateSQLWithoutIndexes() {
return getCreateSQLForCopy(table, refTable, getSQL(), false);
}
public String getCreateSQL() {
return getCreateSQLForCopy(table, getSQL());
}
public void setColumns(IndexColumn[] cols) {
columns = cols;
}
public IndexColumn[] getColumns() {
return columns;
}
public void setRefColumns(IndexColumn[] refCols) {
refColumns = refCols;
}
public IndexColumn[] getRefColumns() {
return refColumns;
}
public void setRefTable(Table refTable) {
this.refTable = refTable;
if (refTable.getTemporary()) {
setTemporary(true);
}
}
public void setIndex(Index index, boolean isOwner) {
this.index = index;
this.indexOwner = isOwner;
}
public void setRefIndex(Index refIndex, boolean isRefOwner) {
this.refIndex = refIndex;
this.refIndexOwner = isRefOwner;
}
public void removeChildrenAndResources(Session session) throws SQLException {
table.removeConstraint(this);
refTable.removeConstraint(this);
if (indexOwner) {
table.removeIndexOrTransferOwnership(session, index);
}
if (refIndexOwner) {
refTable.removeIndexOrTransferOwnership(session, refIndex);
}
database.removeMeta(session, getId());
refTable = null;
index = null;
refIndex = null;
columns = null;
refColumns = null;
deleteSQL = null;
updateSQL = null;
table = null;
invalidate();
}
public void checkRow(Session session, Table t, Row oldRow, Row newRow) throws SQLException {
if (!database.getReferentialIntegrity()) {
return;
}
if (!table.getCheckForeignKeyConstraints() || !refTable.getCheckForeignKeyConstraints()) {
return;
}
if (t == table) {
if (!skipOwnTable) {
checkRowOwnTable(session, newRow);
}
}
if (t == refTable) {
checkRowRefTable(session, oldRow, newRow);
}
}
private void checkRowOwnTable(Session session, Row newRow) throws SQLException {
if (newRow == null) {
return;
}
boolean containsNull = false;
for (int i = 0; i < columns.length; i++) {
int idx = columns[i].column.getColumnId();
Value v = newRow.getValue(idx);
if (v == ValueNull.INSTANCE) {
containsNull = true;
break;
}
}
if (containsNull) {
return;
}
if (refTable == table) {
// special case self referencing constraints: check the inserted row
// first
boolean self = true;
for (int i = 0; i < columns.length; i++) {
int idx = columns[i].column.getColumnId();
Value v = newRow.getValue(idx);
Column refCol = refColumns[i].column;
int refIdx = refCol.getColumnId();
Value r = newRow.getValue(refIdx);
if (!database.areEqual(r, v)) {
self = false;
break;
}
}
if (self) {
return;
}
}
Row check = refTable.getTemplateRow();
for (int i = 0; i < columns.length; i++) {
int idx = columns[i].column.getColumnId();
Value v = newRow.getValue(idx);
Column refCol = refColumns[i].column;
int refIdx = refCol.getColumnId();
check.setValue(refIdx, v.convertTo(refCol.getType()));
}
if (!found(session, refIndex, check)) {
throw Message.getSQLException(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1,
getShortDescription());
}
}
private boolean found(Session session, Index index, SearchRow check) throws SQLException {
index.getTable().lock(session, false, false);
Cursor cursor = index.find(session, check, check);
while (cursor.next()) {
SearchRow found;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -