/[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 734 by torben, Tue May 18 20:39:22 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          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {          public List<StationBean> getByLocation(double latitude, double longitude) throws SQLException {
105                  String SQL = "SELECT * FROM ( "+                  String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +
106                  "               SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," +                                           "       earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " +
107                  "                     earth_distance( ll_to_earth(latitude,longitude), ll_to_earth(?,?))::int AS calcdist " +                                           "FROM trainstations " +
108                  "               FROM trainstations " +                                           "WHERE enabled = true " +
109                  "               WHERE enabled = true " +                                           "ORDER BY calcdist ASC " +
110                  "       ) AS trainstations2 " +                                           "LIMIT 4 ";
                 "ORDER BY calcdist ASC " +  
                 "LIMIT 4 ";  
111                  List<StationBean> result;                  List<StationBean> result;
112                  Connection conn = null;                  Connection conn = null;
113                    PreparedStatement stmt = null;
114                    ResultSet res = null;
115                  try {                  try {
116                          conn = DBConnection.getConnection();                          conn = DBConnection.getConnection();
117                          PreparedStatement stmt = conn.prepareStatement(SQL);                          stmt = conn.prepareStatement(SQL);
118                          stmt.setDouble(1, latitude);                          stmt.setDouble(1, latitude);
119                          stmt.setDouble(2, longitude);                          stmt.setDouble(2, longitude);
120                          ResultSet rs = stmt.executeQuery();                          res = stmt.executeQuery();
121                          result = convertResultset(rs);                          result = convertResultset(res);
122                                                    
123                  } finally {                  } finally {
124                          if (conn != null)                          if (res != null)
125                                    res.close();
126                            if (stmt != null)
127                                    stmt.close();
128                            if (conn!= null)
129                                  conn.close();                                  conn.close();
130                  }                  }
131                  return result;                  return result;
# Line 116  public class StationDAO { Line 138  public class StationDAO {
138                          "ORDER BY name ";                          "ORDER BY name ";
139                                                    
140                          Connection conn = null;                          Connection conn = null;
141                            Statement stmt = null;
142                            ResultSet res = null;
143                          List<StationBean> result;                          List<StationBean> result;
144                                                    
145                          try {                          try {
146                                  conn = DBConnection.getConnection();                                  conn = DBConnection.getConnection();
147                                  Statement stmt = conn.createStatement();                                  stmt = conn.createStatement();
148                                  ResultSet res = stmt.executeQuery(SQL);                                  res = stmt.executeQuery(SQL);
149                                  result = convertResultset(res);                                  result = convertResultset(res);
150                          } finally {                          } finally {
151                                    if (res != null)
152                                            res.close();
153                                    if (stmt != null)
154                                            stmt.close();
155                                  if (conn!= null)                                  if (conn!= null)
156                                          conn.close();                                          conn.close();
157                          }                          }
# Line 131  public class StationDAO { Line 159  public class StationDAO {
159                          return result;                          return result;
160                                                    
161           }           }
162                     public static String getStationName(int stationID) {
163                     String station = "";
164    
165                     Connection conn = null;
166                     try {
167                             conn = DBConnection.getConnection();
168                             Statement stmt = conn.createStatement();
169                             ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
170                             if (rs.next()) {
171                                     station = rs.getString(1);
172                             }
173    
174                     } catch (Exception e) {        
175                     } finally {
176                             try {
177                                     if (conn != null && !conn.isClosed())
178                                             conn.close();
179                             } catch (Exception e) {}
180                     }
181    
182                     return station;
183             }
184  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20