Skip to content
Permalink
Browse files

0003955: Change incoming Integer to Varchar for Postgres BIT data type

(fixed in 3.9.23 on issue 0003954 also).
  • Loading branch information...
philipmarzullo64 committed May 13, 2019
1 parent 938f821 commit 8d6dec10db427c3b6526bdb37335a1f1f8ac98bd
@@ -26,11 +26,14 @@

import javax.sql.DataSource;

import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.platform.DatabaseInfo;
import org.jumpmind.db.sql.JdbcSqlTemplate;
import org.jumpmind.db.sql.SqlTemplateSettings;
import org.jumpmind.db.sql.SymmetricLobHandler;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.StatementCreatorUtils;
import org.springframework.jdbc.support.lob.LobHandler;

public class PostgreSqlJdbcSqlTemplate extends JdbcSqlTemplate {

@@ -78,5 +81,35 @@ public boolean isDataTruncationViolation(Throwable ex) {
}
return dataTruncationViolation;
}

@Override
public void setValues(PreparedStatement ps, Object[] args, int[] argTypes,
LobHandler lobHandler) throws SQLException {
for (int i = 1; i <= args.length; i++) {
Object arg = args[i - 1];
int argType = argTypes != null && argTypes.length >= i ? argTypes[i - 1] : SqlTypeValue.TYPE_UNKNOWN;
try {
if (argType == Types.BLOB && lobHandler != null && arg instanceof byte[]) {
lobHandler.getLobCreator().setBlobAsBytes(ps, i, (byte[]) arg);
} else if (argType == Types.BLOB && lobHandler != null && arg instanceof String) {
lobHandler.getLobCreator().setBlobAsBytes(ps, i, arg.toString().getBytes());
} else if (argType == Types.CLOB && lobHandler != null) {
lobHandler.getLobCreator().setClobAsString(ps, i, (String) arg);
} else if ((argType == Types.DECIMAL || argType == Types.NUMERIC) && arg != null) {
setDecimalValue(ps, i, arg, argType);
} else if (argType == Types.TINYINT) {
setTinyIntValue(ps, i, arg, argType);
} else if (argType == Types.BIT && arg instanceof Number) {
// Incoming value is Integer (value of 1 = true, 0 = false), convert to string so Postgres can handle it
setBitValue(ps, i, arg, argType);
} else {
StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), arg);
}
} catch (SQLException ex) {
String msg = String.format("Parameter arg '%s' type: %s caused exception: %s", arg, TypeMap.getJdbcTypeName(argType), ex.getMessage());
throw new SQLException(msg, ex);
}
}
}

}
@@ -1098,6 +1098,21 @@ protected void setDecimalValue(PreparedStatement ps, int i, Object arg, int argT
}
}

protected void setBitValue(PreparedStatement ps, int i, Object arg, int argType) throws SQLException {
if(argType == Types.BIT && arg != null && arg instanceof Number) {
Number n = (Number) arg;
if(n.intValue() > 0) {
StatementCreatorUtils.setParameterValue(ps, i, Types.VARCHAR, "1");
} else if(n.intValue() == 0) {
StatementCreatorUtils.setParameterValue(ps, i, Types.VARCHAR, "0");
} else {
StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), arg);
}
} else {
StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), arg);
}
}

protected void setNanOrNull(PreparedStatement ps, int i, Object arg, int argType) throws SQLException {
StatementCreatorUtils.setParameterValue(ps, i, verifyArgType(arg, argType), null);
}

0 comments on commit 8d6dec1

Please sign in to comment.
You can’t perform that action at this time.