Kenmegne
2025-12-10 e9d80d486b912144b59ebd5939d4837105b37b99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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++;
        }
 
    }
 
}