From: Juergen H. <jho...@us...> - 2008-10-21 19:55:33
|
Update of /cvsroot/springframework/spring/src/org/springframework/jdbc/core In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv23464/src/org/springframework/jdbc/core Modified Files: DisposableSqlTypeValue.java SqlTypeValue.java StatementCreatorUtils.java Log Message: provided Jdbc4SqlXmlHandler as default implementation of the SqlXmlHandler interface; added SqlValue class to "jdbc.support" package, with SqlXmlValue derived from SqlValue instead of SqlTypeValue Index: SqlTypeValue.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/core/SqlTypeValue.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** SqlTypeValue.java 8 Apr 2006 20:55:26 -0000 1.12 --- SqlTypeValue.java 21 Oct 2008 19:55:26 -0000 1.13 *************** *** 1,4 **** /* ! * Copyright 2002-2006 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 23,28 **** /** ! * Interface to be implemented for setting values for more complex database specific ! * types not supported by the standard setObject method. * * <p>Implementations perform the actual work of setting the actual values. They must --- 23,29 ---- /** ! * Interface to be implemented for setting values for more complex database-specific ! * types not supported by the standard <code>setObject</code> method. This is ! * effectively an extended variant of {@link org.springframework.jdbc.support.SqlValue}. * * <p>Implementations perform the actual work of setting the actual values. They must *************** *** 38,41 **** --- 39,43 ---- * @see java.sql.PreparedStatement#setObject * @see JdbcOperations#update(String, Object[], int[]) + * @see org.springframework.jdbc.support.SqlValue */ public interface SqlTypeValue { *************** *** 57,62 **** * @param sqlType SQL type of the parameter we are setting * @param typeName the type name of the parameter (optional) ! * @throws SQLException if a SQLException is encountered setting parameter values ! * (that is, there's no need to catch SQLException) * @see java.sql.Types * @see java.sql.PreparedStatement#setObject --- 59,63 ---- * @param sqlType SQL type of the parameter we are setting * @param typeName the type name of the parameter (optional) ! * @throws SQLException if a SQLException is encountered while setting parameter values * @see java.sql.Types * @see java.sql.PreparedStatement#setObject Index: StatementCreatorUtils.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/core/StatementCreatorUtils.java,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** StatementCreatorUtils.java 2 Jul 2008 10:44:17 -0000 1.37 --- StatementCreatorUtils.java 21 Oct 2008 19:55:26 -0000 1.38 *************** *** 36,39 **** --- 36,41 ---- import org.apache.commons.logging.LogFactory; + import org.springframework.jdbc.support.SqlValue; + /** * Utility methods for PreparedStatementSetter/Creator and CallableStatementCreator *************** *** 187,191 **** // override type info? if (inValue instanceof SqlParameterValue) { ! SqlParameterValue parameterValue = (SqlParameterValue)inValue; if (logger.isDebugEnabled()) { logger.debug("Overriding typeinfo with runtime info from SqlParameterValue: column index " + paramIndex + --- 189,193 ---- // override type info? if (inValue instanceof SqlParameterValue) { ! SqlParameterValue parameterValue = (SqlParameterValue) inValue; if (logger.isDebugEnabled()) { logger.debug("Overriding typeinfo with runtime info from SqlParameterValue: column index " + paramIndex + *************** *** 202,207 **** } ! if (logger.isDebugEnabled()) { ! logger.debug("Setting SQL statement parameter value: column index " + paramIndex + ", parameter value [" + inValueToUse + "], value class [" + (inValueToUse != null ? inValueToUse.getClass().getName() : "null") + --- 204,209 ---- } ! if (logger.isTraceEnabled()) { ! logger.trace("Setting SQL statement parameter value: column index " + paramIndex + ", parameter value [" + inValueToUse + "], value class [" + (inValueToUse != null ? inValueToUse.getClass().getName() : "null") + *************** *** 210,340 **** if (inValueToUse == null) { ! if (sqlTypeToUse == SqlTypeValue.TYPE_UNKNOWN) { ! boolean useSetObject = false; ! sqlTypeToUse = Types.NULL; ! try { ! DatabaseMetaData dbmd = ps.getConnection().getMetaData(); ! String databaseProductName = dbmd.getDatabaseProductName(); ! String jdbcDriverName = dbmd.getDriverName(); ! if (databaseProductName.startsWith("Informix") || ! jdbcDriverName.startsWith("Microsoft SQL Server")) { ! useSetObject = true; ! } ! else if (databaseProductName.startsWith("DB2") || ! jdbcDriverName.startsWith("jConnect") || ! jdbcDriverName.startsWith("SQLServer")|| ! jdbcDriverName.startsWith("Apache Derby Embedded")) { ! sqlTypeToUse = Types.VARCHAR; ! } ! } ! catch (Throwable ex) { ! logger.debug("Could not check database or driver name", ex); ! } ! if (useSetObject) { ! ps.setObject(paramIndex, null); } ! else { ! ps.setNull(paramIndex, sqlTypeToUse); } } ! else if (typeNameToUse != null) { ! ps.setNull(paramIndex, sqlTypeToUse, typeNameToUse); } else { ! ps.setNull(paramIndex, sqlTypeToUse); } } ! else { // inValue != null ! if (inValueToUse instanceof SqlTypeValue) { ! ((SqlTypeValue) inValueToUse).setTypeValue(ps, paramIndex, sqlTypeToUse, typeNameToUse); } ! else if (sqlTypeToUse == Types.VARCHAR || sqlTypeToUse == Types.LONGVARCHAR || ! (sqlTypeToUse == Types.CLOB && isStringValue(inValueToUse.getClass()))) { ! ps.setString(paramIndex, inValueToUse.toString()); } ! else if (sqlTypeToUse == Types.DECIMAL || sqlTypeToUse == Types.NUMERIC) { ! if (inValueToUse instanceof BigDecimal) { ! ps.setBigDecimal(paramIndex, (BigDecimal) inValueToUse); ! } ! else if (scale != null) { ! ps.setObject(paramIndex, inValueToUse, sqlTypeToUse, scale.intValue()); ! } ! else { ! ps.setObject(paramIndex, inValueToUse, sqlTypeToUse); ! } } ! else if (sqlTypeToUse == Types.DATE) { ! if (inValueToUse instanceof java.util.Date) { ! if (inValueToUse instanceof java.sql.Date) { ! ps.setDate(paramIndex, (java.sql.Date) inValueToUse); ! } ! else { ! ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValueToUse).getTime())); ! } ! } ! else if (inValueToUse instanceof Calendar) { ! Calendar cal = (Calendar) inValueToUse; ! ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal); } else { ! ps.setObject(paramIndex, inValueToUse, Types.DATE); } } ! else if (sqlTypeToUse == Types.TIME) { ! if (inValueToUse instanceof java.util.Date) { ! if (inValueToUse instanceof java.sql.Time) { ! ps.setTime(paramIndex, (java.sql.Time) inValueToUse); ! } ! else { ! ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValueToUse).getTime())); ! } ! } ! else if (inValueToUse instanceof Calendar) { ! Calendar cal = (Calendar) inValueToUse; ! ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal); ! } ! else { ! ps.setObject(paramIndex, inValueToUse, Types.TIME); ! } } ! else if (sqlTypeToUse == Types.TIMESTAMP) { ! if (inValueToUse instanceof java.util.Date) { ! if (inValueToUse instanceof java.sql.Timestamp) { ! ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValueToUse); ! } ! else { ! ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValueToUse).getTime())); ! } ! } ! else if (inValueToUse instanceof Calendar) { ! Calendar cal = (Calendar) inValueToUse; ! ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); } else { ! ps.setObject(paramIndex, inValueToUse, Types.TIMESTAMP); } } ! else if (sqlTypeToUse == SqlTypeValue.TYPE_UNKNOWN) { ! if (isStringValue(inValueToUse.getClass())) { ! ps.setString(paramIndex, inValueToUse.toString()); ! } ! else if (isDateValue(inValueToUse.getClass())) { ! ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValueToUse).getTime())); ! } ! else if (inValueToUse instanceof Calendar) { ! Calendar cal = (Calendar) inValueToUse; ! ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); } else { ! // Fall back to generic setObject call without SQL type specified. ! ps.setObject(paramIndex, inValueToUse); } } else { ! // Fall back to generic setObject call with SQL type specified. ! ps.setObject(paramIndex, inValueToUse, sqlTypeToUse); } } } --- 212,354 ---- if (inValueToUse == null) { ! setNull(ps, paramIndex, sqlTypeToUse, typeNameToUse); ! } ! else { ! setValue(ps, paramIndex, sqlTypeToUse, typeNameToUse, scale, inValueToUse); ! } ! } ! ! private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, String typeName) throws SQLException { ! if (sqlType == SqlTypeValue.TYPE_UNKNOWN) { ! boolean useSetObject = false; ! sqlType = Types.NULL; ! try { ! DatabaseMetaData dbmd = ps.getConnection().getMetaData(); ! String databaseProductName = dbmd.getDatabaseProductName(); ! String jdbcDriverName = dbmd.getDriverName(); ! if (databaseProductName.startsWith("Informix") || ! jdbcDriverName.startsWith("Microsoft SQL Server")) { ! useSetObject = true; } ! else if (databaseProductName.startsWith("DB2") || ! jdbcDriverName.startsWith("jConnect") || ! jdbcDriverName.startsWith("SQLServer")|| ! jdbcDriverName.startsWith("Apache Derby Embedded")) { ! sqlType = Types.VARCHAR; } } ! catch (Throwable ex) { ! logger.debug("Could not check database or driver name", ex); ! } ! if (useSetObject) { ! ps.setObject(paramIndex, null); } else { ! ps.setNull(paramIndex, sqlType); } } + else if (typeName != null) { + ps.setNull(paramIndex, sqlType, typeName); + } + else { + ps.setNull(paramIndex, sqlType); + } + } ! private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName, ! Integer scale, Object inValue) throws SQLException { ! ! if (inValue instanceof SqlTypeValue) { ! ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName); ! } ! else if (inValue instanceof SqlValue) { ! ((SqlValue) inValue).setValue(ps, paramIndex); ! } ! else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR || ! (sqlType == Types.CLOB && isStringValue(inValue.getClass()))) { ! ps.setString(paramIndex, inValue.toString()); ! } ! else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) { ! if (inValue instanceof BigDecimal) { ! ps.setBigDecimal(paramIndex, (BigDecimal) inValue); } ! else if (scale != null) { ! ps.setObject(paramIndex, inValue, sqlType, scale.intValue()); } ! else { ! ps.setObject(paramIndex, inValue, sqlType); } ! } ! else if (sqlType == Types.DATE) { ! if (inValue instanceof java.util.Date) { ! if (inValue instanceof java.sql.Date) { ! ps.setDate(paramIndex, (java.sql.Date) inValue); } else { ! ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime())); } } ! else if (inValue instanceof Calendar) { ! Calendar cal = (Calendar) inValue; ! ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal); } ! else { ! ps.setObject(paramIndex, inValue, Types.DATE); ! } ! } ! else if (sqlType == Types.TIME) { ! if (inValue instanceof java.util.Date) { ! if (inValue instanceof java.sql.Time) { ! ps.setTime(paramIndex, (java.sql.Time) inValue); } else { ! ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime())); } } ! else if (inValue instanceof Calendar) { ! Calendar cal = (Calendar) inValue; ! ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal); ! } ! else { ! ps.setObject(paramIndex, inValue, Types.TIME); ! } ! } ! else if (sqlType == Types.TIMESTAMP) { ! if (inValue instanceof java.util.Date) { ! if (inValue instanceof java.sql.Timestamp) { ! ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue); } else { ! ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); } } + else if (inValue instanceof Calendar) { + Calendar cal = (Calendar) inValue; + ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); + } else { ! ps.setObject(paramIndex, inValue, Types.TIMESTAMP); } } + else if (sqlType == SqlTypeValue.TYPE_UNKNOWN) { + if (isStringValue(inValue.getClass())) { + ps.setString(paramIndex, inValue.toString()); + } + else if (isDateValue(inValue.getClass())) { + ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime())); + } + else if (inValue instanceof Calendar) { + Calendar cal = (Calendar) inValue; + ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal); + } + else { + // Fall back to generic setObject call without SQL type specified. + ps.setObject(paramIndex, inValue); + } + } + else { + // Fall back to generic setObject call with SQL type specified. + ps.setObject(paramIndex, inValue, sqlType); + } } *************** *** 386,389 **** --- 400,406 ---- ((DisposableSqlTypeValue) inValue).cleanup(); } + else if (inValue instanceof SqlValue) { + ((SqlValue) inValue).cleanup(); + } } } Index: DisposableSqlTypeValue.java =================================================================== RCS file: /cvsroot/springframework/spring/src/org/springframework/jdbc/core/DisposableSqlTypeValue.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** DisposableSqlTypeValue.java 27 May 2005 19:50:02 -0000 1.4 --- DisposableSqlTypeValue.java 21 Oct 2008 19:55:26 -0000 1.5 *************** *** 1,4 **** /* ! * Copyright 2002-2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 18,22 **** /** ! * Subinterface of SqlTypeValue that adds a cleanup callback, * to be invoked after the value has been set and the corresponding * statement has been executed. --- 18,22 ---- /** ! * Subinterface of {@link SqlTypeValue} that adds a cleanup callback, * to be invoked after the value has been set and the corresponding * statement has been executed. *************** *** 31,35 **** * Clean up resources held by this type value, * for example the LobCreator in case of a SqlLobValue. ! * @see org.springframework.jdbc.core.support.SqlLobValue#cleanup */ void cleanup(); --- 31,36 ---- * Clean up resources held by this type value, * for example the LobCreator in case of a SqlLobValue. ! * @see org.springframework.jdbc.core.support.SqlLobValue#cleanup() ! * @see org.springframework.jdbc.support.SqlValue#cleanup() */ void cleanup(); |