Mercurial > dive4elements > river
annotate 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 |
rev | line source |
---|---|
4068
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.db; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.Properties; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import java.io.IOException; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 import java.io.InputStream; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import org.apache.log4j.Logger; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
4069
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
10 import java.util.Map; |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
11 import java.util.HashMap; |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
12 import java.util.Enumeration; |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
13 |
4068
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 public class Statements |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 private static Logger log = Logger.getLogger(Statements.class); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 public static final String RESOURCE_PATH = "/sql/"; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 public static final String COMMON_PROPERTIES = "-common.properties"; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 protected String type; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 protected String driver; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
4069
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
24 protected Map<String, SymbolicStatement> statements; |
4068
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 public Statements(String type, String driver) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 this.type = type; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 this.driver = driver; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 |
4069
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
31 public SymbolicStatement getStatement(String key) { |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
32 if (statements == null) { |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
33 statements = loadStatements(); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
34 } |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
35 return statements.get(key); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
36 } |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
37 |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
38 protected Map<String, SymbolicStatement> loadStatements() { |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
39 Map<String, SymbolicStatement> statements = |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
40 new HashMap<String, SymbolicStatement>(); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
41 |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
42 Properties properties = loadProperties(); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
43 |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
44 for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) { |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
45 String key = (String)e.nextElement(); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
46 String value = properties.getProperty(key); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
47 SymbolicStatement symbolic = new SymbolicStatement(value); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
48 statements.put(key, symbolic); |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
49 } |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
50 |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
51 return statements; |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
52 } |
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
53 |
4068
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 protected String driverToProperties() { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 return |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 type + "-" + |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 driver.replace('.', '-').toLowerCase() + ".properties"; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 protected Properties loadCommon() { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 Properties common = new Properties(); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 String path = RESOURCE_PATH + type + COMMON_PROPERTIES; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 InputStream in = Statements.class.getResourceAsStream(path); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 if (in != null) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 try { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 common.load(in); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 catch (IOException ioe) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 log.error("cannot load defaults: " + path, ioe); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 finally { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 try { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 in.close(); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 catch (IOException ioe) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 else { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 log.warn("cannot find: " + path); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 return common; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
4069
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
89 protected Properties loadProperties() { |
4068
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 Properties common = loadCommon(); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 |
4069
a4e79e8e0aa0
Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4068
diff
changeset
|
93 Properties properties = new Properties(common); |
4068
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 String path = RESOURCE_PATH + driverToProperties(); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 InputStream in = Statements.class.getResourceAsStream(path); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 if (in != null) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 try { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 properties.load(in); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 catch (IOException ioe) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 log.error("cannot load statements: " + path, ioe); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 finally { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 try { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 in.close(); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 catch (IOException ioe) { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 else { |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 log.warn("cannot find: " + path); |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
118 return properties; |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 } |
21e49e0a2307
Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |