KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.util.CharBuffer;
37
38 import java.sql.SQLException JavaDoc;
39
40 /**
41  * Represents a numeric column.
42  */

43 class NumericColumn extends Column {
44   private int _precision;
45   private int _scale;
46   private long _offset;
47   
48   /**
49    * Creates a date column.
50    *
51    * @param row the row the column is being added to
52    * @param name the column's name
53    */

54   NumericColumn(Row row, String JavaDoc name, int precision, int scale)
55   {
56     super(row, name);
57
58     _precision = precision;
59     _scale = scale;
60
61     _offset = 1;
62     
63     for (int i = 0; i < scale; i++)
64       _offset *= 10;
65   }
66
67   /**
68    * Returns the column's type code.
69    */

70   public int getTypeCode()
71   {
72     return NUMERIC;
73   }
74
75   /**
76    * Returns the precision.
77    */

78   public int getPrecision()
79   {
80     return _precision;
81   }
82
83   /**
84    * Returns the scale.
85    */

86   public int getScale()
87   {
88     return _scale;
89   }
90
91   /**
92    * Returns the column's Java type.
93    */

94   public Class JavaDoc getJavaType()
95   {
96     return double.class;
97   }
98   
99   /**
100    * Returns the column's declaration size.
101    */

102   public int getDeclarationSize()
103   {
104     return 8;
105   }
106   
107   /**
108    * Returns the column's length
109    */

110   public int getLength()
111   {
112     return 8;
113   }
114   
115   /**
116    * Sets a string 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 setString(Transaction xa, byte []block, int rowOffset, String JavaDoc str)
123     throws SQLException JavaDoc
124   {
125     if (str == null || str.length() == 0)
126       setNull(block, rowOffset);
127     else
128       setDouble(xa, block, rowOffset, Double.parseDouble(str));
129   }
130   
131   /**
132    * Gets a string value from the column.
133    *
134    * @param block the block's buffer
135    * @param rowOffset the offset of the row in the block
136    */

137   public String JavaDoc getString(byte []block, int rowOffset)
138     throws SQLException JavaDoc
139   {
140     if (isNull(block, rowOffset))
141       return null;
142     else {
143       long value = getNumeric(block, rowOffset);
144
145       CharBuffer cb = new CharBuffer();
146
147       long head = value / _offset;
148       long tail = value % _offset;
149
150       cb.append(head);
151       cb.append('.');
152       cb.append(tail);
153
154       return cb.toString();
155     }
156   }
157   
158   /**
159    * Sets a double value in the column.
160    *
161    * @param block the block's buffer
162    * @param rowOffset the offset of the row in the block
163    * @param value the value to store
164    */

165   public void setDouble(Transaction xa, byte []block, int rowOffset, double v)
166     throws SQLException JavaDoc
167   {
168     setNumeric(xa, block, rowOffset, (long) (v * _offset + 0.5));
169   }
170   
171   /**
172    * Sets a double value in the column.
173    *
174    * @param block the block's buffer
175    * @param rowOffset the offset of the row in the block
176    * @param value the value to store
177    */

178   public double getDouble(Transaction xa, byte []block, int rowOffset)
179     throws SQLException JavaDoc
180   {
181     return (double) getNumeric(block, rowOffset) / _offset;
182   }
183
184   /**
185    * Evaluates the column to a stream.
186    */

187   public void evalToResult(byte []block, int rowOffset, SelectResult result)
188     throws SQLException JavaDoc
189   {
190     if (isNull(block, rowOffset)) {
191       result.writeNull();
192       return;
193     }
194
195     result.writeString(getString(block, rowOffset));
196   }
197   
198   /**
199    * Evaluate to a buffer.
200    *
201    * @param block the block's buffer
202    * @param rowOffset the offset of the row in the block
203    * @param buffer the result buffer
204    * @param buffer the result buffer offset
205    *
206    * @return the length of the value
207    */

208   int evalToBuffer(byte []block, int rowOffset,
209            byte []buffer, int bufferOffset)
210     throws SQLException JavaDoc
211   {
212     if (isNull(block, rowOffset))
213       return 0;
214
215     int startOffset = rowOffset + _columnOffset;
216     int len = 8;
217
218     System.arraycopy(block, startOffset, buffer, bufferOffset, len);
219
220     return len;
221   }
222   
223   /**
224    * Sets the column based on an expression.
225    *
226    * @param block the block's buffer
227    * @param rowOffset the offset of the row in the block
228    * @param expr the expression to store
229    */

230   void setExpr(Transaction xa,
231            byte []block, int rowOffset,
232            Expr expr, QueryContext context)
233     throws SQLException JavaDoc
234   {
235     if (expr.isNull(null))
236       setNull(block, rowOffset);
237     else
238       setDouble(xa, block, rowOffset, expr.evalDouble(context));
239   }
240
241   /**
242    * Returns true if the items in the given rows match.
243    */

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

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

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

301   void delete(Transaction xa, byte []block, int rowOffset)
302     throws SQLException JavaDoc
303   {
304     BTree index = getIndex();
305
306     if (index != null)
307       index.remove(block, rowOffset + _columnOffset, 8, xa);
308   }
309   
310   /**
311    * Sets a numeric value in the column.
312    *
313    * @param block the block's buffer
314    * @param rowOffset the offset of the row in the block
315    * @param value the value to store
316    */

317   void setNumeric(Transaction xa, byte []block, int rowOffset, long value)
318   {
319     int offset = rowOffset + _columnOffset;
320     
321     block[offset++] = (byte) (value >> 56);
322     block[offset++] = (byte) (value >> 48);
323     block[offset++] = (byte) (value >> 40);
324     block[offset++] = (byte) (value >> 32);
325     block[offset++] = (byte) (value >> 24);
326     block[offset++] = (byte) (value >> 16);
327     block[offset++] = (byte) (value >> 8);
328     block[offset++] = (byte) (value);
329
330     setNonNull(block, rowOffset);
331   }
332   
333   /**
334    * Gets a long value from the column.
335    *
336    * @param block the block's buffer
337    * @param rowOffset the offset of the row in the block
338    */

339   long getNumeric(byte []block, int rowOffset)
340   {
341     if (isNull(block, rowOffset))
342       return 0;
343     
344     int offset = rowOffset + _columnOffset;
345     long value = 0;
346     
347     value = (block[offset++] & 0xffL) << 56;
348     value |= (block[offset++] & 0xffL) << 48;
349     value |= (block[offset++] & 0xffL) << 40;
350     value |= (block[offset++] & 0xffL) << 32;
351     value |= (block[offset++] & 0xffL) << 24;
352     value |= (block[offset++] & 0xffL) << 16;
353     value |= (block[offset++] & 0xffL) << 8;
354     value |= (block[offset++] & 0xffL);
355
356     return value;
357   }
358 }
359
Popular Tags