KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.table;
30
31 import com.caucho.db.index.BTree;
32 import com.caucho.db.sql.Expr;
33 import com.caucho.db.sql.QueryContext;
34 import com.caucho.db.sql.SelectResult;
35 import com.caucho.db.store.Transaction;
36
37 import java.sql.SQLException JavaDoc;
38
39 /**
40  * Represents a 64-bit long integer column.
41  */

42 class LongColumn extends Column {
43   /**
44    * Creates a long column.
45    *
46    * @param row the row the column is being added to
47    * @param name the column's name
48    */

49   LongColumn(Row row, String JavaDoc name)
50   {
51     super(row, name);
52   }
53
54   /**
55    * Returns the column's type code.
56    */

57   public int getTypeCode()
58   {
59     return LONG;
60   }
61
62   /**
63    * Returns the column's Java type.
64    */

65   public Class JavaDoc getJavaType()
66   {
67     return long.class;
68   }
69   
70   /**
71    * Returns the column's declaration size.
72    */

73   public int getDeclarationSize()
74   {
75     return 8;
76   }
77   
78   /**
79    * Returns the column's length
80    */

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

93   void setString(Transaction xa, byte []block, int rowOffset, String JavaDoc str)
94   {
95     if (str == null)
96       setNull(block, rowOffset);
97     else
98       setLong(xa, block, rowOffset, Long.parseLong(str));
99   }
100   
101   /**
102    * Gets a string value from the column.
103    *
104    * @param block the block's buffer
105    * @param rowOffset the offset of the row in the block
106    */

107   public String JavaDoc getString(byte []block, int rowOffset)
108   {
109     if (isNull(block, rowOffset))
110       return null;
111     else
112       return String.valueOf(getLong(block, rowOffset));
113   }
114   
115   /**
116    * Sets an integer value in the column.
117    *
118    * @param block the block's buffer
119    * @param rowOffset the offset of the row in the block
120    * @param value the value to store
121    */

122   void setInteger(Transaction xa, byte []block, int rowOffset, int value)
123   {
124     setLong(xa, block, rowOffset, value);
125   }
126   
127   /**
128    * Gets an integer value from the column.
129    *
130    * @param block the block's buffer
131    * @param rowOffset the offset of the row in the block
132    */

133   public int getInteger(byte []block, int rowOffset)
134   {
135     return (int) getLong(block, rowOffset);
136   }
137   
138   /**
139    * Sets a long value in the column.
140    *
141    * @param block the block's buffer
142    * @param rowOffset the offset of the row in the block
143    * @param value the value to store
144    */

145   void setLong(Transaction xa, byte []block, int rowOffset, long value)
146   {
147     int offset = rowOffset + _columnOffset;
148     
149     block[offset++] = (byte) (value >> 56);
150     block[offset++] = (byte) (value >> 48);
151     block[offset++] = (byte) (value >> 40);
152     block[offset++] = (byte) (value >> 32);
153     block[offset++] = (byte) (value >> 24);
154     block[offset++] = (byte) (value >> 16);
155     block[offset++] = (byte) (value >> 8);
156     block[offset++] = (byte) (value);
157
158     setNonNull(block, rowOffset);
159   }
160   
161   /**
162    * Gets a long value from the column.
163    *
164    * @param block the block's buffer
165    * @param rowOffset the offset of the row in the block
166    */

167   public long getLong(byte []block, int rowOffset)
168   {
169     if (isNull(block, rowOffset))
170       return 0;
171     
172     int offset = rowOffset + _columnOffset;
173     long value = 0;
174     
175     value = (block[offset++] & 0xffL) << 56;
176     value |= (block[offset++] & 0xffL) << 48;
177     value |= (block[offset++] & 0xffL) << 40;
178     value |= (block[offset++] & 0xffL) << 32;
179     value |= (block[offset++] & 0xffL) << 24;
180     value |= (block[offset++] & 0xffL) << 16;
181     value |= (block[offset++] & 0xffL) << 8;
182     value |= (block[offset++] & 0xffL);
183
184     return value;
185   }
186   
187   /**
188    * Sets the column based on an expression.
189    *
190    * @param block the block's buffer
191    * @param rowOffset the offset of the row in the block
192    * @param expr the expression to store
193    */

194   void setExpr(Transaction xa,
195            byte []block, int rowOffset,
196            Expr expr, QueryContext context)
197     throws SQLException JavaDoc
198   {
199     if (expr.isNull(null))
200       setNull(block, rowOffset);
201     else
202       setLong(xa, block, rowOffset, expr.evalLong(context));
203   }
204
205   /**
206    * Evaluates the column to a stream.
207    */

208   public void evalToResult(byte []block, int rowOffset, SelectResult result)
209   {
210     if (isNull(block, rowOffset)) {
211       result.writeNull();
212       return;
213     }
214
215     result.writeLong(getLong(block, rowOffset));
216   }
217   
218   /**
219    * Evaluate to a buffer.
220    *
221    * @param block the block's buffer
222    * @param rowOffset the offset of the row in the block
223    * @param buffer the result buffer
224    * @param buffer the result buffer offset
225    *
226    * @return the length of the value
227    */

228   int evalToBuffer(byte []block, int rowOffset,
229            byte []buffer, int bufferOffset)
230     throws SQLException JavaDoc
231   {
232     if (isNull(block, rowOffset))
233       return 0;
234
235     int startOffset = rowOffset + _columnOffset;
236     int len = 8;
237
238     System.arraycopy(block, startOffset, buffer, bufferOffset, len);
239
240     return len;
241   }
242
243   /**
244    * Returns true if the items in the given rows match.
245    */

246   public boolean isEqual(byte []block1, int rowOffset1,
247              byte []block2, int rowOffset2)
248   {
249     if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2))
250       return false;
251
252     int startOffset1 = rowOffset1 + _columnOffset;
253     int startOffset2 = rowOffset2 + _columnOffset;
254
255     return (block1[startOffset1 + 0] == block2[startOffset2 + 0] &&
256         block1[startOffset1 + 1] == block2[startOffset2 + 1] &&
257         block1[startOffset1 + 2] == block2[startOffset2 + 2] &&
258         block1[startOffset1 + 3] == block2[startOffset2 + 3] &&
259         block1[startOffset1 + 4] == block2[startOffset2 + 4] &&
260         block1[startOffset1 + 5] == block2[startOffset2 + 5] &&
261         block1[startOffset1 + 6] == block2[startOffset2 + 6] &&
262         block1[startOffset1 + 7] == block2[startOffset2 + 7]);
263   }
264   
265   /**
266    * Sets any index for the column.
267    *
268    * @param block the block's buffer
269    * @param rowOffset the offset of the row in the block
270    * @param rowAddr the address of the row
271    */

272   void setIndex(Transaction xa,
273         byte []block, int rowOffset,
274         long rowAddr, QueryContext context)
275     throws SQLException JavaDoc
276   {
277     BTree index = getIndex();
278
279     if (index == null)
280       return;
281
282     index.insert(block, rowOffset + _columnOffset, 8, rowAddr, xa, false);
283   }
284
285   /**
286    * Sets based on an iterator.
287    */

288   public void set(TableIterator iter, Expr expr, QueryContext context)
289     throws SQLException JavaDoc
290   {
291     iter.setDirty();
292     setLong(iter.getTransaction(),
293         iter.getBuffer(), iter.getRowOffset(), expr.evalLong(context));
294   }
295   
296   /**
297    * Deleting the row, based on the column.
298    *
299    * @param block the block's buffer
300    * @param rowOffset the offset of the row in the block
301    * @param expr the expression to store
302    */

303   void delete(Transaction xa, byte []block, int rowOffset)
304     throws SQLException JavaDoc
305   {
306     BTree index = getIndex();
307
308     if (index != null)
309       index.remove(block, rowOffset + _columnOffset, 8, xa);
310   }
311 }
312
Popular Tags