comparison flys-aft/src/main/java/org/dive4elements/etl/db/SymbolicStatement.java @ 5824:06643e440d1e

Moved directories to org.dive4elements.etl
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:35:06 +0200
parents flys-aft/src/main/java/de/intevation/db/SymbolicStatement.java@7a68967ca72a
children
comparison
equal deleted inserted replaced
5823:52e966cc7d35 5824:06643e440d1e
1 package de.intevation.db;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Timestamp;
8
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import org.apache.log4j.Logger;
18
19 public class SymbolicStatement {
20
21 private static Logger log = Logger.getLogger(SymbolicStatement.class);
22
23 public static final Pattern VAR = Pattern.compile(":([a-zA-Z0-9_]+)");
24
25 protected String statement;
26 protected String compiled;
27 protected Map<String, List<Integer>> positions;
28
29 public class Instance {
30
31 /** TODO: Support more types. */
32
33 protected PreparedStatement stmnt;
34
35 public Instance(Connection connection) throws SQLException {
36 stmnt = connection.prepareStatement(compiled);
37 }
38
39 public void close() {
40 try {
41 stmnt.close();
42 }
43 catch (SQLException sqle) {
44 log.error("cannot close statement", sqle);
45 }
46 }
47
48 public Instance setInt(String key, int value)
49 throws SQLException
50 {
51 List<Integer> pos = positions.get(key.toLowerCase());
52 if (pos != null) {
53 for (Integer p: pos) {
54 stmnt.setInt(p, value);
55 }
56 }
57
58 return this;
59 }
60
61 public Instance setString(String key, String value)
62 throws SQLException
63 {
64 List<Integer> pos = positions.get(key.toLowerCase());
65 if (pos != null) {
66 for (Integer p: pos) {
67 stmnt.setString(p, value);
68 }
69 }
70 return this;
71 }
72
73 public Instance setObject(String key, Object value)
74 throws SQLException
75 {
76 List<Integer> pos = positions.get(key.toLowerCase());
77 if (pos != null) {
78 for (Integer p: pos) {
79 stmnt.setObject(p, value);
80 }
81 }
82 return this;
83 }
84
85 public Instance setTimestamp(String key, Timestamp value)
86 throws SQLException
87 {
88 List<Integer> pos = positions.get(key.toLowerCase());
89 if (pos != null) {
90 for (Integer p: pos) {
91 stmnt.setTimestamp(p, value);
92 }
93 }
94 return this;
95 }
96
97 public Instance setDouble(String key, double value)
98 throws SQLException
99 {
100 List<Integer> pos = positions.get(key.toLowerCase());
101 if (pos != null) {
102 for (Integer p: pos) {
103 stmnt.setDouble(p, value);
104 }
105 }
106 return this;
107 }
108
109 public Instance setLong(String key, long value)
110 throws SQLException
111 {
112 List<Integer> pos = positions.get(key.toLowerCase());
113 if (pos != null) {
114 for (Integer p: pos) {
115 stmnt.setLong(p, value);
116 }
117 }
118 return this;
119 }
120
121 public Instance setNull(String key, int sqlType)
122 throws SQLException
123 {
124 List<Integer> pos = positions.get(key.toLowerCase());
125 if (pos != null) {
126 for (Integer p: pos) {
127 stmnt.setNull(p, sqlType);
128 }
129 }
130 return this;
131 }
132
133 public Instance set(Map<String, Object> map) throws SQLException {
134 for (Map.Entry<String, Object> entry: map.entrySet()) {
135 setObject(entry.getKey(), entry.getValue());
136 }
137 return this;
138 }
139
140 public Instance clearParameters() throws SQLException {
141 stmnt.clearParameters();
142 return this;
143 }
144
145 public boolean execute() throws SQLException {
146 if (log.isDebugEnabled()) {
147 log.debug("execute: " + compiled);
148 }
149 return stmnt.execute();
150 }
151
152 public ResultSet executeQuery() throws SQLException {
153 if (log.isDebugEnabled()) {
154 log.debug("query: " + compiled);
155 }
156 return stmnt.executeQuery();
157 }
158
159 public int executeUpdate() throws SQLException {
160 if (log.isDebugEnabled()) {
161 log.debug("update: " + compiled);
162 }
163 return stmnt.executeUpdate();
164 }
165
166 } // class Instance
167
168 public SymbolicStatement(String statement) {
169 this.statement = statement;
170 compile();
171 }
172
173 public String getStatement() {
174 return statement;
175 }
176
177 protected void compile() {
178 positions = new HashMap<String, List<Integer>>();
179
180 StringBuffer sb = new StringBuffer();
181 Matcher m = VAR.matcher(statement);
182 int index = 1;
183 while (m.find()) {
184 String key = m.group(1).toLowerCase();
185 List<Integer> list = positions.get(key);
186 if (list == null) {
187 list = new ArrayList<Integer>();
188 positions.put(key, list);
189 }
190 list.add(index++);
191 m.appendReplacement(sb, "?");
192 }
193 m.appendTail(sb);
194 compiled = sb.toString();
195 }
196 } // class SymbolicStatement

http://dive4elements.wald.intevation.org