--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/02/08 19:12:15 588 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/05/18 17:56:43 733 @@ -40,16 +40,22 @@ "FROM trainstations WHERE id=" + id + " AND enabled=true"; Connection conn = null; + Statement stmt = null; + ResultSet res = null; StationBean result; try { conn = DBConnection.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet res = stmt.executeQuery(SQL); + stmt = conn.createStatement(); + res = stmt.executeQuery(SQL); res.next(); result = convertSingleRow(res); } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); if (conn != null) conn.close(); } @@ -57,27 +63,39 @@ 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 "; List result; Connection conn = null; + PreparedStatement stmt = null; + ResultSet res = null; try { conn = DBConnection.getConnection(); - PreparedStatement stmt = conn.prepareStatement(SQL); + stmt = conn.prepareStatement(SQL); stmt.setString(1, name + "%"); + stmt.setString(2, name + "%"); - ResultSet rs = stmt.executeQuery(); - result = convertResultset(rs); + res = stmt.executeQuery(); + result = convertResultset(res); } finally { - if (conn != null) + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn!= null) conn.close(); } return result; @@ -86,7 +104,7 @@ 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 " + + " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + " FROM trainstations " + " WHERE enabled = true " + " ) AS trainstations2 " + @@ -94,16 +112,22 @@ "LIMIT 4 "; List result; Connection conn = null; + PreparedStatement stmt = null; + ResultSet res = null; try { conn = DBConnection.getConnection(); - PreparedStatement stmt = conn.prepareStatement(SQL); + stmt = conn.prepareStatement(SQL); stmt.setDouble(1, latitude); stmt.setDouble(2, longitude); - ResultSet rs = stmt.executeQuery(); - result = convertResultset(rs); + res = stmt.executeQuery(); + result = convertResultset(res); } finally { - if (conn != null) + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); + if (conn!= null) conn.close(); } return result; @@ -116,14 +140,20 @@ "ORDER BY name "; Connection conn = null; + Statement stmt = null; + ResultSet res = null; List result; try { conn = DBConnection.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet res = stmt.executeQuery(SQL); + stmt = conn.createStatement(); + res = stmt.executeQuery(SQL); result = convertResultset(res); } finally { + if (res != null) + res.close(); + if (stmt != null) + stmt.close(); if (conn!= null) conn.close(); } @@ -131,5 +161,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; + } }