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