Skip to content

Commit

Permalink
take into account the calendar in the prepared statement
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriy.tseyler committed Dec 20, 2017
1 parent 81986fb commit b03149b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/sqlite/core/CorePreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;

import org.sqlite.SQLiteConnection;
import org.sqlite.date.FastDateFormat;
import org.sqlite.jdbc4.JDBC4Statement;

public abstract class CorePreparedStatement extends JDBC4Statement
Expand Down Expand Up @@ -125,10 +127,10 @@ protected void batch(int pos, Object value) throws SQLException {
/**
* Store the date in the user's preferred format (text, int, or real)
*/
protected void setDateByMilliseconds(int pos, Long value) throws SQLException {
protected void setDateByMilliseconds(int pos, Long value, Calendar calendar) throws SQLException {
switch(conn.dateClass) {
case TEXT:
batch(pos, conn.dateFormat.format(new Date(value)));
batch(pos, FastDateFormat.getInstance(conn.dateStringFormat, calendar.getTimeZone()).format(new Date(value)));
break;

case REAL:
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/org/sqlite/jdbc3/JDBC3PreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,7 @@ public void setObject(int pos, Object value) throws SQLException {
batch(pos, null);
}
else if (value instanceof java.util.Date) {
setDateByMilliseconds(pos, ((java.util.Date) value).getTime());
}
else if (value instanceof Date) {
setDateByMilliseconds(pos, new Long(((Date) value).getTime()));
}
else if (value instanceof Time) {
setDateByMilliseconds(pos, new Long(((Time) value).getTime()));
}
else if (value instanceof Timestamp) {
setDateByMilliseconds(pos, new Long(((Timestamp) value).getTime()));
setDateByMilliseconds(pos, ((java.util.Date)value).getTime(), Calendar.getInstance());
}
else if (value instanceof Long) {
batch(pos, value);
Expand Down Expand Up @@ -447,43 +438,43 @@ public void setCharacterStream(int pos, Reader reader, int length) throws SQLExc
* @see java.sql.PreparedStatement#setDate(int, java.sql.Date)
*/
public void setDate(int pos, Date x) throws SQLException {
setObject(pos, x);
setDate(pos, x, Calendar.getInstance());
}

/**
* @see java.sql.PreparedStatement#setDate(int, java.sql.Date, java.util.Calendar)
*/
public void setDate(int pos, Date x, Calendar cal) throws SQLException {
setObject(pos, x);
setDateByMilliseconds(pos, x.getTime(), cal);
}


/**
* @see java.sql.PreparedStatement#setTime(int, java.sql.Time)
*/
public void setTime(int pos, Time x) throws SQLException {
setObject(pos, x);
setTime(pos, x, Calendar.getInstance());
}

/**
* @see java.sql.PreparedStatement#setTime(int, java.sql.Time, java.util.Calendar)
*/
public void setTime(int pos, Time x, Calendar cal) throws SQLException {
setObject(pos, x);
setDateByMilliseconds(pos, x.getTime(), cal);
}

/**
* @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp)
*/
public void setTimestamp(int pos, Timestamp x) throws SQLException {
setObject(pos, x);
setTimestamp(pos, x, Calendar.getInstance());
}

/**
* @see java.sql.PreparedStatement#setTimestamp(int, java.sql.Timestamp, java.util.Calendar)
*/
public void setTimestamp(int pos, Timestamp x, Calendar cal) throws SQLException {
setObject(pos, x);
setDateByMilliseconds(pos, x.getTime(), cal);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/sqlite/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;

import org.junit.Test;
import org.sqlite.date.FastDateFormat;
Expand Down Expand Up @@ -104,6 +107,59 @@ public void dateTimeTest() throws Exception {
stmt.setDate(1, new java.sql.Date(now.getTime()));
}

@Test
public void dateTimeWithTimeZoneTest() throws Exception {
Properties properties = new Properties();
properties.setProperty(SQLiteConfig.Pragma.DATE_CLASS.pragmaName, "text");
Connection conn = DriverManager.getConnection("jdbc:sqlite:", properties);

Statement statement = null;
try {
statement = conn.createStatement();
statement.execute("create table sample (date_time datetime)");
}
finally {
if (statement != null) statement.close();
}

TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
TimeZone customTimeZone = TimeZone.getTimeZone("GMT+3");
Calendar utcCalendar = Calendar.getInstance(utcTimeZone);
Calendar customCalendar = Calendar.getInstance(customTimeZone);

java.sql.Date now = new java.sql.Date(new Date().getTime());
FastDateFormat customFormat = FastDateFormat.getInstance(SQLiteConfig.DEFAULT_DATE_STRING_FORMAT, customTimeZone);
FastDateFormat utcFormat = FastDateFormat.getInstance(SQLiteConfig.DEFAULT_DATE_STRING_FORMAT, utcTimeZone);
java.sql.Date nowInUtc = new java.sql.Date(utcFormat.parse(customFormat.format(now)).getTime());

PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement("insert into sample (date_time) values(?)");
preparedStatement.setDate(1, now, customCalendar);
preparedStatement.executeUpdate();
preparedStatement.setDate(1, nowInUtc, utcCalendar);
preparedStatement.executeUpdate();
}
finally {
if (preparedStatement != null) preparedStatement.close();
}

ResultSet resultSet = null;
try {
resultSet = conn.createStatement().executeQuery("select * from sample");
assertTrue(resultSet.next());
assertEquals(now, resultSet.getDate(1, customCalendar));
assertEquals(nowInUtc, resultSet.getDate(1, utcCalendar));

assertTrue(resultSet.next());
assertEquals(now, resultSet.getDate(1, customCalendar));
assertEquals(nowInUtc, resultSet.getDate(1, utcCalendar));
}
finally {
if (resultSet != null) resultSet.close();
}
}

@Test
public void notEmptyBlob() throws Exception {
Connection conn = getConnection();
Expand Down

0 comments on commit b03149b

Please sign in to comment.