Source Code Cross Referenced for DefaultTypeHandler.java in  » Database-ORM » ODAL » com » completex » objective » components » persistency » type » 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 » Database ORM » ODAL » com.completex.objective.components.persistency.type 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Objective Database Abstraction Layer (ODAL)
003:         *  Copyright (c) 2004, The ODAL Development Group
004:         *  All rights reserved.
005:         *  For definition of the ODAL Development Group please refer to LICENCE.txt file
006:         *
007:         *  Distributable under LGPL license.
008:         *  See terms of license at gnu.org.
009:         */package com.completex.objective.components.persistency.type;
010:
011:        import com.completex.objective.components.persistency.ColumnType;
012:        import com.completex.objective.components.persistency.OdalPersistencyException;
013:        import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
014:        import com.completex.objective.components.persistency.PersistentObject;
015:        import com.completex.objective.components.persistency.core.DatabasePolicy;
016:
017:        import java.sql.CallableStatement;
018:        import java.sql.PreparedStatement;
019:        import java.sql.ResultSet;
020:        import java.sql.SQLException;
021:
022:        /**
023:         * Basic TypeHandler that can be used as a base class for majority of implementations
024:         *
025:         * @author Gennady Krizhevsky
026:         */
027:        public class DefaultTypeHandler implements  TypeHandler {
028:
029:            public DefaultTypeHandler() {
030:            }
031:
032:            /**
033:             * Transformes value read from the database
034:             *
035:             * @param data value read from the database
036:             * @return transformed value
037:             * @throws SQLException
038:             */
039:            public Object transformRead(Object data) throws SQLException {
040:                return data;
041:            }
042:
043:            protected Object readValue(ResultSet resultSet, int index,
044:                    DatabasePolicy databasePolicy) throws SQLException {
045:                return resultSet.getObject(index);
046:            }
047:
048:            protected Object readValue(ResultSet resultSet, String key,
049:                    DatabasePolicy databasePolicy) throws SQLException {
050:                return resultSet.getObject(key);
051:            }
052:
053:            /**
054:             * @see TypeHandler
055:             */
056:            public Object handleRead(ResultSet resultSet, int index,
057:                    DatabasePolicy databasePolicy) throws SQLException {
058:                return transformRead(readValue(resultSet, index, databasePolicy));
059:            }
060:
061:            /**
062:             * @see TypeHandler
063:             */
064:            public Object handleRead(ResultSet resultSet, String key,
065:                    DatabasePolicy databasePolicy) throws SQLException {
066:                return transformRead(readValue(resultSet, key, databasePolicy));
067:            }
068:
069:            /**
070:             * @see TypeHandler
071:             */
072:            public Object handleOutParamRead(CallableStatement statement,
073:                    int index, DatabasePolicy databasePolicy)
074:                    throws SQLException {
075:                return statement.getObject(index);
076:            }
077:
078:            /**
079:             * @see TypeHandler
080:             */
081:            public Object handleOutParamRead(CallableStatement statement,
082:                    String key, DatabasePolicy databasePolicy)
083:                    throws SQLException {
084:                return statement.getObject(key);
085:            }
086:
087:            /**
088:             * Transforms data before binding it
089:             *
090:             * @param data value to bind
091:             * @return transformed value
092:             * @throws SQLException
093:             */
094:            protected Object transformBind(Object data) throws SQLException {
095:                return data;
096:            }
097:
098:            /**
099:             * @see TypeHandler
100:             */
101:            public void handleBind(PreparedStatement statement, int index,
102:                    Object paramValue) throws SQLException {
103:                handleBind(statement, index, paramValue, 0, false);
104:            }
105:
106:            protected void handleBind(PreparedStatement statement, int index,
107:                    Object paramValue, int jdbcType, boolean useType)
108:                    throws SQLException {
109:                if (paramValue != null) {
110:                    Object value = transformBind(paramValue);
111:                    handleBindSetValue(statement, index, value, jdbcType,
112:                            useType);
113:                }
114:            }
115:
116:            protected void handleBindSetValue(PreparedStatement statement,
117:                    int index, Object value, int jdbcType, boolean useType)
118:                    throws SQLException {
119:                if (value != null) {
120:                    try {
121:                        if (useType && jdbcType != 0) {
122:                            statement.setObject(index, value, jdbcType);
123:                        } else {
124:                            statement.setObject(index, value);
125:                        }
126:                    } catch (SQLException e) {
127:                        String message = "Error while binding parameter with index ["
128:                                + index
129:                                + "], value ["
130:                                + value
131:                                + "] "
132:                                + "; using "
133:                                + getClass().getName()
134:                                + " type handler";
135:                        throw new OdalPersistencyException(message, e);
136:                    }
137:                } else {
138:                    throw new OdalRuntimePersistencyException(
139:                            "handleBindSetValue is used instead of handleBindNull "
140:                                    + "while binding parameterIndex [" + index
141:                                    + "] to bind NULL value using "
142:                                    + getClass().getName() + " type handler");
143:                }
144:
145:            }
146:
147:            /**
148:             * Used for binding null values in PreparedStatement
149:             *
150:             * @param statement        PreparedStatement
151:             * @param parameterIndex   1-based binding index
152:             * @param columnType       column type
153:             * @param columnIdentifier string identifying the column. It is used in error message if bind fails
154:             */
155:            public void handleBindNull(PreparedStatement statement,
156:                    int parameterIndex, ColumnType columnType,
157:                    String columnIdentifier) throws OdalPersistencyException {
158:                if (columnType == null) {
159:                    throw new OdalRuntimePersistencyException(
160:                            "columnType is null value "
161:                                    + "when binding null parameter;  parameterIndex "
162:                                    + parameterIndex + "] using "
163:                                    + getClass().getName() + " type handler");
164:                }
165:                try {
166:                    if (columnType.getDefaultJdbcTypeName() == null) {
167:                        statement.setNull(parameterIndex,
168:                                getJdbcTypeSafe(columnType));
169:                    } else {
170:                        statement.setNull(parameterIndex,
171:                                getJdbcTypeSafe(columnType), columnType
172:                                        .getDefaultJdbcTypeName());
173:                    }
174:                } catch (SQLException e) {
175:                    String message = "Error (setNull) while binding null value with "
176:                            + "index ["
177:                            + parameterIndex
178:                            + "] "
179:                            + "; columnType ["
180:                            + columnType
181:                            + "] "
182:                            + "; using "
183:                            + getClass().getName() + " type handler";
184:                    throw new OdalPersistencyException(message, e);
185:                }
186:            }
187:
188:            private int getJdbcTypeSafe(ColumnType columnType)
189:                    throws SQLException {
190:                int defaultJdbcType = columnType.getDefaultJdbcType();
191:                if (defaultJdbcType == 0) {
192:                    throw new SQLException(
193:                            "JDBC type is not set for setNull operation");
194:                }
195:                return defaultJdbcType;
196:            }
197:
198:            /**
199:             * @see TypeHandler
200:             */
201:            public void handleBind(PreparedStatement statement, int index,
202:                    Object value, DatabasePolicy databasePolicy,
203:                    LobPostProcessings postProcessings,
204:                    PersistentObject persistentObject, int fieldIndex)
205:                    throws SQLException {
206:                handleBind(statement, index, value);
207:            }
208:
209:            public boolean useTwoStepBinaryUpdate(DatabasePolicy databasePolicy) {
210:                return databasePolicy.useTwoStepBinaryUpdate();
211:            }
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.