* The big picture is, we parse input args; load a RCData;
* get a JDBC Connection with the RCData; instantiate and
* execute as many SqlFiles as we need to.
*/
String rcFile = null;
PipedReader tmpReader = null;
String sqlText = null;
String driver = null;
String targetDb = null;
boolean debug = false;
File[] scriptFiles = null;
int i = -1;
boolean listMode = false;
boolean interactive = false;
boolean noinput = false;
boolean noautoFile = false;
boolean autoCommit = false;
Boolean coeOverride = null;
Boolean stdinputOverride = null;
String rcParams = null;
String rcUrl = null;
String rcUsername = null;
String rcPassword = null;
String rcCharset = null;
String rcTruststore = null;
String rcTransIso = null;
Map<String, String> rcFields = null;
String parameter;
SqlFile[] sqlFiles = null;
Connection conn = null;
Map<String, String> userVars = new HashMap<String, String>();
try { // Try block to GC tmpReader
try { // Try block for BadCmdline
while ((i + 1 < arg.length))
if (arg[i + 1].startsWith("--")) {
i++;
if (arg[i].length() == 2) {
break; // "--"
}
parameter = arg[i].substring(2).toLowerCase();
if (parameter.equals("help")) {
System.out.println(SqltoolRB.SqlTool_syntax.getString(
revnum, RCData.DEFAULT_JDBC_DRIVER));
return;
}
if (parameter.equals("abortonerr")) {
if (coeOverride != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_abort_continue_mutuallyexclusive.getString());
}
coeOverride = Boolean.FALSE;
} else if (parameter.equals("continueonerr")) {
if (coeOverride != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_abort_continue_mutuallyexclusive.getString());
}
coeOverride = Boolean.TRUE;
} else if (parameter.startsWith("continueonerr=")) {
if (coeOverride != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_abort_continue_mutuallyexclusive.getString());
}
coeOverride = Boolean.valueOf(
arg[i].substring("--continueonerr=".length()));
} else if (parameter.equals("list")) {
if (listMode) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
listMode = true;
} else if (parameter.equals("rcfile")) {
if (++i == arg.length) {
throw bcl;
}
if (rcFile != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
rcFile = arg[i];
} else if (parameter.startsWith("rcfile=")) {
if (rcFile != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
rcFile = arg[i].substring("--rcfile=".length());
} else if (parameter.equals("setvar")) {
if (++i == arg.length) {
throw bcl;
}
try {
varParser(arg[i], userVars, false);
} catch (PrivateException pe) {
throw new SqlToolException(
RCERR_EXITVAL, pe.getMessage());
}
} else if (parameter.startsWith("setvar=")) {
try {
varParser(arg[i].substring("--setvar=".length()),
userVars, false);
} catch (PrivateException pe) {
throw new SqlToolException(
RCERR_EXITVAL, pe.getMessage());
}
} else if (parameter.equals("sql")) {
noinput = true; // but turn back on if file "-" specd.
if (++i == arg.length) {
throw bcl;
}
if (sqlText != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
sqlText = arg[i];
} else if (parameter.startsWith("sql=")) {
noinput = true; // but turn back on if file "-" specd.
if (sqlText != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
sqlText = arg[i].substring("--sql=".length());
} else if (parameter.equals("debug")) {
if (debug) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
debug = true;
} else if (parameter.equals("noautofile")) {
if (noautoFile) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
noautoFile = true;
} else if (parameter.equals("autocommit")) {
if (autoCommit) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
autoCommit = true;
} else if (parameter.equals("stdinput")) {
noinput = false;
if (stdinputOverride != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
stdinputOverride = Boolean.TRUE;
} else if (parameter.equals("noinput")) {
noinput = true;
if (stdinputOverride != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
stdinputOverride = Boolean.FALSE;
} else if (parameter.equals("driver")) {
if (++i == arg.length) {
throw bcl;
}
if (driver != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
driver = arg[i];
} else if (parameter.startsWith("driver=")) {
if (driver != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
driver = arg[i].substring("--driver=".length());
} else if (parameter.equals("inlinerc")) {
if (++i == arg.length) {
throw bcl;
}
if (rcParams != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
rcParams = arg[i];
} else if (parameter.startsWith("inlinerc=")) {
if (rcParams != null) {
throw new SqlToolException(SYNTAXERR_EXITVAL,
SqltoolRB.SqlTool_params_redundant.getString());
}
rcParams = arg[i].substring("--inlinerc=".length());
} else {
throw bcl;
}
} else if (arg[i + 1].startsWith("-P")
|| arg[i + 1].startsWith("-p")) {
i++;
boolean sepSwitch = arg[i].length() < 3;
if (sepSwitch) {
if (++i == arg.length) {
throw bcl;
}
}
int equalAt = arg[i].indexOf('=');
if (equalAt < (sepSwitch ? 1 : 3)) {
throw new SqlToolException(RCERR_EXITVAL,
"Specified var assignment contains no '='");
}
userVars.put(arg[i].substring(sepSwitch ? 0 : 2, equalAt),
arg[i].substring(equalAt + 1));
} else {
break;
}
if (!listMode && rcParams == null && ++i != arg.length) {
// If an inline RC file was specified, don't look for targetDb
targetDb = arg[i];
if (targetDb.equals("-")) targetDb = null;
}
int scriptIndex = 0;
if (sqlText != null) {
try {
tmpReader = new PipedReader();
PipedWriter tmpWriter = new PipedWriter(tmpReader);
// My best guess is that the encoding here will be however
// we read the SQL text from the command-line, which will
// be the platform default encoding. Therefore, don't
// specify an encoding for this pipe.