Skip to content

Commit

Permalink
[SQLLINE-146] Add read-only "version" property
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin authored and julianhyde committed Oct 2, 2018
1 parent 64f8e6a commit c222391
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/docbkx/manual.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,12 @@ java.sql.SQLException: ORA-00942: table or view does not exist
Defaults to <literal>false</literal>.
</para>
</sect1>
<sect1 id="setting_version">
<title>version</title>
<para>
Show the current sqlline version. The property is read only.
</para>
</sect1>
</chapter>

<chapter id="known_drivers">
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/sqlline/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public Application() {
* @see #DEFAULT_APP_INFO_MESSAGE
*/
public String getInfoMessage() throws Exception {
return getVersion();
}

public String getVersion() throws Exception {
final String path = "/META-INF/maven/sqlline/sqlline/pom.properties";
InputStream inputStream = getClass().getResourceAsStream(path);
Properties properties = new Properties();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/sqlline/SqlLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ String getApplicationTitle() {
}
}

String getVersion() {
try {
return application.getVersion();
} catch (Exception e) {
handleException(e);
return Application.DEFAULT_APP_INFO_MESSAGE;
}
}

static String getApplicationContactInformation() {
return getManifestAttribute("Implementation-Vendor");
}
Expand Down
34 changes: 24 additions & 10 deletions src/main/java/sqlline/SqlLineOpts.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SqlLineOpts implements Completer {
public static final String DEFAULT_TRANSACTION_ISOLATION =
"TRANSACTION_REPEATABLE_READ";
private SqlLine sqlLine;
private final String version;
private boolean autoSave = false;
private boolean silent = false;
private boolean color = false;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class SqlLineOpts implements Completer {

public SqlLineOpts(SqlLine sqlLine) {
this.sqlLine = sqlLine;
this.version = sqlLine.getVersion();
}

public SqlLineOpts(SqlLine sqlLine, Properties props) {
Expand Down Expand Up @@ -144,6 +146,10 @@ public void save(OutputStream out) throws IOException {
// the terminal configuration
props.remove(PROPERTY_PREFIX + "maxwidth");

// don't save version: it is automatically set based on
// Application#getVersion
props.remove("version");

props.store(out, sqlLine.getApplicationTitle());
} catch (Exception e) {
sqlLine.handleException(e);
Expand Down Expand Up @@ -245,16 +251,20 @@ public boolean set(String key, String value, boolean quiet) {
return true;
} catch (Exception e) {
if (!quiet) {
// need to use System.err here because when bad command args
// are passed this is called before init is done, meaning
// that sqlline's error() output chokes because it depends
// on properties like text coloring that can get set in
// arbitrary order.
System.err.println(
sqlLine.loc(
"error-setting",
key,
e.getCause() == null ? e : e.getCause()));
if ("version".equals(key)) {
sqlLine.error(sqlLine.loc("property-readonly", key));
} else {
// need to use System.err here because when bad command args
// are passed this is called before init is done, meaning
// that sqlline's error() output chokes because it depends
// on properties like text coloring that can get set in
// arbitrary order.
System.err.println(
sqlLine.loc(
"error-setting",
key,
e.getCause() == null ? e : e.getCause()));
}
}
return false;
}
Expand Down Expand Up @@ -534,6 +544,10 @@ public File getPropertiesFile() {
return rcFile;
}

public String getVersion() {
return version;
}

public void setRun(String runFile) {
this.runFile = runFile;
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/sqlline/SqlLine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ variables:\
\ntimestampFormat pattern Format timestamps using SimpleDateFormat pattern\
\ntrimScripts true/false Remove trailing spaces from lines read from script\
\n files\
\nverbose true/false Show verbose error messages and debug info
\nverbose true/false Show verbose error messages and debug info\
\nversion version Show the current sqlline version.\
\n The property is read only.
help-save: Save the current variables and aliases
help-native: Show the database''s native SQL for a command
help-alias: Create a new command alias
Expand Down Expand Up @@ -133,6 +135,7 @@ possible-methods: Possible methods:
closing: Closing: {0}
already-closed: Connection is already closed.
error-setting: Error setting configuration: {0}: {1}
property-readonly: {0} property is read only
no-method: No method matching "{0}" was found in {1}.


Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/sqlline/manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ timeformat
timestampformat
trimscripts
verbose
version
JDBC Driver Support
Project Information
SQLLine Manual
Expand Down Expand Up @@ -248,6 +249,7 @@ timeformat
timestampformat
trimscripts
verbose
version
JDBC Driver Support
Introduction

Expand Down Expand Up @@ -1128,6 +1130,8 @@ timestampFormat pattern Format timestamps using SimpleDateFormat pattern
trimScripts true/false Remove trailing spaces from lines read from script
files
verbose true/false Show verbose error messages and debug info
version version Show the current sqlline version.
The property is read only.

Comments, bug reports, and patches go to ???

Expand Down Expand Up @@ -2034,6 +2038,8 @@ timeformat
timestampformat
trimscripts
verbose
version

autocommit

If true, then new connections will have autocommit set, otherwise, transactions will need to be explicitely committed or rolled back. Defaults to true. To change the autocommit status for a connection that is already open and active, use the autocommit command instead.
Expand Down Expand Up @@ -2142,6 +2148,10 @@ verbose

If true, then print out the entire java stack trace whenever an error occurs, as well as displaying debugging information. Defaults to false.

version

Show the current sqlline version. The property is read only.

JDBC Driver Support

Project Information
14 changes: 14 additions & 0 deletions src/test/java/sqlline/SqlLineArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,20 @@ public void testCustomOpts() throws Throwable {
containsString("custom_null"));
}

@Test
public void testVersion() throws Throwable {
final String script = "!set\n";
checkScriptFile(script, true, equalTo(SqlLine.Status.OK),
containsString(new Application().getVersion()));
}

@Test
public void testSetVersion() throws Throwable {
final String script = "!set version test-version\n";
checkScriptFile(script, true, equalTo(SqlLine.Status.OK),
containsString("version property is read only"));
}

// Work around compile error in JDK 1.6
private static Matcher<String> allOf(Matcher<String> m1,
Matcher<String> m2) {
Expand Down

0 comments on commit c222391

Please sign in to comment.