KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > ParameterBinder


1 // $Id: ParameterBinder.java,v 1.3 2005/07/06 17:10:56 steveebersole Exp $
2
package org.hibernate.engine;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.type.Type;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 /**
15  * Centralizes the commonality regarding binding of parameter values into
16  * PreparedStatements as this logic is used in many places.
17  * <p/>
18  * Ideally would like to move to the parameter handling as it is done in
19  * the hql.ast package.
20  *
21  * @author Steve Ebersole
22  */

23 public class ParameterBinder {
24
25     private static final Log log = LogFactory.getLog( ParameterBinder.class );
26
27     public static interface NamedParameterSource {
28         public int[] getNamedParameterLocations(String JavaDoc name);
29     }
30
31     private ParameterBinder() {
32     }
33
34     public static int bindQueryParameters(
35             final PreparedStatement JavaDoc st,
36             final QueryParameters queryParameters,
37             final int start,
38             final NamedParameterSource source,
39             SessionImplementor session) throws SQLException JavaDoc, HibernateException {
40         int col = start;
41         col += bindPositionalParameters( st, queryParameters, col, session );
42         col += bindNamedParameters( st, queryParameters, col, source, session );
43         return col;
44     }
45
46     public static int bindPositionalParameters(
47             final PreparedStatement JavaDoc st,
48             final QueryParameters queryParameters,
49             final int start,
50             final SessionImplementor session) throws SQLException JavaDoc, HibernateException {
51         return bindPositionalParameters(
52                 st,
53                 queryParameters.getPositionalParameterValues(),
54                 queryParameters.getPositionalParameterTypes(),
55                 start,
56                 session
57         );
58     }
59
60     public static int bindPositionalParameters(
61             final PreparedStatement JavaDoc st,
62             final Object JavaDoc[] values,
63             final Type[] types,
64             final int start,
65             final SessionImplementor session) throws SQLException JavaDoc, HibernateException {
66         int span = 0;
67         for ( int i = 0; i < values.length; i++ ) {
68             types[i].nullSafeSet( st, values[i], start + span, session );
69             span += types[i].getColumnSpan( session.getFactory() );
70         }
71         return span;
72     }
73
74     public static int bindNamedParameters(
75             final PreparedStatement JavaDoc ps,
76             final QueryParameters queryParameters,
77             final int start,
78             final NamedParameterSource source,
79             final SessionImplementor session) throws SQLException JavaDoc, HibernateException {
80         return bindNamedParameters( ps, queryParameters.getNamedParameters(), start, source, session );
81     }
82
83     public static int bindNamedParameters(
84             final PreparedStatement JavaDoc ps,
85             final Map JavaDoc namedParams,
86             final int start,
87             final NamedParameterSource source,
88             final SessionImplementor session) throws SQLException JavaDoc, HibernateException {
89         if ( namedParams != null ) {
90             // assumes that types are all of span 1
91
Iterator JavaDoc iter = namedParams.entrySet().iterator();
92             int result = 0;
93             while ( iter.hasNext() ) {
94                 Map.Entry JavaDoc e = ( Map.Entry JavaDoc ) iter.next();
95                 String JavaDoc name = ( String JavaDoc ) e.getKey();
96                 TypedValue typedval = ( TypedValue ) e.getValue();
97                 int[] locations = source.getNamedParameterLocations( name );
98                 for ( int i = 0; i < locations.length; i++ ) {
99                     if ( log.isDebugEnabled() ) {
100                         log.debug( "bindNamedParameters() " +
101                                 typedval.getValue() + " -> " + name +
102                                 " [" + ( locations[i] + start ) + "]" );
103                     }
104                     typedval.getType().nullSafeSet( ps, typedval.getValue(), locations[i] + start, session );
105                 }
106                 result += locations.length;
107             }
108             return result;
109         }
110         else {
111             return 0;
112         }
113     }
114 }
115
Popular Tags