Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/Builder.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 | d830c398c8f4 |
comparison
equal
deleted
inserted
replaced
972:0c8aca463bd4 | 973:c30ada285d45 |
---|---|
41 private static final XPathFactory XPATH_FACTORY = | 41 private static final XPathFactory XPATH_FACTORY = |
42 XPathFactory.newInstance(); | 42 XPathFactory.newInstance(); |
43 | 43 |
44 protected Document template; | 44 protected Document template; |
45 | 45 |
46 protected Map<String, CompiledStatement> compiledStatements; | |
47 | |
46 | 48 |
47 public class BuildHelper | 49 public class BuildHelper |
48 { | 50 { |
49 protected Node output; | 51 protected Node output; |
50 protected Document owner; | 52 protected Document owner; |
51 protected StackFrames frames; | 53 protected StackFrames frames; |
52 protected Connection connection; | 54 protected Connection connection; |
53 protected Map<String, CompiledStatement> statements; | 55 protected Map<String, CompiledStatement.Instance> statements; |
54 | 56 |
55 public BuildHelper( | 57 public BuildHelper( |
56 Node output, | 58 Node output, |
57 Connection connection, | 59 Connection connection, |
58 Map<String, Object> parameters | 60 Map<String, Object> parameters |
59 ) { | 61 ) { |
60 this.output = output; | 62 this.output = output; |
61 this.connection = connection; | 63 this.connection = connection; |
62 frames = new StackFrames(parameters); | 64 frames = new StackFrames(parameters); |
63 statements = new HashMap<String, CompiledStatement>(); | 65 statements = new HashMap<String, CompiledStatement.Instance>(); |
64 owner = getOwnerDocument(output); | 66 owner = getOwnerDocument(output); |
65 } | 67 } |
66 | 68 |
67 public void build(List<Node> elements) throws SQLException { | 69 public void build(List<Node> elements) throws SQLException { |
68 try { | 70 try { |
69 for (Node current: elements) { | 71 for (Node current: elements) { |
70 build(output, current); | 72 build(output, current); |
71 } | 73 } |
72 } | 74 } |
73 finally { | 75 finally { |
74 for (CompiledStatement cs: statements.values()) { | 76 closeStatements(); |
75 cs.close(); | 77 } |
76 } | 78 } |
77 statements.clear(); | 79 |
78 } | 80 protected void closeStatements() { |
81 for (CompiledStatement.Instance csi: statements.values()) { | |
82 csi.close(); | |
83 } | |
84 statements.clear(); | |
79 } | 85 } |
80 | 86 |
81 protected void context(Node parent, Element current) | 87 protected void context(Node parent, Element current) |
82 throws SQLException | 88 throws SQLException |
83 { | 89 { |
105 return; | 111 return; |
106 } | 112 } |
107 | 113 |
108 String stmntText = stmntNode.item(0).getTextContent(); | 114 String stmntText = stmntNode.item(0).getTextContent(); |
109 | 115 |
110 if (stmntText == null | 116 CompiledStatement.Instance csi = statements.get(stmntText); |
111 || (stmntText = trimStatement(stmntText)).length() == 0) { | 117 |
112 log.warn("dc:context: no sql statement found -> ignored"); | 118 if (csi == null) { |
113 return; | 119 CompiledStatement cs = compiledStatements.get(stmntText); |
114 } | 120 csi = cs.new Instance(); |
115 | 121 statements.put(stmntText, csi); |
116 CompiledStatement cs = statements.get(stmntText); | 122 } |
117 | 123 |
118 if (cs == null) { | 124 ResultData rd = csi.execute(connection, frames); |
119 cs = new CompiledStatement(stmntText); | |
120 statements.put(stmntText, cs); | |
121 } | |
122 | |
123 ResultData rd = cs.execute(connection, frames); | |
124 | 125 |
125 String [] columns = rd.getColumnLabels(); | 126 String [] columns = rd.getColumnLabels(); |
126 | 127 |
127 | 128 |
128 for (Object [] row: rd.getRows()) { | 129 for (Object [] row: rd.getRows()) { |
385 } | 386 } |
386 } // class BuildHelper | 387 } // class BuildHelper |
387 | 388 |
388 | 389 |
389 public Builder() { | 390 public Builder() { |
391 compiledStatements = new HashMap<String, CompiledStatement>(); | |
390 } | 392 } |
391 | 393 |
392 public Builder(Document template) { | 394 public Builder(Document template) { |
395 this(); | |
393 this.template = template; | 396 this.template = template; |
397 compileStatements(); | |
398 } | |
399 | |
400 protected void compileStatements() { | |
401 | |
402 NodeList nodes = template.getElementsByTagNameNS( | |
403 DC_NAMESPACE_URI, "statement"); | |
404 | |
405 for (int i = 0, N = nodes.getLength(); i < N; ++i) { | |
406 Element stmntElement = (Element)nodes.item(i); | |
407 String stmnt = trimStatement(stmntElement.getTextContent()); | |
408 if (stmnt == null || stmnt.length() == 0) { | |
409 throw new IllegalArgumentException("found empty statement"); | |
410 } | |
411 CompiledStatement cs = new CompiledStatement(stmnt); | |
412 // for faster lookup store a shortend string into the template | |
413 stmnt = "s" + i; | |
414 stmntElement.setTextContent(stmnt); | |
415 compiledStatements.put(stmnt, cs); | |
416 } | |
394 } | 417 } |
395 | 418 |
396 protected static List<Node> rootsToList(NodeList roots) { | 419 protected static List<Node> rootsToList(NodeList roots) { |
397 | 420 |
398 List<Node> elements = new ArrayList<Node>(); | 421 List<Node> elements = new ArrayList<Node>(); |
409 | 432 |
410 return elements; | 433 return elements; |
411 } | 434 } |
412 | 435 |
413 protected static final String trimStatement(String stmnt) { | 436 protected static final String trimStatement(String stmnt) { |
437 if (stmnt == null) return null; | |
414 //XXX: Maybe a bit to radical for multiline strings? | 438 //XXX: Maybe a bit to radical for multiline strings? |
415 return STRIP_LINE_INDENT.matcher(stmnt.trim()).replaceAll(" "); | 439 return STRIP_LINE_INDENT.matcher(stmnt.trim()).replaceAll(" "); |
416 } | 440 } |
417 | 441 |
418 protected static Document getOwnerDocument(Node node) { | 442 protected static Document getOwnerDocument(Node node) { |