protected PreparedStatement getPreparedStatement(Statement stmnt,
MessageContext msgCtx) throws SQLException {
boolean traceOn = isTraceOn(msgCtx);
boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
Log serviceLog = msgCtx.getServiceLog();
if (traceOrDebugOn) {
traceOrDebug(traceOn, "Getting a connection from DataSource " + getDSName() +
" and preparing statement : " + stmnt.getRawStatement());
}
Connection con = getDataSource().getConnection();
PreparedStatement ps = con.prepareStatement(stmnt.getRawStatement());
// set parameters if any
List params = stmnt.getParameters();
int column = 1;
for (Iterator pi = params.iterator(); pi.hasNext(); ) {
Statement.Parameter param = (Statement.Parameter) pi.next();
String value = (param.getPropertyName() != null ?
param.getPropertyName() : param.getXpath().stringValueOf(msgCtx));
if (traceOrDebugOn) {
traceOrDebug(traceOn, "Setting as parameter : " + column + " value : " + value +
" as JDBC Type : " + param.getType() + "(see java.sql.Types for valid types)");
}
switch (param.getType()) {
// according to J2SE 1.5 /docs/guide/jdbc/getstart/mapping.html
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR: {
ps.setString(column++, value);
break;
}
case Types.NUMERIC:
case Types.DECIMAL: {
ps.setBigDecimal(column++, new BigDecimal(value));
break;
}
case Types.BIT: {
ps.setBoolean(column++, Boolean.parseBoolean(value));
break;
}
case Types.TINYINT: {
ps.setByte(column++, Byte.parseByte(value));
break;
}
case Types.SMALLINT: {
ps.setShort(column++, Short.parseShort(value));
break;
}
case Types.INTEGER: {
ps.setInt(column++, Integer.parseInt(value));
break;
}
case Types.BIGINT: {
ps.setLong(column++, Long.parseLong(value));
break;
}
case Types.REAL: {
ps.setFloat(column++, Float.parseFloat(value));
break;
}
case Types.FLOAT: {
ps.setDouble(column++, Double.parseDouble(value));
break;
}
case Types.DOUBLE: {
ps.setDouble(column++, Double.parseDouble(value));
break;
}
// skip BINARY, VARBINARY and LONGVARBINARY
case Types.DATE: {
ps.setDate(column++, Date.valueOf(value));
break;
}
case Types.TIME: {
ps.setTime(column++, Time.valueOf(value));
break;
}
case Types.TIMESTAMP: {
ps.setTimestamp(column++, Timestamp.valueOf(value));
break;
}
// skip CLOB, BLOB, ARRAY, DISTINCT, STRUCT, REF, JAVA_OBJECT
default: {
String msg = "Trying to set an un-supported JDBC Type : " + param.getType() +
" against column : " + column + " and statement : " + stmnt.getRawStatement() +
" used by a DB mediator against DataSource : " + getDSName() +
" (see java.sql.Types for valid type values)";
handleException(msg, msgCtx);
}
}
}
if (traceOrDebugOn) {
traceOrDebug(traceOn, "Successfully prepared statement : " + stmnt.getRawStatement() +
" against DataSource : " + getDSName());
}
return ps;
}
Return a Prepared statement for the given Statement object, which is ready to be executed |