package com.megatim.fdxcommons.tools.database.contrat; import com.megatim.fdxcommons.tools.database.queries.metadata.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 queryParameters) throws NamingException, SQLException { processStatement(statement, queryParameters); } @Override public int lastProcessedIndex() { return lastIndex.get(); } private void processStatement(PreparedStatement preparedStatement, List 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++; } } }