1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice.db; |
2 |
|
|
3 |
import java.sql.Array; |
import java.sql.Array; |
4 |
import java.sql.Connection; |
import java.sql.Connection; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
11 |
import dk.thoerup.android.traininfo.common.StationBean; |
import dk.thoerup.android.traininfo.common.StationBean; |
12 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
13 |
|
|
14 |
public class StationDAO { |
public class StationDAO { |
15 |
|
|
16 |
|
private interface StatementParamSetter { |
17 |
|
public void setParams(PreparedStatement stmt) throws SQLException ; |
18 |
|
} |
19 |
|
private class NullSetter implements StatementParamSetter { |
20 |
|
@Override |
21 |
|
public void setParams(PreparedStatement stmt) throws SQLException {} |
22 |
|
} |
23 |
|
|
24 |
public static class NostationException extends Exception { |
public static class NostationException extends Exception { |
25 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
26 |
} |
} |
58 |
|
|
59 |
} |
} |
60 |
|
|
61 |
|
private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException { |
62 |
public StationEntry getById(int id) throws SQLException,NostationException { |
StationBean stations; |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + |
|
|
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
|
|
|
|
63 |
Connection conn = null; |
Connection conn = null; |
64 |
Statement stmt = null; |
PreparedStatement stmt = null; |
65 |
ResultSet res = null; |
ResultSet res = null; |
|
StationEntry result; |
|
|
|
|
66 |
try { |
try { |
67 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
68 |
|
stmt = conn.prepareStatement(SQL); |
|
stmt = conn.createStatement(); |
|
|
res = stmt.executeQuery(SQL); |
|
69 |
|
|
70 |
if (res.next()) { |
setter.setParams(stmt); |
71 |
result = convertSingleRow(res); |
|
72 |
} else { |
res = stmt.executeQuery(); |
73 |
throw new NostationException(); |
stations = convertResultset(res); |
74 |
} |
|
75 |
} finally { |
} finally { |
76 |
if (res != null) |
if (res != null) |
77 |
res.close(); |
res.close(); |
78 |
if (stmt != null) |
if (stmt != null) |
79 |
stmt.close(); |
stmt.close(); |
80 |
if (conn != null) |
if (conn!= null) |
81 |
conn.close(); |
conn.close(); |
82 |
} |
} |
83 |
|
return stations; |
84 |
|
|
85 |
|
} |
86 |
|
|
87 |
return result; |
public StationEntry getById(int id) throws SQLException,NostationException { |
88 |
|
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + |
89 |
|
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
90 |
|
|
91 |
|
StationBean stations = fetchStations(SQL, new NullSetter() ); |
92 |
|
|
93 |
|
if (stations.entries.size() > 0 ) { |
94 |
|
return stations.entries.get(0); |
95 |
|
} else { |
96 |
|
throw new NostationException(); |
97 |
|
} |
98 |
} |
} |
99 |
|
|
100 |
public StationBean dumpAll() throws SQLException { |
public StationBean dumpAll() throws SQLException { |
147 |
* 'select $2 ilike $1' language sql strict immutable; |
* 'select $2 ilike $1' language sql strict immutable; |
148 |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
149 |
*/ |
*/ |
150 |
public StationBean getByName(String name) throws SQLException { |
public StationBean getByName(final String name) throws SQLException { |
151 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
152 |
"FROM trainstations " + |
"FROM trainstations " + |
153 |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
154 |
"ORDER BY name "; |
"ORDER BY name "; |
155 |
|
|
156 |
|
class NameSetter implements StatementParamSetter { |
157 |
StationBean result; |
@Override |
158 |
Connection conn = null; |
public void setParams(PreparedStatement stmt) throws SQLException { |
159 |
PreparedStatement stmt = null; |
stmt.setString(1, name + "%"); |
160 |
ResultSet res = null; |
stmt.setString(2, name + "%"); |
161 |
try { |
} |
|
conn = DBConnection.getConnection(); |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
|
|
|
stmt.setString(1, name + "%"); |
|
|
stmt.setString(2, name + "%"); |
|
|
|
|
|
res = stmt.executeQuery(); |
|
|
result = convertResultset(res); |
|
|
|
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
162 |
} |
} |
163 |
return result; |
|
164 |
|
return fetchStations(SQL, new NameSetter() ); |
165 |
} |
} |
166 |
|
|
167 |
//Latitude (horizonal), longitude(vertical) so |
//Latitude (horizonal), longitude(vertical) so |
175 |
// calculate the distance: |
// calculate the distance: |
176 |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
177 |
|
|
178 |
public StationBean getByLocationWorker(double latitude, double longitude, boolean geolimit) throws SQLException { |
public StationBean getByLocationWorker(final double latitude, final double longitude, final boolean geolimit) throws SQLException { |
179 |
|
|
180 |
String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; |
181 |
|
|
186 |
"ORDER BY calcdist ASC " + |
"ORDER BY calcdist ASC " + |
187 |
"LIMIT " + LOCATION_LIMIT; |
"LIMIT " + LOCATION_LIMIT; |
188 |
|
|
|
|
|
189 |
|
|
190 |
StationBean result; |
class LatlongSetter implements StatementParamSetter { |
191 |
Connection conn = null; |
@Override |
192 |
PreparedStatement stmt = null; |
public void setParams(PreparedStatement stmt) throws SQLException { |
193 |
ResultSet res = null; |
stmt.setDouble(1, latitude); |
194 |
try { |
stmt.setDouble(2, longitude); |
195 |
conn = DBConnection.getConnection(); |
if (geolimit == true) { |
196 |
stmt = conn.prepareStatement(SQL); |
stmt.setDouble(3, latitude); |
197 |
stmt.setDouble(1, latitude); |
stmt.setDouble(4, longitude); |
198 |
stmt.setDouble(2, longitude); |
} |
199 |
if (geolimit == true) { |
} |
|
stmt.setDouble(3, latitude); |
|
|
stmt.setDouble(4, longitude); |
|
|
} |
|
|
res = stmt.executeQuery(); |
|
|
result = convertResultset(res); |
|
|
|
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
200 |
} |
} |
201 |
return result; |
|
202 |
|
return fetchStations(SQL, new LatlongSetter() ); |
203 |
} |
} |
204 |
|
|
205 |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
public StationBean getByLocation(double latitude, double longitude) throws SQLException { |
221 |
"FROM trainstations " + |
"FROM trainstations " + |
222 |
"WHERE id IN " + list + " AND enabled = true " + |
"WHERE id IN " + list + " AND enabled = true " + |
223 |
"ORDER BY name "; |
"ORDER BY name "; |
224 |
|
|
225 |
|
return fetchStations(SQL, new NullSetter() ); |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
Connection conn = null; |
|
230 |
Statement stmt = null; |
public StationEntry getSimpleByName(final String name) throws SQLException { |
231 |
ResultSet res = null; |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
232 |
StationBean result; |
"FROM trainstations " + |
233 |
|
"WHERE name = ? AND enabled = true " + |
234 |
|
"LIMIT 1 "; |
235 |
|
|
236 |
try { |
class NameSetter implements StatementParamSetter { |
237 |
conn = DBConnection.getConnection(); |
@Override |
238 |
stmt = conn.createStatement(); |
public void setParams(PreparedStatement stmt) throws SQLException { |
239 |
res = stmt.executeQuery(SQL); |
stmt.setString(1, name ); |
240 |
result = convertResultset(res); |
} |
241 |
} finally { |
} |
242 |
if (res != null) |
|
243 |
res.close(); |
StationBean stations = fetchStations(SQL, new NameSetter() ); |
244 |
if (stmt != null) |
|
245 |
stmt.close(); |
if (stations.entries.size() == 1) { |
246 |
if (conn!= null) |
return stations.entries.get(0); |
247 |
conn.close(); |
} else { |
248 |
|
return null; |
249 |
} |
} |
|
|
|
|
return result; |
|
|
|
|
250 |
} |
} |
251 |
|
|
252 |
|
@Deprecated |
253 |
public static String getStationName(int stationID) { |
public static String getStationName(int stationID) { |
254 |
String station = ""; |
String station = ""; |
255 |
|
|
272 |
|
|
273 |
return station; |
return station; |
274 |
} |
} |
275 |
|
|
276 |
public int getIdByName(String name) throws SQLException { |
|
277 |
|
|
278 |
|
@Deprecated |
279 |
|
public int getIdByName(final String name) throws SQLException { |
280 |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + |
281 |
"FROM trainstations " + |
"FROM trainstations " + |
282 |
"WHERE name = ? AND enabled = true " + |
"WHERE name = ? AND enabled = true " + |
283 |
"LIMIT 1 "; |
"LIMIT 1 "; |
284 |
|
|
285 |
StationBean result; |
class NameSetter implements StatementParamSetter { |
286 |
Connection conn = null; |
@Override |
287 |
PreparedStatement stmt = null; |
public void setParams(PreparedStatement stmt) throws SQLException { |
288 |
ResultSet res = null; |
stmt.setString(1, name ); |
289 |
try { |
} |
|
conn = DBConnection.getConnection(); |
|
|
stmt = conn.prepareStatement(SQL); |
|
|
|
|
|
stmt.setString(1, name ); |
|
|
|
|
|
res = stmt.executeQuery(); |
|
|
result = convertResultset(res); |
|
|
|
|
|
} finally { |
|
|
if (res != null) |
|
|
res.close(); |
|
|
if (stmt != null) |
|
|
stmt.close(); |
|
|
if (conn!= null) |
|
|
conn.close(); |
|
290 |
} |
} |
291 |
|
|
292 |
if (result.entries.size() == 1) { |
StationBean stations = fetchStations(SQL, new NameSetter() ); |
293 |
return result.entries.get(0).getId(); |
|
294 |
|
if (stations.entries.size() == 1) { |
295 |
|
return stations.entries.get(0).getId(); |
296 |
} else { |
} else { |
297 |
return -1; |
return -1; |
298 |
} |
} |