1 23 24 package org.continuent.sequoia.console.text.module; 25 26 import java.sql.Connection ; 27 import java.sql.DriverManager ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Savepoint ; 32 import java.util.Hashtable ; 33 34 import org.continuent.sequoia.common.i18n.ConsoleTranslate; 35 import org.continuent.sequoia.common.util.Constants; 36 import org.continuent.sequoia.console.text.Console; 37 import org.continuent.sequoia.console.text.ConsoleException; 38 import org.continuent.sequoia.console.text.ConsoleLauncher; 39 import org.continuent.sequoia.console.text.commands.ConsoleCommand; 40 import org.continuent.sequoia.console.text.formatter.ResultSetFormatter; 41 42 50 public class VirtualDatabaseConsole extends AbstractConsoleModule 51 { 52 private Connection connection = null; 53 57 private Hashtable savePoints = new Hashtable (); 58 59 60 private int timeout = 60; 61 62 private int fetchsize = 0; 63 64 private int maxrows = 0; 65 66 private String login; 67 private String url; 68 69 private boolean multilineStatementsEnabled = false; 71 private String delimiter = DEFAULT_REQUEST_DELIMITER; 73 private static final String DEFAULT_REQUEST_DELIMITER = ";"; 74 private StringBuffer currentRequest = new StringBuffer (""); 77 78 84 public VirtualDatabaseConsole(Console console) 85 { 86 super(console); 87 try 88 { 89 Class.forName(System.getProperty("console.driver", 90 "org.continuent.sequoia.driver.Driver")); 91 } 92 catch (Exception e) 93 { 94 console.printError(ConsoleTranslate.get( 95 "sql.login.cannot.load.driver", ConsoleLauncher.PRODUCT_NAME), e); 96 Runtime.getRuntime().exit(1); 97 } 98 if (console.isInteractive()) 99 console.println(ConsoleTranslate.get("sql.login.loaded.driver", 100 new String [] {ConsoleLauncher.PRODUCT_NAME, Constants.VERSION})); 101 } 102 103 108 public Connection getConnection() 109 { 110 return connection; 111 } 112 113 122 public Connection createConnection(String url, String login, String password) 123 throws ConsoleException 124 { 125 try 126 { 127 return DriverManager.getConnection(url, login, password); 128 } 129 catch (Exception e) 130 { 131 throw new ConsoleException(ConsoleTranslate.get( 132 "sql.login.connection.failed", new String []{url, e.getMessage()})); 133 } 134 } 135 136 143 public synchronized void execSQL(String request, boolean displayResult) 144 { 145 PreparedStatement stmt = null; 146 try 147 { 148 stmt = connection.prepareStatement(request); 149 stmt.setQueryTimeout(timeout); 150 if (fetchsize != 0) 151 stmt.setFetchSize(fetchsize); 152 if (maxrows != 0) 153 stmt.setMaxRows(maxrows); 154 155 long start = System.currentTimeMillis(); 156 long end; 157 if (request.toLowerCase().matches("^(\\(|\\s+)*select.*")) 158 { 159 ResultSet rs = stmt.executeQuery(); 160 end = System.currentTimeMillis(); 161 if (displayResult) 162 ResultSetFormatter.formatAndDisplay(rs, fetchsize, console); 163 } 164 else 165 { 166 boolean hasResult = stmt.execute(); 167 end = System.currentTimeMillis(); 168 169 int updatedRows; 170 do 171 { 172 updatedRows = -1; 173 if (hasResult) 174 { 175 ResultSet rs = stmt.getResultSet(); 176 if (displayResult) 177 ResultSetFormatter.formatAndDisplay(rs, fetchsize, console); 178 rs.close(); 179 } 180 else 181 { 182 updatedRows = stmt.getUpdateCount(); 183 if (displayResult) 184 console.printInfo(ConsoleTranslate.get("sql.display.affected.rows", 185 updatedRows)); 186 } 187 hasResult = stmt.getMoreResults(); 188 updatedRows = stmt.getUpdateCount(); 189 } 190 while (hasResult || (updatedRows != -1)); 191 } 192 console.printInfo(ConsoleTranslate.get("sql.display.query.time", 193 new Long []{new Long ((end - start) / 1000), 194 new Long ((end - start) % 1000)})); 195 } 196 catch (Exception e) 197 { 198 console.printError(ConsoleTranslate.get("sql.command.sqlquery.error", e), 199 e); 200 } 201 finally 202 { 203 try 204 { 205 stmt.close(); 206 } 207 catch (Exception ignore) 208 { 209 } 210 } 211 } 212 213 216 public void handlePrompt() 217 { 218 while (!quit) 219 { 220 try 221 { 222 String cmd = console.readLine(this.getPromptString()); 223 if (cmd == null) 224 { 225 quit(); 226 break; 227 } 228 229 if (cmd.length() == 0) 230 continue; 231 232 ConsoleCommand currentCommand = findConsoleCommand(cmd, 233 getHashCommands()); 234 235 if (currentCommand == null) 236 { 237 if (multilineStatementsEnabled) 238 { 239 currentRequest.append(" ").append(cmd); 241 if (currentRequest.toString().endsWith(delimiter)) 242 { 243 String request = removeDelimiterAndTrim(currentRequest.toString()); 244 execSQL(request, true); 245 currentRequest = new StringBuffer (""); 246 } 247 } 248 else 249 { 250 execSQL(cmd, true); 251 } 252 } 253 else 254 { 255 currentCommand.execute(cmd.substring( 256 currentCommand.getCommandName().length()).trim()); 257 } 258 } 259 catch (Exception e) 260 { 261 console.printError(ConsoleTranslate.get("sql.display.exception", e), e); 262 if ((e instanceof RuntimeException ) 263 || (!console.isInteractive() && console.isExitOnError())) 264 { 265 System.exit(1); 266 } 267 } 268 } 269 } 270 271 279 private String removeDelimiterAndTrim(String request) 280 { 281 if (request == null) 282 { 283 return ""; 284 } 285 return request.substring(0, request.length() - delimiter.length()).trim(); 286 } 287 288 291 public String getDescriptionString() 292 { 293 return "SQL Console"; 294 } 295 296 299 protected String getModuleID() 300 { 301 return "sql"; } 303 304 307 public String getPromptString() 308 { 309 int ind1 = url.lastIndexOf('?'); 310 int ind2 = url.lastIndexOf(';'); 311 if (ind1 != -1 || ind2 != -1) 312 { 313 String prompt; 314 prompt = (ind1 != -1) ? url.substring(0, ind1) : url; 315 prompt = (ind2 != -1) ? url.substring(0, ind2) : url; 316 return prompt + " (" + login + ")"; 317 } 318 else 319 return url + " (" + login + ")"; 320 } 321 322 329 public Savepoint getSavePoint(String name) 330 { 331 return (Savepoint ) savePoints.get(name); 332 } 333 334 340 public void addSavePoint(Savepoint savePoint) throws SQLException 341 { 342 savePoints.put(savePoint.getSavepointName(), savePoint); 343 } 344 345 350 public int getTimeout() 351 { 352 return timeout; 353 } 354 355 360 public void setTimeout(int timeout) 361 { 362 this.timeout = timeout; 363 } 364 365 370 public int getFetchsize() 371 { 372 return fetchsize; 373 } 374 375 380 public void setFetchsize(int fetchsize) 381 { 382 this.fetchsize = fetchsize; 383 } 384 385 390 public void setMaxrows(int maxrows) 391 { 392 this.maxrows = maxrows; 393 } 394 395 398 public void login(String [] params) throws Exception 399 { 400 quit = false; 401 402 login = null; 403 url = (params.length > 0 && params[0] != null) ? params[0].trim() : null; 404 try 405 { 406 if ((url == null) || url.trim().equals("")) 407 { 408 url = console.readLine(ConsoleTranslate 409 .get("sql.login.prompt.url", ConsoleLauncher.PRODUCT_NAME)); 410 if (url == null) 411 return; 412 } 413 login = console.readLine(ConsoleTranslate.get("sql.login.prompt.user")); 414 if (login == null) 415 return; 416 String password = console.readPassword(ConsoleTranslate 417 .get("sql.login.prompt.password")); 418 if (password == null) 419 return; 420 421 connection = createConnection(url, login, password); 422 423 console.getConsoleReader().removeCompletor( 425 console.getControllerModule().getCompletor()); 426 console.getConsoleReader().addCompletor(this.getCompletor()); 427 } 428 catch (ConsoleException e) 429 { 430 throw e; 431 } 432 catch (Exception e) 433 { 434 throw new ConsoleException(ConsoleTranslate.get("sql.login.exception", e)); 435 } 436 } 437 438 441 public void quit() 442 { 443 if (connection != null) 444 { 445 try 446 { 447 connection.close(); 448 } 449 catch (Exception e) 450 { 451 } 453 } 454 super.quit(); 455 } 456 457 460 public void help() 461 { 462 super.help(); 463 console.println(); 464 console.println(ConsoleTranslate.get("sql.command.description")); 465 } 466 467 473 public void setRequestDelimiter(String delimiter) 474 { 475 this.delimiter = delimiter; 476 } 477 478 483 public String getRequestDelimiter() 484 { 485 return delimiter; 486 } 487 488 497 public void enableMultilineStatement(boolean multilineStatementsEnabled) 498 { 499 this.multilineStatementsEnabled = multilineStatementsEnabled; 500 } 501 } 502 | Popular Tags |