📄 sqltool.java
字号:
continue; } if (arg[i].substring(2).equals("stdinput")) { noinput = false; stdinputOverride = Boolean.TRUE; continue; } if (arg[i].substring(2).equals("noinput")) { noinput = true; stdinputOverride = Boolean.FALSE; continue; } if (arg[i].substring(2).equals("driver")) { if (++i == arg.length) { throw bcl; } driver = arg[i]; continue; } throw bcl; } if (!listMode) { if (++i == arg.length) { throw bcl; } targetDb = arg[i]; } int scriptIndex = 0; if (sqlText != null) { try { tmpFile = File.createTempFile("sqltool-", ".sql"); //(new java.io.FileWriter(tmpFile)).write(sqlText); java.io.FileWriter fw = new java.io.FileWriter(tmpFile); fw.write("/* " + (new java.util.Date()) + ". " + SqlTool.class.getName() + " command-line SQL. */\n\n"); fw.write(sqlText + '\n'); fw.flush(); fw.close(); } catch (IOException ioe) { exitMain(4, "Failed to write given sql to temp file: " + ioe); return; } } if (stdinputOverride != null) { noinput = !stdinputOverride.booleanValue(); } interactive = (!noinput) && (arg.length <= i + 1); if (arg.length == i + 2 && arg[i + 1].equals("-")) { if (stdinputOverride == null) { noinput = false; } } else if (arg.length > i + 1) { // I.e., if there are any SQL files specified. scriptFiles = new File[arg.length - i - 1 + ((stdinputOverride == null ||!stdinputOverride.booleanValue()) ? 0 : 1)]; if (debug) { System.err.println("scriptFiles has " + scriptFiles.length + " elements"); } while (i + 1 < arg.length) { scriptFiles[scriptIndex++] = new File(arg[++i]); } if (stdinputOverride != null && stdinputOverride.booleanValue()) { scriptFiles[scriptIndex++] = null; noinput = true; } } } catch (BadCmdline bcl) { exitMain(2, SYNTAX_MESSAGE); return; } RCData conData = null; try { conData = new RCData(new File((rcFile == null) ? DEFAULT_RCFILE : rcFile), targetDb); } catch (Exception e) { exitMain(1, "Failed to retrieve connection info for database '" + targetDb + "': " + e.getMessage()); return; } if (listMode) { exitMain(0); return; } if (debug) { conData.report(); } try { conn = conData.getConnection( driver, System.getProperty("sqlfile.charset"), System.getProperty("javax.net.ssl.trustStore")); conn.setAutoCommit(autoCommit); DatabaseMetaData md = null; if (interactive && (md = conn.getMetaData()) != null) { System.out.println("JDBC Connection established to a " + md.getDatabaseProductName() + " v. " + md.getDatabaseProductVersion() + " database as '" + md.getUserName() + "'."); } } catch (Exception e) { e.printStackTrace(); // Let's not continue as if nothing is wrong. exitMain(10, "Failed to get a connection to " + conData.url + " as " + conData.username + ". " + e.getMessage()); return; } File[] emptyFileArray = {}; File[] singleNullFileArray = { null }; File autoFile = null; if (interactive &&!noautoFile) { autoFile = new File(System.getProperty("user.home") + "/auto.sql"); if ((!autoFile.isFile()) ||!autoFile.canRead()) { autoFile = null; } } if (scriptFiles == null) { // I.e., if no SQL files given on command-line. // Input file list is either nothing or {null} to read stdin. scriptFiles = (noinput ? emptyFileArray : singleNullFileArray); } int numFiles = scriptFiles.length; if (tmpFile != null) { numFiles += 1; } if (autoFile != null) { numFiles += 1; } SqlFile[] sqlFiles = new SqlFile[numFiles]; HashMap userVars = new HashMap(); if (varSettings != null) { int equals; String curSetting, var, val; StringTokenizer allvars = new StringTokenizer(varSettings, ","); while (allvars.hasMoreTokens()) { curSetting = allvars.nextToken().trim(); equals = curSetting.indexOf('='); if (equals < 1) { exitMain(24, "Var settings not of format NAME=var[,...]"); return; } var = curSetting.substring(0, equals).trim(); val = curSetting.substring(equals + 1).trim(); if (var.length() < 1 || val.length() < 1) { exitMain(24, "Var settings not of format NAME=var[,...]"); return; } userVars.put(var, val); } } // We print version before execing this one. int interactiveFileIndex = -1; try { int fileIndex = 0; if (autoFile != null) { sqlFiles[fileIndex++] = new SqlFile(autoFile, false, userVars); } if (tmpFile != null) { sqlFiles[fileIndex++] = new SqlFile(tmpFile, false, userVars); } for (int j = 0; j < scriptFiles.length; j++) { if (interactiveFileIndex < 0 && interactive) { interactiveFileIndex = fileIndex; } sqlFiles[fileIndex++] = new SqlFile(scriptFiles[j], interactive, userVars); } } catch (IOException ioe) { try { conn.close(); } catch (Exception e) {} exitMain(2, ioe.getMessage()); return; } int retval = 0; // Value we will return via System.exit(). try { for (int j = 0; j < sqlFiles.length; j++) { if (j == interactiveFileIndex) { System.out.print("SqlTool v. " + revnum + ". "); } sqlFiles[j].execute(conn, coeOverride); } } catch (IOException ioe) { System.err.println("Failed to execute SQL: " + ioe.getMessage()); retval = 3; // These two Exception types are handled properly inside of SqlFile. // We just need to return an appropriate error status. } catch (SqlToolError ste) { retval = 2; // Should not be handling SQLExceptions here! SqlFile should handle // them. } catch (SQLException se) { retval = 1; } finally { try { conn.close(); } catch (Exception e) {} } // Taking file removal out of final block because this is good debug // info to keep around if the program aborts. if (tmpFile != null &&!tmpFile.delete()) { System.err.println( "Error occurred while trying to remove temp file '" + tmpFile + "'"); } exitMain(retval); return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -