1 |
torben |
296 |
package dk.thoerup.traininfoservice; |
2 |
|
|
|
3 |
|
|
import java.io.IOException; |
4 |
|
|
import java.sql.Connection; |
5 |
|
|
import java.sql.ResultSet; |
6 |
|
|
import java.sql.ResultSetMetaData; |
7 |
|
|
import java.sql.Statement; |
8 |
|
|
|
9 |
|
|
import javax.servlet.ServletException; |
10 |
|
|
import javax.servlet.http.HttpServlet; |
11 |
|
|
import javax.servlet.http.HttpServletRequest; |
12 |
|
|
import javax.servlet.http.HttpServletResponse; |
13 |
|
|
|
14 |
|
|
public class DumpResultSet extends HttpServlet { |
15 |
|
|
|
16 |
torben |
298 |
public DumpResultSet() { |
17 |
|
|
super(); |
18 |
|
|
} |
19 |
torben |
296 |
|
20 |
torben |
298 |
|
21 |
torben |
296 |
private static final long serialVersionUID = 1L; |
22 |
|
|
|
23 |
|
|
String dumpResultset(String sql) throws ServletException { |
24 |
|
|
StringBuilder sb = new StringBuilder(); |
25 |
|
|
|
26 |
|
|
Connection conn = null; |
27 |
|
|
Statement stmt = null; |
28 |
|
|
ResultSet rs = null; |
29 |
torben |
298 |
|
30 |
torben |
296 |
try { |
31 |
torben |
304 |
int count = 0; |
32 |
|
|
|
33 |
torben |
296 |
conn = DBConnection.getConnection(); |
34 |
|
|
stmt = conn.createStatement(); |
35 |
|
|
|
36 |
|
|
rs = stmt.executeQuery(sql); |
37 |
|
|
ResultSetMetaData meta = rs.getMetaData(); |
38 |
|
|
int columns = meta.getColumnCount(); |
39 |
|
|
|
40 |
|
|
sb.append("<h2>").append(sql).append("</h2>"); |
41 |
torben |
298 |
sb.append("<table border=1><tr>"); |
42 |
torben |
296 |
for (int i=1; i<=columns; i++) { |
43 |
|
|
sb.append("<th>").append( meta.getColumnName(i)).append("</th>"); |
44 |
|
|
} |
45 |
torben |
304 |
sb.append("</tr>\n\n"); |
46 |
torben |
296 |
while(rs.next()) |
47 |
|
|
{ |
48 |
torben |
304 |
count++; |
49 |
|
|
|
50 |
torben |
296 |
sb.append("<tr>"); |
51 |
|
|
for (int i=1; i<=columns; i++) { |
52 |
torben |
494 |
String value = rs.getString(i); |
53 |
|
|
if (value != null) { |
54 |
|
|
value = value.replace("\n", "<br>"); |
55 |
|
|
} |
56 |
|
|
sb.append("<td>").append( value ).append("</td>"); |
57 |
torben |
296 |
} |
58 |
torben |
304 |
sb.append("</tr>\n"); |
59 |
torben |
296 |
} |
60 |
|
|
sb.append("</table>"); |
61 |
torben |
304 |
sb.append("Rowcount: ").append(count); |
62 |
torben |
296 |
} catch (Exception e) { |
63 |
|
|
throw new ServletException(e); |
64 |
|
|
} finally { |
65 |
|
|
|
66 |
|
|
try { |
67 |
|
|
if (rs != null && !rs.isClosed()) |
68 |
|
|
rs.close(); |
69 |
|
|
} catch (Exception e) {} |
70 |
|
|
try { |
71 |
|
|
if (stmt != null && !stmt.isClosed()) |
72 |
|
|
stmt.close(); |
73 |
|
|
} catch (Exception e) {} |
74 |
|
|
|
75 |
|
|
try { |
76 |
|
|
if (conn != null && !conn.isClosed()) |
77 |
|
|
conn.close(); |
78 |
|
|
} catch (Exception e) {} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
return sb.toString(); |
82 |
|
|
} |
83 |
|
|
|
84 |
torben |
733 |
String dumpUpdate(String query) throws ServletException { |
85 |
|
|
StringBuilder sb = new StringBuilder(); |
86 |
|
|
|
87 |
|
|
Connection conn = null; |
88 |
|
|
Statement stmt = null; |
89 |
|
|
try { |
90 |
|
|
conn = DBConnection.getConnection(); |
91 |
|
|
stmt = conn.createStatement(); |
92 |
|
|
stmt.execute(query); |
93 |
|
|
int count = stmt.getUpdateCount(); |
94 |
|
|
|
95 |
|
|
sb.append("<h2>").append(query).append("</h2>"); |
96 |
|
|
sb.append("Affected rows: ").append(count); |
97 |
|
|
} catch (Exception e) { |
98 |
|
|
throw new ServletException(e); |
99 |
|
|
} finally { |
100 |
|
|
try { |
101 |
|
|
if (stmt != null && !stmt.isClosed()) |
102 |
|
|
stmt.close(); |
103 |
|
|
} catch (Exception e) {} |
104 |
|
|
|
105 |
|
|
try { |
106 |
|
|
if (conn != null && !conn.isClosed()) |
107 |
|
|
conn.close(); |
108 |
|
|
} catch (Exception e) {} |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
return sb.toString(); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
torben |
296 |
@Override |
117 |
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
118 |
|
|
String dump = req.getParameter("dump"); |
119 |
|
|
|
120 |
torben |
298 |
String res = null; |
121 |
torben |
296 |
|
122 |
torben |
298 |
if (dump != null) { |
123 |
|
|
if (dump.equals("all")) { |
124 |
torben |
304 |
res = dumpResultset("SELECT * FROM trainstations ORDER BY id"); |
125 |
torben |
298 |
} else if (dump.equals("coords")) { |
126 |
torben |
304 |
res = dumpResultset("SELECT * FROM trainstations WHERE latitude IS NULL OR longitude IS NULL ORDER BY id"); |
127 |
torben |
298 |
} else if (dump.equals("duplicate")) { |
128 |
|
|
res = dumpResultset("SELECT name,count(*) FROM trainstations GROUP BY name HAVING count(*) > 1"); |
129 |
torben |
304 |
} else if (dump.equals("allfull")) { |
130 |
|
|
res = dumpResultset("select *, " + |
131 |
torben |
497 |
"'<a href=\"http://maps.google.dk/?q=' ||latitude || ',' || longitude || '\">Maps</a>' AS maps, " + |
132 |
torben |
314 |
"'<a href=\"http://www.bane.dk/visStation.asp?ArtikelID=4275&W=FJRN&S=' || stationcode_fjrn || '\">Bane.dk Fjern</a>' as banedk1, " + |
133 |
|
|
"'<a href=\"http://www.bane.dk/visStation.asp?ArtikelID=4275&W=S2&S=' || stationcode_stog || '\">Bane.dk stog</a>' as banedk2 " + |
134 |
|
|
"FROM trainstations ORDER BY id"); |
135 |
torben |
377 |
} else if (dump.equals("disabled")) { |
136 |
|
|
res = dumpResultset("SELECT * FROM trainstations WHERE enabled = false ORDER BY id"); |
137 |
torben |
492 |
} else if (dump.equals("noaddress")) { |
138 |
|
|
res = dumpResultset("SELECT * FROM trainstations WHERE address IS NULL or address = '' "); |
139 |
torben |
715 |
} else if (dump.equals("aliases")) { |
140 |
|
|
res = dumpResultset("SELECT * FROM trainstations WHERE aliases IS NOT null"); |
141 |
torben |
733 |
} else if (dump.equals("updatecoords")) { |
142 |
|
|
res = dumpUpdate("UPDATE trainstations SET earth_coord = ll_to_earth(latitude,longitude)"); |
143 |
torben |
811 |
} else if (dump.equals("trainstatistics")) { |
144 |
|
|
res = dumpResultset("SELECT * FROM trainstatistics"); |
145 |
|
|
} else if (dump.equals("trainstatistics_avg")) { |
146 |
|
|
res = dumpResultset("SELECT avg(location), avg(name), avg(favorites), avg(departure), avg(depcache), avg(deperror), avg(timetable), avg(timecache), avg(timeerror) FROM trainstatistics"); |
147 |
torben |
298 |
} |
148 |
torben |
296 |
} |
149 |
torben |
298 |
if (res == null) { |
150 |
torben |
733 |
res = "<a href=DumpResultSet?dump=all>All</a><br><a href=DumpResultSet?dump=allfull>All with links</a><br><a href=DumpResultSet?dump=coords>Missing coords</a><br><a href=DumpResultSet?dump=duplicate>Duplicate stations</a><br><a href=DumpResultSet?dump=disabled>Disabled stations</a><br><a href=DumpResultSet?dump=noaddress>No address</a><br><a href=DumpResultSet?dump=aliases>Has aliases</a><br><a href=DumpResultSet?dump=updatecoords>update coords</a>"; |
151 |
torben |
298 |
} |
152 |
|
|
|
153 |
torben |
296 |
resp.setContentType("text/html"); |
154 |
|
|
resp.getWriter().println(res); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
} |