HHH-6838 - Query Paramater Binding should quote strings

This commit is contained in:
Steve Ebersole 2013-10-14 16:56:31 -05:00
parent a96cc4bfbd
commit 67fccf231b
3 changed files with 63 additions and 36 deletions

View File

@ -28,7 +28,7 @@ import java.sql.SQLException;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreLogging;
import org.hibernate.type.descriptor.JdbcTypeNameMapper; import org.hibernate.type.descriptor.JdbcTypeNameMapper;
import org.hibernate.type.descriptor.ValueBinder; import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
@ -40,11 +40,10 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public abstract class BasicBinder<J> implements ValueBinder<J> { public abstract class BasicBinder<J> implements ValueBinder<J> {
private static final Logger log = CoreLogging.logger( BasicBinder.class );
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, BasicBinder.class.getName()); private static final String BIND_MSG_TEMPLATE = "binding parameter [%s] as [%s] - [%s]";
private static final String NULL_BIND_MSG_TEMPLATE = "binding parameter [%s] as [%s] - [null]";
private static final String BIND_MSG_TEMPLATE = "binding parameter [%s] as [%s] - %s";
private static final String NULL_BIND_MSG_TEMPLATE = "binding parameter [%s] as [%s] - <null>";
private final JavaTypeDescriptor<J> javaDescriptor; private final JavaTypeDescriptor<J> javaDescriptor;
private final SqlTypeDescriptor sqlDescriptor; private final SqlTypeDescriptor sqlDescriptor;
@ -62,33 +61,31 @@ public abstract class BasicBinder<J> implements ValueBinder<J> {
this.sqlDescriptor = sqlDescriptor; this.sqlDescriptor = sqlDescriptor;
} }
/** @Override
* {@inheritDoc}
*/
public final void bind(PreparedStatement st, J value, int index, WrapperOptions options) throws SQLException { public final void bind(PreparedStatement st, J value, int index, WrapperOptions options) throws SQLException {
final boolean traceEnabled = LOG.isTraceEnabled(); final boolean traceEnabled = log.isTraceEnabled();
if ( value == null ) { if ( value == null ) {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.trace( log.trace(
String.format( String.format(
NULL_BIND_MSG_TEMPLATE, NULL_BIND_MSG_TEMPLATE,
index, index,
JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ) JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
) )
); );
} }
st.setNull( index, sqlDescriptor.getSqlType() ); st.setNull( index, sqlDescriptor.getSqlType() );
} }
else { else {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.trace( log.trace(
String.format( String.format(
BIND_MSG_TEMPLATE, BIND_MSG_TEMPLATE,
index, index,
JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ), JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ),
getJavaDescriptor().extractLoggableRepresentation( value ) getJavaDescriptor().extractLoggableRepresentation( value )
) )
); );
} }
doBind( st, value, index, options ); doBind( st, value, index, options );
} }

View File

@ -29,7 +29,8 @@ import java.sql.SQLException;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreLogging;
import org.hibernate.type.descriptor.JdbcTypeNameMapper;
import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
@ -40,8 +41,7 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public abstract class BasicExtractor<J> implements ValueExtractor<J> { public abstract class BasicExtractor<J> implements ValueExtractor<J> {
private static final Logger log = CoreLogging.logger( BasicExtractor.class );
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, BasicExtractor.class.getName() );
private final JavaTypeDescriptor<J> javaDescriptor; private final JavaTypeDescriptor<J> javaDescriptor;
private final SqlTypeDescriptor sqlDescriptor; private final SqlTypeDescriptor sqlDescriptor;
@ -62,16 +62,25 @@ public abstract class BasicExtractor<J> implements ValueExtractor<J> {
@Override @Override
public J extract(ResultSet rs, String name, WrapperOptions options) throws SQLException { public J extract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
final J value = doExtract( rs, name, options ); final J value = doExtract( rs, name, options );
final boolean traceEnabled = LOG.isTraceEnabled(); final boolean traceEnabled = log.isTraceEnabled();
if ( value == null || rs.wasNull() ) { if ( value == null || rs.wasNull() ) {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.tracev( "Found [null] as column [{0}]", name ); log.tracef(
"extracted value ([%s] : [%s]) - [null]",
name,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
);
} }
return null; return null;
} }
else { else {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.tracev( "Found [{0}] as column [{1}]", getJavaDescriptor().extractLoggableRepresentation( value ), name ); log.tracef(
"extracted value ([%s] : [%s]) - [%s]",
name,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() ),
getJavaDescriptor().extractLoggableRepresentation( value )
);
} }
return value; return value;
} }
@ -96,16 +105,25 @@ public abstract class BasicExtractor<J> implements ValueExtractor<J> {
@Override @Override
public J extract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { public J extract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
final J value = doExtract( statement, index, options ); final J value = doExtract( statement, index, options );
final boolean traceEnabled = LOG.isTraceEnabled(); final boolean traceEnabled = log.isTraceEnabled();
if ( value == null || statement.wasNull() ) { if ( value == null || statement.wasNull() ) {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.tracev( "Found [null] as procedure output parameter [{0}]", index ); log.tracef(
"extracted procedure output parameter ([%s] : [%s]) - [null]",
index,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
);
} }
return null; return null;
} }
else { else {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.tracev( "Found [{0}] as procedure output parameter [{1}]", getJavaDescriptor().extractLoggableRepresentation( value ), index ); log.tracef(
"extracted procedure output parameter ([%s] : [%s]) - [%s]",
index,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() ),
getJavaDescriptor().extractLoggableRepresentation( value )
);
} }
return value; return value;
} }
@ -134,16 +152,25 @@ public abstract class BasicExtractor<J> implements ValueExtractor<J> {
} }
final String paramName = paramNames[0]; final String paramName = paramNames[0];
final J value = doExtract( statement, paramName, options ); final J value = doExtract( statement, paramName, options );
final boolean traceEnabled = LOG.isTraceEnabled(); final boolean traceEnabled = log.isTraceEnabled();
if ( value == null || statement.wasNull() ) { if ( value == null || statement.wasNull() ) {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.tracev( "Found [null] as procedure output parameter [{0}]", paramName ); log.tracef(
"extracted named procedure output parameter ([%s] : [%s]) - [null]",
paramName,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
);
} }
return null; return null;
} }
else { else {
if ( traceEnabled ) { if ( traceEnabled ) {
LOG.tracev( "Found [{0}] as procedure output parameter [{1}]", getJavaDescriptor().extractLoggableRepresentation( value ), paramName ); log.tracef(
"extracted named procedure output parameter ([%s] : [%s]) - [%s]",
paramName,
JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() ),
getJavaDescriptor().extractLoggableRepresentation( value )
);
} }
return value; return value;
} }

View File

@ -45,6 +45,9 @@ log4j.logger.org.hibernate.testing.cache=debug
# SQL Logging - HHH-6833 # SQL Logging - HHH-6833
log4j.logger.org.hibernate.SQL=debug log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=trace
log4j.logger.org.hibernate.hql.internal.ast=debug log4j.logger.org.hibernate.hql.internal.ast=debug
log4j.logger.org.hibernate.sql.ordering.antlr=debug log4j.logger.org.hibernate.sql.ordering.antlr=debug