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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 588 by torben, Mon Feb 8 19:12:15 2010 UTC revision 739 by torben, Wed May 19 09:53:25 2010 UTC
# Line 40  public class StationDAO { Line 40  public class StationDAO {
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                  }                  }
# Line 57  public class StationDAO { Line 63  public class StationDAO {
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          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
107                  String SQL = "SELECT * FROM ( "+                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
108                  "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +                                           "       earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
109                  "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +                                           "FROM trainstations " +
110                  "               FROM trainstations " +                                           "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " +
111                  "               WHERE enabled = true " +                                           "ORDER BY calcdist ASC " +
112                  "       ) AS trainstations2 " +                                           "LIMIT 4 ";
                 "ORDER BY calcdist ASC " +  
                 "LIMIT 4 ";  
113                  List<StationBean> result;                  List<StationBean> result;
114                  Connection conn = null;                  Connection conn = null;
115                    PreparedStatement stmt = null;
116                    ResultSet res = null;
117                  try {                  try {
118                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
119                          PreparedStatement stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
120                          stmt.setDouble(1, latitude);                          stmt.setDouble(1, latitude);
121                          stmt.setDouble(2, longitude);                          stmt.setDouble(2, longitude);
122                          ResultSet rs = stmt.executeQuery();                          stmt.setDouble(3, latitude);
123                          result = convertResultset(rs);                          stmt.setDouble(4, longitude);                  
124                            res = stmt.executeQuery();
125                            result = convertResultset(res);
126                                                    
127                  } finally {                  } finally {
128                          if (conn != null)                          if (res != null)
129                                    res.close();
130                            if (stmt != null)
131                                    stmt.close();
132                            if (conn!= null)
133                                  conn.close();                                  conn.close();
134                  }                  }
135                  return result;                  return result;
# Line 116  public class StationDAO { Line 142  public class StationDAO {
142                          "ORDER BY name ";                          "ORDER BY name ";
143                                                    
144                          Connection conn = null;                          Connection conn = null;
145                            Statement stmt = null;
146                            ResultSet res = null;
147                          List<StationBean> result;                          List<StationBean> result;
148                                                    
149                          try {                          try {
150                                  conn = DBConnection.getConnection();                                  conn = DBConnection.getConnection();
151                                  Statement stmt = conn.createStatement();                                  stmt = conn.createStatement();
152                                  ResultSet res = stmt.executeQuery(SQL);                                  res = stmt.executeQuery(SQL);
153                                  result = convertResultset(res);                                  result = convertResultset(res);
154                          } finally {                          } finally {
155                                    if (res != null)
156                                            res.close();
157                                    if (stmt != null)
158                                            stmt.close();
159                                  if (conn!= null)                                  if (conn!= null)
160                                          conn.close();                                          conn.close();
161                          }                          }
# Line 131  public class StationDAO { Line 163  public class StationDAO {
163                          return result;                          return result;
164                                                    
165           }           }
166                     public static String getStationName(int stationID) {
167                     String station = "";
168    
169                     Connection conn = null;
170                     try {
171                             conn = DBConnection.getConnection();
172                             Statement stmt = conn.createStatement();
173                             ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
174                             if (rs.next()) {
175                                     station = rs.getString(1);
176                             }
177    
178                     } catch (Exception e) {        
179                     } finally {
180                             try {
181                                     if (conn != null && !conn.isClosed())
182                                             conn.close();
183                             } catch (Exception e) {}
184                     }
185    
186                     return station;
187             }
188  }  }

Legend:
Removed from v.588  
changed lines
  Added in v.739

  ViewVC Help
Powered by ViewVC 1.1.20