Skip to content

Commit

Permalink
Merge pull request #60 from aaberg/date_default_timestamp
Browse files Browse the repository at this point in the history
default java.util.Date parameters to setting timestamp rather than date
  • Loading branch information
aaberg committed Apr 3, 2014
2 parents a2e0fc5 + b75d866 commit 7cf7cdf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
23 changes: 6 additions & 17 deletions src/main/java/org/sql2o/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,15 @@ public Query addParameter(String name, Timestamp value){
}

public Query addParameter(String name, Date value){
if (this.getConnection().getSql2o().quirksMode == QuirksMode.MSSqlServer){
Timestamp timestamp = value == null ? null : new Timestamp(value.getTime());
return addParameter(name, timestamp);
}

if (value != null && this.connection.getSql2o().quirksMode == QuirksMode.DB2){
// With the DB2 driver you can get an error if trying to put a date value into a timestamp column,
// but of some reason it works if using setObject().
return addParameter(name, (Object)value);
}

try{
if (value == null) {
statement.setNull(name, Types.DATE);
} else {
statement.setDate(name, value);
}
} catch (Exception ex) {
throw new Sql2oException("Error setting parameter '" + name + "'", ex);
}

return this;
// by default add a timestamp, because it works with DATE, DATETIME, TIMESTAMP columns
Timestamp timestamp = value == null ? null : new Timestamp(value.getTime());
return addParameter(name, timestamp);
}

public Query addParameter(String name, java.util.Date value){
Expand Down Expand Up @@ -247,8 +234,10 @@ public Query bind(Object bean){
logger.debug("Using addParameter(String, Object)", ex);
addParameter(param, res);
}
}else
}
else {
addParameter(param, res);
}
}
}catch(IllegalArgumentException ex){
logger.debug("Ignoring Illegal Arguments", ex);
Expand Down
19 changes: 14 additions & 5 deletions src/test/java/org/sql2o/Sql2oTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,27 @@ public void testJodaTime(){

@Test
public void testUtilDate(){
sql2o.createQuery("create table testutildate(id int primary key, d1 datetime, d2 timestamp)").executeUpdate();
sql2o.createQuery("create table testutildate(id int primary key, d1 datetime, d2 timestamp, d3 date)").executeUpdate();

sql2o.createQuery("insert into testutildate(id, d1, d2) values(:id, :d1, :d2)")
.addParameter("id", 1).addParameter("d1", new Date()).addParameter("d2", new Date()).addToBatch()
.addParameter("id", 2).addParameter("d1", new Date()).addParameter("d2", new Date()).addToBatch()
.addParameter("id", 3).addParameter("d1", new Date()).addParameter("d2", new Date()).addToBatch()
Date now = new Date();

sql2o.createQuery("insert into testutildate(id, d1, d2, d3) values(:id, :d1, :d2, :d3)")
.addParameter("id", 1).addParameter("d1", now).addParameter("d2", now).addParameter("d3", now).addToBatch()
.addParameter("id", 2).addParameter("d1", now).addParameter("d2", now).addParameter("d3", now).addToBatch()
.addParameter("id", 3).addParameter("d1", now).addParameter("d2", now).addParameter("d3", now).addToBatch()
.executeBatch();

List<UtilDateEntity> list = sql2o.createQuery("select * from testutildate").executeAndFetch(UtilDateEntity.class);

assertTrue(list.size() == 3);

// make sure d1, d2, d3 were properly inserted and selected
for (UtilDateEntity e : list) {
assertEquals(now, e.d1);
assertEquals(now, e.getD2());
Date dateOnly = new DateTime(now).toDateMidnight().toDate();
assertEquals(dateOnly, e.getD3());
}
}

@Test
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/sql2o/UtilDateEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UtilDateEntity {
public int id;
public Date d1;
private Date d2;
private Date d3;

public Date getD2() {
return d2;
Expand All @@ -22,4 +23,12 @@ public Date getD2() {
public void setD2(Date d2) {
this.d2 = d2;
}

public Date getD3() {
return d3;
}

public void setD3(Date d3) {
this.d3 = d3;
}
}

0 comments on commit 7cf7cdf

Please sign in to comment.