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: }
|