Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/CompiledStatement.java @ 372:fc3cf0ef777e
Added meta data service.
flys-artifacts/trunk@1781 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 29 Apr 2011 15:10:44 +0000 |
parents | |
children | c09c9e05ecfa |
comparison
equal
deleted
inserted
replaced
371:dfbb3d50b0bd | 372:fc3cf0ef777e |
---|---|
1 package de.intevation.flys.artifacts.services.meta; | |
2 | |
3 import java.util.regex.Pattern; | |
4 import java.util.regex.Matcher; | |
5 | |
6 import java.util.List; | |
7 import java.util.Map; | |
8 import java.util.HashMap; | |
9 import java.util.ArrayList; | |
10 | |
11 import java.sql.PreparedStatement; | |
12 import java.sql.SQLException; | |
13 import java.sql.Connection; | |
14 import java.sql.ResultSet; | |
15 | |
16 public class CompiledStatement | |
17 { | |
18 public static final Pattern VAR = Pattern.compile("\\$\\{([a-zA-Z0-9_]+)\\}"); | |
19 | |
20 protected String original; | |
21 protected String statement; | |
22 | |
23 protected Map<String, List<Integer>> positions; | |
24 | |
25 protected PreparedStatement preparedStatement; | |
26 | |
27 public CompiledStatement() { | |
28 } | |
29 | |
30 public CompiledStatement(String original) { | |
31 this.original = original; | |
32 positions = new HashMap<String, List<Integer>>(); | |
33 compile(); | |
34 } | |
35 | |
36 protected void compile() { | |
37 | |
38 StringBuffer sb = new StringBuffer(); | |
39 | |
40 Matcher m = VAR.matcher(original); | |
41 | |
42 int index = 1; | |
43 | |
44 while (m.find()) { | |
45 String key = m.group(1); | |
46 List<Integer> indices = positions.get(key); | |
47 if (indices == null) { | |
48 indices = new ArrayList<Integer>(); | |
49 positions.put(key, indices); | |
50 } | |
51 indices.add(index); | |
52 m.appendReplacement(sb, "?"); | |
53 ++index; | |
54 } | |
55 | |
56 m.appendTail(sb); | |
57 | |
58 statement = sb.toString(); | |
59 } | |
60 | |
61 public String getStatement() { | |
62 return statement; | |
63 } | |
64 | |
65 public ResultData execute(Connection connection, StackFrames frames) | |
66 throws SQLException | |
67 { | |
68 if (preparedStatement == null) { | |
69 preparedStatement = connection.prepareStatement(statement); | |
70 } | |
71 | |
72 for (Map.Entry<String, List<Integer>> entry: positions.entrySet()) { | |
73 Object value = frames.get(entry.getKey()); | |
74 for (Integer index: entry.getValue()) { | |
75 preparedStatement.setObject(index, value); | |
76 } | |
77 } | |
78 | |
79 ResultSet result = preparedStatement.executeQuery(); | |
80 try { | |
81 return new ResultData(preparedStatement.getMetaData()) | |
82 .addAll(result); | |
83 } | |
84 finally { | |
85 result.close(); | |
86 } | |
87 } | |
88 | |
89 public void close() { | |
90 if (preparedStatement != null) { | |
91 try { | |
92 preparedStatement.close(); | |
93 } | |
94 catch (SQLException sqle) { | |
95 } | |
96 preparedStatement = null; | |
97 } | |
98 } | |
99 } | |
100 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |