32
package org.hsqldb.util;
35
import java.io.IOException;
36
import java.sql.Connection;
37
import java.sql.DatabaseMetaData;
38
import java.sql.SQLException;
39
import java.util.HashMap;
40
import java.util.StringTokenizer;
42
/* $Id: SqlTool.java,v 1.49 2005/06/08 19:52:38 fredt Exp $ */
45
* Sql Tool. A command-line and/or interactive SQL tool.
46
* (Note: For every Javadoc block comment, I'm using a single blank line
47
* immediately after the description, just like's Sun's examples in
48
* their Coding Conventions document).
50
* See JavaDocs for the main method for syntax of how to run.
53
* @version $Revision: 1.49 $
54
* @author Blaine Simpson unsaved@users
56
public class SqlTool {
58
private static final String DEFAULT_RCFILE =
59
System.getProperty("user.home") + "/sqltool.rc";
60
private static Connection conn;
62
// N.b. the following is static!
63
private static boolean noexit; // Whether System.exit() may be called.
64
private static String revnum = null;
67
revnum = "$Revision: 1.49 $".substring("$Revision: ".length(),
68
"$Revision: 1.49 $".length()
72
private static final String SYNTAX_MESSAGE =
73
"Usage: java [-Dsqlfile.X=Y...] org.hsqldb.util.SqlTool \\\n"
74
+ " [--optname [optval...]] urlid [file1.sql...]\n"
75
+ "where arguments are:\n"
76
+ " --help Displays this message\n"
77
+ " --list List urlids in the rcfile\n"
78
+ " --noinput Do not read stdin (default if sql file given\n"
79
+ " or --sql switch used).\n"
80
+ " --stdinput Read stdin IN ADDITION to sql files/--sql input\n"
81
+ " --debug Print Debug info to stderr\n"
82
+ " --noAutoFile Do not execute auto.sql from home dir\n"
83
+ " --autoCommit Auto-commit JDBC DML commands\n"
84
+ " --sql \"SQL; Statements\" Execute given SQL instead of stdin (before\n"
85
+ " SQL files if any are specified) where \"SQL\"\n"
86
+ " consists of SQL command(s). See the Guide.\n"
87
+ " --rcfile /file/path.rc Connect Info File [$HOME/sqltool.rc]\n"
88
+ " --abortOnErr Abort on Error (overrides defaults)\n"
89
+ " --continueOnErr Continue on Error (overrides defaults)\n"
90
+ " --setvar NAME1=val1[,NAME2=val2...] PL variables\n"
91
+ " --driver a.b.c.Driver JDBC driver class ["
92
+ RCData.DEFAULT_JDBC_DRIVER + "]\n"
93
+ " urlid ID of url/userame/password in rcfile\n"
94
+ " file1.sql... SQL files to be executed [stdin]\n"
96
+ "(Use '-' for non-interactively stdin).\n"
97
+ "See the SqlTool Manual for the supported sqltool.* System Properties.\n"
98
+ "SqlTool v. " + revnum + ".";
100
/** Utility nested class for internal use. */
101
private static class BadCmdline extends Exception {}
104
/** Utility object for internal use. */
105
private static BadCmdline bcl = new BadCmdline();
107
/** Nested class for external callers of SqlTool.main() */
108
public static class SqlToolException extends Exception {
110
public SqlToolException() {
114
public SqlToolException(String s) {
120
* Exit the main() method by either throwing an exception or exiting JVM.
122
* Call return() right after you call this method, because this method
123
* will not exit if (noexit is true && retval == 0).
125
private static void exitMain(int retval) throws SqlToolException {
126
exitMain(retval, null);
130
* Exit the main() method by either throwing an exception or exiting JVM.
132
* Call return() right after you call this method, because this method
133
* will not exit if (noexit is true && retval == 0).
135
private static void exitMain(int retval,
136
String msg) throws SqlToolException {
141
} else if (msg == null) {
142
throw new SqlToolException();
144
throw new SqlToolException(msg);
148
((retval == 0) ? System.out
149
: System.err).println(msg);
157
* Connect to a JDBC Database and execute the commands given on
158
* stdin or in SQL file(s).
159
* Like most main methods, this is not intended to be thread-safe.
161
* @param arg Run "java... org.hsqldb.util.SqlTool --help" for syntax.
162
* @throws SqlToolException May be thrown only if the system property
163
* 'sqltool.noexit' is set (to anything).
165
public static void main(String[] arg) throws SqlToolException {
168
* The big picture is, we parse input args; load a RCData;
169
* get a JDBC Connection with the RCData; instantiate and
170
* execute as many SqlFiles as we need to.
172
String rcFile = null;
174
String sqlText = null;
175
String driver = null;
176
String targetDb = null;
177
String varSettings = null;
178
boolean debug = false;
179
File[] scriptFiles = null;
181
boolean listMode = false;
182
boolean interactive = false;
183
boolean noinput = false;
184
boolean noautoFile = false;
185
boolean autoCommit = false;
186
Boolean coeOverride = null;
187
Boolean stdinputOverride = null;
189
noexit = System.getProperty("sqltool.noexit") != null;
192
while ((i + 1 < arg.length) && arg[i + 1].startsWith("--")) {
195
if (arg[i].length() == 2) {
199
if (arg[i].substring(2).equals("help")) {
200
exitMain(0, SYNTAX_MESSAGE);
205
if (arg[i].substring(2).equals("abortOnErr")) {
206
if (coeOverride != null) {
208
0, "Switches '--abortOnErr' and "
209
+ "'--continueOnErr' are mutually exclusive");
214
coeOverride = Boolean.FALSE;
219
if (arg[i].substring(2).equals("continueOnErr")) {
220
if (coeOverride != null) {
222
0, "Switches '--abortOnErr' and "
223
+ "'--continueOnErr' are mutually exclusive");
228
coeOverride = Boolean.TRUE;
233
if (arg[i].substring(2).equals("list")) {
239
if (arg[i].substring(2).equals("rcfile")) {
240
if (++i == arg.length) {
249
if (arg[i].substring(2).equals("setvar")) {
250
if (++i == arg.length) {
254
varSettings = arg[i];
259
if (arg[i].substring(2).equals("sql")) {
260
noinput = true; // but turn back on if file "-" specd.
262
if (++i == arg.length) {
268
if (sqlText.charAt(sqlText.length() - 1) != ';') {
275
if (arg[i].substring(2).equals("debug")) {
281
if (arg[i].substring(2).equals("noAutoFile")) {
287
if (arg[i].substring(2).equals("autoCommit")) {
293
if (arg[i].substring(2).equals("stdinput")) {
295
stdinputOverride = Boolean.TRUE;
300
if (arg[i].substring(2).equals("noinput")) {
302
stdinputOverride = Boolean.FALSE;
307
if (arg[i].substring(2).equals("driver")) {
308
if (++i == arg.length) {
321
if (++i == arg.length) {
330
if (sqlText != null) {
332
tmpFile = File.createTempFile("sqltool-", ".sql");
334
//(new java.io.FileWriter(tmpFile)).write(sqlText);
335
java.io.FileWriter fw = new java.io.FileWriter(tmpFile);
337
fw.write("/* " + (new java.util.Date()) + ". "
338
+ SqlTool.class.getName()
339
+ " command-line SQL. */\n\n");
340
fw.write(sqlText + '\n');
343
} catch (IOException ioe) {
344
exitMain(4, "Failed to write given sql to temp file: "
351
if (stdinputOverride != null) {
352
noinput = !stdinputOverride.booleanValue();
355
interactive = (!noinput) && (arg.length <= i + 1);
357
if (arg.length == i + 2 && arg[i + 1].equals("-")) {
358
if (stdinputOverride == null) {
361
} else if (arg.length > i + 1) {
363
// I.e., if there are any SQL files specified.
365
new File[arg.length - i - 1 + ((stdinputOverride == null ||!stdinputOverride.booleanValue()) ? 0
369
System.err.println("scriptFiles has "
370
+ scriptFiles.length + " elements");
373
while (i + 1 < arg.length) {
374
scriptFiles[scriptIndex++] = new File(arg[++i]);
377
if (stdinputOverride != null
378
&& stdinputOverride.booleanValue()) {
379
scriptFiles[scriptIndex++] = null;
383
} catch (BadCmdline bcl) {
384
exitMain(2, SYNTAX_MESSAGE);
389
RCData conData = null;
392
conData = new RCData(new File((rcFile == null) ? DEFAULT_RCFILE
393
: rcFile), targetDb);
394
} catch (Exception e) {
395
exitMain(1, "Failed to retrieve connection info for database '"
396
+ targetDb + "': " + e.getMessage());
412
conn = conData.getConnection(
413
driver, System.getProperty("sqlfile.charset"),
414
System.getProperty("javax.net.ssl.trustStore"));
416
conn.setAutoCommit(autoCommit);
418
DatabaseMetaData md = null;
420
if (interactive && (md = conn.getMetaData()) != null) {
421
System.out.println("JDBC Connection established to a "
422
+ md.getDatabaseProductName() + " v. "
423
+ md.getDatabaseProductVersion()
424
+ " database as '" + md.getUserName()
427
} catch (Exception e) {
430
// Let's not continue as if nothing is wrong.
432
"Failed to get a connection to " + conData.url + " as "
433
+ conData.username + ". " + e.getMessage());
438
File[] emptyFileArray = {};
439
File[] singleNullFileArray = { null };
440
File autoFile = null;
442
if (interactive &&!noautoFile) {
443
autoFile = new File(System.getProperty("user.home")
446
if ((!autoFile.isFile()) ||!autoFile.canRead()) {
451
if (scriptFiles == null) {
453
// I.e., if no SQL files given on command-line.
454
// Input file list is either nothing or {null} to read stdin.
455
scriptFiles = (noinput ? emptyFileArray
456
: singleNullFileArray);
459
int numFiles = scriptFiles.length;
461
if (tmpFile != null) {
465
if (autoFile != null) {
469
SqlFile[] sqlFiles = new SqlFile[numFiles];
470
HashMap userVars = new HashMap();
472
if (varSettings != null) {
474
String curSetting, var, val;
475
StringTokenizer allvars = new StringTokenizer(varSettings, ",");
477
while (allvars.hasMoreTokens()) {
478
curSetting = allvars.nextToken().trim();
479
equals = curSetting.indexOf('=');
482
exitMain(24, "Var settings not of format NAME=var[,...]");
487
var = curSetting.substring(0, equals).trim();
488
val = curSetting.substring(equals + 1).trim();
490
if (var.length() < 1 || val.length() < 1) {
491
exitMain(24, "Var settings not of format NAME=var[,...]");
496
userVars.put(var, val);
500
// We print version before execing this one.
501
int interactiveFileIndex = -1;
506
if (autoFile != null) {
507
sqlFiles[fileIndex++] = new SqlFile(autoFile, false,
511
if (tmpFile != null) {
512
sqlFiles[fileIndex++] = new SqlFile(tmpFile, false, userVars);
515
for (int j = 0; j < scriptFiles.length; j++) {
516
if (interactiveFileIndex < 0 && interactive) {
517
interactiveFileIndex = fileIndex;
520
sqlFiles[fileIndex++] = new SqlFile(scriptFiles[j],
521
interactive, userVars);
523
} catch (IOException ioe) {
526
} catch (Exception e) {}
528
exitMain(2, ioe.getMessage());
533
int retval = 0; // Value we will return via System.exit().
536
for (int j = 0; j < sqlFiles.length; j++) {
537
if (j == interactiveFileIndex) {
538
System.out.print("SqlTool v. " + revnum
542
sqlFiles[j].execute(conn, coeOverride);
544
} catch (IOException ioe) {
545
System.err.println("Failed to execute SQL: " + ioe.getMessage());
549
// These two Exception types are handled properly inside of SqlFile.
550
// We just need to return an appropriate error status.
551
} catch (SqlToolError ste) {
554
// Should not be handling SQLExceptions here! SqlFile should handle
556
} catch (SQLException se) {
561
} catch (Exception e) {}
564
// Taking file removal out of final block because this is good debug
565
// info to keep around if the program aborts.
566
if (tmpFile != null &&!tmpFile.delete()) {
568
"Error occurred while trying to remove temp file '" + tmpFile
32
package org.hsqldb.util;
34
import java.io.BufferedReader;
36
import java.io.IOException;
37
import java.io.InputStreamReader;
38
import java.sql.Connection;
39
import java.sql.DatabaseMetaData;
40
import java.sql.SQLException;
41
import java.util.HashMap;
43
import java.util.StringTokenizer;
45
/* $Id: SqlTool.java,v 1.55 2006/07/27 20:04:31 fredt Exp $ */
48
* Sql Tool. A command-line and/or interactive SQL tool.
49
* (Note: For every Javadoc block comment, I'm using a single blank line
50
* immediately after the description, just like's Sun's examples in
51
* their Coding Conventions document).
53
* See JavaDocs for the main method for syntax of how to run.
56
* @version $Revision: 1.55 $
57
* @author Blaine Simpson unsaved@users
59
public class SqlTool {
61
private static final String DEFAULT_RCFILE =
62
System.getProperty("user.home") + "/sqltool.rc";
63
private static Connection conn;
65
// N.b. the following is static!
66
private static boolean noexit; // Whether System.exit() may be called.
67
private static String revnum = null;
70
* The configuration identifier to use when connection parameters are
71
* specified on the command line
73
private static String CMDLINE_ID = "cmdline";
76
revnum = "$Revision: 1.55 $".substring("$Revision: ".length(),
77
"$Revision: 1.55 $".length()
81
private static final String SYNTAX_MESSAGE =
82
"Usage: java [-Dsqlfile.X=Y...] org.hsqldb.util.SqlTool \\\n"
83
+ " [--optname [optval...]] urlid [file1.sql...]\n"
84
+ "where arguments are:\n"
85
+ " --help Displays this message\n"
86
+ " --list List urlids in the rc file\n"
87
+ " --noInput Do not read stdin (default if sql file given\n"
88
+ " or --sql switch used).\n"
89
+ " --stdInput Read stdin IN ADDITION to sql files/--sql input\n"
90
+ " --inlineRc URL=val1,USER=val2[,DRIVER=val3][,CHARSET=val4][,TRUST=val5]\n"
91
+ " Inline RC file variables\n"
92
+ " --debug Print Debug info to stderr\n"
93
+ " --noAutoFile Do not execute auto.sql from home dir\n"
94
+ " --autoCommit Auto-commit JDBC DML commands\n"
95
+ " --sql \"SQL; Statements\" Execute given SQL instead of stdin (before\n"
96
+ " SQL files if any are specified) where \"SQL\"\n"
97
+ " consists of SQL command(s). See the Guide.\n"
98
+ " --rcFile /file/path.rc Connect Info File [$HOME/sqltool.rc]\n"
99
+ " --abortOnErr Abort on Error (overrides defaults)\n"
100
+ " --continueOnErr Continue on Error (overrides defaults)\n"
101
+ " --setVar NAME1=val1[,NAME2=val2...] PL variables\n"
102
+ " --driver a.b.c.Driver JDBC driver class ["
103
+ RCData.DEFAULT_JDBC_DRIVER + "]\n"
104
+ " urlid ID of url/userame/password in rcfile\n"
105
+ " file1.sql... SQL files to be executed [stdin]\n"
107
+ "(Use '-' for non-interactively stdin).\n"
108
+ "See the SqlTool Manual for the supported sqltool.* System Properties.\n"
109
+ "SqlTool v. " + revnum + ".";
111
/** Utility nested class for internal use. */
112
private static class BadCmdline extends Exception {}
115
/** Utility object for internal use. */
116
private static BadCmdline bcl = new BadCmdline();
118
/** Nested class for external callers of SqlTool.main() */
119
public static class SqlToolException extends Exception {
121
public SqlToolException() {
125
public SqlToolException(String s) {
131
* Exit the main() method by either throwing an exception or exiting JVM.
133
* Call return() right after you call this method, because this method
134
* will not exit if (noexit is true && retval == 0).
136
private static void exitMain(int retval) throws SqlToolException {
137
exitMain(retval, null);
141
* Exit the main() method by either throwing an exception or exiting JVM.
143
* Call return() right after you call this method, because this method
144
* will not exit if (noexit is true && retval == 0).
146
private static void exitMain(int retval,
147
String msg) throws SqlToolException {
152
} else if (msg == null) {
153
throw new SqlToolException();
155
throw new SqlToolException(msg);
159
((retval == 0) ? System.out
160
: System.err).println(msg);
168
* Prompt the user for a password.
170
* @param username The user the password is for
171
* @return The password the user entered
173
private static String promptForPassword(String username)
174
throws SqlToolException {
176
BufferedReader console;
182
console = new BufferedReader(new InputStreamReader(System.in));
184
// Prompt for password
185
System.out.print(username + "'s password: ");
187
// Read the password from the command line
188
password = console.readLine();
190
if (password == null) {
193
password = password.trim();
195
} catch (IOException e) {
197
"Error while reading password from console: "
205
* Parses a comma delimited string of name value pairs into a
206
* <code>Map</code> object.
208
* @param varString The string to parse
209
* @param varMap The map to save the pared values into
210
* @param lowerCaseKeys Set to <code>true</code> if the map keys should be
211
* converted to lower case
213
private static void varParser(String varString, Map varMap,
214
boolean lowerCaseKeys)
215
throws SqlToolException {
221
StringTokenizer allvars;
223
if ((varMap == null) || (varString == null)) {
227
allvars = new StringTokenizer(varString, ",");
229
while (allvars.hasMoreTokens()) {
230
curSetting = allvars.nextToken().trim();
231
equals = curSetting.indexOf('=');
234
throw new SqlToolException(
235
"Var settings not of format NAME=var[,...]");
238
var = curSetting.substring(0, equals).trim();
239
val = curSetting.substring(equals + 1).trim();
241
if (var.length() < 1 || val.length() < 1) {
242
throw new SqlToolException(
243
"Var settings not of format NAME=var[,...]");
247
var = var.toLowerCase();
250
varMap.put(var, val);
255
* Connect to a JDBC Database and execute the commands given on
256
* stdin or in SQL file(s).
257
* Like most main methods, this is not intended to be thread-safe.
259
* @param arg Run "java... org.hsqldb.util.SqlTool --help" for syntax.
260
* @throws SqlToolException May be thrown only if the system property
261
* 'sqltool.noexit' is set (to anything).
263
public static void main(String[] arg) throws SqlToolException {
266
* The big picture is, we parse input args; load a RCData;
267
* get a JDBC Connection with the RCData; instantiate and
268
* execute as many SqlFiles as we need to.
270
String rcFile = null;
272
String sqlText = null;
273
String driver = null;
274
String targetDb = null;
275
String varSettings = null;
276
boolean debug = false;
277
File[] scriptFiles = null;
279
boolean listMode = false;
280
boolean interactive = false;
281
boolean noinput = false;
282
boolean noautoFile = false;
283
boolean autoCommit = false;
284
Boolean coeOverride = null;
285
Boolean stdinputOverride = null;
286
String rcParams = null;
288
String rcUsername = null;
289
String rcPassword = null;
290
String rcDriver = null;
291
String rcCharset = null;
292
String rcTruststore = null;
296
noexit = System.getProperty("sqltool.noexit") != null;
299
while ((i + 1 < arg.length) && arg[i + 1].startsWith("--")) {
302
if (arg[i].length() == 2) {
306
parameter = arg[i].substring(2).toLowerCase();
308
if (parameter.equals("help")) {
309
exitMain(0, SYNTAX_MESSAGE);
312
} else if (parameter.equals("abortonerr")) {
313
if (coeOverride != null) {
315
0, "Switches '--abortOnErr' and "
316
+ "'--continueOnErr' are mutually exclusive");
321
coeOverride = Boolean.FALSE;
322
} else if (parameter.equals("continueonerr")) {
323
if (coeOverride != null) {
325
0, "Switches '--abortOnErr' and "
326
+ "'--continueOnErr' are mutually exclusive");
331
coeOverride = Boolean.TRUE;
332
} else if (parameter.equals("list")) {
334
} else if (parameter.equals("rcfile")) {
335
if (++i == arg.length) {
340
} else if (parameter.equals("setvar")) {
341
if (++i == arg.length) {
345
varSettings = arg[i];
346
} else if (parameter.equals("sql")) {
347
noinput = true; // but turn back on if file "-" specd.
349
if (++i == arg.length) {
355
if (sqlText.charAt(sqlText.length() - 1) != ';') {
358
} else if (parameter.equals("debug")) {
360
} else if (parameter.equals("noautofile")) {
362
} else if (parameter.equals("autocommit")) {
364
} else if (parameter.equals("stdinput")) {
366
stdinputOverride = Boolean.TRUE;
367
} else if (parameter.equals("noinput")) {
369
stdinputOverride = Boolean.FALSE;
370
} else if (parameter.equals("driver")) {
371
if (++i == arg.length) {
376
} else if (parameter.equals("inlinerc")) {
377
if (++i == arg.length) {
389
// If an inline RC file was specified, don't worry about the targetDb
390
if (rcParams == null) {
391
if (++i == arg.length) {
401
if (sqlText != null) {
403
tmpFile = File.createTempFile("sqltool-", ".sql");
405
//(new java.io.FileWriter(tmpFile)).write(sqlText);
406
java.io.FileWriter fw = new java.io.FileWriter(tmpFile);
408
fw.write("/* " + (new java.util.Date()) + ". "
409
+ SqlTool.class.getName()
410
+ " command-line SQL. */\n\n");
411
fw.write(sqlText + '\n');
414
} catch (IOException ioe) {
415
exitMain(4, "Failed to write given sql to temp file: "
422
if (stdinputOverride != null) {
423
noinput = !stdinputOverride.booleanValue();
426
interactive = (!noinput) && (arg.length <= i + 1);
428
if (arg.length == i + 2 && arg[i + 1].equals("-")) {
429
if (stdinputOverride == null) {
432
} else if (arg.length > i + 1) {
434
// I.e., if there are any SQL files specified.
436
new File[arg.length - i - 1 + ((stdinputOverride == null ||!stdinputOverride.booleanValue()) ? 0
440
System.err.println("scriptFiles has "
441
+ scriptFiles.length + " elements");
444
while (i + 1 < arg.length) {
445
scriptFiles[scriptIndex++] = new File(arg[++i]);
448
if (stdinputOverride != null
449
&& stdinputOverride.booleanValue()) {
450
scriptFiles[scriptIndex++] = null;
454
} catch (BadCmdline bcl) {
455
exitMain(2, SYNTAX_MESSAGE);
460
RCData conData = null;
462
// Use the inline RC file if it was specified
463
if (rcParams != null) {
464
rcFields = new HashMap();
467
varParser(rcParams, rcFields, true);
468
} catch (SqlToolException e) {
469
exitMain(24, e.getMessage());
473
rcUrl = (String) rcFields.get("url");
474
rcUsername = (String) rcFields.get("user");
475
rcDriver = (String) rcFields.get("driver");
476
rcCharset = (String) rcFields.get("charset");
477
rcTruststore = (String) rcFields.get("truststore");
478
rcPassword = promptForPassword(rcUsername);
479
conData = new RCData(CMDLINE_ID, rcUrl, rcUsername,
480
rcPassword, rcDriver, rcCharset,
482
} catch (SqlToolException e) {
484
} catch (Exception e) {
485
exitMain(1, "Invalid inline RC file specified: "
492
conData = new RCData(new File((rcFile == null)
494
: rcFile), targetDb);
495
} catch (Exception e) {
497
1, "Failed to retrieve connection info for database '"
498
+ targetDb + "': " + e.getMessage());
515
conn = conData.getConnection(
516
driver, System.getProperty("sqlfile.charset"),
517
System.getProperty("javax.net.ssl.trustStore"));
519
conn.setAutoCommit(autoCommit);
521
DatabaseMetaData md = null;
523
if (interactive && (md = conn.getMetaData()) != null) {
524
System.out.println("JDBC Connection established to a "
525
+ md.getDatabaseProductName() + " v. "
526
+ md.getDatabaseProductVersion()
527
+ " database as '" + md.getUserName()
530
} catch (Exception e) {
533
// Let's not continue as if nothing is wrong.
535
"Failed to get a connection to " + conData.url + " as "
536
+ conData.username + ". " + e.getMessage());
541
File[] emptyFileArray = {};
542
File[] singleNullFileArray = { null };
543
File autoFile = null;
545
if (interactive &&!noautoFile) {
546
autoFile = new File(System.getProperty("user.home")
549
if ((!autoFile.isFile()) ||!autoFile.canRead()) {
554
if (scriptFiles == null) {
556
// I.e., if no SQL files given on command-line.
557
// Input file list is either nothing or {null} to read stdin.
558
scriptFiles = (noinput ? emptyFileArray
559
: singleNullFileArray);
562
int numFiles = scriptFiles.length;
564
if (tmpFile != null) {
568
if (autoFile != null) {
572
SqlFile[] sqlFiles = new SqlFile[numFiles];
573
HashMap userVars = new HashMap();
575
if (varSettings != null) {
576
varParser(varSettings, userVars, false);
579
// We print version before execing this one.
580
int interactiveFileIndex = -1;
585
if (autoFile != null) {
586
sqlFiles[fileIndex++] = new SqlFile(autoFile, false,
590
if (tmpFile != null) {
591
sqlFiles[fileIndex++] = new SqlFile(tmpFile, false, userVars);
594
for (int j = 0; j < scriptFiles.length; j++) {
595
if (interactiveFileIndex < 0 && interactive) {
596
interactiveFileIndex = fileIndex;
599
sqlFiles[fileIndex++] = new SqlFile(scriptFiles[j],
600
interactive, userVars);
602
} catch (IOException ioe) {
605
} catch (Exception e) {}
607
exitMain(2, ioe.getMessage());
612
int retval = 0; // Value we will return via System.exit().
615
for (int j = 0; j < sqlFiles.length; j++) {
616
if (j == interactiveFileIndex) {
617
System.out.print("SqlTool v. " + revnum
621
sqlFiles[j].execute(conn, coeOverride);
623
} catch (IOException ioe) {
624
System.err.println("Failed to execute SQL: " + ioe.getMessage());
628
// These two Exception types are handled properly inside of SqlFile.
629
// We just need to return an appropriate error status.
630
} catch (SqlToolError ste) {
633
// Should not be handling SQLExceptions here! SqlFile should handle
635
} catch (SQLException se) {
640
} catch (Exception e) {}
643
// Taking file removal out of final block because this is good debug
644
// info to keep around if the program aborts.
645
if (tmpFile != null &&!tmpFile.delete()) {
647
"Error occurred while trying to remove temp file '" + tmpFile