KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > ConstraintDef


1 /**
2  * com.mckoi.database.interpret.ConstraintDef 09 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * Represents a constraint definition (description) for a table.
32  *
33  * @author Tobias Downer
34  */

35
36 public final class ConstraintDef
37             implements java.io.Serializable JavaDoc, StatementTreeObject, Cloneable JavaDoc {
38
39   static final long serialVersionUID = -6648793780645431100L;
40
41   // ---------- Statics that represent the base types of constraints ----------
42

43   /**
44    * A PRIMARY_KEY constraint. With this constraint, the 'column_list'
45    * list contains the names of the columns in this table that are defined as
46    * the primary key. There may only be one primary key constraint per table.
47    */

48   public static final int PRIMARY_KEY = 1;
49
50   /**
51    * A UNIQUE constraint. With this constraint, the 'column_list' list
52    * contains the names of the columns in this table that must be unique.
53    */

54   public static final int UNIQUE = 2;
55
56   /**
57    * A FOREIGN_KEY constraint. With this constraint, the 'table_name' string
58    * contains the name of the table that this is a foreign key for, the
59    * 'column_list' list contains the list of foreign key columns, and
60    * 'column_list2' optionally contains the referenced columns.
61    */

62   public static final int FOREIGN_KEY = 3;
63
64   /**
65    * A CHECK constraint. With this constraint, the 'expression' object
66    * contains the expression that must evaluate to true when adding a
67    * column to the table.
68    */

69   public static final int CHECK = 4;
70
71
72   // The type of constraint (from types in DataTableConstraintDef)
73
int type;
74
75   // The name of the constraint or null if the constraint has no name (in
76
// which case it must be given an auto generated unique name at some point).
77
String JavaDoc name;
78
79   // The Check Expression
80
Expression check_expression;
81   // The serializable plain check expression as originally parsed
82
Expression original_check_expression;
83
84   // The first column list
85
ArrayList JavaDoc column_list;
86
87   // The second column list
88
ArrayList JavaDoc column_list2;
89
90   // The name of the table if referenced.
91
String JavaDoc reference_table_name;
92
93   // The foreign key update rule
94
String JavaDoc update_rule;
95
96   // The foreign key delete rule
97
String JavaDoc delete_rule;
98
99   // Whether this constraint is deferred to when the transaction commits.
100
// ( By default we are 'initially immediate deferrable' )
101
short deferred = Transaction.INITIALLY_IMMEDIATE;
102
103   public ConstraintDef() {
104   }
105
106   /**
107    * Sets the name of the constraint.
108    */

109   public void setName(String JavaDoc name) {
110     this.name = name;
111   }
112
113   /**
114    * Sets object up for a primary key constraint.
115    */

116   public void setPrimaryKey(ArrayList JavaDoc list) {
117     type = PRIMARY_KEY;
118     column_list = list;
119   }
120
121   /**
122    * Sets object up for a unique constraint.
123    */

124   public void setUnique(ArrayList JavaDoc list) {
125     type = UNIQUE;
126     column_list = list;
127   }
128
129   /**
130    * Sets object up for a check constraint.
131    */

132   public void setCheck(Expression exp) {
133     type = CHECK;
134     check_expression = exp;
135     try {
136       original_check_expression = (Expression) exp.clone();
137     }
138     catch (CloneNotSupportedException JavaDoc e) {
139       throw new Error JavaDoc(e.getMessage());
140     }
141   }
142
143   /**
144    * Sets object up for foreign key reference.
145    */

146   public void setForeignKey(String JavaDoc ref_table, ArrayList JavaDoc col_list,
147                             ArrayList JavaDoc ref_col_list,
148                             String JavaDoc delete_rule, String JavaDoc update_rule) {
149     type = FOREIGN_KEY;
150     reference_table_name = ref_table;
151     column_list = col_list;
152     column_list2 = ref_col_list;
153     this.delete_rule = delete_rule;
154     this.update_rule = update_rule;
155
156 // System.out.println("ConstraintDef setting rules: " + delete_rule + ", " + update_rule);
157
}
158
159   /**
160    * Sets that this constraint is initially deferred.
161    */

162   public void setInitiallyDeferred() {
163     deferred = Transaction.INITIALLY_DEFERRED;
164   }
165
166   /**
167    * Sets that this constraint is not deferrable.
168    */

169   public void setNotDeferrable() {
170     deferred = Transaction.NOT_DEFERRABLE;
171   }
172
173
174
175   /**
176    * Returns the first column list as a string array.
177    */

178   public String JavaDoc[] getColumnList() {
179     return (String JavaDoc[]) column_list.toArray(new String JavaDoc[column_list.size()]);
180   }
181
182   /**
183    * Returns the first column list as a string array.
184    */

185   public String JavaDoc[] getColumnList2() {
186     return (String JavaDoc[]) column_list2.toArray(new String JavaDoc[column_list2.size()]);
187   }
188
189   /**
190    * Returns the delete rule if this is a foreign key reference.
191    */

192   public String JavaDoc getDeleteRule() {
193     return delete_rule;
194   }
195
196   /**
197    * Returns the update rule if this is a foreign key reference.
198    */

199   public String JavaDoc getUpdateRule() {
200     return update_rule;
201   }
202
203
204   // Implemented from StatementTreeObject
205
public void prepareExpressions(ExpressionPreparer preparer)
206                                                   throws DatabaseException {
207     if (check_expression != null) {
208       check_expression.prepare(preparer);
209     }
210   }
211
212   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
213     ConstraintDef v = (ConstraintDef) super.clone();
214     if (check_expression != null) {
215       v.check_expression = (Expression) check_expression.clone();
216     }
217     if (column_list != null) {
218       v.column_list = (ArrayList JavaDoc) column_list.clone();
219     }
220     if (column_list2 != null) {
221       v.column_list2 = (ArrayList JavaDoc) column_list2.clone();
222     }
223     return v;
224   }
225
226 }
227
Popular Tags