Skip to content

Commit

Permalink
[SQLLINE-67] Add '-log' command-line argument, equivalent to '!record…
Browse files Browse the repository at this point in the history
…' command (Sergey Nuyanzin)

Close file writers and buffers at the end of processing.
  • Loading branch information
snuyanzin authored and julianhyde committed Aug 8, 2018
1 parent 4bb2ed1 commit f62bb74
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 32 deletions.
7 changes: 7 additions & 0 deletions src/main/java/sqlline/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -898,11 +898,18 @@ public void closeall(String line, DispatchCallback callback) {

/**
* Closes the current connection.
* Closes the current file writer.
*
* @param line Command line
* @param callback Callback for command status
*/
public void close(String line, DispatchCallback callback) {
// close file writer
if (sqlLine.getRecordOutputFile() != null) {
// instead of line could be any string
stopRecording(line, callback);
}

DatabaseConnection databaseConnection = sqlLine.getDatabaseConnection();
if (databaseConnection == null) {
callback.setToFailure();
Expand Down
42 changes: 28 additions & 14 deletions src/main/java/sqlline/SqlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ Status initArgs(String[] args, DispatchCallback callback) {
String pass = null;
String url = null;
String nickname = null;
String logFile = null;

for (int i = 0; i < args.length; i++) {
if (args[i].equals("--help") || args[i].equals("-h")) {
Expand Down Expand Up @@ -537,20 +538,29 @@ Status initArgs(String[] args, DispatchCallback callback) {
continue;
}

if (args[i].equals("-d")) {
driver = args[++i];
} else if (args[i].equals("-n")) {
user = args[++i];
} else if (args[i].equals("-p")) {
pass = args[++i];
} else if (args[i].equals("-u")) {
url = args[++i];
} else if (args[i].equals("-e")) {
commands.add(args[++i]);
} else if (args[i].equals("-f")) {
getOpts().setRun(args[++i]);
} else if (args[i].equals("-nn")) {
nickname = args[++i];
if (args[i].charAt(0) == '-') {
if (i == args.length - 1) {
return Status.ARGS;
}
if (args[i].equals("-d")) {
driver = args[++i];
} else if (args[i].equals("-n")) {
user = args[++i];
} else if (args[i].equals("-p")) {
pass = args[++i];
} else if (args[i].equals("-u")) {
url = args[++i];
} else if (args[i].equals("-e")) {
commands.add(args[++i]);
} else if (args[i].equals("-f")) {
getOpts().setRun(args[++i]);
} else if (args[i].equals("-log")) {
logFile = args[++i];
} else if (args[i].equals("-nn")) {
nickname = args[++i];
} else {
return Status.ARGS;
}
} else {
files.add(args[i]);
}
Expand All @@ -571,6 +581,10 @@ Status initArgs(String[] args, DispatchCallback callback) {
dispatch(COMMAND_PREFIX + "nickname " + nickname, new DispatchCallback());
}

if (logFile != null) {
dispatch(COMMAND_PREFIX + "record " + logFile, new DispatchCallback());
}

// now load properties files
for (String file : files) {
dispatch(COMMAND_PREFIX + "properties " + file, new DispatchCallback());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/sqlline/SqlLine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ cmd-usage: Usage: java sqlline.SqlLine \n \
\ -d <driver class> the driver class to use\n \
\ -nn <nickname> nickname for the connection\n \
\ -f <file> script file to execute (same as --run)\n \
\ -log <file> file to write output\n \
\ --color=[true/false] control whether color is used for display\n \
\ --csvDelimeter=[delimiter] Delimiter in csv outputFormat\n \
\ --csvQuoteCharacter=[char] Quote character in csv outputFormat\n \
Expand Down
60 changes: 42 additions & 18 deletions src/test/java/sqlline/SqlLineArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package sqlline;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -79,17 +80,16 @@ private static Pair run(String... args) throws Throwable {
beeLine.setErrorStream(beelineOutputStream);
final InputStream is = new ByteArrayInputStream(new byte[0]);
SqlLine.Status status = beeLine.begin(args, is, false);

return new Pair(status, os.toString("UTF8"));
}

private Pair runScript(File scriptFile, boolean flag)
private Pair runScript(File scriptFile, boolean flag, String outputFileName)
throws Throwable {
return runScript(connectionSpec, scriptFile, flag);
return runScript(connectionSpec, scriptFile, flag, outputFileName);
}

private static Pair runScript(ConnectionSpec connectionSpec, File scriptFile,
boolean flag) throws Throwable {
boolean flag, String outputFileName) throws Throwable {
List<String> args = new ArrayList<String>();
Collections.addAll(args,
"-d", connectionSpec.driver,
Expand All @@ -102,6 +102,10 @@ private static Pair runScript(ConnectionSpec connectionSpec, File scriptFile,
} else {
args.add("--run=" + scriptFile.getAbsolutePath());
}
if (outputFileName != null) {
args.add("-log");
args.add(outputFileName);
}
return run(args.toArray(new String[args.size()]));
}

Expand All @@ -125,7 +129,7 @@ private void checkScriptFile(String scriptText, boolean flag,
os.print(scriptText);
os.close();

Pair pair = runScript(scriptFile, flag);
Pair pair = runScript(scriptFile, flag, null);

// Check output before status. It gives a better clue what went wrong.
assertThat(toLinux(pair.output), outputMatcher);
Expand Down Expand Up @@ -213,12 +217,35 @@ public void testScriptFilenameWithSpace() throws Throwable {
os.print(scriptText);
os.close();

Pair pair = runScript(scriptFile, true);
Pair pair = runScript(scriptFile, true, null);
assertThat(pair.status, equalTo(SqlLine.Status.OK));
assertThat(pair.output,
allOf(containsString(" 33 "), containsString(" 123 ")));
}

@Test
public void testScriptWithOutput() throws Throwable {
final String scriptText = "values 100 + 123;\n"
+ "-- a comment\n"
+ "values 100 + 253;\n";

File scriptFile = File.createTempFile("Script file name", ".sql");
scriptFile.deleteOnExit();
PrintStream os = new PrintStream(new FileOutputStream(scriptFile));
os.print(scriptText);
os.close();

File outputFile = new File("testScriptWithOutput.out");
System.out.println("outputFile " + outputFile.getAbsoluteFile());
outputFile.deleteOnExit();
runScript(scriptFile, true, outputFile.getAbsolutePath());
assertFileContains(outputFile,
allOf(containsString("| 223 |"),
containsString("| 353 |")));
final boolean delete = outputFile.delete();
assertThat(delete, is(true));
}

/**
* Values that contain null.
*/
Expand Down Expand Up @@ -445,11 +472,7 @@ public void testRecordHome() throws Throwable {
// Now check that the right stuff got into the file.
assertFileContains(file, RegexMatcher.of(s));
final boolean delete = file.delete();
if (File.separatorChar == '/') {
// For unknown reasons, File.delete returns false on Windows, so only
// check on Linux and macOS.
assertThat(delete, is(true));
}
assertThat(delete, is(true));
}

/**
Expand Down Expand Up @@ -503,18 +526,19 @@ public void testRecordFilenameWithSpace() throws Throwable {
+ "5/7 !record\n"));
}

private void assertFileContains(File file, RegexMatcher matcher)
private void assertFileContains(File file, Matcher matcher)
throws IOException {
final FileReader fileReader = new FileReader(file);
final BufferedReader br = new BufferedReader(new FileReader(file));
final StringWriter stringWriter = new StringWriter();
final char[] chars = new char[1024];
for (;;) {
int c = fileReader.read(chars);
if (c < 0) {
final String line = br.readLine();
if (line == null) {
break;
}
stringWriter.write(chars, 0, c);
stringWriter.write(line);
stringWriter.write("\n");
}
br.close();
assertThat(toLinux(stringWriter.toString()), matcher);
}

Expand Down Expand Up @@ -610,7 +634,7 @@ public void testNegativeScriptFile() throws Throwable {
final boolean delete = scriptFile.delete();
assertThat(delete, is(true));

Pair pair = runScript(scriptFile, true);
Pair pair = runScript(scriptFile, true, null);
assertThat(pair.status, equalTo(SqlLine.Status.OTHER));
assertThat(pair.output, not(containsString(" 123 ")));
}
Expand Down

0 comments on commit f62bb74

Please sign in to comment.