Skip to content

Commit

Permalink
Merge pull request #1702 from MicroStep-MIS/GEOT-5823
Browse files Browse the repository at this point in the history
[GEOT-5823] Mapping java.sql.Timestamp to TIMESTAMP database type
  • Loading branch information
aaime committed Sep 24, 2017
2 parents f1b0c27 + 87a2c3c commit 92d6315
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public GeomClasses() {
put("NVARCHAR", String.class);
put("NVARCHAR2", String.class);
put("DATE", java.sql.Date.class);
put("TIMESTAMP", java.sql.Timestamp.class);
}
};

Expand Down Expand Up @@ -1436,6 +1437,8 @@ public void registerSqlTypeToSqlTypeNameOverrides(
// starting with Oracle 11 + recent JDBC drivers the DATE type does not have a mapping
// anymore in the JDBC driver, manually register it instead
overrides.put(Types.DATE, "DATE");
// overriding default java.sql.Timestamp to Oracle DATE mapping
overrides.put(Types.TIMESTAMP, "TIMESTAMP");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

import java.sql.Date;
import java.sql.Timestamp;
import java.util.TimeZone;

import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.jdbc.JDBCDateOnlineTest;
import org.geotools.jdbc.JDBCDateTestSetup;
import org.geotools.jdbc.JDBCFeatureStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;

/**
*
Expand Down Expand Up @@ -36,4 +46,40 @@ public void testFilterByTime() throws Exception {
// Oracle makes you go through various stages of pain to work simply against Time,
// not worth supporting it until someone has real time to deal with it
}

public void testInsertTemporal() throws Exception {
TimeZone originalTimeZone = TimeZone.getDefault();
TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
try {
TimeZone.setDefault(gmtTimeZone);

final String timestampsTable = tname("timestamps");
SimpleFeatureType ft = dataStore.getSchema(timestampsTable);
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft);
long theTimestamp = 1503926476000L; // August 28, 2017 13:21:16 GMT
builder.add(new Timestamp(theTimestamp));
SimpleFeature feature = builder.buildFeature(null);

Transaction t = new DefaultTransaction("add");
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(timestampsTable, t);
try {
fs.addFeatures(DataUtilities.collection(feature));
t.commit();
} catch (Exception ex) {
t.rollback();
throw ex;
} finally {
t.close();
}

FilterFactory ff = dataStore.getFilterFactory();
Filter f = ff.equals(ff.property(aname("t")), ff.literal("2017-08-28 13:21:16"));

JDBCFeatureStore fs2 = (JDBCFeatureStore) dataStore.getFeatureSource(timestampsTable);

assertEquals(1, fs2.getCount(new Query(timestampsTable, f)));
} finally {
TimeZone.setDefault(originalTimeZone);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ protected void createDateTable() throws Exception {
"TO_DATE('2009-09-29', 'yyyy-MM-dd'), " +
"TO_DATE('2009-09-29 17:54:23', 'yyyy-MM-dd HH24:mi:ss')," +
"TO_DATE('17:54:23', 'HH24:mi:ss') )");

run( "CREATE TABLE TIMESTAMPS (" +
"ID NUMERIC(10) NOT NULL, " +
"T TIMESTAMP, " +
"CONSTRAINT temporals_pk PRIMARY KEY (id))");
}

@Override
protected void dropDateTable() throws Exception {
runSafe("DROP TABLE DATES");
runSafe("DROP TABLE TIMESTAMPS");
}

}

0 comments on commit 92d6315

Please sign in to comment.