Source Code Cross Referenced for NumericColumn.java in  » EJB-Server-resin-3.1.5 » resin » com » caucho » db » table » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server resin 3.1.5 » resin » com.caucho.db.table 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.