package com.megatim.queryadhoc.contrat;
|
|
import com.megatim.queryadhoc.model.QueryParam;
|
import java.sql.PreparedStatement;
|
import java.sql.SQLException;
|
import java.sql.Timestamp;
|
import java.time.LocalDateTime;
|
import java.util.List;
|
import java.util.concurrent.atomic.AtomicInteger;
|
import javax.naming.NamingException;
|
|
/**
|
*
|
* @author Gabuntu
|
*/
|
public class DefaultPreparedStatementProcessor implements PreparedStatementProcessor {
|
|
private final AtomicInteger lastIndex;
|
|
public DefaultPreparedStatementProcessor() {
|
lastIndex = new AtomicInteger(0);
|
}
|
|
public DefaultPreparedStatementProcessor(int startIndex) {
|
lastIndex = new AtomicInteger(startIndex);
|
}
|
|
@Override
|
public void process(PreparedStatement statement, List<QueryParam> queryParameters) throws NamingException, SQLException {
|
processStatement(statement, queryParameters);
|
}
|
|
@Override
|
public int lastProcessedIndex() {
|
return lastIndex.get();
|
}
|
|
private void processStatement(PreparedStatement preparedStatement, List<QueryParam> params) throws SQLException {
|
|
int statementIndex = 1;
|
|
for (QueryParam parameter : params) {
|
|
Object value = parameter.getParamValue();
|
if (value instanceof String) {
|
preparedStatement.setString(statementIndex, value.toString());
|
} else if (value instanceof Integer) {
|
preparedStatement.setInt(statementIndex, (Integer) value);
|
} else if (value instanceof Long) {
|
preparedStatement.setLong(statementIndex, (Long) value);
|
} else if (value instanceof Double) {
|
preparedStatement.setDouble(statementIndex, (Double) value);
|
} else if (value instanceof LocalDateTime) {
|
preparedStatement.setTimestamp(statementIndex, Timestamp.valueOf((LocalDateTime) value));
|
} else {
|
preparedStatement.setObject(statementIndex, value);
|
}
|
|
statementIndex++;
|
}
|
|
}
|
|
}
|