01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.jdbc.core.support;
18:
19: import java.sql.Connection;
20: import java.sql.PreparedStatement;
21: import java.sql.SQLException;
22:
23: import org.springframework.jdbc.core.SqlTypeValue;
24:
25: /**
26: * Abstract implementation of the SqlTypeValue interface, for convenient
27: * creation of type values that are supposed to be passed into the
28: * <code>PreparedStatement.setObject</code> method. The <code>createTypeValue</code>
29: * callback method has access to the underlying Connection, if that should
30: * be needed to create any database-specific objects.
31: *
32: * <p>A usage example from a StoredProcedure (compare this to the plain
33: * SqlTypeValue version in the superclass javadoc):
34: *
35: * <pre class="code">proc.declareParameter(new SqlParameter("myarray", Types.ARRAY, "NUMBERS"));
36: * ...
37: *
38: * Map in = new HashMap();
39: * in.put("myarray", new AbstractSqlTypeValue() {
40: * public Object createTypeValue(Connection con, int sqlType, String typeName) throws SQLException {
41: * oracle.sql.ArrayDescriptor desc = new oracle.sql.ArrayDescriptor(typeName, con);
42: * return new oracle.sql.ARRAY(desc, con, seats);
43: * }
44: * });
45: * Map out = execute(in);
46: * </pre>
47: *
48: * @author Juergen Hoeller
49: * @since 1.1
50: * @see java.sql.PreparedStatement#setObject(int, Object, int)
51: * @see org.springframework.jdbc.object.StoredProcedure
52: */
53: public abstract class AbstractSqlTypeValue implements SqlTypeValue {
54:
55: public final void setTypeValue(PreparedStatement ps,
56: int paramIndex, int sqlType, String typeName)
57: throws SQLException {
58:
59: Object value = createTypeValue(ps.getConnection(), sqlType,
60: typeName);
61: if (sqlType == TYPE_UNKNOWN) {
62: ps.setObject(paramIndex, value);
63: } else {
64: ps.setObject(paramIndex, value, sqlType);
65: }
66: }
67:
68: /**
69: * Create the type value to be passed into <code>PreparedStatement.setObject</code>.
70: * @param con the JDBC Connection, if needed to create any database-specific objects
71: * @param sqlType SQL type of the parameter we are setting
72: * @param typeName the type name of the parameter
73: * @return the type value
74: * @throws SQLException if a SQLException is encountered setting
75: * parameter values (that is, there's no need to catch SQLException)
76: * @see java.sql.PreparedStatement#setObject(int, Object, int)
77: */
78: protected abstract Object createTypeValue(Connection con,
79: int sqlType, String typeName) throws SQLException;
80:
81: }
|