40 |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
"FROM trainstations WHERE id=" + id + " AND enabled=true"; |
41 |
|
|
42 |
Connection conn = null; |
Connection conn = null; |
43 |
|
Statement stmt = null; |
44 |
|
ResultSet res = null; |
45 |
StationBean result; |
StationBean result; |
46 |
|
|
47 |
try { |
try { |
48 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
49 |
|
|
50 |
Statement stmt = conn.createStatement(); |
stmt = conn.createStatement(); |
51 |
ResultSet res = stmt.executeQuery(SQL); |
res = stmt.executeQuery(SQL); |
52 |
res.next(); |
res.next(); |
53 |
result = convertSingleRow(res); |
result = convertSingleRow(res); |
54 |
} finally { |
} finally { |
55 |
|
if (res != null) |
56 |
|
res.close(); |
57 |
|
if (stmt != null) |
58 |
|
stmt.close(); |
59 |
if (conn != null) |
if (conn != null) |
60 |
conn.close(); |
conn.close(); |
61 |
} |
} |
63 |
return result; |
return result; |
64 |
} |
} |
65 |
|
|
66 |
|
/* |
67 |
|
* this code requires theses statements are run on database in order to do ILIKE searches against aliases (which is defines as array of varchar(64) ) |
68 |
|
* create function rlike(text,text) returns bool as |
69 |
|
* 'select $2 ilike $1' language sql strict immutable; |
70 |
|
* create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); |
71 |
|
*/ |
72 |
public List<StationBean> getByName(String name) throws SQLException { |
public List<StationBean> getByName(String name) throws SQLException { |
73 |
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 " + |
74 |
"FROM trainstations " + |
"FROM trainstations " + |
75 |
"WHERE name ILIKE ? AND enabled = true " + |
"WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + |
76 |
"ORDER BY name "; |
"ORDER BY name "; |
77 |
|
|
78 |
|
|
79 |
List<StationBean> result; |
List<StationBean> result; |
80 |
Connection conn = null; |
Connection conn = null; |
81 |
|
PreparedStatement stmt = null; |
82 |
|
ResultSet res = null; |
83 |
try { |
try { |
84 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
85 |
PreparedStatement stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
86 |
|
|
87 |
stmt.setString(1, name + "%"); |
stmt.setString(1, name + "%"); |
88 |
|
stmt.setString(2, name + "%"); |
89 |
|
|
90 |
ResultSet rs = stmt.executeQuery(); |
res = stmt.executeQuery(); |
91 |
result = convertResultset(rs); |
result = convertResultset(res); |
92 |
|
|
93 |
} finally { |
} finally { |
94 |
if (conn != null) |
if (res != null) |
95 |
|
res.close(); |
96 |
|
if (stmt != null) |
97 |
|
stmt.close(); |
98 |
|
if (conn!= null) |
99 |
conn.close(); |
conn.close(); |
100 |
} |
} |
101 |
return result; |
return result; |
102 |
} |
} |
103 |
|
|
104 |
|
//the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains |
105 |
|
// in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km |
106 |
|
|
107 |
|
// the ultra fast method (and only slightly inaccurate as long as we only cover a limited geographically area) |
108 |
|
// is using an aproximation of the length of 1 latitude degree and 1 longitude degree and just use pythagoras to |
109 |
|
// calculate the distance: |
110 |
|
// sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist |
111 |
|
|
112 |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException { |
113 |
String SQL = "SELECT * FROM ( "+ |
String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
114 |
" SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + |
" earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + |
115 |
" earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + |
"FROM trainstations " + |
116 |
" FROM trainstations " + |
"WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + |
117 |
" WHERE enabled = true " + |
"ORDER BY calcdist ASC " + |
118 |
" ) AS trainstations2 " + |
"LIMIT 4 "; |
|
"ORDER BY calcdist ASC " + |
|
|
"LIMIT 4 "; |
|
119 |
List<StationBean> result; |
List<StationBean> result; |
120 |
Connection conn = null; |
Connection conn = null; |
121 |
|
PreparedStatement stmt = null; |
122 |
|
ResultSet res = null; |
123 |
try { |
try { |
124 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
125 |
PreparedStatement stmt = conn.prepareStatement(SQL); |
stmt = conn.prepareStatement(SQL); |
126 |
stmt.setDouble(1, latitude); |
stmt.setDouble(1, latitude); |
127 |
stmt.setDouble(2, longitude); |
stmt.setDouble(2, longitude); |
128 |
ResultSet rs = stmt.executeQuery(); |
stmt.setDouble(3, latitude); |
129 |
result = convertResultset(rs); |
stmt.setDouble(4, longitude); |
130 |
|
res = stmt.executeQuery(); |
131 |
|
result = convertResultset(res); |
132 |
|
|
133 |
} finally { |
} finally { |
134 |
if (conn != null) |
if (res != null) |
135 |
|
res.close(); |
136 |
|
if (stmt != null) |
137 |
|
stmt.close(); |
138 |
|
if (conn!= null) |
139 |
conn.close(); |
conn.close(); |
140 |
} |
} |
141 |
return result; |
return result; |
148 |
"ORDER BY name "; |
"ORDER BY name "; |
149 |
|
|
150 |
Connection conn = null; |
Connection conn = null; |
151 |
|
Statement stmt = null; |
152 |
|
ResultSet res = null; |
153 |
List<StationBean> result; |
List<StationBean> result; |
154 |
|
|
155 |
try { |
try { |
156 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
157 |
Statement stmt = conn.createStatement(); |
stmt = conn.createStatement(); |
158 |
ResultSet res = stmt.executeQuery(SQL); |
res = stmt.executeQuery(SQL); |
159 |
result = convertResultset(res); |
result = convertResultset(res); |
160 |
} finally { |
} finally { |
161 |
|
if (res != null) |
162 |
|
res.close(); |
163 |
|
if (stmt != null) |
164 |
|
stmt.close(); |
165 |
if (conn!= null) |
if (conn!= null) |
166 |
conn.close(); |
conn.close(); |
167 |
} |
} |
169 |
return result; |
return result; |
170 |
|
|
171 |
} |
} |
172 |
|
public static String getStationName(int stationID) { |
173 |
|
String station = ""; |
174 |
|
|
175 |
|
Connection conn = null; |
176 |
|
try { |
177 |
|
conn = DBConnection.getConnection(); |
178 |
|
Statement stmt = conn.createStatement(); |
179 |
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
180 |
|
if (rs.next()) { |
181 |
|
station = rs.getString(1); |
182 |
|
} |
183 |
|
|
184 |
|
} catch (Exception e) { |
185 |
|
} finally { |
186 |
|
try { |
187 |
|
if (conn != null && !conn.isClosed()) |
188 |
|
conn.close(); |
189 |
|
} catch (Exception e) {} |
190 |
|
} |
191 |
|
|
192 |
|
return station; |
193 |
|
} |
194 |
} |
} |