annotate flys-aft/src/main/java/de/intevation/db/Statements.java @ 4846:298d540f7b70

WINFOArtifact: Use RangeAccess.getLocations instead of FLYSUtils.getLocations.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 23 Jan 2013 10:40:35 +0100
parents f939e1e6cfa4
children
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.io.IOException;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.io.InputStream;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5
4772
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4736
diff changeset
6 import java.util.Enumeration;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4736
diff changeset
7 import java.util.HashMap;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4736
diff changeset
8 import java.util.Map;
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4736
diff changeset
9 import java.util.Properties;
4068
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
4772
f939e1e6cfa4 Sorted imports.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4736
diff changeset
11 import org.apache.log4j.Logger;
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
12
4068
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 public class Statements
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 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
16
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 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
18 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
19
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 protected String type;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 protected String driver;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
23 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
24
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 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
26 this.type = type;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 this.driver = driver;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
30 public SymbolicStatement getStatement(String key) {
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
31 return getStatements().get(key);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
32 }
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 public Map<String, SymbolicStatement> getStatements() {
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
35 if (statements == null) {
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
36 statements = loadStatements();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
37 }
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4073
diff changeset
38 return statements;
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
39 }
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 protected Map<String, SymbolicStatement> loadStatements() {
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
42 Map<String, SymbolicStatement> statements =
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
43 new HashMap<String, SymbolicStatement>();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
44
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
45 Properties properties = loadProperties();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
46
4477
79bb64f66c74 Remove dead code and warnings.
Christian Lins <christian.lins@intevation.de>
parents: 4074
diff changeset
47 for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) {
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
48 String key = (String)e.nextElement();
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
49 String value = properties.getProperty(key);
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
50 SymbolicStatement symbolic = new SymbolicStatement(value);
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
51 statements.put(key, symbolic);
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
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
54 return statements;
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
55 }
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
56
4068
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 protected String driverToProperties() {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 return
4736
b195fede1c3b Remove trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4477
diff changeset
59 type + "-" +
4068
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 driver.replace('.', '-').toLowerCase() + ".properties";
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 }
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 protected Properties loadCommon() {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 Properties common = new Properties();
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 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
67
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 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
69
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 if (in != null) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 common.load(in);
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 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 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
76 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 finally {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 in.close();
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 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 }
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 else {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 log.warn("cannot find: " + path);
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
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 return common;
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
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
92 protected Properties loadProperties() {
4068
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 Properties common = loadCommon();
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95
4069
a4e79e8e0aa0 Added support for symbolic SQL statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4068
diff changeset
96 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
97
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 String path = RESOURCE_PATH + driverToProperties();
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 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
101
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 if (in != null) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 properties.load(in);
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 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 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
108 }
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 finally {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 try {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 in.close();
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 catch (IOException ioe) {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
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 else {
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 log.warn("cannot find: " + path);
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 return properties;
21e49e0a2307 Add infrastructure to load SQL statements for databases.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 }
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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org