/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 737 - (show annotations) (download)
Wed May 19 07:40:00 2010 UTC (13 years, 11 months ago) by torben
File size: 5277 byte(s)
limit the number of distance calculations by only calculating on those stations within 2.5 degrees (lat/long)-range 
on current server this reduces query time by ~45 ms
1 package dk.thoerup.traininfoservice;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 public class StationDAO {
12 private StationBean convertSingleRow(ResultSet res) throws SQLException {
13 StationBean station = new StationBean();
14
15 station.setId( res.getInt(1) );
16 station.setName( res.getString(2) );
17 station.setLatitude( res.getDouble(3) );
18 station.setLongitude( res.getDouble(4) );
19 station.setRegional( res.getString(5) );
20 station.setStrain( res.getString(6) );
21 station.setMetro( res.getString(7) );
22 station.setAddress( res.getString(8) );
23 station.setCalcdist( (int)res.getDouble(9) );
24
25 return station;
26 }
27
28 private List<StationBean> convertResultset(ResultSet res) throws SQLException {
29 List<StationBean> stations = new ArrayList<StationBean>();
30 while (res.next()) {
31 stations.add( convertSingleRow(res) );
32 }
33 return stations;
34
35 }
36
37
38 public StationBean getById(int id) throws SQLException {
39 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " +
40 "FROM trainstations WHERE id=" + id + " AND enabled=true";
41
42 Connection conn = null;
43 Statement stmt = null;
44 ResultSet res = null;
45 StationBean result;
46
47 try {
48 conn = DBConnection.getConnection();
49
50 stmt = conn.createStatement();
51 res = stmt.executeQuery(SQL);
52 res.next();
53 result = convertSingleRow(res);
54 } finally {
55 if (res != null)
56 res.close();
57 if (stmt != null)
58 stmt.close();
59 if (conn != null)
60 conn.close();
61 }
62
63 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 {
73 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " +
74 "FROM trainstations " +
75 "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " +
76 "ORDER BY name ";
77
78
79 List<StationBean> result;
80 Connection conn = null;
81 PreparedStatement stmt = null;
82 ResultSet res = null;
83 try {
84 conn = DBConnection.getConnection();
85 stmt = conn.prepareStatement(SQL);
86
87 stmt.setString(1, name + "%");
88 stmt.setString(2, name + "%");
89
90 res = stmt.executeQuery();
91 result = convertResultset(res);
92
93 } finally {
94 if (res != null)
95 res.close();
96 if (stmt != null)
97 stmt.close();
98 if (conn!= null)
99 conn.close();
100 }
101 return result;
102 }
103
104 //the "hack" with max 2.5 degrees latitude and longitude is only valid since we only service danish trains
105 public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
106 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
107 " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
108 "FROM trainstations " +
109 "WHERE enabled = true AND abs(latitude-?)<2.5 AND abs (longitude-?)<2.5 " +
110 "ORDER BY calcdist ASC " +
111 "LIMIT 4 ";
112 List<StationBean> result;
113 Connection conn = null;
114 PreparedStatement stmt = null;
115 ResultSet res = null;
116 try {
117 conn = DBConnection.getConnection();
118 stmt = conn.prepareStatement(SQL);
119 stmt.setDouble(1, latitude);
120 stmt.setDouble(2, longitude);
121 stmt.setDouble(3, latitude);
122 stmt.setDouble(4, longitude);
123 res = stmt.executeQuery();
124 result = convertResultset(res);
125
126 } finally {
127 if (res != null)
128 res.close();
129 if (stmt != null)
130 stmt.close();
131 if (conn!= null)
132 conn.close();
133 }
134 return result;
135 }
136
137 public List<StationBean> getByList(String list) throws SQLException {
138 String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro, address,0.0 " +
139 "FROM trainstations " +
140 "WHERE id IN " + list + " AND enabled = true " +
141 "ORDER BY name ";
142
143 Connection conn = null;
144 Statement stmt = null;
145 ResultSet res = null;
146 List<StationBean> result;
147
148 try {
149 conn = DBConnection.getConnection();
150 stmt = conn.createStatement();
151 res = stmt.executeQuery(SQL);
152 result = convertResultset(res);
153 } finally {
154 if (res != null)
155 res.close();
156 if (stmt != null)
157 stmt.close();
158 if (conn!= null)
159 conn.close();
160 }
161
162 return result;
163
164 }
165 public static String getStationName(int stationID) {
166 String station = "";
167
168 Connection conn = null;
169 try {
170 conn = DBConnection.getConnection();
171 Statement stmt = conn.createStatement();
172 ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
173 if (rs.next()) {
174 station = rs.getString(1);
175 }
176
177 } catch (Exception e) {
178 } finally {
179 try {
180 if (conn != null && !conn.isClosed())
181 conn.close();
182 } catch (Exception e) {}
183 }
184
185 return station;
186 }
187 }

  ViewVC Help
Powered by ViewVC 1.1.20