1 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 ; 10 import java.sql.SQLException ; 11 import java.util.Map ; 12 import java.util.Iterator ; 13 14 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 name); 29 } 30 31 private ParameterBinder() { 32 } 33 34 public static int bindQueryParameters( 35 final PreparedStatement st, 36 final QueryParameters queryParameters, 37 final int start, 38 final NamedParameterSource source, 39 SessionImplementor session) throws SQLException , 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 st, 48 final QueryParameters queryParameters, 49 final int start, 50 final SessionImplementor session) throws SQLException , 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 st, 62 final Object [] values, 63 final Type[] types, 64 final int start, 65 final SessionImplementor session) throws SQLException , 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 ps, 76 final QueryParameters queryParameters, 77 final int start, 78 final NamedParameterSource source, 79 final SessionImplementor session) throws SQLException , HibernateException { 80 return bindNamedParameters( ps, queryParameters.getNamedParameters(), start, source, session ); 81 } 82 83 public static int bindNamedParameters( 84 final PreparedStatement ps, 85 final Map namedParams, 86 final int start, 87 final NamedParameterSource source, 88 final SessionImplementor session) throws SQLException , HibernateException { 89 if ( namedParams != null ) { 90 Iterator iter = namedParams.entrySet().iterator(); 92 int result = 0; 93 while ( iter.hasNext() ) { 94 Map.Entry e = ( Map.Entry ) iter.next(); 95 String name = ( String ) 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 |