Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date, Time, Timestamp Distinguishing String Formats. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/main/java/org/sqlite/SQLiteConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not concat?

public DateClass dateClass;
public DatePrecision datePrecision;
public long dateMultiplier;
public String dateStringFormat;
public String timeStringFormat;
public String timestampStringFormat;

/**
* Default constructor.
Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and reuse them here?

BUSY_TIMEOUT("busy_timeout", null);

public final String pragmaName;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/sqlite/core/CoreConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sqlite/core/CorePreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/sqlite/jdbc3/JDBC3ResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand Down