001: /**
002: * com.mckoi.database.interpret.ConstraintDef 09 Sep 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.interpret;
024:
025: import com.mckoi.database.*;
026: import java.util.ArrayList;
027:
028: /**
029: * Represents a constraint definition (description) for a table.
030: *
031: * @author Tobias Downer
032: */
033:
034: public final class ConstraintDef implements java.io.Serializable,
035: StatementTreeObject, Cloneable {
036:
037: static final long serialVersionUID = -6648793780645431100L;
038:
039: // ---------- Statics that represent the base types of constraints ----------
040:
041: /**
042: * A PRIMARY_KEY constraint. With this constraint, the 'column_list'
043: * list contains the names of the columns in this table that are defined as
044: * the primary key. There may only be one primary key constraint per table.
045: */
046: public static final int PRIMARY_KEY = 1;
047:
048: /**
049: * A UNIQUE constraint. With this constraint, the 'column_list' list
050: * contains the names of the columns in this table that must be unique.
051: */
052: public static final int UNIQUE = 2;
053:
054: /**
055: * A FOREIGN_KEY constraint. With this constraint, the 'table_name' string
056: * contains the name of the table that this is a foreign key for, the
057: * 'column_list' list contains the list of foreign key columns, and
058: * 'column_list2' optionally contains the referenced columns.
059: */
060: public static final int FOREIGN_KEY = 3;
061:
062: /**
063: * A CHECK constraint. With this constraint, the 'expression' object
064: * contains the expression that must evaluate to true when adding a
065: * column to the table.
066: */
067: public static final int CHECK = 4;
068:
069: // The type of constraint (from types in DataTableConstraintDef)
070: int type;
071:
072: // The name of the constraint or null if the constraint has no name (in
073: // which case it must be given an auto generated unique name at some point).
074: String name;
075:
076: // The Check Expression
077: Expression check_expression;
078: // The serializable plain check expression as originally parsed
079: Expression original_check_expression;
080:
081: // The first column list
082: ArrayList column_list;
083:
084: // The second column list
085: ArrayList column_list2;
086:
087: // The name of the table if referenced.
088: String reference_table_name;
089:
090: // The foreign key update rule
091: String update_rule;
092:
093: // The foreign key delete rule
094: String delete_rule;
095:
096: // Whether this constraint is deferred to when the transaction commits.
097: // ( By default we are 'initially immediate deferrable' )
098: short deferred = Transaction.INITIALLY_IMMEDIATE;
099:
100: public ConstraintDef() {
101: }
102:
103: /**
104: * Sets the name of the constraint.
105: */
106: public void setName(String name) {
107: this .name = name;
108: }
109:
110: /**
111: * Sets object up for a primary key constraint.
112: */
113: public void setPrimaryKey(ArrayList list) {
114: type = PRIMARY_KEY;
115: column_list = list;
116: }
117:
118: /**
119: * Sets object up for a unique constraint.
120: */
121: public void setUnique(ArrayList list) {
122: type = UNIQUE;
123: column_list = list;
124: }
125:
126: /**
127: * Sets object up for a check constraint.
128: */
129: public void setCheck(Expression exp) {
130: type = CHECK;
131: check_expression = exp;
132: try {
133: original_check_expression = (Expression) exp.clone();
134: } catch (CloneNotSupportedException e) {
135: throw new Error(e.getMessage());
136: }
137: }
138:
139: /**
140: * Sets object up for foreign key reference.
141: */
142: public void setForeignKey(String ref_table, ArrayList col_list,
143: ArrayList ref_col_list, String delete_rule,
144: String update_rule) {
145: type = FOREIGN_KEY;
146: reference_table_name = ref_table;
147: column_list = col_list;
148: column_list2 = ref_col_list;
149: this .delete_rule = delete_rule;
150: this .update_rule = update_rule;
151:
152: // System.out.println("ConstraintDef setting rules: " + delete_rule + ", " + update_rule);
153: }
154:
155: /**
156: * Sets that this constraint is initially deferred.
157: */
158: public void setInitiallyDeferred() {
159: deferred = Transaction.INITIALLY_DEFERRED;
160: }
161:
162: /**
163: * Sets that this constraint is not deferrable.
164: */
165: public void setNotDeferrable() {
166: deferred = Transaction.NOT_DEFERRABLE;
167: }
168:
169: /**
170: * Returns the first column list as a string array.
171: */
172: public String[] getColumnList() {
173: return (String[]) column_list.toArray(new String[column_list
174: .size()]);
175: }
176:
177: /**
178: * Returns the first column list as a string array.
179: */
180: public String[] getColumnList2() {
181: return (String[]) column_list2.toArray(new String[column_list2
182: .size()]);
183: }
184:
185: /**
186: * Returns the delete rule if this is a foreign key reference.
187: */
188: public String getDeleteRule() {
189: return delete_rule;
190: }
191:
192: /**
193: * Returns the update rule if this is a foreign key reference.
194: */
195: public String getUpdateRule() {
196: return update_rule;
197: }
198:
199: // Implemented from StatementTreeObject
200: public void prepareExpressions(ExpressionPreparer preparer)
201: throws DatabaseException {
202: if (check_expression != null) {
203: check_expression.prepare(preparer);
204: }
205: }
206:
207: public Object clone() throws CloneNotSupportedException {
208: ConstraintDef v = (ConstraintDef) super .clone();
209: if (check_expression != null) {
210: v.check_expression = (Expression) check_expression.clone();
211: }
212: if (column_list != null) {
213: v.column_list = (ArrayList) column_list.clone();
214: }
215: if (column_list2 != null) {
216: v.column_list2 = (ArrayList) column_list2.clone();
217: }
218: return v;
219: }
220:
221: }
|