/[projects]/dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java
ViewVC logotype

Contents of /dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20