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:
037: import java.sql.SQLException;
038:
039: /**
040: * Represents a 64-bit double-precision floating point column.
041: */
042: class DoubleColumn extends Column {
043: /**
044: * Creates a double column.
045: *
046: * @param row the row the column is being added to
047: * @param name the column's name
048: */
049: DoubleColumn(Row row, String name) {
050: super (row, name);
051: }
052:
053: /**
054: * Returns the column's type code.
055: */
056: public int getTypeCode() {
057: return DOUBLE;
058: }
059:
060: /**
061: * Returns the column's Java type.
062: */
063: public Class getJavaType() {
064: return double.class;
065: }
066:
067: /**
068: * Returns the column's declaration size.
069: */
070: public int getDeclarationSize() {
071: return 8;
072: }
073:
074: /**
075: * Returns the column's size.
076: */
077: public int getLength() {
078: return 8;
079: }
080:
081: /**
082: * Sets a string value in the column.
083: *
084: * @param block the block's buffer
085: * @param rowOffset the offset of the row in the block
086: * @param value the value to store
087: */
088: void setString(Transaction xa, byte[] block, int rowOffset,
089: String str) {
090: if (str == null)
091: setNull(block, rowOffset);
092: else
093: setDouble(xa, block, rowOffset, Double.parseDouble(str));
094: }
095:
096: /**
097: * Gets a string value from the column.
098: *
099: * @param block the block's buffer
100: * @param rowOffset the offset of the row in the block
101: */
102: public String getString(byte[] block, int rowOffset) {
103: if (isNull(block, rowOffset))
104: return null;
105: else
106: return String.valueOf(getDouble(block, rowOffset));
107: }
108:
109: /**
110: * Sets an integer value in the column.
111: *
112: * @param block the block's buffer
113: * @param rowOffset the offset of the row in the block
114: * @param value the value to store
115: */
116: void setInteger(Transaction xa, byte[] block, int rowOffset,
117: int value) {
118: setDouble(xa, block, rowOffset, value);
119: }
120:
121: /**
122: * Gets an integer value from the column.
123: *
124: * @param block the block's buffer
125: * @param rowOffset the offset of the row in the block
126: */
127: public int getInteger(byte[] block, int rowOffset) {
128: return (int) getDouble(block, rowOffset);
129: }
130:
131: /**
132: * Sets a long 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 setLong(Transaction xa, byte[] block, int rowOffset, long value) {
139: setDouble(xa, block, rowOffset, value);
140: }
141:
142: /**
143: * Gets a long value from the column.
144: *
145: * @param block the block's buffer
146: * @param rowOffset the offset of the row in the block
147: */
148: public long getLong(byte[] block, int rowOffset) {
149: return (long) getDouble(block, rowOffset);
150: }
151:
152: /**
153: * Sets a double value in the column.
154: *
155: * @param block the block's buffer
156: * @param rowOffset the offset of the row in the block
157: * @param value the value to store
158: */
159: void setDouble(Transaction xa, byte[] block, int rowOffset,
160: double value) {
161: int offset = rowOffset + _columnOffset;
162:
163: long longValue = Double.doubleToRawLongBits(value);
164:
165: block[offset++] = (byte) (longValue >> 56);
166: block[offset++] = (byte) (longValue >> 48);
167: block[offset++] = (byte) (longValue >> 40);
168: block[offset++] = (byte) (longValue >> 32);
169:
170: block[offset++] = (byte) (longValue >> 24);
171: block[offset++] = (byte) (longValue >> 16);
172: block[offset++] = (byte) (longValue >> 8);
173: block[offset++] = (byte) (longValue);
174:
175: setNonNull(block, rowOffset);
176: }
177:
178: /**
179: * Gets a double value from the column.
180: *
181: * @param block the block's buffer
182: * @param rowOffset the offset of the row in the block
183: */
184: public double getDouble(byte[] block, int rowOffset) {
185: if (isNull(block, rowOffset))
186: return 0;
187:
188: int offset = rowOffset + _columnOffset;
189: long longValue = 0;
190:
191: longValue = (block[offset++] & 0xffL) << 56;
192: longValue |= (block[offset++] & 0xffL) << 48;
193: longValue |= (block[offset++] & 0xffL) << 40;
194: longValue |= (block[offset++] & 0xffL) << 32;
195:
196: longValue |= (block[offset++] & 0xffL) << 24;
197: longValue |= (block[offset++] & 0xffL) << 16;
198: longValue |= (block[offset++] & 0xffL) << 8;
199: longValue |= (block[offset++] & 0xffL);
200:
201: return Double.longBitsToDouble(longValue);
202: }
203:
204: /**
205: * Sets the column based on an expression.
206: *
207: * @param block the block's buffer
208: * @param rowOffset the offset of the row in the block
209: * @param expr the expression to store
210: */
211: void setExpr(Transaction xa, byte[] block, int rowOffset,
212: Expr expr, QueryContext context) throws SQLException {
213: if (expr.isNull(null))
214: setNull(block, rowOffset);
215: else
216: setDouble(xa, block, rowOffset, expr.evalDouble(context));
217: }
218:
219: /**
220: * Evaluates the column to a stream.
221: */
222: public void evalToResult(byte[] block, int rowOffset,
223: SelectResult result) {
224: if (isNull(block, rowOffset)) {
225: result.writeNull();
226: return;
227: }
228:
229: result.writeDouble(getDouble(block, rowOffset));
230: }
231:
232: /**
233: * Evaluate to a buffer.
234: *
235: * @param block the block's buffer
236: * @param rowOffset the offset of the row in the block
237: * @param buffer the result buffer
238: * @param buffer the result buffer offset
239: *
240: * @return the length of the value
241: */
242: int evalToBuffer(byte[] block, int rowOffset, byte[] buffer,
243: int bufferOffset) throws SQLException {
244: if (isNull(block, rowOffset))
245: return 0;
246:
247: int startOffset = rowOffset + _columnOffset;
248: int len = 8;
249:
250: System.arraycopy(block, startOffset, buffer, bufferOffset, len);
251:
252: return len;
253: }
254:
255: /**
256: * Returns true if the items in the given rows match.
257: */
258: public boolean isEqual(byte[] block1, int rowOffset1,
259: byte[] block2, int rowOffset2) {
260: if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2))
261: return false;
262:
263: int startOffset1 = rowOffset1 + _columnOffset;
264: int startOffset2 = rowOffset2 + _columnOffset;
265:
266: return (block1[startOffset1 + 0] == block2[startOffset2 + 0]
267: && block1[startOffset1 + 1] == block2[startOffset2 + 1]
268: && block1[startOffset1 + 2] == block2[startOffset2 + 2]
269: && block1[startOffset1 + 3] == block2[startOffset2 + 3]
270: && block1[startOffset1 + 4] == block2[startOffset2 + 4]
271: && block1[startOffset1 + 5] == block2[startOffset2 + 5]
272: && block1[startOffset1 + 6] == block2[startOffset2 + 6] && block1[startOffset1 + 7] == block2[startOffset2 + 7]);
273: }
274:
275: /**
276: * Sets based on an iterator.
277: */
278: public void set(TableIterator iter, Expr expr, QueryContext context)
279: throws SQLException {
280: iter.setDirty();
281: setDouble(iter.getTransaction(), iter.getBuffer(), iter
282: .getRowOffset(), expr.evalDouble(context));
283: }
284:
285: /**
286: * Deleting the row, based on the column.
287: *
288: * @param block the block's buffer
289: * @param rowOffset the offset of the row in the block
290: * @param expr the expression to store
291: */
292: void delete(Transaction xa, byte[] block, int rowOffset)
293: throws SQLException {
294: BTree index = getIndex();
295:
296: if (index != null)
297: index.remove(block, rowOffset + _columnOffset, 8, xa);
298: }
299: }
|