KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.ColumnDef 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 com.mckoi.database.sql.ParseException;
29 import com.mckoi.database.sql.SQLConstants;
30 import com.mckoi.database.sql.Token;
31 import com.mckoi.database.global.SQLTypes;
32 import java.util.ArrayList JavaDoc;
33
34 /**
35  * Represents a column definition (description).
36  *
37  * @author Tobias Downer
38  */

39
40 public final class ColumnDef
41             implements java.io.Serializable JavaDoc, StatementTreeObject, Cloneable JavaDoc {
42
43   static final long serialVersionUID = 8347617136528650961L;
44
45 // DataTableColumnDef col;
46

47   String JavaDoc name;
48
49 // int sql_type;
50
// int size;
51
// int scale;
52
// String class_constraint;
53
//
54
// String locale_str;
55
// int strength;
56
// int decomposition;
57

58   TType type;
59   String JavaDoc index_str;
60
61   Expression default_expression;
62   Expression original_default_expression;
63
64   private boolean not_null = false;
65   private boolean primary_key = false;
66   private boolean unique = false;
67
68   public ColumnDef() {
69 // col = new DataTableColumnDef();
70
}
71
72   /**
73    * Returns true if this column has a primary key constraint set on it.
74    */

75   public boolean isPrimaryKey() {
76     return primary_key;
77   }
78
79   /**
80    * Returns true if this column has the unique constraint set for it.
81    */

82   public boolean isUnique() {
83     return unique;
84   }
85
86   /**
87    * Returns true if this column has the not null constraint set for it.
88    */

89   public boolean isNotNull() {
90     return not_null;
91   }
92
93   /**
94    * Sets the name of the column.
95    */

96   public void setName(String JavaDoc name) {
97     this.name = name;
98   }
99
100   /**
101    * Adds a constraint to this column.
102    */

103   public void addConstraint(String JavaDoc constraint) {
104     if (constraint.equals("NOT NULL")) {
105       not_null = true;
106 // col.setNotNull(true);
107
}
108     else if (constraint.equals("NULL")) {
109       not_null = false;
110 // col.setNotNull(false);
111
}
112     else if (constraint.equals("PRIMARY")) {
113       primary_key = true;
114     }
115     else if (constraint.equals("UNIQUE")) {
116       unique = true;
117     }
118     else {
119       throw new RuntimeException JavaDoc("Unknown constraint: " + constraint);
120     }
121   }
122
123   /**
124    * Sets the type of data of this column.
125    */

126   public void setDataType(TType type) {
127     this.type = type;
128   }
129   
130 // /**
131
// * Sets the type of data this column is.
132
// */
133
// public void setDataType(String type, int size, int scale)
134
// throws ParseException {
135
// int data_type;
136
//
137
// String ltype = type.toLowerCase();
138
// if (ltype.equals("bit") || ltype.equals("boolean")) {
139
// data_type = SQLTypes.BIT;
140
// if (size != -1 || scale != -1) {
141
// throw new ParseException("size/scale for bit.");
142
// }
143
// }
144
// else if (ltype.equals("tinyint")) {
145
// data_type = SQLTypes.TINYINT;
146
// }
147
// else if (ltype.equals("smallint")) {
148
// data_type = SQLTypes.SMALLINT;
149
// }
150
// else if (ltype.equals("integer") || ltype.equals("int")) {
151
// data_type = SQLTypes.INTEGER;
152
// }
153
// else if (ltype.equals("bigint")) {
154
// data_type = SQLTypes.BIGINT;
155
// }
156
// else if (ltype.equals("float")) {
157
// data_type = SQLTypes.FLOAT;
158
// }
159
// else if (ltype.equals("real")) {
160
// data_type = SQLTypes.REAL;
161
// }
162
// else if (ltype.equals("double")) {
163
// data_type = SQLTypes.DOUBLE;
164
// }
165
// else if (ltype.equals("numeric")) {
166
// data_type = SQLTypes.NUMERIC;
167
// }
168
// else if (ltype.equals("decimal")) {
169
// data_type = SQLTypes.DECIMAL;
170
// }
171
// else if (ltype.equals("char")) {
172
// data_type = SQLTypes.CHAR;
173
// if (scale != -1) {
174
// throw new ParseException("scale for char.");
175
// }
176
// if (size == -1) {
177
// size = 1;
178
// }
179
// }
180
// else if (ltype.equals("varchar")) {
181
// data_type = SQLTypes.VARCHAR;
182
// if (scale != -1) {
183
// throw new ParseException("scale for varchar.");
184
// }
185
// if (size == -1) size = Integer.MAX_VALUE;
186
// }
187
// else if (ltype.equals("longvarchar") || ltype.equals("string") ||
188
// ltype.equals("text") ) {
189
// data_type = SQLTypes.LONGVARCHAR;
190
// if (scale != -1) {
191
// throw new ParseException("scale for longvarchar.");
192
// }
193
// if (size == -1) size = Integer.MAX_VALUE;
194
// }
195
// else if (ltype.equals("date")) {
196
// data_type = SQLTypes.DATE;
197
// if (size != -1 || scale != -1) {
198
// throw new ParseException("size/scale for date.");
199
// }
200
// }
201
// else if (ltype.equals("time")) {
202
// data_type = SQLTypes.TIME;
203
// if (size != -1 || scale != -1) {
204
// throw new ParseException("size/scale for time.");
205
// }
206
// }
207
// else if (ltype.equals("timestamp")) {
208
// data_type = SQLTypes.TIMESTAMP;
209
// if (size != -1 || scale != -1) {
210
// throw new ParseException("size/scale for timestamp.");
211
// }
212
// }
213
// else if (ltype.equals("binary")) {
214
// data_type = SQLTypes.BINARY;
215
// if (scale != -1) {
216
// throw new ParseException("scale for binary.");
217
// }
218
// if (size == -1) {
219
// size = Integer.MAX_VALUE;
220
// }
221
// }
222
// else if (ltype.equals("varbinary")) {
223
// data_type = SQLTypes.VARBINARY;
224
// if (scale != -1) {
225
// throw new ParseException("scale for varbinary.");
226
// }
227
// if (size == -1) {
228
// size = Integer.MAX_VALUE;
229
// }
230
// }
231
// else if (ltype.equals("longvarbinary") ||
232
// ltype.equals("blob")) {
233
// data_type = SQLTypes.LONGVARBINARY;
234
// if (scale != -1) {
235
// throw new ParseException("scale for longvarbinary.");
236
// }
237
// if (size == -1) {
238
// size = Integer.MAX_VALUE;
239
// }
240
// }
241
// else {
242
// throw new ParseException("Unknown type: " + ltype);
243
// }
244
//
245
// this.sql_type = data_type;
246
// this.size = size;
247
// this.scale = scale;
248
//
249
// }
250
//
251
// /**
252
// * Sets the column definition for a java object type.
253
// */
254
// public void setDataType(String type, Token class_ref) {
255
// if (!type.equals("JAVA_OBJECT")) {
256
// throw new Error("setDataType called with incorrect type.");
257
// }
258
//
259
// // Default class constraint is 'java.lang.Object'
260
// String class_constraint = "java.lang.Object";
261
// if (class_ref != null) {
262
// class_constraint = class_ref.image;
263
// }
264
//
265
// this.sql_type = SQLTypes.JAVA_OBJECT;
266
// this.size = -1;
267
// this.scale = -1;
268
// this.class_constraint = class_constraint;
269
//
270
// }
271
//
272
// /**
273
// * Sets the locale, and collate strength and decomposition of this string
274
// * column. If strength or decomposition are -1 then use the default
275
// * strength and decomposition levels.
276
// */
277
// public void setCollateType(String locale_str,
278
// int strength, int decomposition) {
279
// this.locale_str = locale_str;
280
// this.strength = strength;
281
// this.decomposition = decomposition;
282
// }
283

284   /**
285    * Sets the indexing.
286    */

287   public void setIndex(Token t) throws ParseException {
288     if (t.kind == SQLConstants.INDEX_NONE) {
289       index_str = "BlindSearch";
290 // col.setIndexScheme("BlindSearch");
291
}
292     else if (t.kind == SQLConstants.INDEX_BLIST) {
293       index_str = "InsertSearch";
294 // col.setIndexScheme("InsertSearch");
295
}
296     else {
297       throw new ParseException("Unrecognized indexing scheme.");
298     }
299   }
300
301   /**
302    * Sets the default expression (this is used to make a new constraint).
303    */

304   public void setDefaultExpression(Expression exp) {
305     default_expression = exp;
306     try {
307       original_default_expression = (Expression) exp.clone();
308     }
309     catch (CloneNotSupportedException JavaDoc e) {
310       throw new Error JavaDoc(e.getMessage());
311     }
312   }
313
314
315   // Implemented from StatementTreeObject
316
public void prepareExpressions(ExpressionPreparer preparer)
317                                                   throws DatabaseException {
318     if (default_expression != null) {
319       default_expression.prepare(preparer);
320     }
321   }
322
323   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
324     ColumnDef v = (ColumnDef) super.clone();
325     if (default_expression != null) {
326       v.default_expression = (Expression) default_expression.clone();
327     }
328     return v;
329   }
330
331 }
332
Popular Tags