001: /**
002: * com.mckoi.database.interpret.ColumnDef 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 com.mckoi.database.sql.ParseException;
027: import com.mckoi.database.sql.SQLConstants;
028: import com.mckoi.database.sql.Token;
029: import com.mckoi.database.global.SQLTypes;
030: import java.util.ArrayList;
031:
032: /**
033: * Represents a column definition (description).
034: *
035: * @author Tobias Downer
036: */
037:
038: public final class ColumnDef implements java.io.Serializable,
039: StatementTreeObject, Cloneable {
040:
041: static final long serialVersionUID = 8347617136528650961L;
042:
043: // DataTableColumnDef col;
044:
045: String name;
046:
047: // int sql_type;
048: // int size;
049: // int scale;
050: // String class_constraint;
051: //
052: // String locale_str;
053: // int strength;
054: // int decomposition;
055:
056: TType type;
057: String index_str;
058:
059: Expression default_expression;
060: Expression original_default_expression;
061:
062: private boolean not_null = false;
063: private boolean primary_key = false;
064: private boolean unique = false;
065:
066: public ColumnDef() {
067: // col = new DataTableColumnDef();
068: }
069:
070: /**
071: * Returns true if this column has a primary key constraint set on it.
072: */
073: public boolean isPrimaryKey() {
074: return primary_key;
075: }
076:
077: /**
078: * Returns true if this column has the unique constraint set for it.
079: */
080: public boolean isUnique() {
081: return unique;
082: }
083:
084: /**
085: * Returns true if this column has the not null constraint set for it.
086: */
087: public boolean isNotNull() {
088: return not_null;
089: }
090:
091: /**
092: * Sets the name of the column.
093: */
094: public void setName(String name) {
095: this .name = name;
096: }
097:
098: /**
099: * Adds a constraint to this column.
100: */
101: public void addConstraint(String constraint) {
102: if (constraint.equals("NOT NULL")) {
103: not_null = true;
104: // col.setNotNull(true);
105: } else if (constraint.equals("NULL")) {
106: not_null = false;
107: // col.setNotNull(false);
108: } else if (constraint.equals("PRIMARY")) {
109: primary_key = true;
110: } else if (constraint.equals("UNIQUE")) {
111: unique = true;
112: } else {
113: throw new RuntimeException("Unknown constraint: "
114: + constraint);
115: }
116: }
117:
118: /**
119: * Sets the type of data of this column.
120: */
121: public void setDataType(TType type) {
122: this .type = type;
123: }
124:
125: // /**
126: // * Sets the type of data this column is.
127: // */
128: // public void setDataType(String type, int size, int scale)
129: // throws ParseException {
130: // int data_type;
131: //
132: // String ltype = type.toLowerCase();
133: // if (ltype.equals("bit") || ltype.equals("boolean")) {
134: // data_type = SQLTypes.BIT;
135: // if (size != -1 || scale != -1) {
136: // throw new ParseException("size/scale for bit.");
137: // }
138: // }
139: // else if (ltype.equals("tinyint")) {
140: // data_type = SQLTypes.TINYINT;
141: // }
142: // else if (ltype.equals("smallint")) {
143: // data_type = SQLTypes.SMALLINT;
144: // }
145: // else if (ltype.equals("integer") || ltype.equals("int")) {
146: // data_type = SQLTypes.INTEGER;
147: // }
148: // else if (ltype.equals("bigint")) {
149: // data_type = SQLTypes.BIGINT;
150: // }
151: // else if (ltype.equals("float")) {
152: // data_type = SQLTypes.FLOAT;
153: // }
154: // else if (ltype.equals("real")) {
155: // data_type = SQLTypes.REAL;
156: // }
157: // else if (ltype.equals("double")) {
158: // data_type = SQLTypes.DOUBLE;
159: // }
160: // else if (ltype.equals("numeric")) {
161: // data_type = SQLTypes.NUMERIC;
162: // }
163: // else if (ltype.equals("decimal")) {
164: // data_type = SQLTypes.DECIMAL;
165: // }
166: // else if (ltype.equals("char")) {
167: // data_type = SQLTypes.CHAR;
168: // if (scale != -1) {
169: // throw new ParseException("scale for char.");
170: // }
171: // if (size == -1) {
172: // size = 1;
173: // }
174: // }
175: // else if (ltype.equals("varchar")) {
176: // data_type = SQLTypes.VARCHAR;
177: // if (scale != -1) {
178: // throw new ParseException("scale for varchar.");
179: // }
180: // if (size == -1) size = Integer.MAX_VALUE;
181: // }
182: // else if (ltype.equals("longvarchar") || ltype.equals("string") ||
183: // ltype.equals("text") ) {
184: // data_type = SQLTypes.LONGVARCHAR;
185: // if (scale != -1) {
186: // throw new ParseException("scale for longvarchar.");
187: // }
188: // if (size == -1) size = Integer.MAX_VALUE;
189: // }
190: // else if (ltype.equals("date")) {
191: // data_type = SQLTypes.DATE;
192: // if (size != -1 || scale != -1) {
193: // throw new ParseException("size/scale for date.");
194: // }
195: // }
196: // else if (ltype.equals("time")) {
197: // data_type = SQLTypes.TIME;
198: // if (size != -1 || scale != -1) {
199: // throw new ParseException("size/scale for time.");
200: // }
201: // }
202: // else if (ltype.equals("timestamp")) {
203: // data_type = SQLTypes.TIMESTAMP;
204: // if (size != -1 || scale != -1) {
205: // throw new ParseException("size/scale for timestamp.");
206: // }
207: // }
208: // else if (ltype.equals("binary")) {
209: // data_type = SQLTypes.BINARY;
210: // if (scale != -1) {
211: // throw new ParseException("scale for binary.");
212: // }
213: // if (size == -1) {
214: // size = Integer.MAX_VALUE;
215: // }
216: // }
217: // else if (ltype.equals("varbinary")) {
218: // data_type = SQLTypes.VARBINARY;
219: // if (scale != -1) {
220: // throw new ParseException("scale for varbinary.");
221: // }
222: // if (size == -1) {
223: // size = Integer.MAX_VALUE;
224: // }
225: // }
226: // else if (ltype.equals("longvarbinary") ||
227: // ltype.equals("blob")) {
228: // data_type = SQLTypes.LONGVARBINARY;
229: // if (scale != -1) {
230: // throw new ParseException("scale for longvarbinary.");
231: // }
232: // if (size == -1) {
233: // size = Integer.MAX_VALUE;
234: // }
235: // }
236: // else {
237: // throw new ParseException("Unknown type: " + ltype);
238: // }
239: //
240: // this.sql_type = data_type;
241: // this.size = size;
242: // this.scale = scale;
243: //
244: // }
245: //
246: // /**
247: // * Sets the column definition for a java object type.
248: // */
249: // public void setDataType(String type, Token class_ref) {
250: // if (!type.equals("JAVA_OBJECT")) {
251: // throw new Error("setDataType called with incorrect type.");
252: // }
253: //
254: // // Default class constraint is 'java.lang.Object'
255: // String class_constraint = "java.lang.Object";
256: // if (class_ref != null) {
257: // class_constraint = class_ref.image;
258: // }
259: //
260: // this.sql_type = SQLTypes.JAVA_OBJECT;
261: // this.size = -1;
262: // this.scale = -1;
263: // this.class_constraint = class_constraint;
264: //
265: // }
266: //
267: // /**
268: // * Sets the locale, and collate strength and decomposition of this string
269: // * column. If strength or decomposition are -1 then use the default
270: // * strength and decomposition levels.
271: // */
272: // public void setCollateType(String locale_str,
273: // int strength, int decomposition) {
274: // this.locale_str = locale_str;
275: // this.strength = strength;
276: // this.decomposition = decomposition;
277: // }
278:
279: /**
280: * Sets the indexing.
281: */
282: public void setIndex(Token t) throws ParseException {
283: if (t.kind == SQLConstants.INDEX_NONE) {
284: index_str = "BlindSearch";
285: // col.setIndexScheme("BlindSearch");
286: } else if (t.kind == SQLConstants.INDEX_BLIST) {
287: index_str = "InsertSearch";
288: // col.setIndexScheme("InsertSearch");
289: } else {
290: throw new ParseException("Unrecognized indexing scheme.");
291: }
292: }
293:
294: /**
295: * Sets the default expression (this is used to make a new constraint).
296: */
297: public void setDefaultExpression(Expression exp) {
298: default_expression = exp;
299: try {
300: original_default_expression = (Expression) exp.clone();
301: } catch (CloneNotSupportedException e) {
302: throw new Error(e.getMessage());
303: }
304: }
305:
306: // Implemented from StatementTreeObject
307: public void prepareExpressions(ExpressionPreparer preparer)
308: throws DatabaseException {
309: if (default_expression != null) {
310: default_expression.prepare(preparer);
311: }
312: }
313:
314: public Object clone() throws CloneNotSupportedException {
315: ColumnDef v = (ColumnDef) super .clone();
316: if (default_expression != null) {
317: v.default_expression = (Expression) default_expression
318: .clone();
319: }
320: return v;
321: }
322:
323: }
|