Skip to content

Commit

Permalink
0003954: Change incoming Integer to Varchar for Postgres BIT data type
Browse files Browse the repository at this point in the history
  • Loading branch information
philipmarzullo64 committed May 13, 2019
1 parent 2c00ad2 commit a143391
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Expand Up @@ -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 {

Expand Down Expand Up @@ -77,4 +80,34 @@ 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);
}
}
}
}
Expand Up @@ -1041,6 +1041,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);
}
Expand Down

0 comments on commit a143391

Please sign in to comment.