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