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 long integer column.
041: */
042: class LongColumn extends Column {
043: /**
044: * Creates a long column.
045: *
046: * @param row the row the column is being added to
047: * @param name the column's name
048: */
049: LongColumn(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 LONG;
058: }
059:
060: /**
061: * Returns the column's Java type.
062: */
063: public Class getJavaType() {
064: return long.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 length
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: setLong(xa, block, rowOffset, Long.parseLong(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(getLong(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: setLong(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) getLong(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: int offset = rowOffset + _columnOffset;
140:
141: block[offset++] = (byte) (value >> 56);
142: block[offset++] = (byte) (value >> 48);
143: block[offset++] = (byte) (value >> 40);
144: block[offset++] = (byte) (value >> 32);
145: block[offset++] = (byte) (value >> 24);
146: block[offset++] = (byte) (value >> 16);
147: block[offset++] = (byte) (value >> 8);
148: block[offset++] = (byte) (value);
149:
150: setNonNull(block, rowOffset);
151: }
152:
153: /**
154: * Gets a long value from the column.
155: *
156: * @param block the block's buffer
157: * @param rowOffset the offset of the row in the block
158: */
159: public long getLong(byte[] block, int rowOffset) {
160: if (isNull(block, rowOffset))
161: return 0;
162:
163: int offset = rowOffset + _columnOffset;
164: long value = 0;
165:
166: value = (block[offset++] & 0xffL) << 56;
167: value |= (block[offset++] & 0xffL) << 48;
168: value |= (block[offset++] & 0xffL) << 40;
169: value |= (block[offset++] & 0xffL) << 32;
170: value |= (block[offset++] & 0xffL) << 24;
171: value |= (block[offset++] & 0xffL) << 16;
172: value |= (block[offset++] & 0xffL) << 8;
173: value |= (block[offset++] & 0xffL);
174:
175: return value;
176: }
177:
178: /**
179: * Sets the column based on an expression.
180: *
181: * @param block the block's buffer
182: * @param rowOffset the offset of the row in the block
183: * @param expr the expression to store
184: */
185: void setExpr(Transaction xa, byte[] block, int rowOffset,
186: Expr expr, QueryContext context) throws SQLException {
187: if (expr.isNull(null))
188: setNull(block, rowOffset);
189: else
190: setLong(xa, block, rowOffset, expr.evalLong(context));
191: }
192:
193: /**
194: * Evaluates the column to a stream.
195: */
196: public void evalToResult(byte[] block, int rowOffset,
197: SelectResult result) {
198: if (isNull(block, rowOffset)) {
199: result.writeNull();
200: return;
201: }
202:
203: result.writeLong(getLong(block, rowOffset));
204: }
205:
206: /**
207: * Evaluate to a buffer.
208: *
209: * @param block the block's buffer
210: * @param rowOffset the offset of the row in the block
211: * @param buffer the result buffer
212: * @param buffer the result buffer offset
213: *
214: * @return the length of the value
215: */
216: int evalToBuffer(byte[] block, int rowOffset, byte[] buffer,
217: int bufferOffset) throws SQLException {
218: if (isNull(block, rowOffset))
219: return 0;
220:
221: int startOffset = rowOffset + _columnOffset;
222: int len = 8;
223:
224: System.arraycopy(block, startOffset, buffer, bufferOffset, len);
225:
226: return len;
227: }
228:
229: /**
230: * Returns true if the items in the given rows match.
231: */
232: public boolean isEqual(byte[] block1, int rowOffset1,
233: byte[] block2, int rowOffset2) {
234: if (isNull(block1, rowOffset1) != isNull(block2, rowOffset2))
235: return false;
236:
237: int startOffset1 = rowOffset1 + _columnOffset;
238: int startOffset2 = rowOffset2 + _columnOffset;
239:
240: return (block1[startOffset1 + 0] == block2[startOffset2 + 0]
241: && block1[startOffset1 + 1] == block2[startOffset2 + 1]
242: && block1[startOffset1 + 2] == block2[startOffset2 + 2]
243: && block1[startOffset1 + 3] == block2[startOffset2 + 3]
244: && block1[startOffset1 + 4] == block2[startOffset2 + 4]
245: && block1[startOffset1 + 5] == block2[startOffset2 + 5]
246: && block1[startOffset1 + 6] == block2[startOffset2 + 6] && block1[startOffset1 + 7] == block2[startOffset2 + 7]);
247: }
248:
249: /**
250: * Sets any index for the column.
251: *
252: * @param block the block's buffer
253: * @param rowOffset the offset of the row in the block
254: * @param rowAddr the address of the row
255: */
256: void setIndex(Transaction xa, byte[] block, int rowOffset,
257: long rowAddr, QueryContext context) throws SQLException {
258: BTree index = getIndex();
259:
260: if (index == null)
261: return;
262:
263: index.insert(block, rowOffset + _columnOffset, 8, rowAddr, xa,
264: false);
265: }
266:
267: /**
268: * Sets based on an iterator.
269: */
270: public void set(TableIterator iter, Expr expr, QueryContext context)
271: throws SQLException {
272: iter.setDirty();
273: setLong(iter.getTransaction(), iter.getBuffer(), iter
274: .getRowOffset(), expr.evalLong(context));
275: }
276:
277: /**
278: * Deleting the row, based on the column.
279: *
280: * @param block the block's buffer
281: * @param rowOffset the offset of the row in the block
282: * @param expr the expression to store
283: */
284: void delete(Transaction xa, byte[] block, int rowOffset)
285: throws SQLException {
286: BTree index = getIndex();
287:
288: if (index != null)
289: index.remove(block, rowOffset + _columnOffset, 8, xa);
290: }
291: }
|