Skip to content

Commit

Permalink
[SQLLINE-66] Add dateFormat, timeFormat and timestampFormat propertie…
Browse files Browse the repository at this point in the history
…s (Sergey Nuyanzin)
  • Loading branch information
snuyanzin authored and julianhyde committed Jul 25, 2018
1 parent 5a8f8bf commit a75929d
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -2845,4 +2845,4 @@
<p>
</p>
</div>
</div></body></html>
</div></body></html>
38 changes: 37 additions & 1 deletion src/docbkx/manual.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2614,6 +2614,18 @@ java.sql.SQLException: ORA-00942: table or view does not exist
Defaults to <literal>false</literal>.
</para>
</sect1>
<sect1 id="setting_dateformat">
<title>dateformat</title>
<para>
The format for how date values are displayed. Setting
to <literal>default</literal> causes date values
to be fetched and rendered via ResultSet.getString.
Any other setting results in fetch via ResultSet.getObject
and rendering via <ulink url="https://docs.oracle.com/javase/9/docs/api/java/text/SimpleDateFormat.html">
java.text.SimpleDateFormat</ulink>. For example,
the setting "YYYY_MM_dd" yields values like "1970_01_01".
</para>
</sect1>
<sect1 id="setting_fastconnect">
<title>fastconnect</title>
<para>
Expand Down Expand Up @@ -2710,7 +2722,7 @@ java.sql.SQLException: ORA-00942: table or view does not exist
and rendering via <ulink url="https://docs.oracle.com/javase/9/docs/api/java/text/DecimalFormat.html">
java.text.DecimalFormat</ulink>. For example,
the setting "0.###E0" yields scientific notation
with up to three fractional digits.
with up to three fractional digits, such as "6.022E23".
</para>
</sect1>
<sect1 id="setting_outputformat">
Expand Down Expand Up @@ -2775,6 +2787,30 @@ java.sql.SQLException: ORA-00942: table or view does not exist
<literal>false</literal>.
</para>
</sect1>
<sect1 id="setting_timeformat">
<title>timeformat</title>
<para>
The format for how time values are displayed. Setting
to <literal>default</literal> causes time values
to be fetched and rendered via ResultSet.getString.
Any other setting results in fetch via ResultSet.getObject
and rendering via <ulink url="https://docs.oracle.com/javase/9/docs/api/java/text/SimpleDateFormat.html">
java.text.SimpleDateFormat</ulink>. For example,
the setting "HH:mm:ss" yields values like "14:12:11".
</para>
</sect1>
<sect1 id="setting_timestampformat">
<title>timestampformat</title>
<para>
The format for how date values are displayed. Setting
to <literal>default</literal> causes date values
to be fetched and rendered via ResultSet.getString.
Any other setting results in fetch via ResultSet.getObject
and rendering via <ulink url="https://docs.oracle.com/javase/9/docs/api/java/text/SimpleDateFormat.html">
java.text.SimpleDateFormat</ulink>. For example,
the setting "dd/MM/YYYY'T'HH:mm:ss" yields values like "01/01/1970T12:32:12".
</para>
</sect1>
<sect1 id="setting_trimscripts">
<title>trimscripts</title>
<para>
Expand Down
61 changes: 45 additions & 16 deletions src/main/java/sqlline/Rows.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -34,17 +37,36 @@ abstract class Rows implements Iterator<Rows.Row> {
final Map<TableKey, Set<String>> tablePrimaryKeysCache =
new HashMap<TableKey, Set<String>>();
final NumberFormat numberFormat;
final DateFormat dateFormat;
final DateFormat timeFormat;
final DateFormat timestampFormat;

Rows(SqlLine sqlLine, ResultSet rs) throws SQLException {
this.sqlLine = sqlLine;
rsMeta = rs.getMetaData();
int count = rsMeta.getColumnCount();
primaryKeys = new Boolean[count];
if (sqlLine.getOpts().getNumberFormat().equals("default")) {
if (sqlLine.getOpts().getNumberFormat().equals(SqlLineOpts.DEFAULT)) {
numberFormat = null;
} else {
numberFormat = new DecimalFormat(sqlLine.getOpts().getNumberFormat());
}
if (sqlLine.getOpts().getDateFormat().equals(SqlLineOpts.DEFAULT)) {
dateFormat = null;
} else {
dateFormat = new SimpleDateFormat(sqlLine.getOpts().getDateFormat());
}
if (sqlLine.getOpts().getTimeFormat().equals(SqlLineOpts.DEFAULT)) {
timeFormat = null;
} else {
timeFormat = new SimpleDateFormat(sqlLine.getOpts().getTimeFormat());
}
if (sqlLine.getOpts().getTimestampFormat().equals(SqlLineOpts.DEFAULT)) {
timestampFormat = null;
} else {
timestampFormat =
new SimpleDateFormat(sqlLine.getOpts().getTimestampFormat());
}
}

public void remove() {
Expand Down Expand Up @@ -165,14 +187,7 @@ class Row {
case Types.DOUBLE:
case Types.DECIMAL:
case Types.NUMERIC:
o = rs.getObject(i + 1);
if (o == null) {
values[i] = "null";
} else if (numberFormat != null) {
values[i] = numberFormat.format(o);
} else {
values[i] = o.toString();
}
setFormat(rs.getObject(i + 1), numberFormat, i);
break;
case Types.BIT:
case Types.CLOB:
Expand All @@ -183,12 +198,16 @@ class Row {
case Types.ROWID:
case Types.NCLOB:
case Types.SQLXML:
o = rs.getObject(i + 1);
if (o == null) {
values[i] = "null";
} else {
values[i] = o.toString();
}
setFormat(rs.getObject(i + 1), null, i);
break;
case Types.TIME:
setFormat(rs.getObject(i + 1), timeFormat, i);
break;
case Types.DATE:
setFormat(rs.getObject(i + 1), dateFormat, i);
break;
case Types.TIMESTAMP:
setFormat(rs.getObject(i + 1), timestampFormat, i);
break;
default:
values[i] = rs.getString(i + 1);
Expand All @@ -197,6 +216,16 @@ class Row {
sizes[i] = values[i] == null ? 1 : values[i].length();
}
}

private void setFormat(Object o, Format format, int i) {
if (o == null) {
values[i] = "null";
} else if (format != null) {
values[i] = format.format(o);
} else {
values[i] = o.toString();
}
}
}

/**
Expand Down Expand Up @@ -236,7 +265,7 @@ private Set<String> loadAndCachePrimaryKeysForTable(TableKey tableKey) {
* schema and table name). The returned set may be cached as a result of
* previous requests for the same table key.
*
* The result cannot be considered authoritative as since it depends on
* <p>The result cannot be considered authoritative as since it depends on
* whether the JDBC driver property implements
* {@link java.sql.ResultSetMetaData#getTableName} and many drivers/databases
* do not.
Expand Down
51 changes: 49 additions & 2 deletions src/main/java/sqlline/SqlLineOpts.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

import jline.TerminalFactory;
Expand All @@ -27,6 +28,8 @@ class SqlLineOpts implements Completer {
public static final String PROPERTY_PREFIX = "sqlline.";
public static final String PROPERTY_NAME_EXIT =
PROPERTY_PREFIX + "system.exit";
public static final String DEFAULT = "default";
public static final Date TEST_DATE = new Date();
private SqlLine sqlLine;
private boolean autoSave = false;
private boolean silent = false;
Expand All @@ -41,7 +44,10 @@ class SqlLineOpts implements Completer {
private boolean showElapsedTime = true;
private boolean showWarnings = true;
private boolean showNestedErrs = false;
private String numberFormat = "default";
private String numberFormat = DEFAULT;
private String dateFormat = DEFAULT;
private String timeFormat = DEFAULT;
private String timestampFormat = DEFAULT;
private int maxWidth = TerminalFactory.get().getWidth();
private int maxHeight = TerminalFactory.get().getHeight();
private int maxColumnWidth = 15;
Expand Down Expand Up @@ -233,7 +239,11 @@ public boolean set(String key, String value, boolean quiet) {
// 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));
System.err.println(
sqlLine.loc(
"error-setting",
key,
e.getCause() == null ? e : e.getCause()));
}
return false;
}
Expand Down Expand Up @@ -295,6 +305,30 @@ public String getNumberFormat() {
return this.numberFormat;
}

public String getDateFormat() {
return this.dateFormat;
}

public void setDateFormat(String dateFormat) {
this.dateFormat = getValidDateTimePatternOrThrow(dateFormat);
}

public String getTimeFormat() {
return this.timeFormat;
}

public void setTimeFormat(String timeFormat) {
this.timeFormat = getValidDateTimePatternOrThrow(timeFormat);
}

public String getTimestampFormat() {
return this.timestampFormat;
}

public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = getValidDateTimePatternOrThrow(timestampFormat);
}

public void setMaxWidth(int maxWidth) {
this.maxWidth = maxWidth;
}
Expand Down Expand Up @@ -446,6 +480,19 @@ public void setRun(String runFile) {
public String getRun() {
return this.runFile;
}

private String getValidDateTimePatternOrThrow(String dateTimePattern) {
if (DEFAULT.equals(dateTimePattern)) {
return dateTimePattern;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateTimePattern);
sdf.format(TEST_DATE);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
}
return dateTimePattern;
}
}

// End SqlLineOpts.java
9 changes: 9 additions & 0 deletions src/main/resources/sqlline/SqlLine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ help-set: Set a sqlline variable\
\nautoSave true/false Automatically save preferences\
\ncolor true/false Control whether color is used\
\n for display\
\ndateFormat pattern Format dates using\
\n SimpleDateFormat pattern\
\nfastConnect true/false Skip building table/column list\
\n for tab-completion\
\nforce true/false Continue running script even\
Expand Down Expand Up @@ -102,8 +104,12 @@ help-set: Set a sqlline variable\
\nshowNestedErrs true/false Display nested errors\
\nshowWarnings true/false Display connection warnings\
\nsilent true/false Be more silent\
\ntimeFormat pattern Format times using\
\n SimpleDateFormat pattern\
\ntimeout integer Query timeout in seconds; less\
\n than zero means no timeout\
\ntimestampFormat pattern Format timestamps using\
\n SimpleDateFormat pattern\
\ntrimScripts true/false Remove trailing spaces from\
\n lines read from script files\
\nverbose true/false Show verbose error messages and\
Expand Down Expand Up @@ -215,6 +221,9 @@ cmd-usage: Usage: java sqlline.SqlLine \n \
\ --showWarnings=[true/false] display connection warnings\n \
\ --showNestedErrs=[true/false] display nested errors\n \
\ --numberFormat=[pattern] format numbers using DecimalFormat pattern\n \
\ --dateFormat=[pattern] format dates using SimpleDateFormat pattern\n \
\ --timeFormat=[pattern] format times using SimpleDateFormat pattern\n \
\ --timestampFormat=[pattern] format timestamps using SimpleDateFormat pattern\n \
\ --force=[true/false] continue running script even after errors\n \
\ --maxWidth=MAXWIDTH the maximum width of the terminal\n \
\ --maxColumnWidth=MAXCOLWIDTH the maximum width to use when displaying columns\n \
Expand Down
25 changes: 23 additions & 2 deletions src/main/resources/sqlline/manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Parameter Reference
autocommit
autosave
color
dateformat
fastconnect
force
headerinterval
Expand All @@ -111,6 +112,8 @@ shownestederrs
showtime
showwarnings
silent
timeformat
timestampformat
trimscripts
verbose
JDBC Driver Support
Expand Down Expand Up @@ -209,6 +212,7 @@ Parameter Reference
autocommit
autosave
color
dateformat
fastconnect
force
headerinterval
Expand All @@ -225,6 +229,8 @@ shownestederrs
showtime
showwarnings
silent
timeformat
timestampformat
trimscripts
verbose
JDBC Driver Support
Expand Down Expand Up @@ -1889,6 +1895,7 @@ Parameter Reference
autocommit
autosave
color
dateformat
fastconnect
force
headerinterval
Expand All @@ -1905,6 +1912,8 @@ shownestederrs
showtime
showwarnings
silent
timeformat
timestampformat
trimscripts
verbose
autocommit
Expand All @@ -1919,9 +1928,13 @@ color

If true, then output to the terminal will use color for a more pleasing visual experience. Requires that the terminal support ANSI control codes (most do). Defaults to false.

dateformat

The format for how date values are displayed. Setting to default causes date values to be fetched and rendered via ResultSet.getString. Any other setting results in fetch via ResultSet.getObject and rendering via java.text.SimpleDateFormat. For example, the setting "YYYY_MM_dd" yields values like "1970_01_01".

fastconnect

When false, any new connection will cause SQLLine to access information about the available tables and columns in order to provide them as candidates for tab-completion. This can be a very slow operation for some databases, do by default it is off. Table and column information can always be explicitely retrieved using the rehash command.
When false, any new connection will cause SQLLine to access information about the available tables and columns in order to provide them as candidates for tab-completion. This can be a very slow operation for some databases, do by default it is off. Table and column information can always be explicitly retrieved using the rehash command.

force

Expand Down Expand Up @@ -1953,7 +1966,7 @@ The maximum width to display before truncating data when using the "table" outpu

numberformat

The format for how numeric values are displayed. Setting to default causes numeric values to be fetched and rendered via ResultSet.getString. Any other setting results in fetch via ResultSet.getObject and rendering via java.text.DecimalFormat. For example, the setting "0.###E0" yields scientific notation with up to three fractional digits.
The format for how numeric values are displayed. Setting to default causes numeric values to be fetched and rendered via ResultSet.getString. Any other setting results in fetch via ResultSet.getObject and rendering via java.text.DecimalFormat. For example, the setting "0.###E0" yields scientific notation with up to three fractional digits, values like "6.022E23".

outputformat

Expand Down Expand Up @@ -1983,6 +1996,14 @@ silent

If true, then reduce the amount of informational messages displayed. Useful for redirecting a sqlline command to a file for later parsing. Defaults to false.

timeformat

The format for how time values are displayed. Setting to default causes time values to be fetched and rendered via ResultSet.getString. Any other setting results in fetch via ResultSet.getObject and rendering via java.text.SimpleDateFormat. For example, the setting "HH:mm:ss" yields values like "14:12:11".

timestampformat

The format for how timestamp values are displayed. Setting to default causes timestamp values to be fetched and rendered via ResultSet.getString. Any other setting results in fetch via ResultSet.getObject and rendering via java.text.SimpleDateFormat. For example, the setting "dd/MM/YYYY'T'HH:mm:ss" yields values like "01/01/1970T12:32:12".

trimscripts

If true, then trim leading and trailing whitespace from lines as they are processed in scripts; otherwise, preserve whitespace. Defaults to true.
Expand Down
Loading

0 comments on commit a75929d

Please sign in to comment.