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(int postnr) throws SQLException {
|
62 |
|
63 |
String sql =
|
64 |
"SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin " +
|
65 |
"FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;";
|
66 |
|
67 |
PreparedStatement stmt = conn.prepareStatement(sql);
|
68 |
stmt.setInt(1, postnr);
|
69 |
|
70 |
ResultSet res = stmt.executeQuery();
|
71 |
res.next(); //query returnerer altid 1 række
|
72 |
|
73 |
BoundingBox bbox = new BoundingBox();
|
74 |
bbox.latitudeMax = res.getDouble("latmax");
|
75 |
bbox.latitudeMin = res.getDouble("latmin");
|
76 |
bbox.longitudeMax = res.getDouble("lngmax");
|
77 |
bbox.longitudeMin = res.getDouble("lngmin");
|
78 |
|
79 |
res.close();
|
80 |
stmt.close();
|
81 |
|
82 |
return bbox;
|
83 |
}
|
84 |
|
85 |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException {
|
86 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
|
87 |
|
88 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
|
89 |
"FROM fulddaekning.adressetabel a " +
|
90 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
|
91 |
"WHERE rute IS NULL " + //Ingen dækning
|
92 |
"AND a.postnr=? " +
|
93 |
"AND latitude IS NOT NULL " +
|
94 |
"AND longitude IS NOT NULL " +
|
95 |
"AND gadeid IS NOT NULL ";
|
96 |
PreparedStatement stmt = conn.prepareStatement(sql);
|
97 |
stmt.setInt(1, postnr);
|
98 |
|
99 |
queue.addAll( hentAdresseListe( stmt ) );
|
100 |
return queue;
|
101 |
}
|
102 |
|
103 |
public List<Integer> hentPostnumre() throws SQLException {
|
104 |
ArrayList<Integer> list = new ArrayList<Integer>();
|
105 |
|
106 |
Constants consts = Constants.getInstance();
|
107 |
|
108 |
|
109 |
String sql = "SELECT postnr " +
|
110 |
"FROM fulddaekning.adressetabel " +
|
111 |
//"WHERE distributor = ? and rute is not null " +
|
112 |
"WHERE postnr BETWEEN ? AND ? " +
|
113 |
"GROUP BY postnr " +
|
114 |
"ORDER by postnr";
|
115 |
PreparedStatement stmt = conn.prepareStatement(sql);
|
116 |
//stmt.setString(1, Lookup.distributor );
|
117 |
stmt.setInt(1, consts.getMinPostnr());
|
118 |
stmt.setInt(2, consts.getMaxPostnr());
|
119 |
ResultSet res = stmt.executeQuery();
|
120 |
|
121 |
while (res.next()) {
|
122 |
int postnr = res.getInt("postnr");
|
123 |
list.add(postnr);
|
124 |
}
|
125 |
res.close();
|
126 |
stmt.close();
|
127 |
|
128 |
//list.add(8700);
|
129 |
|
130 |
return list;
|
131 |
}
|
132 |
|
133 |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
|
134 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
|
135 |
"FROM fulddaekning.adressetabel a " +
|
136 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
|
137 |
"WHERE rute IS NOT NULL " +
|
138 |
"AND latitude BETWEEN ? AND ? " +
|
139 |
"AND longitude BETWEEN ? AND ? " +
|
140 |
"AND a.distributor = ? ";
|
141 |
|
142 |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
|
143 |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
|
144 |
//PreparedStatement stmt = conn.prepareStatement(sql);
|
145 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
|
146 |
stmt.setFetchSize(Integer.MIN_VALUE);
|
147 |
|
148 |
stmt.setDouble(1, bbox.latitudeMin);
|
149 |
stmt.setDouble(2, bbox.latitudeMax);
|
150 |
stmt.setDouble(3, bbox.longitudeMin);
|
151 |
stmt.setDouble(4, bbox.longitudeMax);
|
152 |
stmt.setString(5, Lookup.distributor);
|
153 |
|
154 |
List<Adresse> list = hentAdresseListe( stmt );
|
155 |
return list.toArray( new Adresse[ list.size() ] );
|
156 |
}
|
157 |
|
158 |
|
159 |
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
|
160 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
|
161 |
"FROM fulddaekning.adressetabel a " +
|
162 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
|
163 |
"WHERE rute IS NOT NULL " +
|
164 |
"AND latitude IS NOT NULL " +
|
165 |
"AND longitude IS NOT NULL " +
|
166 |
"AND a.distributor = ? ";
|
167 |
|
168 |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
|
169 |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
|
170 |
//PreparedStatement stmt = conn.prepareStatement(sql);
|
171 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
|
172 |
stmt.setFetchSize(Integer.MIN_VALUE);
|
173 |
|
174 |
stmt.setString(1, Lookup.distributor);
|
175 |
|
176 |
List<Adresse> list = hentAdresseListe( stmt );
|
177 |
return list.toArray( new Adresse[ list.size() ] );
|
178 |
}
|
179 |
|
180 |
|
181 |
|
182 |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
|
183 |
/*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`) "+
|
184 |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";
|
185 |
|
186 |
PreparedStatement saveStmt = conn.prepareStatement(sql);*/
|
187 |
|
188 |
saveStmt.setInt(1, orgAdresse.id);
|
189 |
saveStmt.setInt(2, orgAdresse.postnr);
|
190 |
saveStmt.setString(3, orgAdresse.adresse);
|
191 |
saveStmt.setInt(4, orgAdresse.gadeid);
|
192 |
saveStmt.setInt(5, orgAdresse.husnr);
|
193 |
saveStmt.setString(6, orgAdresse.husnrbogstav);
|
194 |
saveStmt.setDouble(7, orgAdresse.latitude);
|
195 |
saveStmt.setDouble(8, orgAdresse.longitude);
|
196 |
saveStmt.setString(9, orgAdresse.rute);
|
197 |
|
198 |
|
199 |
saveStmt.setInt(10, bedsteAdresse.id);
|
200 |
saveStmt.setInt(11, bedsteAdresse.postnr);
|
201 |
saveStmt.setString(12, bedsteAdresse.adresse);
|
202 |
saveStmt.setInt(13, bedsteAdresse.gadeid);
|
203 |
saveStmt.setInt(14, bedsteAdresse.husnr);
|
204 |
saveStmt.setString(15, bedsteAdresse.husnrbogstav);
|
205 |
saveStmt.setDouble(16, bedsteAdresse.latitude);
|
206 |
saveStmt.setDouble(17, bedsteAdresse.longitude);
|
207 |
saveStmt.setString(18, bedsteAdresse.rute);
|
208 |
|
209 |
saveStmt.setDouble(19, bedsteAfstand);
|
210 |
|
211 |
saveStmt.addBatch();
|
212 |
batchCount++;
|
213 |
if (batchCount >= 100) {
|
214 |
saveStmt.executeBatch();
|
215 |
batchCount = 0;
|
216 |
}
|
217 |
//saveStmt.executeUpdate();
|
218 |
//saveStmt.clearParameters();
|
219 |
|
220 |
//saveStmt.close();
|
221 |
}
|
222 |
|
223 |
public synchronized void saveBatch() throws SQLException{
|
224 |
saveStmt.executeBatch();
|
225 |
batchCount = 0;
|
226 |
}
|
227 |
|
228 |
|
229 |
|
230 |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{
|
231 |
ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 );
|
232 |
|
233 |
//logger.info("Starting query");
|
234 |
ResultSet res = stmt.executeQuery();
|
235 |
//logger.info("Starting exec query done");
|
236 |
|
237 |
while (res.next()) {
|
238 |
Adresse adr = new Adresse();
|
239 |
adr.id = res.getInt("id");
|
240 |
adr.postnr = res.getInt("postnr");
|
241 |
adr.adresse = res.getString("adresse");
|
242 |
adr.gadeid = res.getInt("gadeid");
|
243 |
adr.husnr = res.getInt("husnr");
|
244 |
adr.husnrbogstav = res.getString("husnrbogstav");
|
245 |
adr.latitude = res.getDouble("latitude");
|
246 |
adr.longitude = res.getDouble("longitude");
|
247 |
adr.rute = res.getString("rute");
|
248 |
adr.ho = res.getInt("ho");
|
249 |
|
250 |
list.add(adr);
|
251 |
|
252 |
//logger.info( "Adress:" + adr);
|
253 |
}
|
254 |
|
255 |
res.close();
|
256 |
stmt.close();
|
257 |
|
258 |
return list;
|
259 |
}
|
260 |
|
261 |
public Connection getConnection(SafeProperties conf) throws SQLException, IOException {
|
262 |
|
263 |
String db_host = conf.getSafeProperty("DB_HOST");
|
264 |
String db_user = conf.getSafeProperty("DB_USER");
|
265 |
String db_pass = conf.getSafeProperty("DB_PASS");
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
Connection conn = null;
|
271 |
Properties connectionProps = new Properties();
|
272 |
connectionProps.put("user", db_user);
|
273 |
connectionProps.put("password", db_pass);
|
274 |
|
275 |
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true
|
276 |
conn = DriverManager.getConnection(
|
277 |
"jdbc:mysql://" +
|
278 |
db_host +
|
279 |
":3306/?rewriteBatchedStatements=true",
|
280 |
connectionProps);
|
281 |
logger.info("Connected to database");
|
282 |
return conn;
|
283 |
}
|
284 |
|
285 |
}
|