KickJava   Java API By Example, From Geeks To Geeks.

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


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 double-precision floating point column.
41  */

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

49   DoubleColumn(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 DOUBLE;
60   }
61
62   /**
63    * Returns the column's Java type.
64    */

65   public Class JavaDoc getJavaType()
66   {
67     return double.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 size.
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       setDouble(xa, block, rowOffset, Double.parseDouble(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(getDouble(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     setDouble(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) getDouble(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     setDouble(xa, block, rowOffset, value);
148   }
149   
150   /**
151    * Gets a long value from the column.
152    *
153    * @param block the block's buffer
154    * @param rowOffset the offset of the row in the block
155    */

156   public long getLong(byte []block, int rowOffset)
157   {
158     return (long) getDouble(block, rowOffset);
159   }
160   
161   /**
162    * Sets a double value in the column.
163    *
164    * @param block the block's buffer
165    * @param rowOffset the offset of the row in the block
166    * @param value the value to store
167    */

168   void setDouble(Transaction xa,
169          byte []block, int rowOffset, double value)
170   {
171     int offset = rowOffset + _columnOffset;
172
173     long longValue = Double.doubleToRawLongBits(value);
174     
175     block[offset++] = (byte) (longValue >> 56);
176     block[offset++] = (byte) (longValue >> 48);
177     block[offset++] = (byte) (longValue >> 40);
178     block[offset++] = (byte) (longValue >> 32);
179     
180     block[offset++] = (byte) (longValue >> 24);
181     block[offset++] = (byte) (longValue >> 16);
182     block[offset++] = (byte) (longValue >> 8);
183     block[offset++] = (byte) (longValue);
184
185     setNonNull(block, rowOffset);
186   }
187   
188   /**
189    * Gets a double value from the column.
190    *
191    * @param block the block's buffer
192    * @param rowOffset the offset of the row in the block
193    */

194   public double getDouble(byte []block, int rowOffset)
195   {
196     if (isNull(block, rowOffset))
197       return 0;
198     
199     int offset = rowOffset + _columnOffset;
200     long longValue = 0;
201     
202     longValue = (block[offset++] & 0xffL) << 56;
203     longValue |= (block[offset++] & 0xffL) << 48;
204     longValue |= (block[offset++] & 0xffL) << 40;
205     longValue |= (block[offset++] & 0xffL) << 32;
206     
207     longValue |= (block[offset++] & 0xffL) << 24;
208     longValue |= (block[offset++] & 0xffL) << 16;
209     longValue |= (block[offset++] & 0xffL) << 8;
210     longValue |= (block[offset++] & 0xffL);
211     
212     return Double.longBitsToDouble(longValue);
213   }
214   
215   /**
216    * Sets the column based on an expression.
217    *
218    * @param block the block's buffer
219    * @param rowOffset the offset of the row in the block
220    * @param expr the expression to store
221    */

222   void setExpr(Transaction xa, byte []block, int rowOffset,
223            Expr expr, QueryContext context)
224     throws SQLException JavaDoc
225   {
226     if (expr.isNull(null))
227       setNull(block, rowOffset);
228     else
229       setDouble(xa, block, rowOffset, expr.evalDouble(context));
230   }
231
232   /**
233    * Evaluates the column to a stream.
234    */

235   public void evalToResult(byte []block, int rowOffset, SelectResult result)
236   {
237     if (isNull(block, rowOffset)) {
238       result.writeNull();
239       return;
240     }
241
242     result.writeDouble(getDouble(block, rowOffset));
243   }
244   
245   /**
246    * Evaluate to a buffer.
247    *
248    * @param block the block's buffer
249    * @param rowOffset the offset of the row in the block
250    * @param buffer the result buffer
251    * @param buffer the result buffer offset
252    *
253    * @return the length of the value
254    */

255   int evalToBuffer(byte []block, int rowOffset,
256            byte []buffer, int bufferOffset)
257     throws SQLException JavaDoc
258   {
259     if (isNull(block, rowOffset))
260       return 0;
261
262     int startOffset = rowOffset + _columnOffset;
263     int len = 8;
264
265     System.arraycopy(block, startOffset, buffer, bufferOffset, len);
266
267     return len;
268   }
269
270   /**
271    * Returns true if the items in the given rows match.
272    */

273   public boolean isEqual(byte []block1, int rowOffset1,
274              byte []block2, int rowOffset2)
275   {
276     if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2))
277       return false;
278
279     int startOffset1 = rowOffset1 + _columnOffset;
280     int startOffset2 = rowOffset2 + _columnOffset;
281
282     return (block1[startOffset1 + 0] == block2[startOffset2 + 0] &&
283         block1[startOffset1 + 1] == block2[startOffset2 + 1] &&
284         block1[startOffset1 + 2] == block2[startOffset2 + 2] &&
285         block1[startOffset1 + 3] == block2[startOffset2 + 3] &&
286         block1[startOffset1 + 4] == block2[startOffset2 + 4] &&
287         block1[startOffset1 + 5] == block2[startOffset2 + 5] &&
288         block1[startOffset1 + 6] == block2[startOffset2 + 6] &&
289         block1[startOffset1 + 7] == block2[startOffset2 + 7]);
290   }
291
292   /**
293    * Sets based on an iterator.
294    */

295   public void set(TableIterator iter, Expr expr, QueryContext context)
296     throws SQLException JavaDoc
297   {
298     iter.setDirty();
299     setDouble(iter.getTransaction(),
300           iter.getBuffer(), iter.getRowOffset(),
301           expr.evalDouble(context));
302   }
303   
304   /**
305    * Deleting the row, based on the column.
306    *
307    * @param block the block's buffer
308    * @param rowOffset the offset of the row in the block
309    * @param expr the expression to store
310    */

311   void delete(Transaction xa, byte []block, int rowOffset)
312     throws SQLException JavaDoc
313   {
314     BTree index = getIndex();
315
316     if (index != null)
317       index.remove(block, rowOffset + _columnOffset, 8, xa);
318   }
319 }
320
Popular Tags