--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/02/08 19:25:12 589 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/05/19 07:40:00 737 @@ -63,11 +63,16 @@ return result; } - + /* + * 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) ) + * create function rlike(text,text) returns bool as + * 'select $2 ilike $1' language sql strict immutable; + * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); + */ public List getByName(String name) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + "FROM trainstations " + - "WHERE name ILIKE ? AND enabled = true " + + "WHERE (name ILIKE ? OR ? ~~~ ANY(aliases)) AND enabled = true " + "ORDER BY name "; @@ -80,6 +85,7 @@ stmt = conn.prepareStatement(SQL); stmt.setString(1, name + "%"); + stmt.setString(2, name + "%"); res = stmt.executeQuery(); result = convertResultset(res); @@ -95,15 +101,14 @@ return result; } + //the "hack" with max 2.5 degrees latitude and longitude is only valid since we only service danish trains public List getByLocation(double latitude, double longitude) throws SQLException { - String SQL = "SELECT * FROM ( "+ - " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + - " earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " + - " FROM trainstations " + - " WHERE enabled = true " + - " ) AS trainstations2 " + - "ORDER BY calcdist ASC " + - "LIMIT 4 "; + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + + " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + + "FROM trainstations " + + "WHERE enabled = true AND abs(latitude-?)<2.5 AND abs (longitude-?)<2.5 " + + "ORDER BY calcdist ASC " + + "LIMIT 4 "; List result; Connection conn = null; PreparedStatement stmt = null; @@ -113,6 +118,8 @@ stmt = conn.prepareStatement(SQL); stmt.setDouble(1, latitude); stmt.setDouble(2, longitude); + stmt.setDouble(3, latitude); + stmt.setDouble(4, longitude); res = stmt.executeQuery(); result = convertResultset(res); @@ -155,5 +162,26 @@ return result; } - + public static String getStationName(int stationID) { + String station = ""; + + Connection conn = null; + try { + conn = DBConnection.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); + if (rs.next()) { + station = rs.getString(1); + } + + } catch (Exception e) { + } finally { + try { + if (conn != null && !conn.isClosed()) + conn.close(); + } catch (Exception e) {} + } + + return station; + } }