1 |
package dk.daoas.fulddaekning;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.sql.Connection;
|
5 |
import java.sql.DriverManager;
|
6 |
import java.sql.PreparedStatement;
|
7 |
import java.sql.ResultSet;
|
8 |
import java.sql.SQLException;
|
9 |
import java.util.ArrayList;
|
10 |
import java.util.List;
|
11 |
import java.util.Properties;
|
12 |
import java.util.Queue;
|
13 |
import java.util.concurrent.ConcurrentLinkedQueue;
|
14 |
import java.util.logging.Logger;
|
15 |
|
16 |
|
17 |
|
18 |
public class Database {
|
19 |
Logger logger = Logger.getLogger(Database.class.getName());
|
20 |
|
21 |
int batchCount = 0;
|
22 |
|
23 |
Connection conn;
|
24 |
PreparedStatement saveStmt;
|
25 |
|
26 |
public Database(SafeProperties conf) throws SQLException,IOException {
|
27 |
this.conn = getConnection( conf );
|
28 |
|
29 |
String sql = "INSERT INTO fulddaekning.afstand_anden_rute_ny (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+
|
30 |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";
|
31 |
|
32 |
saveStmt = conn.prepareStatement(sql);
|
33 |
|
34 |
}
|
35 |
|
36 |
public void resetResultTable() throws SQLException {
|
37 |
logger.info("Dropping old result table (if exists)");
|
38 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny";
|
39 |
conn.createStatement().executeUpdate(sql);
|
40 |
|
41 |
logger.info("Create new result table");
|
42 |
sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute";
|
43 |
conn.createStatement().executeUpdate(sql);
|
44 |
}
|
45 |
|
46 |
public void renameResultTables() throws SQLException {
|
47 |
Constants consts = Constants.getInstance();
|
48 |
String ext = consts.getTableExtension();
|
49 |
|
50 |
logger.info("Dropping old backup table (if exists)");
|
51 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext;
|
52 |
conn.createStatement().executeUpdate(sql);
|
53 |
|
54 |
logger.info("Rename tables");
|
55 |
sql = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext;
|
56 |
|
57 |
logger.info("Executing: " + sql);
|
58 |
conn.createStatement().executeUpdate(sql);
|
59 |
}
|
60 |
|
61 |
public BoundingBox getBoundingbox(String postnr) throws SQLException {
|
62 |
String minPostnr = postnr.replace('x', '0');
|
63 |
String maxPostnr = postnr.replace('x', '9');
|
64 |
|
65 |
String sql =
|
66 |
"SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin " +
|
67 |
"FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";
|
68 |
|
69 |
PreparedStatement stmt = conn.prepareStatement(sql);
|
70 |
stmt.setString(1, minPostnr);
|
71 |
stmt.setString(2, maxPostnr);
|
72 |
|
73 |
ResultSet res = stmt.executeQuery();
|
74 |
res.next(); //query returnerer altid 1 række
|
75 |
|
76 |
BoundingBox bbox = new BoundingBox();
|
77 |
bbox.latitudeMax = res.getDouble("latmax");
|
78 |
bbox.latitudeMin = res.getDouble("latmin");
|
79 |
bbox.longitudeMax = res.getDouble("lngmax");
|
80 |
bbox.longitudeMin = res.getDouble("lngmin");
|
81 |
|
82 |
res.close();
|
83 |
stmt.close();
|
84 |
|
85 |
return bbox;
|
86 |
}
|
87 |
|
88 |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException {
|
89 |
|
90 |
String minPostnr = postnr.replace('x', '0');
|
91 |
String maxPostnr = postnr.replace('x', '9');
|
92 |
|
93 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
|
94 |
|
95 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
|
96 |
"FROM fulddaekning.adressetabel a " +
|
97 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
|
98 |
"WHERE rute IS NULL " + //Ingen dækning
|
99 |
"AND a.postnr BETWEEN ? AND ? " +
|
100 |
"AND latitude IS NOT NULL " +
|
101 |
"AND longitude IS NOT NULL " +
|
102 |
"AND gadeid IS NOT NULL " +
|
103 |
"AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";
|
104 |
PreparedStatement stmt = conn.prepareStatement(sql);
|
105 |
stmt.setString(1, minPostnr);
|
106 |
stmt.setString(2, maxPostnr);
|
107 |
|
108 |
queue.addAll( hentAdresseListe( stmt ) );
|
109 |
return queue;
|
110 |
}
|
111 |
|
112 |
public List<String> hentPostnumre() throws SQLException {
|
113 |
ArrayList<String> list = new ArrayList<String>();
|
114 |
|
115 |
Constants consts = Constants.getInstance();
|
116 |
|
117 |
/*
|
118 |
String sql = "SELECT postnr " +
|
119 |
"FROM fulddaekning.adressetabel " +
|
120 |
"WHERE postnr BETWEEN ? AND ? " +
|
121 |
"AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser
|
122 |
"GROUP BY postnr " +
|
123 |
"ORDER by postnr";
|
124 |
*/
|
125 |
|
126 |
|
127 |
String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +
|
128 |
"FROM fulddaekning.adressetabel " +
|
129 |
"WHERE postnr BETWEEN ? AND ? " +
|
130 |
"AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser
|
131 |
"AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre
|
132 |
"GROUP BY postnr2 " +
|
133 |
"ORDER by postnr2 ";
|
134 |
|
135 |
|
136 |
|
137 |
PreparedStatement stmt = conn.prepareStatement(sql);
|
138 |
//stmt.setString(1, Lookup.distributor );
|
139 |
|
140 |
stmt.setInt(1, consts.getPostnrGroup() );
|
141 |
|
142 |
stmt.setInt(2, consts.getMinPostnr());
|
143 |
stmt.setInt(3, consts.getMaxPostnr());
|
144 |
ResultSet res = stmt.executeQuery();
|
145 |
|
146 |
while (res.next()) {
|
147 |
String postnr = res.getString("postnr2");
|
148 |
list.add(postnr);
|
149 |
}
|
150 |
res.close();
|
151 |
stmt.close();
|
152 |
|
153 |
//list.add(8700);
|
154 |
|
155 |
return list;
|
156 |
}
|
157 |
|
158 |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
|
159 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
|
160 |
"FROM fulddaekning.adressetabel a " +
|
161 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
|
162 |
"WHERE rute IS NOT NULL " +
|
163 |
"AND latitude BETWEEN ? AND ? " +
|
164 |
"AND longitude BETWEEN ? AND ? " +
|
165 |
"AND a.distributor = ? ";
|
166 |
|
167 |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
|
168 |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
|
169 |
//PreparedStatement stmt = conn.prepareStatement(sql);
|
170 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
|
171 |
stmt.setFetchSize(Integer.MIN_VALUE);
|
172 |
|
173 |
stmt.setDouble(1, bbox.latitudeMin);
|
174 |
stmt.setDouble(2, bbox.latitudeMax);
|
175 |
stmt.setDouble(3, bbox.longitudeMin);
|
176 |
stmt.setDouble(4, bbox.longitudeMax);
|
177 |
stmt.setString(5, Lookup.distributor);
|
178 |
|
179 |
List<Adresse> list = hentAdresseListe( stmt );
|
180 |
return list.toArray( new Adresse[ list.size() ] );
|
181 |
}
|
182 |
|
183 |
|
184 |
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
|
185 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
|
186 |
"FROM fulddaekning.adressetabel a " +
|
187 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
|
188 |
"WHERE rute IS NOT NULL " +
|
189 |
"AND latitude IS NOT NULL " +
|
190 |
"AND longitude IS NOT NULL " +
|
191 |
"AND a.distributor = ? ";
|
192 |
|
193 |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
|
194 |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
|
195 |
//PreparedStatement stmt = conn.prepareStatement(sql);
|
196 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
|
197 |
stmt.setFetchSize(Integer.MIN_VALUE);
|
198 |
|
199 |
stmt.setString(1, Lookup.distributor);
|
200 |
|
201 |
List<Adresse> list = hentAdresseListe( stmt );
|
202 |
return list.toArray( new Adresse[ list.size() ] );
|
203 |
}
|
204 |
|
205 |
|
206 |
|
207 |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
|
208 |
/*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+
|
209 |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";
|
210 |
|
211 |
PreparedStatement saveStmt = conn.prepareStatement(sql);*/
|
212 |
|
213 |
saveStmt.setInt(1, orgAdresse.id);
|
214 |
saveStmt.setInt(2, orgAdresse.postnr);
|
215 |
saveStmt.setString(3, orgAdresse.adresse);
|
216 |
saveStmt.setInt(4, orgAdresse.gadeid);
|
217 |
saveStmt.setInt(5, orgAdresse.husnr);
|
218 |
saveStmt.setString(6, orgAdresse.husnrbogstav);
|
219 |
saveStmt.setDouble(7, orgAdresse.latitude);
|
220 |
saveStmt.setDouble(8, orgAdresse.longitude);
|
221 |
saveStmt.setString(9, orgAdresse.rute);
|
222 |
|
223 |
|
224 |
saveStmt.setInt(10, bedsteAdresse.id);
|
225 |
saveStmt.setInt(11, bedsteAdresse.postnr);
|
226 |
saveStmt.setString(12, bedsteAdresse.adresse);
|
227 |
saveStmt.setInt(13, bedsteAdresse.gadeid);
|
228 |
saveStmt.setInt(14, bedsteAdresse.husnr);
|
229 |
saveStmt.setString(15, bedsteAdresse.husnrbogstav);
|
230 |
saveStmt.setDouble(16, bedsteAdresse.latitude);
|
231 |
saveStmt.setDouble(17, bedsteAdresse.longitude);
|
232 |
saveStmt.setString(18, bedsteAdresse.rute);
|
233 |
|
234 |
saveStmt.setDouble(19, bedsteAfstand);
|
235 |
|
236 |
saveStmt.addBatch();
|
237 |
batchCount++;
|
238 |
if (batchCount >= 100) {
|
239 |
saveStmt.executeBatch();
|
240 |
batchCount = 0;
|
241 |
}
|
242 |
//saveStmt.executeUpdate();
|
243 |
//saveStmt.clearParameters();
|
244 |
|
245 |
//saveStmt.close();
|
246 |
}
|
247 |
|
248 |
public synchronized void saveBatch() throws SQLException{
|
249 |
saveStmt.executeBatch();
|
250 |
batchCount = 0;
|
251 |
}
|
252 |
|
253 |
|
254 |
|
255 |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{
|
256 |
ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 );
|
257 |
|
258 |
//logger.info("Starting query");
|
259 |
ResultSet res = stmt.executeQuery();
|
260 |
//logger.info("Starting exec query done");
|
261 |
|
262 |
while (res.next()) {
|
263 |
Adresse adr = new Adresse();
|
264 |
adr.id = res.getInt("id");
|
265 |
adr.postnr = res.getInt("postnr");
|
266 |
adr.adresse = res.getString("adresse");
|
267 |
adr.gadeid = res.getInt("gadeid");
|
268 |
adr.husnr = res.getInt("husnr");
|
269 |
adr.husnrbogstav = res.getString("husnrbogstav");
|
270 |
adr.latitude = res.getDouble("latitude");
|
271 |
adr.longitude = res.getDouble("longitude");
|
272 |
adr.rute = res.getString("rute");
|
273 |
adr.ho = res.getInt("ho");
|
274 |
|
275 |
list.add(adr);
|
276 |
|
277 |
//logger.info( "Adress:" + adr);
|
278 |
}
|
279 |
|
280 |
res.close();
|
281 |
stmt.close();
|
282 |
|
283 |
return list;
|
284 |
}
|
285 |
|
286 |
public Connection getConnection(SafeProperties conf) throws SQLException, IOException {
|
287 |
|
288 |
String db_host = conf.getSafeProperty("DB_HOST");
|
289 |
String db_user = conf.getSafeProperty("DB_USER");
|
290 |
String db_pass = conf.getSafeProperty("DB_PASS");
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
Connection conn = null;
|
296 |
Properties connectionProps = new Properties();
|
297 |
connectionProps.put("user", db_user);
|
298 |
connectionProps.put("password", db_pass);
|
299 |
|
300 |
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true
|
301 |
conn = DriverManager.getConnection(
|
302 |
"jdbc:mysql://" +
|
303 |
db_host +
|
304 |
":3306/?rewriteBatchedStatements=true",
|
305 |
connectionProps);
|
306 |
logger.info("Connected to database");
|
307 |
return conn;
|
308 |
}
|
309 |
|
310 |
}
|