--- android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/06/07 07:07:46 1503 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/db/StationDAO.java 2011/06/08 15:38:11 1504 @@ -13,6 +13,14 @@ public class StationDAO { + private interface StatementParamSetter { + public void setParams(PreparedStatement stmt) throws SQLException ; + } + private class NullSetter implements StatementParamSetter { + @Override + public void setParams(PreparedStatement stmt) throws SQLException {} + } + public static class NostationException extends Exception { private static final long serialVersionUID = 1L; } @@ -50,37 +58,43 @@ } - - public StationEntry getById(int id) throws SQLException,NostationException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + - "FROM trainstations WHERE id=" + id + " AND enabled=true"; - + private StationBean fetchStations(String SQL, StatementParamSetter setter) throws SQLException { + StationBean stations; Connection conn = null; - Statement stmt = null; + PreparedStatement stmt = null; ResultSet res = null; - StationEntry result; - try { conn = DBConnection.getConnection(); - - stmt = conn.createStatement(); - res = stmt.executeQuery(SQL); + stmt = conn.prepareStatement(SQL); - if (res.next()) { - result = convertSingleRow(res); - } else { - throw new NostationException(); - } + setter.setParams(stmt); + + res = stmt.executeQuery(); + stations = convertResultset(res); + } finally { if (res != null) res.close(); if (stmt != null) stmt.close(); - if (conn != null) + if (conn!= null) conn.close(); } + return stations; + + } - return result; + public StationEntry getById(int id) throws SQLException,NostationException { + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog,stationcode_metro,address,0.0 " + + "FROM trainstations WHERE id=" + id + " AND enabled=true"; + + StationBean stations = fetchStations(SQL, new NullSetter() ); + + if (stations.entries.size() > 0 ) { + return stations.entries.get(0); + } else { + throw new NostationException(); + } } public StationBean dumpAll() throws SQLException { @@ -133,36 +147,21 @@ * 'select $2 ilike $1' language sql strict immutable; * create operator ~~~ (procedure = rlike, leftarg = text, rightarg = text, commutator = ~~); */ - public StationBean getByName(String name) throws SQLException { + public StationBean getByName(final 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 ? OR ? ~~~ ANY(aliases)) AND enabled = true " + "ORDER BY name "; - - StationBean result; - Connection conn = null; - PreparedStatement stmt = null; - ResultSet res = null; - 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(); + class NameSetter implements StatementParamSetter { + @Override + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setString(1, name + "%"); + stmt.setString(2, name + "%"); + } } - return result; + + return fetchStations(SQL, new NameSetter() ); } //Latitude (horizonal), longitude(vertical) so @@ -176,7 +175,7 @@ // calculate the distance: // sqrt( power(abs(latitude-?)*111320, 2) + power(abs(longitude-?)*63000,2) )::int as calcdist - 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 { String limitExpression = (geolimit == true) ? "AND abs(latitude-?)<0.4 AND abs(longitude-?)<0.75 " : ""; @@ -187,33 +186,20 @@ "ORDER BY calcdist ASC " + "LIMIT " + LOCATION_LIMIT; - - - StationBean result; - Connection conn = null; - PreparedStatement stmt = null; - ResultSet res = null; - try { - conn = DBConnection.getConnection(); - stmt = conn.prepareStatement(SQL); - stmt.setDouble(1, latitude); - stmt.setDouble(2, longitude); - 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(); + class LatlongSetter implements StatementParamSetter { + @Override + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setDouble(1, latitude); + stmt.setDouble(2, longitude); + if (geolimit == true) { + stmt.setDouble(3, latitude); + stmt.setDouble(4, longitude); + } + } } - return result; + + return fetchStations(SQL, new LatlongSetter() ); } public StationBean getByLocation(double latitude, double longitude) throws SQLException { @@ -235,29 +221,34 @@ "FROM trainstations " + "WHERE id IN " + list + " AND enabled = true " + "ORDER BY name "; + + return fetchStations(SQL, new NullSetter() ); + } + - Connection conn = null; - Statement stmt = null; - ResultSet res = null; - StationBean result; + + public StationEntry getSimpleByName(final String name) throws SQLException { + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + + "FROM trainstations " + + "WHERE name = ? AND enabled = true " + + "LIMIT 1 "; - try { - conn = DBConnection.getConnection(); - 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(); + class NameSetter implements StatementParamSetter { + @Override + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setString(1, name ); + } + } + + StationBean stations = fetchStations(SQL, new NameSetter() ); + + if (stations.entries.size() == 1) { + return stations.entries.get(0); + } else { + return null; } - - return result; - } + @Deprecated public static String getStationName(int stationID) { String station = ""; @@ -282,72 +273,26 @@ return station; } - public StationEntry getSimpleByName(String name) throws SQLException { - String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + - "FROM trainstations " + - "WHERE name = ? AND enabled = true " + - "LIMIT 1 "; - - StationBean result; - Connection conn = null; - PreparedStatement stmt = null; - ResultSet res = null; - 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(); - } - - if (result.entries.size() == 1) { - return result.entries.get(0); - } else { - return null; - } - } - + + @Deprecated - public int getIdByName(String name) throws SQLException { + public int getIdByName(final String name) throws SQLException { String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address, 0.0 " + "FROM trainstations " + "WHERE name = ? AND enabled = true " + "LIMIT 1 "; - StationBean result; - Connection conn = null; - PreparedStatement stmt = null; - ResultSet res = null; - 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(); + class NameSetter implements StatementParamSetter { + @Override + public void setParams(PreparedStatement stmt) throws SQLException { + stmt.setString(1, name ); + } } - - if (result.entries.size() == 1) { - return result.entries.get(0).getId(); + + StationBean stations = fetchStations(SQL, new NameSetter() ); + + if (stations.entries.size() == 1) { + return stations.entries.get(0).getId(); } else { return -1; }