001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.constraint;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.engine.DbObject;
011: import org.h2.engine.Session;
012: import org.h2.index.Index;
013: import org.h2.message.Message;
014: import org.h2.message.Trace;
015: import org.h2.result.Row;
016: import org.h2.schema.Schema;
017: import org.h2.schema.SchemaObjectBase;
018: import org.h2.table.Column;
019: import org.h2.table.Table;
020:
021: /**
022: * The base class for constraint checking.
023: */
024: public abstract class Constraint extends SchemaObjectBase implements
025: Comparable {
026:
027: /**
028: * The constraint type name for check constraints.
029: */
030: public static final String CHECK = "CHECK";
031:
032: /**
033: * The constraint type name for referential constraints.
034: */
035: public static final String REFERENTIAL = "REFERENTIAL";
036:
037: /**
038: * The constraint type name for unique constraints.
039: */
040: public static final String UNIQUE = "UNIQUE";
041:
042: /**
043: * The constraint type name for primary key constraints.
044: */
045: public static final String PRIMARY_KEY = "PRIMARY KEY";
046:
047: /**
048: * The table for which this constraint is defined.
049: */
050: protected Table table;
051:
052: /**
053: * The constraint type name
054: *
055: * @return the name
056: */
057: public abstract String getConstraintType();
058:
059: /**
060: * Check if this row fulfils the constraint.
061: * This method throws an exception if not.
062: *
063: * @param session the session
064: * @param t the table
065: * @param oldRow the old row
066: * @param newRow the new row
067: */
068: public abstract void checkRow(Session session, Table t, Row oldRow,
069: Row newRow) throws SQLException;
070:
071: /**
072: * Check if this constraint needs the specified index.
073: *
074: * @param index the index
075: * @return true if the index is used
076: */
077: public abstract boolean usesIndex(Index index);
078:
079: /**
080: * This index is now the owner of the specified index.
081: *
082: * @param index
083: */
084: public abstract void setIndexOwner(Index index);
085:
086: /**
087: * Check if this constraint contains the given column.
088: *
089: * @param col the column
090: * @return true if it does
091: */
092: public abstract boolean containsColumn(Column col);
093:
094: /**
095: * Get the SQL statement to create this constraint.
096: *
097: * @return the SQL statement
098: */
099: public abstract String getCreateSQLWithoutIndexes();
100:
101: /**
102: * Check if this constraint needs to be checked before updating the data.
103: *
104: * @return true if it must be checked before updating
105: */
106: public abstract boolean isBefore();
107:
108: /**
109: * Get a short description of the constraint. This includes the constraint
110: * name (if set), and the constraint expression.
111: *
112: * @return the description
113: */
114: public abstract String getShortDescription();
115:
116: /**
117: * Check the existing data. This method is called if the constraint is added
118: * after data has been inserted into the table.
119: *
120: * @param session the session
121: */
122: public abstract void checkExistingData(Session session)
123: throws SQLException;
124:
125: public Constraint(Schema schema, int id, String name, Table table) {
126: super (schema, id, name, Trace.CONSTRAINT);
127: this .table = table;
128: this .setTemporary(table.getTemporary());
129: }
130:
131: public void checkRename() throws SQLException {
132: // ok
133: }
134:
135: public int getType() {
136: return DbObject.CONSTRAINT;
137: }
138:
139: public Table getTable() {
140: return table;
141: }
142:
143: public Table getRefTable() {
144: return table;
145: }
146:
147: public String getDropSQL() {
148: return null;
149: }
150:
151: private int getConstraintTypeOrder() {
152: String constraintType = getConstraintType();
153: if (CHECK.equals(constraintType)) {
154: return 0;
155: } else if (PRIMARY_KEY.equals(constraintType)) {
156: return 1;
157: } else if (UNIQUE.equals(constraintType)) {
158: return 2;
159: } else if (REFERENTIAL.equals(constraintType)) {
160: return 3;
161: } else {
162: throw Message.getInternalError("type: " + constraintType);
163: }
164: }
165:
166: public int compareTo(Object other) {
167: if (this == other) {
168: return 0;
169: }
170: Constraint otherConstraint = (Constraint) other;
171: int this Type = getConstraintTypeOrder();
172: int otherType = otherConstraint.getConstraintTypeOrder();
173: return thisType - otherType;
174: }
175:
176: }
|