KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > table > Column


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.table;
31
32 import com.caucho.db.index.BTree;
33 import com.caucho.db.index.KeyCompare;
34 import com.caucho.db.sql.Expr;
35 import com.caucho.db.sql.QueryContext;
36 import com.caucho.db.sql.SelectResult;
37 import com.caucho.db.store.Transaction;
38 import com.caucho.log.Log;
39
40 import java.sql.SQLException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 abstract public class Column {
44   protected final static Logger JavaDoc log = Log.open(Column.class);
45   
46   public final static int NONE = 0;
47   public final static int VARCHAR = 1;
48   public final static int INT = 2;
49   public final static int LONG = 3;
50   public final static int DOUBLE = 4;
51   public final static int DATE = 5;
52   public final static int BLOB = 6;
53   public final static int NUMERIC = 7;
54   
55   private final Row _row;
56   private final String JavaDoc _name;
57
58   protected final int _columnOffset;
59   protected final int _nullOffset;
60   protected final byte _nullMask;
61
62   private Table _table;
63   
64   private boolean _isPrimaryKey;
65   private boolean _isUnique;
66   private boolean _isNotNull;
67   private int _autoIncrementMin = -1;
68   private Expr _defaultExpr;
69
70   private BTree _index;
71
72   Column(Row row, String JavaDoc name)
73   {
74     _row = row;
75     _name = name;
76
77     _columnOffset = _row.getLength();
78     _nullOffset = _row.getNullOffset();
79     _nullMask = _row.getNullMask();
80   }
81   
82   /**
83    * Returns the column's name.
84    */

85   public String JavaDoc getName()
86   {
87     return _name;
88   }
89
90   /**
91    * Sets the table.
92    */

93   void setTable(Table table)
94   {
95     _table = table;
96   }
97
98   /**
99    * Gets the table.
100    */

101   public Table getTable()
102   {
103     return _table;
104   }
105
106   /**
107    * Returns the column offset.
108    */

109   int getColumnOffset()
110   {
111     return _columnOffset;
112   }
113
114   /**
115    * Returns the column's code.
116    */

117   abstract public int getTypeCode();
118
119   /**
120    * Returns the java type.
121    */

122   public Class JavaDoc getJavaType()
123   {
124     return Object JavaDoc.class;
125   }
126
127   /**
128    * Returns true if the column is a primary key
129    */

130   public boolean isPrimaryKey()
131   {
132     return _isPrimaryKey;
133   }
134
135   /**
136    * Returns true if the column is a primary key
137    */

138   public void setPrimaryKey(boolean primaryKey)
139   {
140     _isPrimaryKey = primaryKey;
141   }
142
143   /**
144    * Returns true if the column is unique.
145    */

146   public boolean isUnique()
147   {
148     return _isUnique;
149   }
150
151   /**
152    * Set if the column is unique.
153    */

154   public void setUnique()
155   {
156     _isUnique = true;
157   }
158
159   /**
160    * Returns the index.
161    */

162   public BTree getIndex()
163   {
164     return _index;
165   }
166
167   /**
168    * Sets the index.
169    */

170   public void setIndex(BTree index)
171   {
172     _index = index;
173   }
174
175   /**
176    * Returns the key compare for the column.
177    */

178   public KeyCompare getIndexKeyCompare()
179   {
180     return null;
181   }
182
183   /**
184    * Set true if the column is NOT NULL.
185    */

186   public void setNotNull()
187   {
188     _isNotNull = true;
189   }
190
191   /**
192    * Returns true if the column is NOT NULL.
193    */

194   public boolean isNotNull()
195   {
196     return _isNotNull;
197   }
198
199   /**
200    * Sets the default expression
201    */

202   public void setDefault(Expr expr)
203   {
204     _defaultExpr = expr;
205   }
206
207   /**
208    * Gets the default expression
209    */

210   public Expr getDefault()
211   {
212     return _defaultExpr;
213   }
214
215   /**
216    * Returns true if the column is auto increment
217    */

218   public void setAutoIncrement(int min)
219   {
220     _autoIncrementMin = min;
221   }
222
223   /**
224    * Set if the column is unique.
225    */

226   public int getAutoIncrement()
227   {
228     return _autoIncrementMin;
229   }
230
231   /**
232    * Returns the column's size (from the decl).
233    */

234   abstract public int getDeclarationSize();
235   
236   abstract int getLength();
237
238   /**
239    * Returns true if the column is null.
240    *
241    * @param block the block's buffer
242    * @param rowOffset the offset of the row in the block
243    */

244   public final boolean isNull(byte []block, int rowOffset)
245   {
246     return (block[rowOffset + _nullOffset] & _nullMask) == 0;
247   }
248
249   /**
250    * Sets the column null.
251    *
252    * @param block the block's buffer
253    * @param rowOffset the offset of the row in the block
254    */

255   public final void setNull(byte []block, int rowOffset)
256   {
257     block[rowOffset + _nullOffset] &= ~_nullMask;
258   }
259
260   /**
261    * Sets the column non-null.
262    *
263    * @param block the block's buffer
264    * @param rowOffset the offset of the row in the block
265    */

266   protected final void setNonNull(byte []block, int rowOffset)
267   {
268     block[rowOffset + _nullOffset] |= _nullMask;
269   }
270
271   /**
272    * Gets a string value from the column.
273    *
274    * @param block the block's buffer
275    * @param rowOffset the offset of the row in the block
276    */

277   public abstract String JavaDoc getString(byte []block, int rowOffset)
278     throws SQLException JavaDoc;
279
280   /**
281    * Sets a string value in the column.
282    *
283    * @param block the block's buffer
284    * @param rowOffset the offset of the row in the block
285    * @param value the value to store
286    */

287   abstract void setString(Transaction xa,
288               byte []block, int rowOffset, String JavaDoc value)
289     throws SQLException JavaDoc;
290   
291   /**
292    * Sets an integer value in the column.
293    *
294    * @param block the block's buffer
295    * @param rowOffset the offset of the row in the block
296    * @param value the value to store
297    */

298   public int getInteger(byte []block, int rowOffset)
299     throws SQLException JavaDoc
300   {
301     String JavaDoc str = getString(block, rowOffset);
302
303     if (str == null)
304       return 0;
305
306     return Integer.parseInt(str);
307   }
308   
309   /**
310    * Sets an integer value in the column.
311    *
312    * @param block the block's buffer
313    * @param rowOffset the offset of the row in the block
314    * @param value the value to store
315    */

316   void setInteger(Transaction xa,
317           byte []block, int rowOffset, int value)
318     throws SQLException JavaDoc
319   {
320     setString(xa, block, rowOffset, String.valueOf(value));
321   }
322   
323   /**
324    * Sets a long value in the column.
325    *
326    * @param block the block's buffer
327    * @param rowOffset the offset of the row in the block
328    * @param value the value to store
329    */

330   public long getLong(byte []block, int rowOffset)
331     throws SQLException JavaDoc
332   {
333     String JavaDoc str = getString(block, rowOffset);
334
335     if (str == null)
336       return 0;
337
338     return Long.parseLong(str);
339   }
340   
341   /**
342    * Sets a long value in the column.
343    *
344    * @param block the block's buffer
345    * @param rowOffset the offset of the row in the block
346    * @param value the value to store
347    */

348   void setLong(Transaction xa,
349            byte []block, int rowOffset, long value)
350     throws SQLException JavaDoc
351   {
352     setString(xa, block, rowOffset, String.valueOf(value));
353   }
354   
355   /**
356    * Sets a double value in the column.
357    *
358    * @param block the block's buffer
359    * @param rowOffset the offset of the row in the block
360    * @param value the value to store
361    */

362   public double getDouble(byte []block, int rowOffset)
363     throws SQLException JavaDoc
364   {
365     String JavaDoc str = getString(block, rowOffset);
366
367     if (str == null)
368       return 0;
369
370     return Double.parseDouble(str);
371   }
372   
373   /**
374    * Sets a double value in the column.
375    *
376    * @param block the block's buffer
377    * @param rowOffset the offset of the row in the block
378    * @param value the value to store
379    */

380   void setDouble(Transaction xa,
381          byte []block, int rowOffset, double value)
382     throws SQLException JavaDoc
383   {
384     setString(xa, block, rowOffset, String.valueOf(value));
385   }
386   
387   /**
388    * Sets the column based on an expression.
389    *
390    * @param block the block's buffer
391    * @param rowOffset the offset of the row in the block
392    * @param expr the expression to store
393    */

394   void setExpr(Transaction xa,
395            byte []block, int rowOffset,
396            Expr expr, QueryContext context)
397     throws SQLException JavaDoc
398   {
399     if (expr.isNull(context))
400       setNull(block, rowOffset);
401     else
402       setString(xa, block, rowOffset, expr.evalString(context));
403   }
404   
405   /**
406    * Gets a double value in the column.
407    *
408    * @param block the block's buffer
409    * @param rowOffset the offset of the row in the block
410    */

411   public long getDate(byte []block, int rowOffset)
412     throws SQLException JavaDoc
413   {
414     throw new UnsupportedOperationException JavaDoc();
415   }
416   
417   /**
418    * Sets a date value in the column.
419    *
420    * @param block the block's buffer
421    * @param rowOffset the offset of the row in the block
422    * @param value the value to store
423    */

424   void setDate(Transaction xa, byte []block, int rowOffset, double value)
425     throws SQLException JavaDoc
426   {
427     throw new UnsupportedOperationException JavaDoc();
428   }
429   
430   /**
431    * Evaluate to a buffer.
432    *
433    * @param block the block's buffer
434    * @param rowOffset the offset of the row in the block
435    * @param buffer the result buffer
436    * @param buffer the result buffer offset
437    *
438    * @return the length of the value
439    */

440   int evalToBuffer(byte []block, int rowOffset,
441            byte []buffer, int bufferOffset)
442     throws SQLException JavaDoc
443   {
444     throw new UnsupportedOperationException JavaDoc(getClass().getName());
445   }
446
447   /**
448    * Returns true if the bytes are equal.
449    */

450   public boolean isEqual(byte []block, int rowOffset,
451              byte []buffer, int offset, int length)
452   {
453     return false;
454   }
455
456   /**
457    * Returns true if the bytes are equal.
458    */

459   public boolean isEqual(byte []buffer1, int rowOffset1,
460              byte []buffer2, int rowOffset2)
461   {
462     throw new UnsupportedOperationException JavaDoc();
463   }
464
465   /**
466    * Returns true if the string is equal.
467    */

468   public boolean isEqual(byte []block, int rowOffset, String JavaDoc string)
469   {
470     return false;
471   }
472
473   /**
474    * Evaluates the column to the result.
475    */

476   public void evalToResult(byte []block, int rowOffset, SelectResult result)
477     throws SQLException JavaDoc
478   {
479     throw new UnsupportedOperationException JavaDoc(getClass().getName());
480   }
481
482   /**
483    * Sets based on an iterator.
484    */

485   public void set(Transaction xa,
486           TableIterator iter, Expr expr, QueryContext context)
487     throws SQLException JavaDoc
488   {
489     setString(xa, iter.getBuffer(), iter.getRowOffset(),
490           expr.evalString(context));
491     
492     iter.setDirty();
493   }
494   
495   /**
496    * Sets any index for the column.
497    *
498    * @param block the block's buffer
499    * @param rowOffset the offset of the row in the block
500    * @param rowAddr the address of the row
501    */

502   void setIndex(Transaction xa,
503         byte []block, int rowOffset,
504         long rowAddr, QueryContext context)
505     throws SQLException JavaDoc
506   {
507   }
508   
509   /**
510    * Deleting the row, based on the column.
511    *
512    * @param block the block's buffer
513    * @param rowOffset the offset of the row in the block
514    * @param expr the expression to store
515    */

516   void delete(Transaction xa, byte []block, int rowOffset)
517     throws SQLException JavaDoc
518   {
519   }
520
521   public String JavaDoc toString()
522   {
523     if (getIndex() != null)
524       return getClass().getName() + "[" + _name + ",index]";
525     else
526       return getClass().getName() + "[" + _name + "]";
527   }
528 }
529
Popular Tags