KickJava   Java API By Example, From Geeks To Geeks.

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


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.index.IntKeyCompare;
33 import com.caucho.db.index.KeyCompare;
34 import com.caucho.db.sql.Expr;
35 import com.caucho.db.sql.QueryContext;
36 import com.caucho.db.sql.SelectResult;
37 import com.caucho.db.store.Transaction;
38
39 import java.sql.SQLException JavaDoc;
40
41 /**
42  * Represents a 32-bit integer column.
43  */

44 class IntColumn extends Column {
45   /**
46    * Creates a utf-8 string column.
47    *
48    * @param columnOffset the offset within the row
49    * @param maxLength the maximum length of the string
50    */

51   IntColumn(Row row, String JavaDoc name)
52   {
53     super(row, name);
54   }
55
56   /**
57    * Returns the column's type code.
58    */

59   public int getTypeCode()
60   {
61     return INT;
62   }
63
64   /**
65    * Returns the column's Java type.
66    */

67   public Class JavaDoc getJavaType()
68   {
69     return int.class;
70   }
71   
72   /**
73    * Returns the column's declaration size.
74    */

75   public int getDeclarationSize()
76   {
77     return 4;
78   }
79
80   /**
81    * Returns the column's size.
82    */

83   public int getLength()
84   {
85     return 4;
86   }
87
88   /**
89    * Returns the key compare for the column.
90    */

91   public KeyCompare getIndexKeyCompare()
92   {
93     return new IntKeyCompare();
94   }
95   
96   /**
97    * Returns a String value from the column.
98    *
99    * @param block the block's buffer
100    * @param rowOffset the offset of the row in the block
101    */

102   public String JavaDoc getString(byte []block, int rowOffset)
103   {
104     if (isNull(block, rowOffset))
105       return null;
106     else
107       return String.valueOf(getInteger(block, rowOffset));
108   }
109   
110   /**
111    * Sets a string value in the column.
112    *
113    * @param block the block's buffer
114    * @param rowOffset the offset of the row in the block
115    * @param value the value to store
116    */

117   void setString(Transaction xa, byte []block, int rowOffset, String JavaDoc str)
118   {
119     if (str == null)
120       setNull(block, rowOffset);
121     else
122       setInteger(xa, block, rowOffset, (int) Long.parseLong(str));
123   }
124   
125   /**
126    * Returns a int value from the column.
127    *
128    * @param block the block's buffer
129    * @param rowOffset the offset of the row in the block
130    */

131   public int getInteger(byte []block, int rowOffset)
132   {
133     if (isNull(block, rowOffset))
134       return 0;
135     
136     int offset = rowOffset + _columnOffset;
137     int value = 0;
138     
139     value = (block[offset++] & 0xff) << 24;
140     value |= (block[offset++] & 0xff) << 16;
141     value |= (block[offset++] & 0xff) << 8;
142     value |= (block[offset++] & 0xff);
143
144     return value;
145   }
146   
147   /**
148    * Sets an integer value in the column.
149    *
150    * @param block the block's buffer
151    * @param rowOffset the offset of the row in the block
152    * @param value the value to store
153    */

154   void setInteger(Transaction xa, byte []block, int rowOffset, int value)
155   {
156     int offset = rowOffset + _columnOffset;
157     
158     block[offset++] = (byte) (value >> 24);
159     block[offset++] = (byte) (value >> 16);
160     block[offset++] = (byte) (value >> 8);
161     block[offset++] = (byte) (value);
162
163     setNonNull(block, rowOffset);
164   }
165
166   /**
167    * Sets a long value in the column.
168    *
169    * @param block the block's buffer
170    * @param rowOffset the offset of the row in the block
171    * @param value the value to store
172    */

173   void setLong(Transaction xa, byte []block, int rowOffset, long value)
174   {
175     setInteger(xa, block, rowOffset, (int) value);
176   }
177   
178   /**
179    * Returns a long 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 long getLong(byte []block, int rowOffset)
185   {
186     return getInteger(block, rowOffset);
187   }
188   
189   /**
190    * Sets the column based on an expression.
191    *
192    * @param block the block's buffer
193    * @param rowOffset the offset of the row in the block
194    * @param expr the expression to store
195    */

196   void setExpr(Transaction xa,
197            byte []block, int rowOffset,
198            Expr expr, QueryContext context)
199     throws SQLException JavaDoc
200   {
201     if (expr.isNull(null))
202       setNull(block, rowOffset);
203     else
204       setInteger(xa, block, rowOffset, (int) expr.evalLong(context));
205   }
206
207   /**
208    * Evaluates the column to a stream.
209    */

210   public void evalToResult(byte []block, int rowOffset, SelectResult result)
211   {
212     if (isNull(block, rowOffset)) {
213       result.writeNull();
214       return;
215     }
216
217     int startOffset = rowOffset + _columnOffset;
218     
219     result.write(Column.INT);
220     result.write(block, startOffset, 4);
221   }
222   
223   /**
224    * Evaluate to a buffer.
225    *
226    * @param block the block's buffer
227    * @param rowOffset the offset of the row in the block
228    * @param buffer the result buffer
229    * @param buffer the result buffer offset
230    *
231    * @return the length of the value
232    */

233   int evalToBuffer(byte []block, int rowOffset,
234            byte []buffer, int bufferOffset)
235     throws SQLException JavaDoc
236   {
237     if (isNull(block, rowOffset))
238       return 0;
239
240     int startOffset = rowOffset + _columnOffset;
241     int len = 4;
242
243     System.arraycopy(block, startOffset, buffer, bufferOffset, len);
244
245     return len;
246   }
247
248   /**
249    * Returns true if the items in the given rows match.
250    */

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

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

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

305   void delete(Transaction xa, byte []block, int rowOffset)
306     throws SQLException JavaDoc
307   {
308     BTree index = getIndex();
309
310     if (index != null)
311       index.remove(block, rowOffset + _columnOffset, 4, xa);
312   }
313 }
314
Popular Tags