comparison flys-aft/src/main/java/de/intevation/db/SymbolicStatement.java @ 4073:2c70fae83d0c

start syncing at river level. flys-aft/trunk@3406 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 13 Dec 2011 13:41:43 +0000
parents
children dbd0b3b1b8b8
comparison
equal deleted inserted replaced
4072:88f801888d85 4073:2c70fae83d0c
1 package de.intevation.db;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.Map;
6 import java.util.HashMap;
7
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 import java.sql.Connection;
12 import java.sql.SQLException;
13 import java.sql.PreparedStatement;
14 import java.sql.Timestamp;
15
16 import org.apache.log4j.Logger;
17
18 public class SymbolicStatement {
19
20 private static Logger log = Logger.getLogger(SymbolicStatement.class);
21
22 public static final Pattern VAR = Pattern.compile(":([a-zA-Z0-9_]+)");
23
24 protected String statement;
25 protected String compiled;
26 protected Map<String, List<Integer>> positions;
27
28 public class Instance {
29
30 /** TODO: Support more types. */
31
32 protected PreparedStatement stmnt;
33
34 public Instance(Connection connection) throws SQLException {
35 stmnt = connection.prepareStatement(compiled);
36 }
37
38 public void close() {
39 try {
40 stmnt.close();
41 }
42 catch (SQLException sqle) {
43 log.error("cannot close statement", sqle);
44 }
45 }
46
47 public void setInt(String key, int value)
48 throws SQLException
49 {
50 List<Integer> pos = positions.get(key.toLowerCase());
51 if (pos != null) {
52 for (Integer p: pos) {
53 stmnt.setInt(p, value);
54 }
55 }
56 }
57
58 public void setString(String key, String value)
59 throws SQLException
60 {
61 List<Integer> pos = positions.get(key.toLowerCase());
62 if (pos != null) {
63 for (Integer p: pos) {
64 stmnt.setString(p, value);
65 }
66 }
67 }
68
69 public void setObject(String key, Object value)
70 throws SQLException
71 {
72 List<Integer> pos = positions.get(key.toLowerCase());
73 if (pos != null) {
74 for (Integer p: pos) {
75 stmnt.setObject(p, value);
76 }
77 }
78 }
79
80 public void setTimestamp(String key, Timestamp value)
81 throws SQLException
82 {
83 List<Integer> pos = positions.get(key.toLowerCase());
84 if (pos != null) {
85 for (Integer p: pos) {
86 stmnt.setTimestamp(p, value);
87 }
88 }
89 }
90
91 public void setDouble(String key, int value)
92 throws SQLException
93 {
94 List<Integer> pos = positions.get(key.toLowerCase());
95 if (pos != null) {
96 for (Integer p: pos) {
97 stmnt.setDouble(p, value);
98 }
99 }
100 }
101
102 public void setNull(String key, int sqlType)
103 throws SQLException
104 {
105 List<Integer> pos = positions.get(key.toLowerCase());
106 if (pos != null) {
107 for (Integer p: pos) {
108 stmnt.setNull(p, sqlType);
109 }
110 }
111 }
112
113 public void set(Map<String, Object> map) throws SQLException {
114 for (Map.Entry<String, Object> entry: map.entrySet()) {
115 setObject(entry.getKey(), entry.getValue());
116 }
117 }
118
119 public void clearParameters() throws SQLException {
120 stmnt.clearParameters();
121 }
122
123 } // class Instance
124
125 public SymbolicStatement(String statement) {
126 this.statement = statement;
127 compile();
128 }
129
130 public String getStatement() {
131 return statement;
132 }
133
134 protected void compile() {
135 positions = new HashMap<String, List<Integer>>();
136
137 StringBuffer sb = new StringBuffer();
138 Matcher m = VAR.matcher(statement);
139 int index = 1;
140 while (m.find()) {
141 String key = m.group(1).toLowerCase();
142 List<Integer> list = positions.get(key);
143 if (list == null) {
144 list = new ArrayList<Integer>();
145 positions.put(key, list);
146 }
147 list.add(index++);
148 m.appendReplacement(sb, "?");
149 }
150 m.appendTail(sb);
151 compiled = sb.toString();
152 }
153 } // class SymbolicStatement

http://dive4elements.wald.intevation.org