Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/CompiledStatement.java @ 973:c30ada285d45
Reuse the compiled statements in meta data service.
flys-artifacts/trunk@2399 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sat, 23 Jul 2011 18:47:08 +0000 |
parents | 0c8aca463bd4 |
children |
comparison
equal
deleted
inserted
replaced
972:0c8aca463bd4 | 973:c30ada285d45 |
---|---|
29 protected String original; | 29 protected String original; |
30 protected String statement; | 30 protected String statement; |
31 | 31 |
32 protected Map<String, List<Integer>> positions; | 32 protected Map<String, List<Integer>> positions; |
33 | 33 |
34 protected PreparedStatement preparedStatement; | 34 protected int numVars; |
35 | 35 |
36 protected int numVars; | 36 public class Instance { |
37 | |
38 protected PreparedStatement preparedStatement; | |
39 | |
40 public Instance() { | |
41 } | |
42 | |
43 protected ResultData executeCached( | |
44 Cache cache, | |
45 Connection connection, | |
46 StackFrames frames | |
47 ) | |
48 throws SQLException | |
49 { | |
50 Object [] values = new Object[numVars]; | |
51 | |
52 StringBuilder sb = new StringBuilder(original); | |
53 | |
54 for (Map.Entry<String, List<Integer>> entry: positions.entrySet()) { | |
55 String key = entry.getKey(); | |
56 Object value = frames.get(key); | |
57 sb.append(';').append(key).append(':').append(value); | |
58 for (Integer index: entry.getValue()) { | |
59 values[index] = value; | |
60 } | |
61 } | |
62 | |
63 // XXX: Maybe too many collisions? | |
64 // String key = original + Arrays.hashCode(values); | |
65 String key = sb.toString(); | |
66 | |
67 Element element = cache.get(key); | |
68 | |
69 if (element != null) { | |
70 return (ResultData)element.getValue(); | |
71 } | |
72 | |
73 if (preparedStatement == null) { | |
74 preparedStatement = connection.prepareStatement(statement); | |
75 } | |
76 | |
77 for (int i = 0; i < values.length; ++i) { | |
78 preparedStatement.setObject(i+1, values[i]); | |
79 } | |
80 | |
81 ResultData data; | |
82 | |
83 ResultSet result = preparedStatement.executeQuery(); | |
84 try { | |
85 data = new ResultData(preparedStatement.getMetaData()) | |
86 .addAll(result); | |
87 } | |
88 finally { | |
89 result.close(); | |
90 } | |
91 | |
92 element = new Element(key, data); | |
93 cache.put(element); | |
94 | |
95 return data; | |
96 } | |
97 | |
98 protected ResultData executeUncached( | |
99 Connection connection, | |
100 StackFrames frames | |
101 ) | |
102 throws SQLException | |
103 { | |
104 if (preparedStatement == null) { | |
105 preparedStatement = connection.prepareStatement(statement); | |
106 } | |
107 | |
108 for (Map.Entry<String, List<Integer>> entry: positions.entrySet()) { | |
109 Object value = frames.get(entry.getKey()); | |
110 for (Integer index: entry.getValue()) { | |
111 preparedStatement.setObject(index+1, value); | |
112 } | |
113 } | |
114 | |
115 ResultSet result = preparedStatement.executeQuery(); | |
116 try { | |
117 return new ResultData(preparedStatement.getMetaData()) | |
118 .addAll(result); | |
119 } | |
120 finally { | |
121 result.close(); | |
122 } | |
123 } | |
124 | |
125 public ResultData execute(Connection connection, StackFrames frames) | |
126 throws SQLException | |
127 { | |
128 Cache cache = CacheFactory.getCache(DATACAGE_DB_CACHE); | |
129 | |
130 return cache != null | |
131 ? executeCached(cache, connection, frames) | |
132 : executeUncached(connection, frames); | |
133 } | |
134 | |
135 public void close() { | |
136 if (preparedStatement != null) { | |
137 try { | |
138 preparedStatement.close(); | |
139 } | |
140 catch (SQLException sqle) { | |
141 } | |
142 preparedStatement = null; | |
143 } | |
144 } | |
145 } // class Instance | |
37 | 146 |
38 public CompiledStatement() { | 147 public CompiledStatement() { |
39 } | 148 } |
40 | 149 |
41 public CompiledStatement(String original) { | 150 public CompiledStatement(String original) { |
73 } | 182 } |
74 | 183 |
75 public String getStatement() { | 184 public String getStatement() { |
76 return statement; | 185 return statement; |
77 } | 186 } |
78 | |
79 protected ResultData executeCached( | |
80 Cache cache, | |
81 Connection connection, | |
82 StackFrames frames | |
83 ) | |
84 throws SQLException | |
85 { | |
86 Object [] values = new Object[numVars]; | |
87 | |
88 StringBuilder sb = new StringBuilder(original); | |
89 | |
90 for (Map.Entry<String, List<Integer>> entry: positions.entrySet()) { | |
91 String key = entry.getKey(); | |
92 Object value = frames.get(key); | |
93 sb.append(';').append(key).append(':').append(value); | |
94 for (Integer index: entry.getValue()) { | |
95 values[index] = value; | |
96 } | |
97 } | |
98 | |
99 // XXX: Maybe too many collisions? | |
100 // String key = original + Arrays.hashCode(values); | |
101 String key = sb.toString(); | |
102 | |
103 Element element = cache.get(key); | |
104 | |
105 if (element != null) { | |
106 return (ResultData)element.getValue(); | |
107 } | |
108 | |
109 if (preparedStatement == null) { | |
110 preparedStatement = connection.prepareStatement(statement); | |
111 } | |
112 | |
113 for (int i = 0; i < values.length; ++i) { | |
114 preparedStatement.setObject(i+1, values[i]); | |
115 } | |
116 | |
117 ResultData data; | |
118 | |
119 ResultSet result = preparedStatement.executeQuery(); | |
120 try { | |
121 data = new ResultData(preparedStatement.getMetaData()) | |
122 .addAll(result); | |
123 } | |
124 finally { | |
125 result.close(); | |
126 } | |
127 | |
128 element = new Element(key, data); | |
129 cache.put(element); | |
130 | |
131 return data; | |
132 } | |
133 | |
134 protected ResultData executeUncached( | |
135 Connection connection, | |
136 StackFrames frames | |
137 ) | |
138 throws SQLException | |
139 { | |
140 if (preparedStatement == null) { | |
141 preparedStatement = connection.prepareStatement(statement); | |
142 } | |
143 | |
144 for (Map.Entry<String, List<Integer>> entry: positions.entrySet()) { | |
145 Object value = frames.get(entry.getKey()); | |
146 for (Integer index: entry.getValue()) { | |
147 preparedStatement.setObject(index+1, value); | |
148 } | |
149 } | |
150 | |
151 ResultSet result = preparedStatement.executeQuery(); | |
152 try { | |
153 return new ResultData(preparedStatement.getMetaData()) | |
154 .addAll(result); | |
155 } | |
156 finally { | |
157 result.close(); | |
158 } | |
159 } | |
160 | |
161 public ResultData execute(Connection connection, StackFrames frames) | |
162 throws SQLException | |
163 { | |
164 Cache cache = CacheFactory.getCache(DATACAGE_DB_CACHE); | |
165 | |
166 return cache != null | |
167 ? executeCached(cache, connection, frames) | |
168 : executeUncached(connection, frames); | |
169 } | |
170 | |
171 public void close() { | |
172 if (preparedStatement != null) { | |
173 try { | |
174 preparedStatement.close(); | |
175 } | |
176 catch (SQLException sqle) { | |
177 } | |
178 preparedStatement = null; | |
179 } | |
180 } | |
181 } | 187 } |
182 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |