| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.server.web; |
| 7 |
|
| 8 |
import java.io.ByteArrayOutputStream; |
| 9 |
import java.io.PrintStream; |
| 10 |
import java.io.PrintWriter; |
| 11 |
import java.io.StringReader; |
| 12 |
import java.io.StringWriter; |
| 13 |
import java.math.BigDecimal; |
| 14 |
import java.nio.charset.StandardCharsets; |
| 15 |
import java.sql.Connection; |
| 16 |
import java.sql.DatabaseMetaData; |
| 17 |
import java.sql.ParameterMetaData; |
| 18 |
import java.sql.PreparedStatement; |
| 19 |
import java.sql.ResultSet; |
| 20 |
import java.sql.ResultSetMetaData; |
| 21 |
import java.sql.SQLException; |
| 22 |
import java.sql.Statement; |
| 23 |
import java.sql.Types; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Arrays; |
| 26 |
import java.util.Collections; |
| 27 |
import java.util.Comparator; |
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.Iterator; |
| 30 |
import java.util.Locale; |
| 31 |
import java.util.Map; |
| 32 |
import java.util.Properties; |
| 33 |
import java.util.Random; |
| 34 |
import java.util.concurrent.atomic.AtomicReference; |
| 35 |
|
| 36 |
import org.h2.api.ErrorCode; |
| 37 |
import org.h2.bnf.Bnf; |
| 38 |
import org.h2.bnf.context.DbColumn; |
| 39 |
import org.h2.bnf.context.DbContents; |
| 40 |
import org.h2.bnf.context.DbSchema; |
| 41 |
import org.h2.bnf.context.DbTableOrView; |
| 42 |
import org.h2.command.Parser; |
| 43 |
import org.h2.engine.Constants; |
| 44 |
import org.h2.engine.SysProperties; |
| 45 |
import org.h2.jdbc.JdbcException; |
| 46 |
import org.h2.message.DbException; |
| 47 |
import org.h2.security.SHA256; |
| 48 |
import org.h2.tools.Backup; |
| 49 |
import org.h2.tools.ChangeFileEncryption; |
| 50 |
import org.h2.tools.ConvertTraceFile; |
| 51 |
import org.h2.tools.CreateCluster; |
| 52 |
import org.h2.tools.DeleteDbFiles; |
| 53 |
import org.h2.tools.Recover; |
| 54 |
import org.h2.tools.Restore; |
| 55 |
import org.h2.tools.RunScript; |
| 56 |
import org.h2.tools.Script; |
| 57 |
import org.h2.tools.SimpleResultSet; |
| 58 |
import org.h2.util.JdbcUtils; |
| 59 |
import org.h2.util.NetUtils; |
| 60 |
import org.h2.util.NetworkConnectionInfo; |
| 61 |
import org.h2.util.Profiler; |
| 62 |
import org.h2.util.ScriptReader; |
| 63 |
import org.h2.util.SortedProperties; |
| 64 |
import org.h2.util.StringUtils; |
| 65 |
import org.h2.util.Tool; |
| 66 |
import org.h2.util.Utils; |
| 67 |
import org.h2.util.Utils10; |
| 68 |
import org.h2.value.DataType; |
| 69 |
|
| 70 |
/** |
| 71 |
* For each connection to a session, an object of this class is created. |
| 72 |
* This class is used by the H2 Console. |
| 73 |
*/ |
| 74 |
public class WebApp { |
| 75 |
|
| 76 |
private static final Comparator<DbTableOrView> SYSTEM_SCHEMA_COMPARATOR = Comparator |
| 77 |
.comparing(DbTableOrView::getName, String.CASE_INSENSITIVE_ORDER); |
| 78 |
|
| 79 |
/** |
| 80 |
* The web server. |
| 81 |
*/ |
| 82 |
protected final WebServer server; |
| 83 |
|
| 84 |
/** |
| 85 |
* The session. |
| 86 |
*/ |
| 87 |
protected WebSession session; |
| 88 |
|
| 89 |
/** |
| 90 |
* The session attributes |
| 91 |
*/ |
| 92 |
protected Properties attributes; |
| 93 |
|
| 94 |
/** |
| 95 |
* The mime type of the current response. |
| 96 |
*/ |
| 97 |
protected String mimeType; |
| 98 |
|
| 99 |
/** |
| 100 |
* Whether the response can be cached. |
| 101 |
*/ |
| 102 |
protected boolean cache; |
| 103 |
|
| 104 |
/** |
| 105 |
* Whether to close the connection. |
| 106 |
*/ |
| 107 |
protected boolean stop; |
| 108 |
|
| 109 |
/** |
| 110 |
* The language in the HTTP header. |
| 111 |
*/ |
| 112 |
protected String headerLanguage; |
| 113 |
|
| 114 |
private Profiler profiler; |
| 115 |
|
| 116 |
WebApp(WebServer server) { |
| 117 |
this.server = server; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Set the web session and attributes. |
| 122 |
* |
| 123 |
* @param session the session |
| 124 |
* @param attributes the attributes |
| 125 |
*/ |
| 126 |
void setSession(WebSession session, Properties attributes) { |
| 127 |
this.session = session; |
| 128 |
this.attributes = attributes; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Process an HTTP request. |
| 133 |
* |
| 134 |
* @param file the file that was requested |
| 135 |
* @param networkConnectionInfo the network connection information |
| 136 |
* @return the name of the file to return to the client |
| 137 |
*/ |
| 138 |
String processRequest(String file, NetworkConnectionInfo networkConnectionInfo) { |
| 139 |
int index = file.lastIndexOf('.'); |
| 140 |
String suffix; |
| 141 |
if (index >= 0) { |
| 142 |
suffix = file.substring(index + 1); |
| 143 |
} else { |
| 144 |
suffix = ""; |
| 145 |
} |
| 146 |
if ("ico".equals(suffix)) { |
| 147 |
mimeType = "image/x-icon"; |
| 148 |
cache = true; |
| 149 |
} else if ("gif".equals(suffix)) { |
| 150 |
mimeType = "image/gif"; |
| 151 |
cache = true; |
| 152 |
} else if ("css".equals(suffix)) { |
| 153 |
cache = true; |
| 154 |
mimeType = "text/css"; |
| 155 |
} else if ("html".equals(suffix) || |
| 156 |
"do".equals(suffix) || |
| 157 |
"jsp".equals(suffix)) { |
| 158 |
cache = false; |
| 159 |
mimeType = "text/html"; |
| 160 |
if (session == null) { |
| 161 |
session = server.createNewSession( |
| 162 |
NetUtils.ipToShortForm(null, networkConnectionInfo.getClientAddr(), false).toString()); |
| 163 |
if (!"notAllowed.jsp".equals(file)) { |
| 164 |
file = "index.do"; |
| 165 |
} |
| 166 |
} |
| 167 |
} else if ("js".equals(suffix)) { |
| 168 |
cache = true; |
| 169 |
mimeType = "text/javascript"; |
| 170 |
} else { |
| 171 |
cache = true; |
| 172 |
mimeType = "application/octet-stream"; |
| 173 |
} |
| 174 |
trace("mimeType=" + mimeType); |
| 175 |
trace(file); |
| 176 |
if (file.endsWith(".do")) { |
| 177 |
file = process(file, networkConnectionInfo); |
| 178 |
} else if (file.endsWith(".jsp")) { |
| 179 |
switch (file) { |
| 180 |
case "admin.jsp": |
| 181 |
case "tools.jsp": |
| 182 |
if (!checkAdmin(file)) { |
| 183 |
file = process("adminLogin.do", networkConnectionInfo); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
return file; |
| 188 |
} |
| 189 |
|
| 190 |
private static String getComboBox(String[] elements, String selected) { |
| 191 |
StringBuilder buff = new StringBuilder(); |
| 192 |
for (String value : elements) { |
| 193 |
buff.append("<option value=\""). |
| 194 |
append(PageParser.escapeHtmlData(value)). |
| 195 |
append('\"'); |
| 196 |
if (value.equals(selected)) { |
| 197 |
buff.append(" selected"); |
| 198 |
} |
| 199 |
buff.append('>'). |
| 200 |
append(PageParser.escapeHtml(value)). |
| 201 |
append("</option>"); |
| 202 |
} |
| 203 |
return buff.toString(); |
| 204 |
} |
| 205 |
|
| 206 |
private static String getComboBox(String[][] elements, String selected) { |
| 207 |
StringBuilder buff = new StringBuilder(); |
| 208 |
for (String[] n : elements) { |
| 209 |
buff.append("<option value=\""). |
| 210 |
append(PageParser.escapeHtmlData(n[0])). |
| 211 |
append('\"'); |
| 212 |
if (n[0].equals(selected)) { |
| 213 |
buff.append(" selected"); |
| 214 |
} |
| 215 |
buff.append('>'). |
| 216 |
append(PageParser.escapeHtml(n[1])). |
| 217 |
append("</option>"); |
| 218 |
} |
| 219 |
return buff.toString(); |
| 220 |
} |
| 221 |
|
| 222 |
private String process(String file, NetworkConnectionInfo networkConnectionInfo) { |
| 223 |
trace("process " + file); |
| 224 |
while (file.endsWith(".do")) { |
| 225 |
switch (file) { |
| 226 |
case "login.do": |
| 227 |
file = login(networkConnectionInfo); |
| 228 |
break; |
| 229 |
case "index.do": |
| 230 |
file = index(); |
| 231 |
break; |
| 232 |
case "logout.do": |
| 233 |
file = logout(); |
| 234 |
break; |
| 235 |
case "settingRemove.do": |
| 236 |
file = settingRemove(); |
| 237 |
break; |
| 238 |
case "settingSave.do": |
| 239 |
file = settingSave(); |
| 240 |
break; |
| 241 |
case "test.do": |
| 242 |
file = test(networkConnectionInfo); |
| 243 |
break; |
| 244 |
case "query.do": |
| 245 |
file = query(); |
| 246 |
break; |
| 247 |
case "tables.do": |
| 248 |
file = tables(); |
| 249 |
break; |
| 250 |
case "editResult.do": |
| 251 |
file = editResult(); |
| 252 |
break; |
| 253 |
case "getHistory.do": |
| 254 |
file = getHistory(); |
| 255 |
break; |
| 256 |
case "admin.do": |
| 257 |
file = checkAdmin(file) ? admin() : "adminLogin.do"; |
| 258 |
break; |
| 259 |
case "adminSave.do": |
| 260 |
file = checkAdmin(file) ? adminSave() : "adminLogin.do"; |
| 261 |
break; |
| 262 |
case "adminStartTranslate.do": |
| 263 |
file = checkAdmin(file) ? adminStartTranslate() : "adminLogin.do"; |
| 264 |
break; |
| 265 |
case "adminShutdown.do": |
| 266 |
file = checkAdmin(file) ? adminShutdown() : "adminLogin.do"; |
| 267 |
break; |
| 268 |
case "autoCompleteList.do": |
| 269 |
file = autoCompleteList(); |
| 270 |
break; |
| 271 |
case "tools.do": |
| 272 |
file = checkAdmin(file) ? tools() : "adminLogin.do"; |
| 273 |
break; |
| 274 |
case "adminLogin.do": |
| 275 |
file = adminLogin(); |
| 276 |
break; |
| 277 |
default: |
| 278 |
file = "error.jsp"; |
| 279 |
break; |
| 280 |
} |
| 281 |
} |
| 282 |
trace("return " + file); |
| 283 |
return file; |
| 284 |
} |
| 285 |
|
| 286 |
private boolean checkAdmin(String file) { |
| 287 |
Boolean b = (Boolean) session.get("admin"); |
| 288 |
if (b != null && b) { |
| 289 |
return true; |
| 290 |
} |
| 291 |
String key = server.getKey(); |
| 292 |
if (key != null && key.equals(session.get("key"))) { |
| 293 |
return true; |
| 294 |
} |
| 295 |
session.put("adminBack", file); |
| 296 |
return false; |
| 297 |
} |
| 298 |
|
| 299 |
private String adminLogin() { |
| 300 |
String password = attributes.getProperty("password"); |
| 301 |
if (password == null || password.isEmpty() || !server.checkAdminPassword(password)) { |
| 302 |
return "adminLogin.jsp"; |
| 303 |
} |
| 304 |
String back = (String) session.remove("adminBack"); |
| 305 |
session.put("admin", true); |
| 306 |
return back != null ? back : "admin.do"; |
| 307 |
} |
| 308 |
|
| 309 |
private String autoCompleteList() { |
| 310 |
String query = (String) attributes.get("query"); |
| 311 |
boolean lowercase = false; |
| 312 |
String tQuery = query.trim(); |
| 313 |
if (!tQuery.isEmpty() && Character.isLowerCase(tQuery.charAt(0))) { |
| 314 |
lowercase = true; |
| 315 |
} |
| 316 |
try { |
| 317 |
String sql = query; |
| 318 |
if (sql.endsWith(";")) { |
| 319 |
sql += " "; |
| 320 |
} |
| 321 |
ScriptReader reader = new ScriptReader(new StringReader(sql)); |
| 322 |
reader.setSkipRemarks(true); |
| 323 |
String lastSql = ""; |
| 324 |
while (true) { |
| 325 |
String n = reader.readStatement(); |
| 326 |
if (n == null) { |
| 327 |
break; |
| 328 |
} |
| 329 |
lastSql = n; |
| 330 |
} |
| 331 |
String result = ""; |
| 332 |
if (reader.isInsideRemark()) { |
| 333 |
if (reader.isBlockRemark()) { |
| 334 |
result = "1#(End Remark)# */\n" + result; |
| 335 |
} else { |
| 336 |
result = "1#(Newline)#\n" + result; |
| 337 |
} |
| 338 |
} else { |
| 339 |
sql = lastSql; |
| 340 |
while (sql.length() > 0 && sql.charAt(0) <= ' ') { |
| 341 |
sql = sql.substring(1); |
| 342 |
} |
| 343 |
String tSql = sql.trim(); |
| 344 |
if (!tSql.isEmpty() && Character.isLowerCase(tSql.charAt(0))) { |
| 345 |
lowercase = true; |
| 346 |
} |
| 347 |
Bnf bnf = session.getBnf(); |
| 348 |
if (bnf == null) { |
| 349 |
return "autoCompleteList.jsp"; |
| 350 |
} |
| 351 |
HashMap<String, String> map = bnf.getNextTokenList(sql); |
| 352 |
String space = ""; |
| 353 |
if (sql.length() > 0) { |
| 354 |
char last = sql.charAt(sql.length() - 1); |
| 355 |
if (!Character.isWhitespace(last) && (last != '.' && |
| 356 |
last >= ' ' && last != '\'' && last != '"')) { |
| 357 |
space = " "; |
| 358 |
} |
| 359 |
} |
| 360 |
ArrayList<String> list = new ArrayList<>(map.size()); |
| 361 |
for (Map.Entry<String, String> entry : map.entrySet()) { |
| 362 |
String key = entry.getKey(); |
| 363 |
String value = entry.getValue(); |
| 364 |
String type = String.valueOf(key.charAt(0)); |
| 365 |
if (Integer.parseInt(type) > 2) { |
| 366 |
continue; |
| 367 |
} |
| 368 |
key = key.substring(2); |
| 369 |
if (Character.isLetter(key.charAt(0)) && lowercase) { |
| 370 |
key = StringUtils.toLowerEnglish(key); |
| 371 |
value = StringUtils.toLowerEnglish(value); |
| 372 |
} |
| 373 |
if (key.equals(value) && !".".equals(value)) { |
| 374 |
value = space + value; |
| 375 |
} |
| 376 |
key = StringUtils.urlEncode(key); |
| 377 |
key = key.replace('+', ' '); |
| 378 |
value = StringUtils.urlEncode(value); |
| 379 |
value = value.replace('+', ' '); |
| 380 |
list.add(type + "#" + key + "#" + value); |
| 381 |
} |
| 382 |
Collections.sort(list); |
| 383 |
if (query.endsWith("\n") || tQuery.endsWith(";")) { |
| 384 |
list.add(0, "1#(Newline)#\n"); |
| 385 |
} |
| 386 |
result = String.join("|", list); |
| 387 |
} |
| 388 |
session.put("autoCompleteList", result); |
| 389 |
} catch (Throwable e) { |
| 390 |
server.traceError(e); |
| 391 |
} |
| 392 |
return "autoCompleteList.jsp"; |
| 393 |
} |
| 394 |
|
| 395 |
private String admin() { |
| 396 |
session.put("port", Integer.toString(server.getPort())); |
| 397 |
session.put("allowOthers", Boolean.toString(server.getAllowOthers())); |
| 398 |
session.put("webExternalNames", server.getExternalNames()); |
| 399 |
session.put("ssl", String.valueOf(server.getSSL())); |
| 400 |
session.put("sessions", server.getSessions()); |
| 401 |
return "admin.jsp"; |
| 402 |
} |
| 403 |
|
| 404 |
private String adminSave() { |
| 405 |
try { |
| 406 |
Properties prop = new SortedProperties(); |
| 407 |
int port = Integer.decode((String) attributes.get("port")); |
| 408 |
prop.setProperty("webPort", Integer.toString(port)); |
| 409 |
server.setPort(port); |
| 410 |
boolean allowOthers = Utils.parseBoolean((String) attributes.get("allowOthers"), false, false); |
| 411 |
prop.setProperty("webAllowOthers", String.valueOf(allowOthers)); |
| 412 |
server.setAllowOthers(allowOthers); |
| 413 |
String externalNames = (String) attributes.get("webExternalNames"); |
| 414 |
prop.setProperty("webExternalNames", externalNames); |
| 415 |
server.setExternalNames(externalNames); |
| 416 |
boolean ssl = Utils.parseBoolean((String) attributes.get("ssl"), false, false); |
| 417 |
prop.setProperty("webSSL", String.valueOf(ssl)); |
| 418 |
server.setSSL(ssl); |
| 419 |
byte[] adminPassword = server.getAdminPassword(); |
| 420 |
if (adminPassword != null) { |
| 421 |
prop.setProperty("webAdminPassword", StringUtils.convertBytesToHex(adminPassword)); |
| 422 |
} |
| 423 |
server.saveProperties(prop); |
| 424 |
} catch (Exception e) { |
| 425 |
trace(e.toString()); |
| 426 |
} |
| 427 |
return admin(); |
| 428 |
} |
| 429 |
|
| 430 |
private String tools() { |
| 431 |
try { |
| 432 |
String toolName = (String) attributes.get("tool"); |
| 433 |
session.put("tool", toolName); |
| 434 |
String args = (String) attributes.get("args"); |
| 435 |
String[] argList = StringUtils.arraySplit(args, ',', false); |
| 436 |
Tool tool = null; |
| 437 |
if ("Backup".equals(toolName)) { |
| 438 |
tool = new Backup(); |
| 439 |
} else if ("Restore".equals(toolName)) { |
| 440 |
tool = new Restore(); |
| 441 |
} else if ("Recover".equals(toolName)) { |
| 442 |
tool = new Recover(); |
| 443 |
} else if ("DeleteDbFiles".equals(toolName)) { |
| 444 |
tool = new DeleteDbFiles(); |
| 445 |
} else if ("ChangeFileEncryption".equals(toolName)) { |
| 446 |
tool = new ChangeFileEncryption(); |
| 447 |
} else if ("Script".equals(toolName)) { |
| 448 |
tool = new Script(); |
| 449 |
} else if ("RunScript".equals(toolName)) { |
| 450 |
tool = new RunScript(); |
| 451 |
} else if ("ConvertTraceFile".equals(toolName)) { |
| 452 |
tool = new ConvertTraceFile(); |
| 453 |
} else if ("CreateCluster".equals(toolName)) { |
| 454 |
tool = new CreateCluster(); |
| 455 |
} else { |
| 456 |
throw DbException.getInternalError(toolName); |
| 457 |
} |
| 458 |
ByteArrayOutputStream outBuff = new ByteArrayOutputStream(); |
| 459 |
PrintStream out = new PrintStream(outBuff, false, "UTF-8"); |
| 460 |
tool.setOut(out); |
| 461 |
try { |
| 462 |
tool.runTool(argList); |
| 463 |
out.flush(); |
| 464 |
String o = Utils10.byteArrayOutputStreamToString(outBuff, StandardCharsets.UTF_8); |
| 465 |
String result = PageParser.escapeHtml(o); |
| 466 |
session.put("toolResult", result); |
| 467 |
} catch (Exception e) { |
| 468 |
session.put("toolResult", getStackTrace(0, e, true)); |
| 469 |
} |
| 470 |
} catch (Exception e) { |
| 471 |
server.traceError(e); |
| 472 |
} |
| 473 |
return "tools.jsp"; |
| 474 |
} |
| 475 |
|
| 476 |
private String adminStartTranslate() { |
| 477 |
Map<?, ?> p = Map.class.cast(session.map.get("text")); |
| 478 |
@SuppressWarnings("unchecked") |
| 479 |
Map<Object, Object> p2 = (Map<Object, Object>) p; |
| 480 |
String file = server.startTranslate(p2); |
| 481 |
session.put("translationFile", file); |
| 482 |
return "helpTranslate.jsp"; |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Stop the application and the server. |
| 487 |
* |
| 488 |
* @return the page to display |
| 489 |
*/ |
| 490 |
protected String adminShutdown() { |
| 491 |
server.shutdown(); |
| 492 |
return "admin.jsp"; |
| 493 |
} |
| 494 |
|
| 495 |
private String index() { |
| 496 |
String[][] languageArray = WebServer.LANGUAGES; |
| 497 |
String language = (String) attributes.get("language"); |
| 498 |
Locale locale = session.locale; |
| 499 |
if (language != null) { |
| 500 |
if (locale == null || !StringUtils.toLowerEnglish( |
| 501 |
locale.getLanguage()).equals(language)) { |
| 502 |
locale = new Locale(language, ""); |
| 503 |
server.readTranslations(session, locale.getLanguage()); |
| 504 |
session.put("language", language); |
| 505 |
session.locale = locale; |
| 506 |
} |
| 507 |
} else { |
| 508 |
language = (String) session.get("language"); |
| 509 |
} |
| 510 |
if (language == null) { |
| 511 |
// if the language is not yet known |
| 512 |
// use the last header |
| 513 |
language = headerLanguage; |
| 514 |
} |
| 515 |
session.put("languageCombo", getComboBox(languageArray, language)); |
| 516 |
String[] settingNames = server.getSettingNames(); |
| 517 |
String setting = attributes.getProperty("setting"); |
| 518 |
if (setting == null && settingNames.length > 0) { |
| 519 |
setting = settingNames[0]; |
| 520 |
} |
| 521 |
String combobox = getComboBox(settingNames, setting); |
| 522 |
session.put("settingsList", combobox); |
| 523 |
ConnectionInfo info = server.getSetting(setting); |
| 524 |
if (info == null) { |
| 525 |
info = new ConnectionInfo(); |
| 526 |
} |
| 527 |
session.put("setting", PageParser.escapeHtmlData(setting)); |
| 528 |
session.put("name", PageParser.escapeHtmlData(setting)); |
| 529 |
session.put("driver", PageParser.escapeHtmlData(info.driver)); |
| 530 |
session.put("url", PageParser.escapeHtmlData(info.url)); |
| 531 |
session.put("user", PageParser.escapeHtmlData(info.user)); |
| 532 |
return "index.jsp"; |
| 533 |
} |
| 534 |
|
| 535 |
private String getHistory() { |
| 536 |
int id = Integer.parseInt(attributes.getProperty("id")); |
| 537 |
String sql = session.getCommand(id); |
| 538 |
session.put("query", PageParser.escapeHtmlData(sql)); |
| 539 |
return "query.jsp"; |
| 540 |
} |
| 541 |
|
| 542 |
private static int addColumns(boolean mainSchema, DbTableOrView table, StringBuilder builder, int treeIndex, |
| 543 |
boolean showColumnTypes, StringBuilder columnsBuilder) { |
| 544 |
DbColumn[] columns = table.getColumns(); |
| 545 |
for (int i = 0; columns != null && i < columns.length; i++) { |
| 546 |
DbColumn column = columns[i]; |
| 547 |
if (columnsBuilder.length() > 0) { |
| 548 |
columnsBuilder.append(' '); |
| 549 |
} |
| 550 |
columnsBuilder.append(column.getName()); |
| 551 |
String col = escapeIdentifier(column.getName()); |
| 552 |
String level = mainSchema ? ", 1, 1" : ", 2, 2"; |
| 553 |
builder.append("setNode(").append(treeIndex).append(level) |
| 554 |
.append(", 'column', '") |
| 555 |
.append(PageParser.escapeJavaScript(column.getName())) |
| 556 |
.append("', 'javascript:ins(\\'").append(col).append("\\')');\n"); |
| 557 |
treeIndex++; |
| 558 |
if (mainSchema && showColumnTypes) { |
| 559 |
builder.append("setNode(").append(treeIndex) |
| 560 |
.append(", 2, 2, 'type', '") |
| 561 |
.append(PageParser.escapeJavaScript(column.getDataType())) |
| 562 |
.append("', null);\n"); |
| 563 |
treeIndex++; |
| 564 |
} |
| 565 |
} |
| 566 |
return treeIndex; |
| 567 |
} |
| 568 |
|
| 569 |
private static String escapeIdentifier(String name) { |
| 570 |
return StringUtils.urlEncode( |
| 571 |
PageParser.escapeJavaScript(name)).replace('+', ' '); |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* This class represents index information for the GUI. |
| 576 |
*/ |
| 577 |
static class IndexInfo { |
| 578 |
|
| 579 |
/** |
| 580 |
* The index name. |
| 581 |
*/ |
| 582 |
String name; |
| 583 |
|
| 584 |
/** |
| 585 |
* The index type name. |
| 586 |
*/ |
| 587 |
String type; |
| 588 |
|
| 589 |
/** |
| 590 |
* The indexed columns. |
| 591 |
*/ |
| 592 |
String columns; |
| 593 |
} |
| 594 |
|
| 595 |
private static int addIndexes(boolean mainSchema, DatabaseMetaData meta, |
| 596 |
String table, String schema, StringBuilder buff, int treeIndex) |
| 597 |
throws SQLException { |
| 598 |
ResultSet rs; |
| 599 |
try { |
| 600 |
rs = meta.getIndexInfo(null, schema, table, false, true); |
| 601 |
} catch (SQLException e) { |
| 602 |
// SQLite |
| 603 |
return treeIndex; |
| 604 |
} |
| 605 |
HashMap<String, IndexInfo> indexMap = new HashMap<>(); |
| 606 |
while (rs.next()) { |
| 607 |
String name = rs.getString("INDEX_NAME"); |
| 608 |
IndexInfo info = indexMap.get(name); |
| 609 |
if (info == null) { |
| 610 |
int t = rs.getInt("TYPE"); |
| 611 |
String type; |
| 612 |
if (t == DatabaseMetaData.tableIndexClustered) { |
| 613 |
type = ""; |
| 614 |
} else if (t == DatabaseMetaData.tableIndexHashed) { |
| 615 |
type = " (${text.tree.hashed})"; |
| 616 |
} else if (t == DatabaseMetaData.tableIndexOther) { |
| 617 |
type = ""; |
| 618 |
} else { |
| 619 |
type = null; |
| 620 |
} |
| 621 |
if (name != null && type != null) { |
| 622 |
info = new IndexInfo(); |
| 623 |
info.name = name; |
| 624 |
type = (rs.getBoolean("NON_UNIQUE") ? |
| 625 |
"${text.tree.nonUnique}" : "${text.tree.unique}") + type; |
| 626 |
info.type = type; |
| 627 |
info.columns = rs.getString("COLUMN_NAME"); |
| 628 |
indexMap.put(name, info); |
| 629 |
} |
| 630 |
} else { |
| 631 |
info.columns += ", " + rs.getString("COLUMN_NAME"); |
| 632 |
} |
| 633 |
} |
| 634 |
rs.close(); |
| 635 |
if (indexMap.size() > 0) { |
| 636 |
String level = mainSchema ? ", 1, 1" : ", 2, 1"; |
| 637 |
String levelIndex = mainSchema ? ", 2, 1" : ", 3, 1"; |
| 638 |
String levelColumnType = mainSchema ? ", 3, 2" : ", 4, 2"; |
| 639 |
buff.append("setNode(").append(treeIndex).append(level) |
| 640 |
.append(", 'index_az', '${text.tree.indexes}', null);\n"); |
| 641 |
treeIndex++; |
| 642 |
for (IndexInfo info : indexMap.values()) { |
| 643 |
buff.append("setNode(").append(treeIndex).append(levelIndex) |
| 644 |
.append(", 'index', '") |
| 645 |
.append(PageParser.escapeJavaScript(info.name)) |
| 646 |
.append("', null);\n"); |
| 647 |
treeIndex++; |
| 648 |
buff.append("setNode(").append(treeIndex).append(levelColumnType) |
| 649 |
.append(", 'type', '").append(info.type).append("', null);\n"); |
| 650 |
treeIndex++; |
| 651 |
buff.append("setNode(").append(treeIndex).append(levelColumnType) |
| 652 |
.append(", 'type', '") |
| 653 |
.append(PageParser.escapeJavaScript(info.columns)) |
| 654 |
.append("', null);\n"); |
| 655 |
treeIndex++; |
| 656 |
} |
| 657 |
} |
| 658 |
return treeIndex; |
| 659 |
} |
| 660 |
|
| 661 |
private int addTablesAndViews(DbSchema schema, boolean mainSchema, StringBuilder builder, int treeIndex) |
| 662 |
throws SQLException { |
| 663 |
if (schema == null) { |
| 664 |
return treeIndex; |
| 665 |
} |
| 666 |
Connection conn = session.getConnection(); |
| 667 |
DatabaseMetaData meta = session.getMetaData(); |
| 668 |
int level = mainSchema ? 0 : 1; |
| 669 |
boolean showColumns = mainSchema || !schema.isSystem; |
| 670 |
String indentation = ", " + level + ", " + (showColumns ? "1" : "2") + ", "; |
| 671 |
String indentNode = ", " + (level + 1) + ", 2, "; |
| 672 |
DbTableOrView[] tables = schema.getTables(); |
| 673 |
if (tables == null) { |
| 674 |
return treeIndex; |
| 675 |
} |
| 676 |
DbContents contents = schema.getContents(); |
| 677 |
boolean isOracle = contents.isOracle(); |
| 678 |
boolean notManyTables = tables.length < SysProperties.CONSOLE_MAX_TABLES_LIST_INDEXES; |
| 679 |
try (PreparedStatement prep = showColumns ? prepareViewDefinitionQuery(conn, contents) : null) { |
| 680 |
if (prep != null) { |
| 681 |
prep.setString(1, schema.name); |
| 682 |
} |
| 683 |
AtomicReference<PreparedStatement> prepRef = new AtomicReference<>(prep); |
| 684 |
if (schema.isSystem) { |
| 685 |
Arrays.sort(tables, SYSTEM_SCHEMA_COMPARATOR); |
| 686 |
for (DbTableOrView table : tables) { |
| 687 |
treeIndex = addTableOrView(schema, mainSchema, builder, treeIndex, meta, false, indentation, |
| 688 |
isOracle, notManyTables, table, table.isView(), prepRef, indentNode); |
| 689 |
} |
| 690 |
} else { |
| 691 |
for (DbTableOrView table : tables) { |
| 692 |
if (table.isView()) { |
| 693 |
continue; |
| 694 |
} |
| 695 |
treeIndex = addTableOrView(schema, mainSchema, builder, treeIndex, meta, showColumns, indentation, |
| 696 |
isOracle, notManyTables, table, false, null, indentNode); |
| 697 |
} |
| 698 |
for (DbTableOrView table : tables) { |
| 699 |
if (!table.isView()) { |
| 700 |
continue; |
| 701 |
} |
| 702 |
treeIndex = addTableOrView(schema, mainSchema, builder, treeIndex, meta, showColumns, indentation, |
| 703 |
isOracle, notManyTables, table, true, prepRef, indentNode); |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
return treeIndex; |
| 708 |
} |
| 709 |
|
| 710 |
private static PreparedStatement prepareViewDefinitionQuery(Connection conn, DbContents contents) { |
| 711 |
if (contents.mayHaveStandardViews()) { |
| 712 |
try { |
| 713 |
return conn.prepareStatement("SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS" |
| 714 |
+ " WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?"); |
| 715 |
} catch (SQLException e) { |
| 716 |
contents.setMayHaveStandardViews(false); |
| 717 |
} |
| 718 |
} |
| 719 |
return null; |
| 720 |
} |
| 721 |
|
| 722 |
private static int addTableOrView(DbSchema schema, boolean mainSchema, StringBuilder builder, int treeIndex, |
| 723 |
DatabaseMetaData meta, boolean showColumns, String indentation, boolean isOracle, boolean notManyTables, |
| 724 |
DbTableOrView table, boolean isView, AtomicReference<PreparedStatement> prepRef, String indentNode) |
| 725 |
throws SQLException { |
| 726 |
int tableId = treeIndex; |
| 727 |
String tab = table.getQuotedName(); |
| 728 |
if (!mainSchema) { |
| 729 |
tab = schema.quotedName + '.' + tab; |
| 730 |
} |
| 731 |
tab = escapeIdentifier(tab); |
| 732 |
builder.append("setNode(").append(treeIndex).append(indentation) |
| 733 |
.append(" '").append(isView ? "view" : "table").append("', '") |
| 734 |
.append(PageParser.escapeJavaScript(table.getName())) |
| 735 |
.append("', 'javascript:ins(\\'").append(tab).append("\\',true)');\n"); |
| 736 |
treeIndex++; |
| 737 |
if (showColumns) { |
| 738 |
StringBuilder columnsBuilder = new StringBuilder(); |
| 739 |
treeIndex = addColumns(mainSchema, table, builder, treeIndex, notManyTables, columnsBuilder); |
| 740 |
if (isView) { |
| 741 |
PreparedStatement prep = prepRef.get(); |
| 742 |
if (prep != null) { |
| 743 |
prep.setString(2, table.getName()); |
| 744 |
try (ResultSet rs = prep.executeQuery()) { |
| 745 |
if (rs.next()) { |
| 746 |
String sql = rs.getString(1); |
| 747 |
if (sql != null) { |
| 748 |
builder.append("setNode(").append(treeIndex).append(indentNode).append(" 'type', '") |
| 749 |
.append(PageParser.escapeJavaScript(sql)).append("', null);\n"); |
| 750 |
treeIndex++; |
| 751 |
} |
| 752 |
} |
| 753 |
} catch (SQLException e) { |
| 754 |
prepRef.set(null); |
| 755 |
} |
| 756 |
} |
| 757 |
} else if (!isOracle && notManyTables) { |
| 758 |
treeIndex = addIndexes(mainSchema, meta, table.getName(), schema.name, builder, treeIndex); |
| 759 |
} |
| 760 |
builder.append("addTable('") |
| 761 |
.append(PageParser.escapeJavaScript(table.getName())).append("', '") |
| 762 |
.append(PageParser.escapeJavaScript(columnsBuilder.toString())).append("', ") |
| 763 |
.append(tableId).append(");\n"); |
| 764 |
} |
| 765 |
return treeIndex; |
| 766 |
} |
| 767 |
|
| 768 |
private String tables() { |
| 769 |
DbContents contents = session.getContents(); |
| 770 |
boolean isH2 = false; |
| 771 |
try { |
| 772 |
String url = (String) session.get("url"); |
| 773 |
Connection conn = session.getConnection(); |
| 774 |
contents.readContents(url, conn); |
| 775 |
session.loadBnf(); |
| 776 |
isH2 = contents.isH2(); |
| 777 |
|
| 778 |
StringBuilder buff = new StringBuilder() |
| 779 |
.append("setNode(0, 0, 0, 'database', '") |
| 780 |
.append(PageParser.escapeJavaScript(url)) |
| 781 |
.append("', null);\n"); |
| 782 |
int treeIndex = 1; |
| 783 |
|
| 784 |
DbSchema defaultSchema = contents.getDefaultSchema(); |
| 785 |
treeIndex = addTablesAndViews(defaultSchema, true, buff, treeIndex); |
| 786 |
DbSchema[] schemas = contents.getSchemas(); |
| 787 |
for (DbSchema schema : schemas) { |
| 788 |
if (schema == defaultSchema || schema == null) { |
| 789 |
continue; |
| 790 |
} |
| 791 |
buff.append("setNode(").append(treeIndex).append(", 0, 1, 'folder', '") |
| 792 |
.append(PageParser.escapeJavaScript(schema.name)) |
| 793 |
.append("', null);\n"); |
| 794 |
treeIndex++; |
| 795 |
treeIndex = addTablesAndViews(schema, false, buff, treeIndex); |
| 796 |
} |
| 797 |
if (isH2) { |
| 798 |
try (Statement stat = conn.createStatement()) { |
| 799 |
ResultSet rs; |
| 800 |
try { |
| 801 |
rs = stat.executeQuery("SELECT SEQUENCE_NAME, BASE_VALUE, INCREMENT FROM " + |
| 802 |
"INFORMATION_SCHEMA.SEQUENCES ORDER BY SEQUENCE_NAME"); |
| 803 |
} catch (SQLException e) { |
| 804 |
rs = stat.executeQuery("SELECT SEQUENCE_NAME, CURRENT_VALUE, INCREMENT FROM " + |
| 805 |
"INFORMATION_SCHEMA.SEQUENCES ORDER BY SEQUENCE_NAME"); |
| 806 |
} |
| 807 |
for (int i = 0; rs.next(); i++) { |
| 808 |
if (i == 0) { |
| 809 |
buff.append("setNode(").append(treeIndex) |
| 810 |
.append(", 0, 1, 'sequences', '${text.tree.sequences}', null);\n"); |
| 811 |
treeIndex++; |
| 812 |
} |
| 813 |
String name = rs.getString(1); |
| 814 |
String currentBase = rs.getString(2); |
| 815 |
String increment = rs.getString(3); |
| 816 |
buff.append("setNode(").append(treeIndex) |
| 817 |
.append(", 1, 1, 'sequence', '") |
| 818 |
.append(PageParser.escapeJavaScript(name)) |
| 819 |
.append("', null);\n"); |
| 820 |
treeIndex++; |
| 821 |
buff.append("setNode(").append(treeIndex) |
| 822 |
.append(", 2, 2, 'type', '${text.tree.current}: ") |
| 823 |
.append(PageParser.escapeJavaScript(currentBase)) |
| 824 |
.append("', null);\n"); |
| 825 |
treeIndex++; |
| 826 |
if (!"1".equals(increment)) { |
| 827 |
buff.append("setNode(").append(treeIndex) |
| 828 |
.append(", 2, 2, 'type', '${text.tree.increment}: ") |
| 829 |
.append(PageParser.escapeJavaScript(increment)) |
| 830 |
.append("', null);\n"); |
| 831 |
treeIndex++; |
| 832 |
} |
| 833 |
} |
| 834 |
rs.close(); |
| 835 |
try { |
| 836 |
rs = stat.executeQuery( |
| 837 |
"SELECT USER_NAME, IS_ADMIN FROM INFORMATION_SCHEMA.USERS ORDER BY USER_NAME"); |
| 838 |
} catch (SQLException e) { |
| 839 |
rs = stat.executeQuery("SELECT NAME, ADMIN FROM INFORMATION_SCHEMA.USERS ORDER BY NAME"); |
| 840 |
} |
| 841 |
for (int i = 0; rs.next(); i++) { |
| 842 |
if (i == 0) { |
| 843 |
buff.append("setNode(").append(treeIndex) |
| 844 |
.append(", 0, 1, 'users', '${text.tree.users}', null);\n"); |
| 845 |
treeIndex++; |
| 846 |
} |
| 847 |
String name = rs.getString(1); |
| 848 |
String admin = rs.getString(2); |
| 849 |
buff.append("setNode(").append(treeIndex) |
| 850 |
.append(", 1, 1, 'user', '") |
| 851 |
.append(PageParser.escapeJavaScript(name)) |
| 852 |
.append("', null);\n"); |
| 853 |
treeIndex++; |
| 854 |
if (admin.equalsIgnoreCase("TRUE")) { |
| 855 |
buff.append("setNode(").append(treeIndex) |
| 856 |
.append(", 2, 2, 'type', '${text.tree.admin}', null);\n"); |
| 857 |
treeIndex++; |
| 858 |
} |
| 859 |
} |
| 860 |
rs.close(); |
| 861 |
} |
| 862 |
} |
| 863 |
DatabaseMetaData meta = session.getMetaData(); |
| 864 |
String version = meta.getDatabaseProductName() + " " + |
| 865 |
meta.getDatabaseProductVersion(); |
| 866 |
buff.append("setNode(").append(treeIndex) |
| 867 |
.append(", 0, 0, 'info', '") |
| 868 |
.append(PageParser.escapeJavaScript(version)) |
| 869 |
.append("', null);\n") |
| 870 |
.append("refreshQueryTables();"); |
| 871 |
session.put("tree", buff.toString()); |
| 872 |
} catch (Exception e) { |
| 873 |
session.put("tree", ""); |
| 874 |
session.put("error", getStackTrace(0, e, isH2)); |
| 875 |
} |
| 876 |
return "tables.jsp"; |
| 877 |
} |
| 878 |
|
| 879 |
private String getStackTrace(int id, Throwable e, boolean isH2) { |
| 880 |
try { |
| 881 |
StringWriter writer = new StringWriter(); |
| 882 |
e.printStackTrace(new PrintWriter(writer)); |
| 883 |
String stackTrace = writer.toString(); |
| 884 |
stackTrace = PageParser.escapeHtml(stackTrace); |
| 885 |
if (isH2) { |
| 886 |
stackTrace = linkToSource(stackTrace); |
| 887 |
} |
| 888 |
stackTrace = StringUtils.replaceAll(stackTrace, "\t", |
| 889 |
" "); |
| 890 |
String message = PageParser.escapeHtml(e.getMessage()); |
| 891 |
String error = "<a class=\"error\" href=\"#\" " + |
| 892 |
"onclick=\"var x=document.getElementById('st" + id + |
| 893 |
"').style;x.display=x.display==''?'none':'';\">" + message + |
| 894 |
"</a>"; |
| 895 |
if (e instanceof SQLException) { |
| 896 |
SQLException se = (SQLException) e; |
| 897 |
error += " " + se.getSQLState() + "/" + se.getErrorCode(); |
| 898 |
if (isH2) { |
| 899 |
int code = se.getErrorCode(); |
| 900 |
error += " <a href=\"https://h2database.com/javadoc/" + |
| 901 |
"org/h2/api/ErrorCode.html#c" + code + |
| 902 |
"\">(${text.a.help})</a>"; |
| 903 |
} |
| 904 |
} |
| 905 |
error += "<span style=\"display: none;\" id=\"st" + id + |
| 906 |
"\"><br />" + stackTrace + "</span>"; |
| 907 |
error = formatAsError(error); |
| 908 |
return error; |
| 909 |
} catch (OutOfMemoryError e2) { |
| 910 |
server.traceError(e); |
| 911 |
return e.toString(); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
private static String linkToSource(String s) { |
| 916 |
try { |
| 917 |
StringBuilder result = new StringBuilder(s.length()); |
| 918 |
int idx = s.indexOf("<br />"); |
| 919 |
result.append(s, 0, idx); |
| 920 |
while (true) { |
| 921 |
int start = s.indexOf("org.h2.", idx); |
| 922 |
if (start < 0) { |
| 923 |
result.append(s.substring(idx)); |
| 924 |
break; |
| 925 |
} |
| 926 |
result.append(s, idx, start); |
| 927 |
int end = s.indexOf(')', start); |
| 928 |
if (end < 0) { |
| 929 |
result.append(s.substring(idx)); |
| 930 |
break; |
| 931 |
} |
| 932 |
String element = s.substring(start, end); |
| 933 |
int open = element.lastIndexOf('('); |
| 934 |
int dotMethod = element.lastIndexOf('.', open - 1); |
| 935 |
int dotClass = element.lastIndexOf('.', dotMethod - 1); |
| 936 |
String packageName = element.substring(0, dotClass); |
| 937 |
int colon = element.lastIndexOf(':'); |
| 938 |
String file = element.substring(open + 1, colon); |
| 939 |
String lineNumber = element.substring(colon + 1, element.length()); |
| 940 |
String fullFileName = packageName.replace('.', '/') + "/" + file; |
| 941 |
result.append("<a href=\"https://h2database.com/html/source.html?file="); |
| 942 |
result.append(fullFileName); |
| 943 |
result.append("&line="); |
| 944 |
result.append(lineNumber); |
| 945 |
result.append("&build="); |
| 946 |
result.append(Constants.BUILD_ID); |
| 947 |
result.append("\">"); |
| 948 |
result.append(element); |
| 949 |
result.append("</a>"); |
| 950 |
idx = end; |
| 951 |
} |
| 952 |
return result.toString(); |
| 953 |
} catch (Throwable t) { |
| 954 |
return s; |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
private static String formatAsError(String s) { |
| 959 |
return "<div class=\"error\">" + s + "</div>"; |
| 960 |
} |
| 961 |
|
| 962 |
private String test(NetworkConnectionInfo networkConnectionInfo) { |
| 963 |
String driver = attributes.getProperty("driver", ""); |
| 964 |
String url = attributes.getProperty("url", ""); |
| 965 |
String user = attributes.getProperty("user", ""); |
| 966 |
String password = attributes.getProperty("password", ""); |
| 967 |
session.put("driver", driver); |
| 968 |
session.put("url", url); |
| 969 |
session.put("user", user); |
| 970 |
boolean isH2 = url.startsWith("jdbc:h2:"); |
| 971 |
try { |
| 972 |
long start = System.currentTimeMillis(); |
| 973 |
String profOpen = "", profClose = ""; |
| 974 |
Profiler prof = new Profiler(); |
| 975 |
prof.startCollecting(); |
| 976 |
Connection conn; |
| 977 |
try { |
| 978 |
conn = server.getConnection(driver, url, user, password, null, networkConnectionInfo); |
| 979 |
} finally { |
| 980 |
prof.stopCollecting(); |
| 981 |
profOpen = prof.getTop(3); |
| 982 |
} |
| 983 |
prof = new Profiler(); |
| 984 |
prof.startCollecting(); |
| 985 |
try { |
| 986 |
JdbcUtils.closeSilently(conn); |
| 987 |
} finally { |
| 988 |
prof.stopCollecting(); |
| 989 |
profClose = prof.getTop(3); |
| 990 |
} |
| 991 |
long time = System.currentTimeMillis() - start; |
| 992 |
String success; |
| 993 |
if (time > 1000) { |
| 994 |
success = "<a class=\"error\" href=\"#\" " + |
| 995 |
"onclick=\"var x=document.getElementById('prof').style;x." + |
| 996 |
"display=x.display==''?'none':'';\">" + |
| 997 |
"${text.login.testSuccessful}</a>" + |
| 998 |
"<span style=\"display: none;\" id=\"prof\"><br />" + |
| 999 |
PageParser.escapeHtml(profOpen) + |
| 1000 |
"<br />" + |
| 1001 |
PageParser.escapeHtml(profClose) + |
| 1002 |
"</span>"; |
| 1003 |
} else { |
| 1004 |
success = "<div class=\"success\">${text.login.testSuccessful}</div>"; |
| 1005 |
} |
| 1006 |
session.put("error", success); |
| 1007 |
// session.put("error", "${text.login.testSuccessful}"); |
| 1008 |
return "login.jsp"; |
| 1009 |
} catch (Exception e) { |
| 1010 |
session.put("error", getLoginError(e, isH2)); |
| 1011 |
return "login.jsp"; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** |
| 1016 |
* Get the formatted login error message. |
| 1017 |
* |
| 1018 |
* @param e the exception |
| 1019 |
* @param isH2 if the current database is a H2 database |
| 1020 |
* @return the formatted error message |
| 1021 |
*/ |
| 1022 |
private String getLoginError(Exception e, boolean isH2) { |
| 1023 |
if (e instanceof JdbcException && ((JdbcException) e).getErrorCode() == ErrorCode.CLASS_NOT_FOUND_1) { |
| 1024 |
return "${text.login.driverNotFound}<br />" + getStackTrace(0, e, isH2); |
| 1025 |
} |
| 1026 |
return getStackTrace(0, e, isH2); |
| 1027 |
} |
| 1028 |
|
| 1029 |
private String login(NetworkConnectionInfo networkConnectionInfo) { |
| 1030 |
String driver = attributes.getProperty("driver", ""); |
| 1031 |
String url = attributes.getProperty("url", ""); |
| 1032 |
String user = attributes.getProperty("user", ""); |
| 1033 |
String password = attributes.getProperty("password", ""); |
| 1034 |
session.put("autoCommit", "checked"); |
| 1035 |
session.put("autoComplete", "1"); |
| 1036 |
session.put("maxrows", "1000"); |
| 1037 |
boolean isH2 = url.startsWith("jdbc:h2:"); |
| 1038 |
try { |
| 1039 |
Connection conn = server.getConnection(driver, url, user, password, (String) session.get("key"), |
| 1040 |
networkConnectionInfo); |
| 1041 |
session.setConnection(conn); |
| 1042 |
session.put("url", url); |
| 1043 |
session.put("user", user); |
| 1044 |
session.remove("error"); |
| 1045 |
settingSave(); |
| 1046 |
return "frame.jsp"; |
| 1047 |
} catch (Exception e) { |
| 1048 |
session.put("error", getLoginError(e, isH2)); |
| 1049 |
return "login.jsp"; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
private String logout() { |
| 1054 |
try { |
| 1055 |
Connection conn = session.getConnection(); |
| 1056 |
session.setConnection(null); |
| 1057 |
session.remove("conn"); |
| 1058 |
session.remove("result"); |
| 1059 |
session.remove("tables"); |
| 1060 |
session.remove("user"); |
| 1061 |
session.remove("tool"); |
| 1062 |
if (conn != null) { |
| 1063 |
if (session.getShutdownServerOnDisconnect()) { |
| 1064 |
server.shutdown(); |
| 1065 |
} else { |
| 1066 |
conn.close(); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} catch (Exception e) { |
| 1070 |
trace(e.toString()); |
| 1071 |
} |
| 1072 |
session.remove("admin"); |
| 1073 |
return "index.do"; |
| 1074 |
} |
| 1075 |
|
| 1076 |
private String query() { |
| 1077 |
String sql = attributes.getProperty("sql").trim(); |
| 1078 |
try { |
| 1079 |
ScriptReader r = new ScriptReader(new StringReader(sql)); |
| 1080 |
final ArrayList<String> list = new ArrayList<>(); |
| 1081 |
while (true) { |
| 1082 |
String s = r.readStatement(); |
| 1083 |
if (s == null) { |
| 1084 |
break; |
| 1085 |
} |
| 1086 |
list.add(s); |
| 1087 |
} |
| 1088 |
final Connection conn = session.getConnection(); |
| 1089 |
if (SysProperties.CONSOLE_STREAM && server.getAllowChunked()) { |
| 1090 |
String page = new String(server.getFile("result.jsp"), StandardCharsets.UTF_8); |
| 1091 |
int idx = page.indexOf("${result}"); |
| 1092 |
// the first element of the list is the header, the last the |
| 1093 |
// footer |
| 1094 |
list.add(0, page.substring(0, idx)); |
| 1095 |
list.add(page.substring(idx + "${result}".length())); |
| 1096 |
session.put("chunks", new Iterator<String>() { |
| 1097 |
private int i; |
| 1098 |
@Override |
| 1099 |
public boolean hasNext() { |
| 1100 |
return i < list.size(); |
| 1101 |
} |
| 1102 |
@Override |
| 1103 |
public String next() { |
| 1104 |
String s = list.get(i++); |
| 1105 |
if (i == 1 || i == list.size()) { |
| 1106 |
return s; |
| 1107 |
} |
| 1108 |
StringBuilder b = new StringBuilder(); |
| 1109 |
query(conn, s, i - 1, list.size() - 2, b); |
| 1110 |
return b.toString(); |
| 1111 |
} |
| 1112 |
}); |
| 1113 |
return "result.jsp"; |
| 1114 |
} |
| 1115 |
String result; |
| 1116 |
StringBuilder buff = new StringBuilder(); |
| 1117 |
for (int i = 0; i < list.size(); i++) { |
| 1118 |
String s = list.get(i); |
| 1119 |
query(conn, s, i, list.size(), buff); |
| 1120 |
} |
| 1121 |
result = buff.toString(); |
| 1122 |
session.put("result", result); |
| 1123 |
} catch (Throwable e) { |
| 1124 |
session.put("result", getStackTrace(0, e, session.getContents().isH2())); |
| 1125 |
} |
| 1126 |
return "result.jsp"; |
| 1127 |
} |
| 1128 |
|
| 1129 |
/** |
| 1130 |
* Execute a query and append the result to the buffer. |
| 1131 |
* |
| 1132 |
* @param conn the connection |
| 1133 |
* @param s the statement |
| 1134 |
* @param i the index |
| 1135 |
* @param size the number of statements |
| 1136 |
* @param buff the target buffer |
| 1137 |
*/ |
| 1138 |
void query(Connection conn, String s, int i, int size, StringBuilder buff) { |
| 1139 |
if (!(s.startsWith("@") && s.endsWith("."))) { |
| 1140 |
buff.append(PageParser.escapeHtml(s + ";")).append("<br />"); |
| 1141 |
} |
| 1142 |
boolean forceEdit = s.startsWith("@edit"); |
| 1143 |
buff.append(getResult(conn, i + 1, s, size == 1, forceEdit)). |
| 1144 |
append("<br />"); |
| 1145 |
} |
| 1146 |
|
| 1147 |
private String editResult() { |
| 1148 |
ResultSet rs = session.result; |
| 1149 |
int row = Integer.parseInt(attributes.getProperty("row")); |
| 1150 |
int op = Integer.parseInt(attributes.getProperty("op")); |
| 1151 |
String result = "", error = ""; |
| 1152 |
try { |
| 1153 |
if (op == 1) { |
| 1154 |
boolean insert = row < 0; |
| 1155 |
if (insert) { |
| 1156 |
rs.moveToInsertRow(); |
| 1157 |
} else { |
| 1158 |
rs.absolute(row); |
| 1159 |
} |
| 1160 |
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { |
| 1161 |
String x = attributes.getProperty("r" + row + "c" + (i + 1)); |
| 1162 |
unescapeData(x, rs, i + 1); |
| 1163 |
} |
| 1164 |
if (insert) { |
| 1165 |
rs.insertRow(); |
| 1166 |
} else { |
| 1167 |
rs.updateRow(); |
| 1168 |
} |
| 1169 |
} else if (op == 2) { |
| 1170 |
rs.absolute(row); |
| 1171 |
rs.deleteRow(); |
| 1172 |
} else if (op == 3) { |
| 1173 |
// cancel |
| 1174 |
} |
| 1175 |
} catch (Throwable e) { |
| 1176 |
result = "<br />" + getStackTrace(0, e, session.getContents().isH2()); |
| 1177 |
error = formatAsError(e.getMessage()); |
| 1178 |
} |
| 1179 |
String sql = "@edit " + (String) session.get("resultSetSQL"); |
| 1180 |
Connection conn = session.getConnection(); |
| 1181 |
result = error + getResult(conn, -1, sql, true, true) + result; |
| 1182 |
session.put("result", result); |
| 1183 |
return "result.jsp"; |
| 1184 |
} |
| 1185 |
|
| 1186 |
private int getMaxrows() { |
| 1187 |
String r = (String) session.get("maxrows"); |
| 1188 |
return r == null ? 0 : Integer.parseInt(r); |
| 1189 |
} |
| 1190 |
|
| 1191 |
private String getResult(Connection conn, int id, String sql, |
| 1192 |
boolean allowEdit, boolean forceEdit) { |
| 1193 |
try { |
| 1194 |
sql = sql.trim(); |
| 1195 |
StringBuilder buff = new StringBuilder(); |
| 1196 |
String sqlUpper = StringUtils.toUpperEnglish(sql); |
| 1197 |
if (sqlUpper.contains("CREATE") || |
| 1198 |
sqlUpper.contains("DROP") || |
| 1199 |
sqlUpper.contains("ALTER") || |
| 1200 |
sqlUpper.contains("RUNSCRIPT")) { |
| 1201 |
String sessionId = attributes.getProperty("jsessionid"); |
| 1202 |
buff.append("<script type=\"text/javascript\">parent['h2menu'].location='tables.do?jsessionid=") |
| 1203 |
.append(sessionId).append("';</script>"); |
| 1204 |
} |
| 1205 |
Statement stat; |
| 1206 |
DbContents contents = session.getContents(); |
| 1207 |
if (forceEdit || (allowEdit && contents.isH2())) { |
| 1208 |
stat = conn.createStatement( |
| 1209 |
ResultSet.TYPE_SCROLL_INSENSITIVE, |
| 1210 |
ResultSet.CONCUR_UPDATABLE); |
| 1211 |
} else { |
| 1212 |
stat = conn.createStatement(); |
| 1213 |
} |
| 1214 |
ResultSet rs; |
| 1215 |
long time = System.currentTimeMillis(); |
| 1216 |
boolean metadata = false; |
| 1217 |
Object generatedKeys = null; |
| 1218 |
boolean edit = false; |
| 1219 |
boolean list = false; |
| 1220 |
if (JdbcUtils.isBuiltIn(sql, "@autocommit_true")) { |
| 1221 |
conn.setAutoCommit(true); |
| 1222 |
return "${text.result.autoCommitOn}"; |
| 1223 |
} else if (JdbcUtils.isBuiltIn(sql, "@autocommit_false")) { |
| 1224 |
conn.setAutoCommit(false); |
| 1225 |
return "${text.result.autoCommitOff}"; |
| 1226 |
} else if (JdbcUtils.isBuiltIn(sql, "@cancel")) { |
| 1227 |
stat = session.executingStatement; |
| 1228 |
if (stat != null) { |
| 1229 |
stat.cancel(); |
| 1230 |
buff.append("${text.result.statementWasCanceled}"); |
| 1231 |
} else { |
| 1232 |
buff.append("${text.result.noRunningStatement}"); |
| 1233 |
} |
| 1234 |
return buff.toString(); |
| 1235 |
} else if (JdbcUtils.isBuiltIn(sql, "@edit")) { |
| 1236 |
edit = true; |
| 1237 |
sql = StringUtils.trimSubstring(sql, "@edit".length()); |
| 1238 |
session.put("resultSetSQL", sql); |
| 1239 |
} |
| 1240 |
if (JdbcUtils.isBuiltIn(sql, "@list")) { |
| 1241 |
list = true; |
| 1242 |
sql = StringUtils.trimSubstring(sql, "@list".length()); |
| 1243 |
} |
| 1244 |
if (JdbcUtils.isBuiltIn(sql, "@meta")) { |
| 1245 |
metadata = true; |
| 1246 |
sql = StringUtils.trimSubstring(sql, "@meta".length()); |
| 1247 |
} |
| 1248 |
if (JdbcUtils.isBuiltIn(sql, "@generated")) { |
| 1249 |
generatedKeys = true; |
| 1250 |
int offset = "@generated".length(); |
| 1251 |
int length = sql.length(); |
| 1252 |
for (; offset < length; offset++) { |
| 1253 |
char c = sql.charAt(offset); |
| 1254 |
if (c == '(') { |
| 1255 |
Parser p = new Parser(); |
| 1256 |
generatedKeys = p.parseColumnList(sql, offset); |
| 1257 |
offset = p.getLastParseIndex(); |
| 1258 |
break; |
| 1259 |
} |
| 1260 |
if (!Character.isWhitespace(c)) { |
| 1261 |
break; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
sql = StringUtils.trimSubstring(sql, offset); |
| 1265 |
} else if (JdbcUtils.isBuiltIn(sql, "@history")) { |
| 1266 |
buff.append(getCommandHistoryString()); |
| 1267 |
return buff.toString(); |
| 1268 |
} else if (JdbcUtils.isBuiltIn(sql, "@loop")) { |
| 1269 |
sql = StringUtils.trimSubstring(sql, "@loop".length()); |
| 1270 |
int idx = sql.indexOf(' '); |
| 1271 |
int count = Integer.decode(sql.substring(0, idx)); |
| 1272 |
sql = StringUtils.trimSubstring(sql, idx); |
| 1273 |
return executeLoop(conn, count, sql); |
| 1274 |
} else if (JdbcUtils.isBuiltIn(sql, "@maxrows")) { |
| 1275 |
int maxrows = (int) Double.parseDouble(StringUtils.trimSubstring(sql, "@maxrows".length())); |
| 1276 |
session.put("maxrows", Integer.toString(maxrows)); |
| 1277 |
return "${text.result.maxrowsSet}"; |
| 1278 |
} else if (JdbcUtils.isBuiltIn(sql, "@parameter_meta")) { |
| 1279 |
sql = StringUtils.trimSubstring(sql, "@parameter_meta".length()); |
| 1280 |
PreparedStatement prep = conn.prepareStatement(sql); |
| 1281 |
buff.append(getParameterResultSet(prep.getParameterMetaData())); |
| 1282 |
return buff.toString(); |
| 1283 |
} else if (JdbcUtils.isBuiltIn(sql, "@password_hash")) { |
| 1284 |
sql = StringUtils.trimSubstring(sql, "@password_hash".length()); |
| 1285 |
String[] p = JdbcUtils.split(sql); |
| 1286 |
return StringUtils.convertBytesToHex( |
| 1287 |
SHA256.getKeyPasswordHash(p[0], p[1].toCharArray())); |
| 1288 |
} else if (JdbcUtils.isBuiltIn(sql, "@prof_start")) { |
| 1289 |
if (profiler != null) { |
| 1290 |
profiler.stopCollecting(); |
| 1291 |
} |
| 1292 |
profiler = new Profiler(); |
| 1293 |
profiler.startCollecting(); |
| 1294 |
return "Ok"; |
| 1295 |
} else if (JdbcUtils.isBuiltIn(sql, "@sleep")) { |
| 1296 |
String s = StringUtils.trimSubstring(sql, "@sleep".length()); |
| 1297 |
int sleep = 1; |
| 1298 |
if (s.length() > 0) { |
| 1299 |
sleep = Integer.parseInt(s); |
| 1300 |
} |
| 1301 |
Thread.sleep(sleep * 1000); |
| 1302 |
return "Ok"; |
| 1303 |
} else if (JdbcUtils.isBuiltIn(sql, "@transaction_isolation")) { |
| 1304 |
String s = StringUtils.trimSubstring(sql, "@transaction_isolation".length()); |
| 1305 |
if (s.length() > 0) { |
| 1306 |
int level = Integer.parseInt(s); |
| 1307 |
conn.setTransactionIsolation(level); |
| 1308 |
} |
| 1309 |
buff.append("Transaction Isolation: ") |
| 1310 |
.append(conn.getTransactionIsolation()) |
| 1311 |
.append("<br />"); |
| 1312 |
buff.append(Connection.TRANSACTION_READ_UNCOMMITTED) |
| 1313 |
.append(": read_uncommitted<br />"); |
| 1314 |
buff.append(Connection.TRANSACTION_READ_COMMITTED) |
| 1315 |
.append(": read_committed<br />"); |
| 1316 |
buff.append(Connection.TRANSACTION_REPEATABLE_READ) |
| 1317 |
.append(": repeatable_read<br />"); |
| 1318 |
buff.append(Constants.TRANSACTION_SNAPSHOT) |
| 1319 |
.append(": snapshot<br />"); |
| 1320 |
buff.append(Connection.TRANSACTION_SERIALIZABLE) |
| 1321 |
.append(": serializable"); |
| 1322 |
} |
| 1323 |
if (sql.startsWith("@")) { |
| 1324 |
rs = JdbcUtils.getMetaResultSet(conn, sql); |
| 1325 |
if (rs == null && JdbcUtils.isBuiltIn(sql, "@prof_stop")) { |
| 1326 |
if (profiler != null) { |
| 1327 |
profiler.stopCollecting(); |
| 1328 |
SimpleResultSet simple = new SimpleResultSet(); |
| 1329 |
simple.addColumn("Top Stack Trace(s)", Types.VARCHAR, 0, 0); |
| 1330 |
simple.addRow(profiler.getTop(3)); |
| 1331 |
rs = simple; |
| 1332 |
profiler = null; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
if (rs == null) { |
| 1336 |
buff.append("?: ").append(sql); |
| 1337 |
return buff.toString(); |
| 1338 |
} |
| 1339 |
} else { |
| 1340 |
int maxrows = getMaxrows(); |
| 1341 |
stat.setMaxRows(maxrows); |
| 1342 |
session.executingStatement = stat; |
| 1343 |
boolean isResultSet; |
| 1344 |
if (generatedKeys == null) { |
| 1345 |
isResultSet = stat.execute(sql); |
| 1346 |
} else if (generatedKeys instanceof Boolean) { |
| 1347 |
isResultSet = stat.execute(sql, |
| 1348 |
((Boolean) generatedKeys) ? Statement.RETURN_GENERATED_KEYS : Statement.NO_GENERATED_KEYS); |
| 1349 |
} else if (generatedKeys instanceof String[]) { |
| 1350 |
isResultSet = stat.execute(sql, (String[]) generatedKeys); |
| 1351 |
} else { |
| 1352 |
isResultSet = stat.execute(sql, (int[]) generatedKeys); |
| 1353 |
} |
| 1354 |
session.addCommand(sql); |
| 1355 |
if (generatedKeys != null) { |
| 1356 |
rs = null; |
| 1357 |
rs = stat.getGeneratedKeys(); |
| 1358 |
} else { |
| 1359 |
if (!isResultSet) { |
| 1360 |
long updateCount; |
| 1361 |
try { |
| 1362 |
updateCount = stat.getLargeUpdateCount(); |
| 1363 |
} catch (UnsupportedOperationException e) { |
| 1364 |
updateCount = stat.getUpdateCount(); |
| 1365 |
} |
| 1366 |
buff.append("${text.result.updateCount}: ").append(updateCount); |
| 1367 |
time = System.currentTimeMillis() - time; |
| 1368 |
buff.append("<br />(").append(time).append(" ms)"); |
| 1369 |
stat.close(); |
| 1370 |
return buff.toString(); |
| 1371 |
} |
| 1372 |
rs = stat.getResultSet(); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
time = System.currentTimeMillis() - time; |
| 1376 |
buff.append(getResultSet(sql, rs, metadata, list, edit, time, allowEdit)); |
| 1377 |
// SQLWarning warning = stat.getWarnings(); |
| 1378 |
// if (warning != null) { |
| 1379 |
// buff.append("<br />Warning:<br />"). |
| 1380 |
// append(getStackTrace(id, warning)); |
| 1381 |
// } |
| 1382 |
if (!edit) { |
| 1383 |
stat.close(); |
| 1384 |
} |
| 1385 |
return buff.toString(); |
| 1386 |
} catch (Throwable e) { |
| 1387 |
// throwable: including OutOfMemoryError and so on |
| 1388 |
return getStackTrace(id, e, session.getContents().isH2()); |
| 1389 |
} finally { |
| 1390 |
session.executingStatement = null; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
private String executeLoop(Connection conn, int count, String sql) |
| 1395 |
throws SQLException { |
| 1396 |
ArrayList<Integer> params = new ArrayList<>(); |
| 1397 |
int idx = 0; |
| 1398 |
while (!stop) { |
| 1399 |
idx = sql.indexOf('?', idx); |
| 1400 |
if (idx < 0) { |
| 1401 |
break; |
| 1402 |
} |
| 1403 |
if (JdbcUtils.isBuiltIn(sql.substring(idx), "?/*rnd*/")) { |
| 1404 |
params.add(1); |
| 1405 |
sql = sql.substring(0, idx) + "?" + sql.substring(idx + "/*rnd*/".length() + 1); |
| 1406 |
} else { |
| 1407 |
params.add(0); |
| 1408 |
} |
| 1409 |
idx++; |
| 1410 |
} |
| 1411 |
boolean prepared; |
| 1412 |
Random random = new Random(1); |
| 1413 |
long time = System.currentTimeMillis(); |
| 1414 |
if (JdbcUtils.isBuiltIn(sql, "@statement")) { |
| 1415 |
sql = StringUtils.trimSubstring(sql, "@statement".length()); |
| 1416 |
prepared = false; |
| 1417 |
Statement stat = conn.createStatement(); |
| 1418 |
for (int i = 0; !stop && i < count; i++) { |
| 1419 |
String s = sql; |
| 1420 |
for (Integer type : params) { |
| 1421 |
idx = s.indexOf('?'); |
| 1422 |
if (type == 1) { |
| 1423 |
s = s.substring(0, idx) + random.nextInt(count) + s.substring(idx + 1); |
| 1424 |
} else { |
| 1425 |
s = s.substring(0, idx) + i + s.substring(idx + 1); |
| 1426 |
} |
| 1427 |
} |
| 1428 |
if (stat.execute(s)) { |
| 1429 |
ResultSet rs = stat.getResultSet(); |
| 1430 |
while (!stop && rs.next()) { |
| 1431 |
// maybe get the data as well |
| 1432 |
} |
| 1433 |
rs.close(); |
| 1434 |
} |
| 1435 |
} |
| 1436 |
} else { |
| 1437 |
prepared = true; |
| 1438 |
PreparedStatement prep = conn.prepareStatement(sql); |
| 1439 |
for (int i = 0; !stop && i < count; i++) { |
| 1440 |
for (int j = 0; j < params.size(); j++) { |
| 1441 |
Integer type = params.get(j); |
| 1442 |
if (type == 1) { |
| 1443 |
prep.setInt(j + 1, random.nextInt(count)); |
| 1444 |
} else { |
| 1445 |
prep.setInt(j + 1, i); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
if (session.getContents().isSQLite()) { |
| 1449 |
// SQLite currently throws an exception on prep.execute() |
| 1450 |
prep.executeUpdate(); |
| 1451 |
} else { |
| 1452 |
if (prep.execute()) { |
| 1453 |
ResultSet rs = prep.getResultSet(); |
| 1454 |
while (!stop && rs.next()) { |
| 1455 |
// maybe get the data as well |
| 1456 |
} |
| 1457 |
rs.close(); |
| 1458 |
} |
| 1459 |
} |
| 1460 |
} |
| 1461 |
} |
| 1462 |
time = System.currentTimeMillis() - time; |
| 1463 |
StringBuilder builder = new StringBuilder().append(time).append(" ms: ").append(count).append(" * ") |
| 1464 |
.append(prepared ? "(Prepared) " : "(Statement) ").append('('); |
| 1465 |
for (int i = 0, size = params.size(); i < size; i++) { |
| 1466 |
if (i > 0) { |
| 1467 |
builder.append(", "); |
| 1468 |
} |
| 1469 |
builder.append(params.get(i) == 0 ? "i" : "rnd"); |
| 1470 |
} |
| 1471 |
return builder.append(") ").append(sql).toString(); |
| 1472 |
} |
| 1473 |
|
| 1474 |
private String getCommandHistoryString() { |
| 1475 |
StringBuilder buff = new StringBuilder(); |
| 1476 |
ArrayList<String> history = session.getCommandHistory(); |
| 1477 |
buff.append("<table cellspacing=0 cellpadding=0>" + |
| 1478 |
"<tr><th></th><th>Command</th></tr>"); |
| 1479 |
for (int i = history.size() - 1; i >= 0; i--) { |
| 1480 |
String sql = history.get(i); |
| 1481 |
buff.append("<tr><td><a href=\"getHistory.do?id="). |
| 1482 |
append(i). |
| 1483 |
append("&jsessionid=${sessionId}\" target=\"h2query\" >"). |
| 1484 |
append("<img width=16 height=16 src=\"ico_write.gif\" " + |
| 1485 |
"onmouseover = \"this.className ='icon_hover'\" "). |
| 1486 |
append("onmouseout = \"this.className ='icon'\" " + |
| 1487 |
"class=\"icon\" alt=\"${text.resultEdit.edit}\" "). |
| 1488 |
append("title=\"${text.resultEdit.edit}\" border=\"1\"/></a>"). |
| 1489 |
append("</td><td>"). |
| 1490 |
append(PageParser.escapeHtml(sql)). |
| 1491 |
append("</td></tr>"); |
| 1492 |
} |
| 1493 |
buff.append("</table>"); |
| 1494 |
return buff.toString(); |
| 1495 |
} |
| 1496 |
|
| 1497 |
private static String getParameterResultSet(ParameterMetaData meta) |
| 1498 |
throws SQLException { |
| 1499 |
StringBuilder buff = new StringBuilder(); |
| 1500 |
if (meta == null) { |
| 1501 |
return "No parameter meta data"; |
| 1502 |
} |
| 1503 |
buff.append("<table cellspacing=0 cellpadding=0>"). |
| 1504 |
append("<tr><th>className</th><th>mode</th><th>type</th>"). |
| 1505 |
append("<th>typeName</th><th>precision</th><th>scale</th></tr>"); |
| 1506 |
for (int i = 0; i < meta.getParameterCount(); i++) { |
| 1507 |
buff.append("</tr><td>"). |
| 1508 |
append(meta.getParameterClassName(i + 1)). |
| 1509 |
append("</td><td>"). |
| 1510 |
append(meta.getParameterMode(i + 1)). |
| 1511 |
append("</td><td>"). |
| 1512 |
append(meta.getParameterType(i + 1)). |
| 1513 |
append("</td><td>"). |
| 1514 |
append(meta.getParameterTypeName(i + 1)). |
| 1515 |
append("</td><td>"). |
| 1516 |
append(meta.getPrecision(i + 1)). |
| 1517 |
append("</td><td>"). |
| 1518 |
append(meta.getScale(i + 1)). |
| 1519 |
append("</td></tr>"); |
| 1520 |
} |
| 1521 |
buff.append("</table>"); |
| 1522 |
return buff.toString(); |
| 1523 |
} |
| 1524 |
|
| 1525 |
private String getResultSet(String sql, ResultSet rs, boolean metadata, |
| 1526 |
boolean list, boolean edit, long time, boolean allowEdit) |
| 1527 |
throws SQLException { |
| 1528 |
int maxrows = getMaxrows(); |
| 1529 |
time = System.currentTimeMillis() - time; |
| 1530 |
StringBuilder buff = new StringBuilder(); |
| 1531 |
if (edit) { |
| 1532 |
buff.append("<form id=\"editing\" name=\"editing\" method=\"post\" " + |
| 1533 |
"action=\"editResult.do?jsessionid=${sessionId}\" " + |
| 1534 |
"id=\"mainForm\" target=\"h2result\">" + |
| 1535 |
"<input type=\"hidden\" name=\"op\" value=\"1\" />" + |
| 1536 |
"<input type=\"hidden\" name=\"row\" value=\"\" />" + |
| 1537 |
"<table class=\"resultSet\" cellspacing=\"0\" cellpadding=\"0\" id=\"editTable\">"); |
| 1538 |
} else { |
| 1539 |
buff.append("<table class=\"resultSet\" cellspacing=\"0\" cellpadding=\"0\">"); |
| 1540 |
} |
| 1541 |
if (metadata) { |
| 1542 |
SimpleResultSet r = new SimpleResultSet(); |
| 1543 |
r.addColumn("#", Types.INTEGER, 0, 0); |
| 1544 |
r.addColumn("label", Types.VARCHAR, 0, 0); |
| 1545 |
r.addColumn("catalog", Types.VARCHAR, 0, 0); |
| 1546 |
r.addColumn("schema", Types.VARCHAR, 0, 0); |
| 1547 |
r.addColumn("table", Types.VARCHAR, 0, 0); |
| 1548 |
r.addColumn("column", Types.VARCHAR, 0, 0); |
| 1549 |
r.addColumn("type", Types.INTEGER, 0, 0); |
| 1550 |
r.addColumn("typeName", Types.VARCHAR, 0, 0); |
| 1551 |
r.addColumn("class", Types.VARCHAR, 0, 0); |
| 1552 |
r.addColumn("precision", Types.INTEGER, 0, 0); |
| 1553 |
r.addColumn("scale", Types.INTEGER, 0, 0); |
| 1554 |
r.addColumn("displaySize", Types.INTEGER, 0, 0); |
| 1555 |
r.addColumn("autoIncrement", Types.BOOLEAN, 0, 0); |
| 1556 |
r.addColumn("caseSensitive", Types.BOOLEAN, 0, 0); |
| 1557 |
r.addColumn("currency", Types.BOOLEAN, 0, 0); |
| 1558 |
r.addColumn("nullable", Types.INTEGER, 0, 0); |
| 1559 |
r.addColumn("readOnly", Types.BOOLEAN, 0, 0); |
| 1560 |
r.addColumn("searchable", Types.BOOLEAN, 0, 0); |
| 1561 |
r.addColumn("signed", Types.BOOLEAN, 0, 0); |
| 1562 |
r.addColumn("writable", Types.BOOLEAN, 0, 0); |
| 1563 |
r.addColumn("definitelyWritable", Types.BOOLEAN, 0, 0); |
| 1564 |
ResultSetMetaData m = rs.getMetaData(); |
| 1565 |
for (int i = 1; i <= m.getColumnCount(); i++) { |
| 1566 |
r.addRow(i, |
| 1567 |
m.getColumnLabel(i), |
| 1568 |
m.getCatalogName(i), |
| 1569 |
m.getSchemaName(i), |
| 1570 |
m.getTableName(i), |
| 1571 |
m.getColumnName(i), |
| 1572 |
m.getColumnType(i), |
| 1573 |
m.getColumnTypeName(i), |
| 1574 |
m.getColumnClassName(i), |
| 1575 |
m.getPrecision(i), |
| 1576 |
m.getScale(i), |
| 1577 |
m.getColumnDisplaySize(i), |
| 1578 |
m.isAutoIncrement(i), |
| 1579 |
m.isCaseSensitive(i), |
| 1580 |
m.isCurrency(i), |
| 1581 |
m.isNullable(i), |
| 1582 |
m.isReadOnly(i), |
| 1583 |
m.isSearchable(i), |
| 1584 |
m.isSigned(i), |
| 1585 |
m.isWritable(i), |
| 1586 |
m.isDefinitelyWritable(i)); |
| 1587 |
} |
| 1588 |
rs = r; |
| 1589 |
} |
| 1590 |
ResultSetMetaData meta = rs.getMetaData(); |
| 1591 |
int columns = meta.getColumnCount(); |
| 1592 |
int rows = 0; |
| 1593 |
if (list) { |
| 1594 |
buff.append("<tr><th>Column</th><th>Data</th></tr><tr>"); |
| 1595 |
while (rs.next()) { |
| 1596 |
if (maxrows > 0 && rows >= maxrows) { |
| 1597 |
break; |
| 1598 |
} |
| 1599 |
rows++; |
| 1600 |
buff.append("<tr><td>Row #</td><td>"). |
| 1601 |
append(rows).append("</tr>"); |
| 1602 |
for (int i = 0; i < columns; i++) { |
| 1603 |
buff.append("<tr><td>"). |
| 1604 |
append(PageParser.escapeHtml(meta.getColumnLabel(i + 1))). |
| 1605 |
append("</td><td>"). |
| 1606 |
append(escapeData(rs, i + 1)). |
| 1607 |
append("</td></tr>"); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
} else { |
| 1611 |
buff.append("<tr>"); |
| 1612 |
if (edit) { |
| 1613 |
buff.append("<th>${text.resultEdit.action}</th>"); |
| 1614 |
} |
| 1615 |
for (int i = 0; i < columns; i++) { |
| 1616 |
buff.append("<th>"). |
| 1617 |
append(PageParser.escapeHtml(meta.getColumnLabel(i + 1))). |
| 1618 |
append("</th>"); |
| 1619 |
} |
| 1620 |
buff.append("</tr>"); |
| 1621 |
while (rs.next()) { |
| 1622 |
if (maxrows > 0 && rows >= maxrows) { |
| 1623 |
break; |
| 1624 |
} |
| 1625 |
rows++; |
| 1626 |
buff.append("<tr>"); |
| 1627 |
if (edit) { |
| 1628 |
buff.append("<td>"). |
| 1629 |
append("<img onclick=\"javascript:editRow("). |
| 1630 |
append(rs.getRow()). |
| 1631 |
append(",'${sessionId}', '${text.resultEdit.save}', " + |
| 1632 |
"'${text.resultEdit.cancel}'"). |
| 1633 |
append(")\" width=16 height=16 src=\"ico_write.gif\" " + |
| 1634 |
"onmouseover = \"this.className ='icon_hover'\" " + |
| 1635 |
"onmouseout = \"this.className ='icon'\" " + |
| 1636 |
"class=\"icon\" alt=\"${text.resultEdit.edit}\" " + |
| 1637 |
"title=\"${text.resultEdit.edit}\" border=\"1\"/>"). |
| 1638 |
append("<img onclick=\"javascript:deleteRow("). |
| 1639 |
append(rs.getRow()). |
| 1640 |
append(",'${sessionId}', '${text.resultEdit.delete}', " + |
| 1641 |
"'${text.resultEdit.cancel}'"). |
| 1642 |
append(")\" width=16 height=16 src=\"ico_remove.gif\" " + |
| 1643 |
"onmouseover = \"this.className ='icon_hover'\" " + |
| 1644 |
"onmouseout = \"this.className ='icon'\" " + |
| 1645 |
"class=\"icon\" alt=\"${text.resultEdit.delete}\" " + |
| 1646 |
"title=\"${text.resultEdit.delete}\" border=\"1\" /></a>"). |
| 1647 |
append("</td>"); |
| 1648 |
} |
| 1649 |
for (int i = 0; i < columns; i++) { |
| 1650 |
buff.append("<td>"). |
| 1651 |
append(escapeData(rs, i + 1)). |
| 1652 |
append("</td>"); |
| 1653 |
} |
| 1654 |
buff.append("</tr>"); |
| 1655 |
} |
| 1656 |
} |
| 1657 |
boolean isUpdatable = false; |
| 1658 |
try { |
| 1659 |
if (!session.getContents().isDB2()) { |
| 1660 |
isUpdatable = rs.getConcurrency() == ResultSet.CONCUR_UPDATABLE |
| 1661 |
&& rs.getType() != ResultSet.TYPE_FORWARD_ONLY; |
| 1662 |
} |
| 1663 |
} catch (NullPointerException e) { |
| 1664 |
// ignore |
| 1665 |
// workaround for a JDBC-ODBC bridge problem |
| 1666 |
} |
| 1667 |
if (edit) { |
| 1668 |
ResultSet old = session.result; |
| 1669 |
if (old != null) { |
| 1670 |
old.close(); |
| 1671 |
} |
| 1672 |
session.result = rs; |
| 1673 |
} else { |
| 1674 |
rs.close(); |
| 1675 |
} |
| 1676 |
if (edit) { |
| 1677 |
buff.append("<tr><td>"). |
| 1678 |
append("<img onclick=\"javascript:editRow(-1, " + |
| 1679 |
"'${sessionId}', '${text.resultEdit.save}', '${text.resultEdit.cancel}'"). |
| 1680 |
append(")\" width=16 height=16 src=\"ico_add.gif\" " + |
| 1681 |
"onmouseover = \"this.className ='icon_hover'\" " + |
| 1682 |
"onmouseout = \"this.className ='icon'\" " + |
| 1683 |
"class=\"icon\" alt=\"${text.resultEdit.add}\" " + |
| 1684 |
"title=\"${text.resultEdit.add}\" border=\"1\"/>"). |
| 1685 |
append("</td>"); |
| 1686 |
for (int i = 0; i < columns; i++) { |
| 1687 |
buff.append("<td></td>"); |
| 1688 |
} |
| 1689 |
buff.append("</tr>"); |
| 1690 |
} |
| 1691 |
buff.append("</table>"); |
| 1692 |
if (edit) { |
| 1693 |
buff.append("</form>"); |
| 1694 |
} |
| 1695 |
if (rows == 0) { |
| 1696 |
buff.append("(${text.result.noRows}"); |
| 1697 |
} else if (rows == 1) { |
| 1698 |
buff.append("(${text.result.1row}"); |
| 1699 |
} else { |
| 1700 |
buff.append('(').append(rows).append(" ${text.result.rows}"); |
| 1701 |
} |
| 1702 |
buff.append(", "); |
| 1703 |
time = System.currentTimeMillis() - time; |
| 1704 |
buff.append(time).append(" ms)"); |
| 1705 |
if (!edit && isUpdatable && allowEdit) { |
| 1706 |
buff.append("<br /><br />" + |
| 1707 |
"<form name=\"editResult\" method=\"post\" " + |
| 1708 |
"action=\"query.do?jsessionid=${sessionId}\" target=\"h2result\">" + |
| 1709 |
"<input type=\"submit\" class=\"button\" " + |
| 1710 |
"value=\"${text.resultEdit.editResult}\" />" + |
| 1711 |
"<input type=\"hidden\" name=\"sql\" value=\"@edit "). |
| 1712 |
append(PageParser.escapeHtmlData(sql)). |
| 1713 |
append("\" /></form>"); |
| 1714 |
} |
| 1715 |
return buff.toString(); |
| 1716 |
} |
| 1717 |
|
| 1718 |
/** |
| 1719 |
* Save the current connection settings to the properties file. |
| 1720 |
* |
| 1721 |
* @return the file to open afterwards |
| 1722 |
*/ |
| 1723 |
private String settingSave() { |
| 1724 |
ConnectionInfo info = new ConnectionInfo(); |
| 1725 |
info.name = attributes.getProperty("name", ""); |
| 1726 |
info.driver = attributes.getProperty("driver", ""); |
| 1727 |
info.url = attributes.getProperty("url", ""); |
| 1728 |
info.user = attributes.getProperty("user", ""); |
| 1729 |
server.updateSetting(info); |
| 1730 |
attributes.put("setting", info.name); |
| 1731 |
server.saveProperties(null); |
| 1732 |
return "index.do"; |
| 1733 |
} |
| 1734 |
|
| 1735 |
private static String escapeData(ResultSet rs, int columnIndex) throws SQLException { |
| 1736 |
if (DataType.isBinaryColumn(rs.getMetaData(), columnIndex)) { |
| 1737 |
byte[] d = rs.getBytes(columnIndex); |
| 1738 |
if (d == null) { |
| 1739 |
return "<i>null</i>"; |
| 1740 |
} else if (d.length > 50_000) { |
| 1741 |
return "<div style='display: none'>=+</div>" + StringUtils.convertBytesToHex(d, 3) + "... (" |
| 1742 |
+ d.length + " ${text.result.bytes})"; |
| 1743 |
} |
| 1744 |
return StringUtils.convertBytesToHex(d); |
| 1745 |
} |
| 1746 |
String d = rs.getString(columnIndex); |
| 1747 |
if (d == null) { |
| 1748 |
return "<i>null</i>"; |
| 1749 |
} else if (d.length() > 100_000) { |
| 1750 |
return "<div style='display: none'>=+</div>" + PageParser.escapeHtml(d.substring(0, 100)) + "... (" |
| 1751 |
+ d.length() + " ${text.result.characters})"; |
| 1752 |
} else if (d.equals("null") || d.startsWith("= ") || d.startsWith("=+")) { |
| 1753 |
return "<div style='display: none'>= </div>" + PageParser.escapeHtml(d); |
| 1754 |
} else if (d.equals("")) { |
| 1755 |
// PageParser.escapeHtml replaces "" with a non-breaking space |
| 1756 |
return ""; |
| 1757 |
} |
| 1758 |
return PageParser.escapeHtml(d); |
| 1759 |
} |
| 1760 |
|
| 1761 |
private void unescapeData(String x, ResultSet rs, int columnIndex) |
| 1762 |
throws SQLException { |
| 1763 |
if (x.equals("null")) { |
| 1764 |
rs.updateNull(columnIndex); |
| 1765 |
return; |
| 1766 |
} else if (x.startsWith("=+")) { |
| 1767 |
// don't update |
| 1768 |
return; |
| 1769 |
} else if (x.equals("=*")) { |
| 1770 |
// set an appropriate default value |
| 1771 |
int type = rs.getMetaData().getColumnType(columnIndex); |
| 1772 |
switch (type) { |
| 1773 |
case Types.TIME: |
| 1774 |
rs.updateString(columnIndex, "12:00:00"); |
| 1775 |
break; |
| 1776 |
case Types.TIMESTAMP: |
| 1777 |
case Types.DATE: |
| 1778 |
rs.updateString(columnIndex, "2001-01-01"); |
| 1779 |
break; |
| 1780 |
default: |
| 1781 |
rs.updateString(columnIndex, "1"); |
| 1782 |
break; |
| 1783 |
} |
| 1784 |
return; |
| 1785 |
} else if (x.startsWith("= ")) { |
| 1786 |
x = x.substring(2); |
| 1787 |
} |
| 1788 |
ResultSetMetaData meta = rs.getMetaData(); |
| 1789 |
if (DataType.isBinaryColumn(meta, columnIndex)) { |
| 1790 |
rs.updateBytes(columnIndex, StringUtils.convertHexToBytes(x)); |
| 1791 |
return; |
| 1792 |
} |
| 1793 |
int type = meta.getColumnType(columnIndex); |
| 1794 |
if (session.getContents().isH2()) { |
| 1795 |
rs.updateString(columnIndex, x); |
| 1796 |
return; |
| 1797 |
} |
| 1798 |
switch (type) { |
| 1799 |
case Types.BIGINT: |
| 1800 |
rs.updateLong(columnIndex, Long.decode(x)); |
| 1801 |
break; |
| 1802 |
case Types.DECIMAL: |
| 1803 |
rs.updateBigDecimal(columnIndex, new BigDecimal(x)); |
| 1804 |
break; |
| 1805 |
case Types.DOUBLE: |
| 1806 |
case Types.FLOAT: |
| 1807 |
rs.updateDouble(columnIndex, Double.parseDouble(x)); |
| 1808 |
break; |
| 1809 |
case Types.REAL: |
| 1810 |
rs.updateFloat(columnIndex, Float.parseFloat(x)); |
| 1811 |
break; |
| 1812 |
case Types.INTEGER: |
| 1813 |
rs.updateInt(columnIndex, Integer.decode(x)); |
| 1814 |
break; |
| 1815 |
case Types.TINYINT: |
| 1816 |
rs.updateShort(columnIndex, Short.decode(x)); |
| 1817 |
break; |
| 1818 |
default: |
| 1819 |
rs.updateString(columnIndex, x); |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|
| 1823 |
private String settingRemove() { |
| 1824 |
String setting = attributes.getProperty("name", ""); |
| 1825 |
server.removeSetting(setting); |
| 1826 |
ArrayList<ConnectionInfo> settings = server.getSettings(); |
| 1827 |
if (!settings.isEmpty()) { |
| 1828 |
attributes.put("setting", settings.get(0)); |
| 1829 |
} |
| 1830 |
server.saveProperties(null); |
| 1831 |
return "index.do"; |
| 1832 |
} |
| 1833 |
|
| 1834 |
/** |
| 1835 |
* Get the current mime type. |
| 1836 |
* |
| 1837 |
* @return the mime type |
| 1838 |
*/ |
| 1839 |
String getMimeType() { |
| 1840 |
return mimeType; |
| 1841 |
} |
| 1842 |
|
| 1843 |
boolean getCache() { |
| 1844 |
return cache; |
| 1845 |
} |
| 1846 |
|
| 1847 |
WebSession getSession() { |
| 1848 |
return session; |
| 1849 |
} |
| 1850 |
|
| 1851 |
private void trace(String s) { |
| 1852 |
server.trace(s); |
| 1853 |
} |
| 1854 |
|
| 1855 |
} |