001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.db.table;
030:
031: import com.caucho.db.index.BTree;
032: import com.caucho.db.sql.Expr;
033: import com.caucho.db.sql.QueryContext;
034: import com.caucho.db.sql.SelectResult;
035: import com.caucho.db.store.Transaction;
036: import com.caucho.util.CharBuffer;
037:
038: import java.sql.SQLException;
039:
040: /**
041: * Represents a numeric column.
042: */
043: class NumericColumn extends Column {
044: private int _precision;
045: private int _scale;
046: private long _offset;
047:
048: /**
049: * Creates a date column.
050: *
051: * @param row the row the column is being added to
052: * @param name the column's name
053: */
054: NumericColumn(Row row, String name, int precision, int scale) {
055: super (row, name);
056:
057: _precision = precision;
058: _scale = scale;
059:
060: _offset = 1;
061:
062: for (int i = 0; i < scale; i++)
063: _offset *= 10;
064: }
065:
066: /**
067: * Returns the column's type code.
068: */
069: public int getTypeCode() {
070: return NUMERIC;
071: }
072:
073: /**
074: * Returns the precision.
075: */
076: public int getPrecision() {
077: return _precision;
078: }
079:
080: /**
081: * Returns the scale.
082: */
083: public int getScale() {
084: return _scale;
085: }
086:
087: /**
088: * Returns the column's Java type.
089: */
090: public Class getJavaType() {
091: return double.class;
092: }
093:
094: /**
095: * Returns the column's declaration size.
096: */
097: public int getDeclarationSize() {
098: return 8;
099: }
100:
101: /**
102: * Returns the column's length
103: */
104: public int getLength() {
105: return 8;
106: }
107:
108: /**
109: * Sets a string value in the column.
110: *
111: * @param block the block's buffer
112: * @param rowOffset the offset of the row in the block
113: * @param value the value to store
114: */
115: void setString(Transaction xa, byte[] block, int rowOffset,
116: String str) throws SQLException {
117: if (str == null || str.length() == 0)
118: setNull(block, rowOffset);
119: else
120: setDouble(xa, block, rowOffset, Double.parseDouble(str));
121: }
122:
123: /**
124: * Gets a string value from the column.
125: *
126: * @param block the block's buffer
127: * @param rowOffset the offset of the row in the block
128: */
129: public String getString(byte[] block, int rowOffset)
130: throws SQLException {
131: if (isNull(block, rowOffset))
132: return null;
133: else {
134: long value = getNumeric(block, rowOffset);
135:
136: CharBuffer cb = new CharBuffer();
137:
138: long head = value / _offset;
139: long tail = value % _offset;
140:
141: cb.append(head);
142: cb.append('.');
143: cb.append(tail);
144:
145: return cb.toString();
146: }
147: }
148:
149: /**
150: * Sets a double value in the column.
151: *
152: * @param block the block's buffer
153: * @param rowOffset the offset of the row in the block
154: * @param value the value to store
155: */
156: public void setDouble(Transaction xa, byte[] block, int rowOffset,
157: double v) throws SQLException {
158: setNumeric(xa, block, rowOffset, (long) (v * _offset + 0.5));
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: public double getDouble(Transaction xa, byte[] block, int rowOffset)
169: throws SQLException {
170: return (double) getNumeric(block, rowOffset) / _offset;
171: }
172:
173: /**
174: * Evaluates the column to a stream.
175: */
176: public void evalToResult(byte[] block, int rowOffset,
177: SelectResult result) throws SQLException {
178: if (isNull(block, rowOffset)) {
179: result.writeNull();
180: return;
181: }
182:
183: result.writeString(getString(block, rowOffset));
184: }
185:
186: /**
187: * Evaluate to a buffer.
188: *
189: * @param block the block's buffer
190: * @param rowOffset the offset of the row in the block
191: * @param buffer the result buffer
192: * @param buffer the result buffer offset
193: *
194: * @return the length of the value
195: */
196: int evalToBuffer(byte[] block, int rowOffset, byte[] buffer,
197: int bufferOffset) throws SQLException {
198: if (isNull(block, rowOffset))
199: return 0;
200:
201: int startOffset = rowOffset + _columnOffset;
202: int len = 8;
203:
204: System.arraycopy(block, startOffset, buffer, bufferOffset, len);
205:
206: return len;
207: }
208:
209: /**
210: * Sets the column based on an expression.
211: *
212: * @param block the block's buffer
213: * @param rowOffset the offset of the row in the block
214: * @param expr the expression to store
215: */
216: void setExpr(Transaction xa, byte[] block, int rowOffset,
217: Expr expr, QueryContext context) throws SQLException {
218: if (expr.isNull(null))
219: setNull(block, rowOffset);
220: else
221: setDouble(xa, block, rowOffset, expr.evalDouble(context));
222: }
223:
224: /**
225: * Returns true if the items in the given rows match.
226: */
227: public boolean isEqual(byte[] block1, int rowOffset1,
228: byte[] block2, int rowOffset2) {
229: if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2))
230: return false;
231:
232: int startOffset1 = rowOffset1 + _columnOffset;
233: int startOffset2 = rowOffset2 + _columnOffset;
234:
235: return (block1[startOffset1 + 0] == block2[startOffset2 + 0]
236: && block1[startOffset1 + 1] == block2[startOffset2 + 1]
237: && block1[startOffset1 + 2] == block2[startOffset2 + 2]
238: && block1[startOffset1 + 3] == block2[startOffset2 + 3]
239: && block1[startOffset1 + 4] == block2[startOffset2 + 4]
240: && block1[startOffset1 + 5] == block2[startOffset2 + 5]
241: && block1[startOffset1 + 6] == block2[startOffset2 + 6] && block1[startOffset1 + 7] == block2[startOffset2 + 7]);
242: }
243:
244: /**
245: * Sets any index for the column.
246: *
247: * @param block the block's buffer
248: * @param rowOffset the offset of the row in the block
249: * @param rowAddr the address of the row
250: */
251: void setIndex(Transaction xa, byte[] block, int rowOffset,
252: long rowAddr, QueryContext context) throws SQLException {
253: BTree index = getIndex();
254:
255: if (index == null)
256: return;
257:
258: index.insert(block, rowOffset + _columnOffset, 8, rowAddr, xa,
259: false);
260: }
261:
262: /**
263: * Sets based on an iterator.
264: */
265: public void set(TableIterator iter, Expr expr, QueryContext context)
266: throws SQLException {
267: iter.setDirty();
268: setDouble(iter.getTransaction(), iter.getBuffer(), iter
269: .getRowOffset(), expr.evalDouble(context));
270: }
271:
272: /**
273: * Deleting the row, based on the column.
274: *
275: * @param block the block's buffer
276: * @param rowOffset the offset of the row in the block
277: * @param expr the expression to store
278: */
279: void delete(Transaction xa, byte[] block, int rowOffset)
280: throws SQLException {
281: BTree index = getIndex();
282:
283: if (index != null)
284: index.remove(block, rowOffset + _columnOffset, 8, xa);
285: }
286:
287: /**
288: * Sets a numeric value in the column.
289: *
290: * @param block the block's buffer
291: * @param rowOffset the offset of the row in the block
292: * @param value the value to store
293: */
294: void setNumeric(Transaction xa, byte[] block, int rowOffset,
295: long value) {
296: int offset = rowOffset + _columnOffset;
297:
298: block[offset++] = (byte) (value >> 56);
299: block[offset++] = (byte) (value >> 48);
300: block[offset++] = (byte) (value >> 40);
301: block[offset++] = (byte) (value >> 32);
302: block[offset++] = (byte) (value >> 24);
303: block[offset++] = (byte) (value >> 16);
304: block[offset++] = (byte) (value >> 8);
305: block[offset++] = (byte) (value);
306:
307: setNonNull(block, rowOffset);
308: }
309:
310: /**
311: * Gets a long value from the column.
312: *
313: * @param block the block's buffer
314: * @param rowOffset the offset of the row in the block
315: */
316: long getNumeric(byte[] block, int rowOffset) {
317: if (isNull(block, rowOffset))
318: return 0;
319:
320: int offset = rowOffset + _columnOffset;
321: long value = 0;
322:
323: value = (block[offset++] & 0xffL) << 56;
324: value |= (block[offset++] & 0xffL) << 48;
325: value |= (block[offset++] & 0xffL) << 40;
326: value |= (block[offset++] & 0xffL) << 32;
327: value |= (block[offset++] & 0xffL) << 24;
328: value |= (block[offset++] & 0xffL) << 16;
329: value |= (block[offset++] & 0xffL) << 8;
330: value |= (block[offset++] & 0xffL);
331:
332: return value;
333: }
334: }
|