--- android/TrainInfoService/src/dk/thoerup/traininfoservice/DumpResultSet.java 2009/09/01 22:18:41 296 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/DumpResultSet.java 2010/05/18 17:56:43 733 @@ -13,6 +13,10 @@ public class DumpResultSet extends HttpServlet { + public DumpResultSet() { + super(); + } + private static final long serialVersionUID = 1L; @@ -22,7 +26,10 @@ Connection conn = null; Statement stmt = null; ResultSet rs = null; + try { + int count = 0; + conn = DBConnection.getConnection(); stmt = conn.createStatement(); @@ -31,20 +38,27 @@ int columns = meta.getColumnCount(); sb.append("

").append(sql).append("

"); - sb.append(""); + sb.append("
"); for (int i=1; i<=columns; i++) { sb.append(""); } - sb.append(""); + sb.append("\n\n"); while(rs.next()) { + count++; + sb.append(""); for (int i=1; i<=columns; i++) { - sb.append(""); + String value = rs.getString(i); + if (value != null) { + value = value.replace("\n", "
"); + } + sb.append(""); } - sb.append(""); + sb.append("\n"); } sb.append("
").append( meta.getColumnName(i)).append("
").append(rs.getString(i)).append("").append( value ).append("
"); + sb.append("Rowcount: ").append(count); } catch (Exception e) { throw new ServletException(e); } finally { @@ -67,17 +81,71 @@ return sb.toString(); } + String dumpUpdate(String query) throws ServletException { + StringBuilder sb = new StringBuilder(); + + Connection conn = null; + Statement stmt = null; + try { + conn = DBConnection.getConnection(); + stmt = conn.createStatement(); + stmt.execute(query); + int count = stmt.getUpdateCount(); + + sb.append("

").append(query).append("

"); + sb.append("Affected rows: ").append(count); + } catch (Exception e) { + throw new ServletException(e); + } finally { + try { + if (stmt != null && !stmt.isClosed()) + stmt.close(); + } catch (Exception e) {} + + try { + if (conn != null && !conn.isClosed()) + conn.close(); + } catch (Exception e) {} + } + + return sb.toString(); + } + + + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String dump = req.getParameter("dump"); - String res = "invalid dump parameter"; + String res = null; - if (dump.equals("all")) { - res = dumpResultset("SELECT * FROM trainstations"); - } else if (dump.equals("coords")) { - res = dumpResultset("SELECT * FROM trainstations WHERE latitude IS NULL OR longitude IS NULL"); + if (dump != null) { + if (dump.equals("all")) { + res = dumpResultset("SELECT * FROM trainstations ORDER BY id"); + } else if (dump.equals("coords")) { + res = dumpResultset("SELECT * FROM trainstations WHERE latitude IS NULL OR longitude IS NULL ORDER BY id"); + } else if (dump.equals("duplicate")) { + res = dumpResultset("SELECT name,count(*) FROM trainstations GROUP BY name HAVING count(*) > 1"); + } else if (dump.equals("allfull")) { + res = dumpResultset("select *, " + + "'Maps' AS maps, " + + "'Bane.dk Fjern' as banedk1, " + + "'Bane.dk stog' as banedk2 " + + "FROM trainstations ORDER BY id"); + } else if (dump.equals("disabled")) { + res = dumpResultset("SELECT * FROM trainstations WHERE enabled = false ORDER BY id"); + } else if (dump.equals("noaddress")) { + res = dumpResultset("SELECT * FROM trainstations WHERE address IS NULL or address = '' "); + } else if (dump.equals("aliases")) { + res = dumpResultset("SELECT * FROM trainstations WHERE aliases IS NOT null"); + } else if (dump.equals("updatecoords")) { + res = dumpUpdate("UPDATE trainstations SET earth_coord = ll_to_earth(latitude,longitude)"); + } } + if (res == null) { + res = "All
All with links
Missing coords
Duplicate stations
Disabled stations
No address
Has aliases
update coords"; + } + resp.setContentType("text/html"); resp.getWriter().println(res); }