KickJava   Java API By Example, From Geeks To Geeks.

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


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 SoftwareFoundation, 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.sql.Expr;
34 import com.caucho.db.sql.QueryContext;
35 import com.caucho.db.sql.SelectResult;
36 import com.caucho.db.store.Transaction;
37 import com.caucho.sql.SQLExceptionWrapper;
38 import com.caucho.util.QDate;
39
40 import java.sql.SQLException JavaDoc;
41
42 /**
43  * Represents a 64-bit long date column.
44  */

45 class DateColumn extends Column {
46   private static QDate _gmtDate = new QDate();
47   /**
48    * Creates a date column.
49    *
50    * @param row the row the column is being added to
51    * @param name the column's name
52    */

53   DateColumn(Row row, String JavaDoc name)
54   {
55     super(row, name);
56   }
57
58   /**
59    * Returns the column's type code.
60    */

61   public int getTypeCode()
62   {
63     return DATE;
64   }
65
66   /**
67    * Returns the column's Java type.
68    */

69   public Class JavaDoc getJavaType()
70   {
71     return java.sql.Date JavaDoc.class;
72   }
73   
74   /**
75    * Returns the column's declaration size.
76    */

77   public int getDeclarationSize()
78   {
79     return 8;
80   }
81   
82   /**
83    * Returns the column's length
84    */

85   public int getLength()
86   {
87     return 8;
88   }
89   
90   /**
91    * Sets a string value in the column.
92    *
93    * @param block the block's buffer
94    * @param rowOffset the offset of the row in the block
95    * @param value the value to store
96    */

97   void setString(Transaction xa, byte []block, int rowOffset, String JavaDoc str)
98     throws SQLException JavaDoc
99   {
100     if (str == null)
101       setNull(block, rowOffset);
102     else {
103       long date = 0;
104
105       try {
106     synchronized (_gmtDate) {
107       date = _gmtDate.parseDate(str);
108     }
109       } catch (Exception JavaDoc e) {
110     throw new SQLExceptionWrapper(e);
111       }
112       
113       setDate(xa, block, rowOffset, date);
114     }
115   }
116   
117   /**
118    * Gets a string value from the column.
119    *
120    * @param block the block's buffer
121    * @param rowOffset the offset of the row in the block
122    */

123   public String JavaDoc getString(byte []block, int rowOffset)
124   {
125     if (isNull(block, rowOffset))
126       return null;
127     else
128       return QDate.formatISO8601(getDate(block, rowOffset));
129   }
130   
131   /**
132    * Sets a date value in the column.
133    *
134    * @param block the block's buffer
135    * @param rowOffset the offset of the row in the block
136    * @param value the value to store
137    */

138   void setDate(Transaction xa, byte []block, int rowOffset, long value)
139   {
140     int offset = rowOffset + _columnOffset;
141     
142     block[offset++] = (byte) (value >> 56);
143     block[offset++] = (byte) (value >> 48);
144     block[offset++] = (byte) (value >> 40);
145     block[offset++] = (byte) (value >> 32);
146     block[offset++] = (byte) (value >> 24);
147     block[offset++] = (byte) (value >> 16);
148     block[offset++] = (byte) (value >> 8);
149     block[offset++] = (byte) (value);
150
151     setNonNull(block, rowOffset);
152   }
153   
154   /**
155    * Gets a date value from the column.
156    *
157    * @param block the block's buffer
158    * @param rowOffset the offset of the row in the block
159    */

160   public long getDate(byte []block, int rowOffset)
161   {
162     if (isNull(block, rowOffset))
163       return 0;
164     
165     int offset = rowOffset + _columnOffset;
166     long value = 0;
167     
168     value = (block[offset++] & 0xffL) << 56;
169     value |= (block[offset++] & 0xffL) << 48;
170     value |= (block[offset++] & 0xffL) << 40;
171     value |= (block[offset++] & 0xffL) << 32;
172     value |= (block[offset++] & 0xffL) << 24;
173     value |= (block[offset++] & 0xffL) << 16;
174     value |= (block[offset++] & 0xffL) << 8;
175     value |= (block[offset++] & 0xffL);
176
177     return value;
178   }
179   
180   /**
181    * Sets a long value in the column.
182    *
183    * @param block the block's buffer
184    * @param rowOffset the offset of the row in the block
185    * @param value the value to store
186    */

187   void setLong(Transaction xa, byte []block, int rowOffset, long value)
188   {
189     setDate(xa, block, rowOffset, value);
190   }
191   
192   /**
193    * Gets a long value in the column.
194    *
195    * @param block the block's buffer
196    * @param rowOffset the offset of the row in the block
197    */

198   public long getLong(byte []block, int rowOffset)
199   {
200     return getDate(block, rowOffset);
201   }
202   
203   /**
204    * Sets a long value in the column.
205    *
206    * @param block the block's buffer
207    * @param rowOffset the offset of the row in the block
208    * @param value the value to store
209    */

210   void setInteger(Transaction xa, byte []block, int rowOffset, int value)
211   {
212     setDate(xa, block, rowOffset, value);
213   }
214   
215   /**
216    * Sets a long value in the column.
217    *
218    * @param block the block's buffer
219    * @param rowOffset the offset of the row in the block
220    * @param value the value to store
221    */

222   public int getInteger(byte []block, int rowOffset)
223   {
224     return (int) getDate(block, rowOffset);
225   }
226
227   /**
228    * Evaluates the column to a stream.
229    */

230   public void evalToResult(byte []block, int rowOffset, SelectResult result)
231   {
232     if (isNull(block, rowOffset)) {
233       result.writeNull();
234       return;
235     }
236
237     result.writeDate(getDate(block, rowOffset));
238   }
239   
240   /**
241    * Evaluate to a buffer.
242    *
243    * @param block the block's buffer
244    * @param rowOffset the offset of the row in the block
245    * @param buffer the result buffer
246    * @param buffer the result buffer offset
247    *
248    * @return the length of the value
249    */

250   int evalToBuffer(byte []block, int rowOffset,
251            byte []buffer, int bufferOffset)
252     throws SQLException JavaDoc
253   {
254     if (isNull(block, rowOffset))
255       return 0;
256
257     int startOffset = rowOffset + _columnOffset;
258     int len = 8;
259
260     System.arraycopy(block, startOffset, buffer, bufferOffset, len);
261
262     return len;
263   }
264   
265   /**
266    * Sets the column based on an expression.
267    *
268    * @param block the block's buffer
269    * @param rowOffset the offset of the row in the block
270    * @param expr the expression to store
271    */

272   void setExpr(Transaction xa,
273            byte []block, int rowOffset,
274            Expr expr, QueryContext context)
275     throws SQLException JavaDoc
276   {
277     if (expr.isNull(null))
278       setNull(block, rowOffset);
279     else
280       setDate(xa, block, rowOffset, expr.evalDate(context));
281   }
282
283   /**
284    * Returns true if the items in the given rows match.
285    */

286   public boolean isEqual(byte []block1, int rowOffset1,
287              byte []block2, int rowOffset2)
288   {
289     //System.out.println("EQ:");
290
if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2))
291       return false;
292
293     int startOffset1 = rowOffset1 + _columnOffset;
294     int startOffset2 = rowOffset2 + _columnOffset;
295
296     return (block1[startOffset1 + 0] == block2[startOffset2 + 0] &&
297         block1[startOffset1 + 1] == block2[startOffset2 + 1] &&
298         block1[startOffset1 + 2] == block2[startOffset2 + 2] &&
299         block1[startOffset1 + 3] == block2[startOffset2 + 3] &&
300         block1[startOffset1 + 4] == block2[startOffset2 + 4] &&
301         block1[startOffset1 + 5] == block2[startOffset2 + 5] &&
302         block1[startOffset1 + 6] == block2[startOffset2 + 6] &&
303         block1[startOffset1 + 7] == block2[startOffset2 + 7]);
304   }
305   
306   /**
307    * Sets any index for the column.
308    *
309    * @param block the block's buffer
310    * @param rowOffset the offset of the row in the block
311    * @param rowAddr the address of the row
312    */

313   void setIndex(Transaction xa,
314         byte []block, int rowOffset,
315         long rowAddr, QueryContext context)
316     throws SQLException JavaDoc
317   {
318     BTree index = getIndex();
319
320     if (index == null)
321       return;
322
323     index.insert(block, rowOffset + _columnOffset, 8, rowAddr, xa, false);
324   }
325
326   /**
327    * Sets based on an iterator.
328    */

329   public void set(TableIterator iter, Expr expr, QueryContext context)
330     throws SQLException JavaDoc
331   {
332     iter.setDirty();
333     setDate(iter.getTransaction(), iter.getBuffer(), iter.getRowOffset(),
334         expr.evalDate(context));
335   }
336   
337   /**
338    * Deleting the row, based on the column.
339    *
340    * @param block the block's buffer
341    * @param rowOffset the offset of the row in the block
342    * @param expr the expression to store
343    */

344   void delete(Transaction xa, byte []block, int rowOffset)
345     throws SQLException JavaDoc
346   {
347     BTree index = getIndex();
348
349     if (index != null)
350       index.remove(block, rowOffset + _columnOffset, 8, xa);
351   }
352 }
353
Popular Tags