annotate flys-aft/src/main/java/de/intevation/db/Statements.java @ 4320:c3ac330d9be4

NaviChartOutputTab: Slight refactoring to work not only with FixAnalysisArtifacts.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 30 Oct 2012 10:44:54 +0100
parents 100c4e0a45e1
children 79bb64f66c74
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) {
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
32 return getStatements().get(key);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
33 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
34
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
35 public Map<String, SymbolicStatement> getStatements() {
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
36 if (statements == null) {
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
37 statements = loadStatements();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
38 }
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
39 return statements;
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
40 }
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 protected Map<String, SymbolicStatement> loadStatements() {
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
43 Map<String, SymbolicStatement> statements =
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
44 new HashMap<String, SymbolicStatement>();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
45
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
46 Properties properties = loadProperties();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
47
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
48 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
49 String key = (String)e.nextElement();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
50 String value = properties.getProperty(key);
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
51 SymbolicStatement symbolic = new SymbolicStatement(value);
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
52 statements.put(key, symbolic);
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
53 }
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
54
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
55 return statements;
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
56 }
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
57
4068
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 protected String driverToProperties() {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 return
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 type + "-" +
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 driver.replace('.', '-').toLowerCase() + ".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
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 protected Properties loadCommon() {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 Properties common = new Properties();
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 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
68
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 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
70
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 if (in != null) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 common.load(in);
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 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
77 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 finally {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 in.close();
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 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 }
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 else {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 log.warn("cannot find: " + path);
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 return common;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 }
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 protected Properties loadProperties() {
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 Properties common = loadCommon();
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
97 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
98
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 String path = RESOURCE_PATH + driverToProperties();
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 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
102
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 if (in != null) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 properties.load(in);
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 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
109 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 finally {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 in.close();
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 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 }
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 else {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 log.warn("cannot find: " + path);
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
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 return properties;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org