From 11f6fbcfb4894b83591a8c4a070613db2ae1fdef Mon Sep 17 00:00:00 2001 From: danap Date: Sun, 10 May 2015 13:19:50 -0600 Subject: [PATCH] Date, Time, Timestamp Distinguishing String Formats. --- src/main/java/org/sqlite/SQLiteConfig.java | 30 +++++++++++++++++-- .../java/org/sqlite/core/CoreConnection.java | 4 +++ .../sqlite/core/CorePreparedStatement.java | 2 +- .../java/org/sqlite/jdbc3/JDBC3ResultSet.java | 10 +++---- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/sqlite/SQLiteConfig.java b/src/main/java/org/sqlite/SQLiteConfig.java index 1335c59fc..18cce49e7 100755 --- a/src/main/java/org/sqlite/SQLiteConfig.java +++ b/src/main/java/org/sqlite/SQLiteConfig.java @@ -48,11 +48,15 @@ public class SQLiteConfig public final int busyTimeout; /* Date storage class*/ - public final static String DEFAULT_DATE_STRING_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; + public final static String DEFAULT_DATE_STRING_FORMAT = "yyyy-MM-dd"; + public final static String DEFAULT_TIME_STRING_FORMAT = "HH:mm:ss"; + public final static String DEFAULT_TIMESTAMP_STRING_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS"; public DateClass dateClass; public DatePrecision datePrecision; public long dateMultiplier; public String dateStringFormat; + public String timeStringFormat; + public String timestampStringFormat; /** * Default constructor. @@ -88,6 +92,8 @@ public SQLiteConfig(Properties prop) { datePrecision = DatePrecision.getPrecision(pragmaTable.getProperty(Pragma.DATE_PRECISION.pragmaName, DatePrecision.MILLISECONDS.name())); dateMultiplier = (datePrecision == DatePrecision.MILLISECONDS) ? 1L : 1000L; dateStringFormat = pragmaTable.getProperty(Pragma.DATE_STRING_FORMAT.pragmaName, DEFAULT_DATE_STRING_FORMAT); + timeStringFormat = pragmaTable.getProperty(Pragma.TIME_STRING_FORMAT.pragmaName, DEFAULT_TIME_STRING_FORMAT); + timestampStringFormat = pragmaTable.getProperty(Pragma.TIMESTAMP_STRING_FORMAT.pragmaName, DEFAULT_TIMESTAMP_STRING_FORMAT); busyTimeout = Integer.parseInt(pragmaTable.getProperty(Pragma.BUSY_TIMEOUT.pragmaName, "3000")); } @@ -119,6 +125,8 @@ public void apply(Connection conn) throws SQLException { pragmaParams.remove(Pragma.DATE_PRECISION.pragmaName); pragmaParams.remove(Pragma.DATE_CLASS.pragmaName); pragmaParams.remove(Pragma.DATE_STRING_FORMAT.pragmaName); + pragmaParams.remove(Pragma.TIME_STRING_FORMAT.pragmaName); + pragmaParams.remove(Pragma.TIMESTAMP_STRING_FORMAT.pragmaName); Statement stat = conn.createStatement(); try { @@ -213,6 +221,8 @@ public Properties toProperties() { pragmaTable.setProperty(Pragma.DATE_CLASS.pragmaName, dateClass.getValue()); pragmaTable.setProperty(Pragma.DATE_PRECISION.pragmaName, datePrecision.getValue()); pragmaTable.setProperty(Pragma.DATE_STRING_FORMAT.pragmaName, dateStringFormat); + pragmaTable.setProperty(Pragma.TIME_STRING_FORMAT.pragmaName, timeStringFormat); + pragmaTable.setProperty(Pragma.TIMESTAMP_STRING_FORMAT.pragmaName, timestampStringFormat); return pragmaTable; } @@ -274,7 +284,9 @@ public static enum Pragma { TRANSACTION_MODE("transaction_mode", toStringArray(TransactionMode.values())), DATE_PRECISION("date_precision", "\"seconds\": Read and store integer dates as seconds from the Unix Epoch (SQLite standard).\n\"milliseconds\": (DEFAULT) Read and store integer dates as milliseconds from the Unix Epoch (Java standard).", toStringArray(DatePrecision.values())), DATE_CLASS("date_class", "\"integer\": (Default) store dates as number of seconds or milliseconds from the Unix Epoch\n\"text\": store dates as a string of text\n\"real\": store dates as Julian Dates", toStringArray(DateClass.values())), - DATE_STRING_FORMAT("date_string_format", "Format to store and retrieve dates stored as text. Defaults to \"yyyy-MM-dd HH:mm:ss.SSS\"", null), + DATE_STRING_FORMAT("date_string_format", "Format to store and retrieve dates stored as text. Defaults to \"yyyy-MM-dd\"", null), + TIME_STRING_FORMAT("time_string_format", "Format to store and retrieve times stored as text. Defaults to \"HH:mm:ss\"", null), + TIMESTAMP_STRING_FORMAT("timestamp_string_format", "Format to store and retrieve timestamps stored as text. Defaults to \"yyyy-MM-dd HH:mm:ss.SSS\"", null), BUSY_TIMEOUT("busy_timeout", null); public final String pragmaName; @@ -791,6 +803,20 @@ public void setDateClass(String dateClass) { public void setDateStringFormat(String dateStringFormat) { this.dateStringFormat = dateStringFormat; } + + /** + * @param timeStringFormat Format of time string + */ + public void setTimeStringFormat(String timeStringFormat) { + this.timeStringFormat = timeStringFormat; + } + + /** + * @param timestampStringFormat Format of timestamp string + */ + public void setTimestampStringFormat(String timestampStringFormat) { + this.timestampStringFormat = timestampStringFormat; + } /** * @param milliseconds Connect to DB timeout in milliseconds diff --git a/src/main/java/org/sqlite/core/CoreConnection.java b/src/main/java/org/sqlite/core/CoreConnection.java index 01c168d7f..c82f15e62 100644 --- a/src/main/java/org/sqlite/core/CoreConnection.java +++ b/src/main/java/org/sqlite/core/CoreConnection.java @@ -57,6 +57,8 @@ public abstract class CoreConnection { public final DatePrecision datePrecision; //Calendar.SECOND or Calendar.MILLISECOND public final long dateMultiplier; public final DateFormat dateFormat; + public final DateFormat timeFormat; + public final DateFormat timestampFormat; protected CoreConnection(String url, String fileName, Properties prop) throws SQLException { @@ -67,6 +69,8 @@ protected CoreConnection(String url, String fileName, Properties prop) throws SQ this.dateClass = config.dateClass; this.dateMultiplier = config.dateMultiplier; this.dateFormat = new SimpleDateFormat(config.dateStringFormat); + this.timeFormat = new SimpleDateFormat(config.timeStringFormat); + this.timestampFormat = new SimpleDateFormat(config.timestampStringFormat); this.datePrecision = config.datePrecision; this.transactionMode = config.getTransactionMode(); this.openModeFlags = config.getOpenModeFlags(); diff --git a/src/main/java/org/sqlite/core/CorePreparedStatement.java b/src/main/java/org/sqlite/core/CorePreparedStatement.java index d0c19cd0d..caa98cb92 100644 --- a/src/main/java/org/sqlite/core/CorePreparedStatement.java +++ b/src/main/java/org/sqlite/core/CorePreparedStatement.java @@ -117,7 +117,7 @@ protected void batch(int pos, Object value) throws SQLException { protected void setDateByMilliseconds(int pos, Long value) throws SQLException { switch(conn.dateClass) { case TEXT: - batch(pos, conn.dateFormat.format(new Date(value))); + batch(pos, conn.timestampFormat.format(new Date(value))); break; case REAL: diff --git a/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java b/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java index 1f7bb22ad..92a0dc6e1 100644 --- a/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java +++ b/src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java @@ -458,7 +458,7 @@ public Time getTime(int col) throws SQLException { case SQLITE_TEXT: try { - return new Time(stmt.conn.dateFormat.parse(db.column_text(stmt.pointer, markCol(col))).getTime()); + return new Time(stmt.conn.timeFormat.parse(db.column_text(stmt.pointer, markCol(col))).getTime()); } catch (Exception e) { SQLException error = new SQLException("Error parsing time"); @@ -487,7 +487,7 @@ public Time getTime(int col, Calendar cal) throws SQLException { case SQLITE_TEXT: try { - DateFormat dateFormat = (DateFormat) stmt.conn.dateFormat.clone(); + DateFormat dateFormat = (DateFormat) stmt.conn.timeFormat.clone(); dateFormat.setCalendar(cal); return new Time(dateFormat.parse(db.column_text(stmt.pointer, markCol(col))).getTime()); @@ -531,8 +531,8 @@ public Timestamp getTimestamp(int col) throws SQLException { return null; case SQLITE_TEXT: - try { - return new Timestamp(stmt.conn.dateFormat.parse(db.column_text(stmt.pointer, markCol(col))).getTime()); + try { + return new Timestamp(stmt.conn.timestampFormat.parse(db.column_text(stmt.pointer, markCol(col))).getTime()); } catch (Exception e) { SQLException error = new SQLException("Error parsing time stamp"); @@ -563,7 +563,7 @@ public Timestamp getTimestamp(int col, Calendar cal) throws SQLException { case SQLITE_TEXT: try { - DateFormat dateFormat = (DateFormat)stmt.conn.dateFormat.clone(); + DateFormat dateFormat = (DateFormat)stmt.conn.timestampFormat.clone(); dateFormat.setCalendar(cal); return new Timestamp(dateFormat.parse(db.column_text(stmt.pointer, markCol(col))).getTime());