summaryrefslogtreecommitdiff
path: root/src/database/NamedPreparedStatement.java
blob: 089e2c7b73a781392142698d1ad0611d2b8921b0 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.joda.time.DateTime;

/**
 * Allows a prepared statement to contain named parameters instead of a question
 * mark position marker.
 *
 * @author Peter Wu
 */
public class NamedPreparedStatement {

    private final List<String> fields;
    private final PreparedStatement stmt;

    public NamedPreparedStatement(Connection conn, String query) throws SQLException {
        fields = new ArrayList<>();
        Pattern pattern = Pattern.compile("(?<!:):(\\w+)(::\\w+)?");
        Matcher matcher = pattern.matcher(query);
        while (matcher.find()) {
            fields.add(matcher.group(1));
        }
        String sql = query.replaceAll(pattern.pattern(), "?$2");
        stmt = conn.prepareStatement(sql);
    }

    private List<Integer> getParamIndices(String fieldName) {
        List<Integer> indices = new ArrayList<>();
        int index = 0;
        for (String name : fields) {
            ++index;
            if (name.equals(fieldName)) {
                indices.add(index);
            }
        }
        if (indices.isEmpty()) {
            System.err.println(stmt);
            throw new RuntimeException("Missing " + fieldName + " in query!");
        }
        return indices;
    }

    public void setLong(String name, long l) throws SQLException {
        for (int paramIndex : getParamIndices(name)) {
            stmt.setLong(paramIndex, l);
        }
    }

    public void setString(String name, String str) throws SQLException {
        for (int paramIndex : getParamIndices(name)) {
            stmt.setString(paramIndex, str);
        }
    }

    public void setString(String name, int i) throws SQLException {
        for (int paramIndex : getParamIndices(name)) {
            stmt.setInt(paramIndex, i);
        }
    }

    public void setTimestamp(String name, DateTime dt) throws SQLException {
        for (int paramIndex : getParamIndices(name)) {
            Timestamp tsp = new Timestamp(dt.getMillis());
            Calendar calendar = dt.toCalendar(Locale.ENGLISH);
            stmt.setTimestamp(paramIndex, tsp, calendar);
        }
    }

    public PreparedStatement getStmt() {
        return stmt;
    }

    public void executeUpdate() throws SQLException {
        try {
            getStmt().executeUpdate();
        } catch (SQLException ex) {
            System.err.println("Query error: " + ex.getMessage());
            System.err.println(stmt);
            throw ex;
        }
    }
}