Skip to content

Commit

Permalink
Bypass getParameterType by default for PostgreSQL and SQL Server drivers
Browse files Browse the repository at this point in the history
Closes gh-25679
  • Loading branch information
jhoeller committed Dec 9, 2023
1 parent a7f9da1 commit 77b0382
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ public abstract class StatementCreatorUtils {
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";


static boolean shouldIgnoreGetParameterType = SpringProperties.getFlag(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);

private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class);

private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<>(64);

@Nullable
static Boolean shouldIgnoreGetParameterType;

static {
javaTypeToSqlTypeMap.put(boolean.class, Types.BOOLEAN);
javaTypeToSqlTypeMap.put(Boolean.class, Types.BOOLEAN);
Expand Down Expand Up @@ -114,6 +115,11 @@ public abstract class StatementCreatorUtils {
javaTypeToSqlTypeMap.put(java.sql.Timestamp.class, Types.TIMESTAMP);
javaTypeToSqlTypeMap.put(Blob.class, Types.BLOB);
javaTypeToSqlTypeMap.put(Clob.class, Types.CLOB);

String flag = SpringProperties.getProperty(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
if (flag != null) {
shouldIgnoreGetParameterType = Boolean.valueOf(flag);
}
}


Expand Down Expand Up @@ -250,9 +256,26 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, @
throws SQLException {

if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER && typeName == null)) {
boolean callGetParameterType = false;
boolean useSetObject = false;
Integer sqlTypeToUse = null;
if (!shouldIgnoreGetParameterType) {
if (shouldIgnoreGetParameterType != null) {
callGetParameterType = !shouldIgnoreGetParameterType;
}
else {
String jdbcDriverName = ps.getConnection().getMetaData().getDriverName();
if (jdbcDriverName.startsWith("PostgreSQL")) {
sqlTypeToUse = Types.NULL;
}
else if (jdbcDriverName.startsWith("Microsoft") && jdbcDriverName.contains("SQL Server")) {
sqlTypeToUse = Types.NULL;
useSetObject = true;
}
else {
callGetParameterType = true;
}
}
if (callGetParameterType) {
try {
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
}
Expand Down

0 comments on commit 77b0382

Please sign in to comment.