Nice!
Jacques
From: <adri...@apache.org>
Author: adrianc
Date: Fri Jul 2 06:28:14 2010
New Revision: 959868
URL: http://svn.apache.org/viewvc?rev=959868&view=rev
Log:
Small enhancement to JdbcValueHandler: add support for sub-second Timestamp
precision on databases that don't support it.
If a user chooses to do so, they can assign the date-time field type to a CHAR(30) SQL type. This will enable sub-second Timestamp
precision on databases that don't support it.
The Timestamp will be stored as a string in JDBC Timestamp format (yyyy-mm-dd hh:mm:ss.fffffffff). Date/time comparisons can still
be performed in SQL statements, but any date/time functions performed on the field will fail. Users could create UDFs to mimic
those functions.
In addition, the CHAR(30) field will consume more storage space than a native
Timestamp field (usually 8 bytes).
It will be up to the user to decide if they want to use this feature. If used in a pure OFBiz environment, everything should work
as expected.
Modified:
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
Modified:
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java?rev=959868&r1=959867&r2=959868&view=diff
==============================================================================
---
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
(original)
+++
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
Fri Jul 2 06:28:14 2010
@@ -29,6 +29,7 @@ import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Timestamp;
import java.sql.Types;
import java.util.Map;
@@ -698,6 +699,11 @@ public abstract class JdbcValueHandler {
/**
* A <code>java.sql.Timestamp</code> JDBC value handler.
+ * <p>This <code>JdbcValueHandler</code> accommodates databases that
+ * don't support sub-second precision. If the date-time field type
+ * is a <code>CHAR(30)</code> SQL type, <code>java.sql.Timestamp</code>s
+ * will be stored as JDBC escape strings
+ * (<code>yyyy-mm-dd hh:mm:ss.fffffffff</code>).</p>
*/
protected static class TimestampJdbcValueHandler extends JdbcValueHandler {
protected TimestampJdbcValueHandler(int jdbcType) {
@@ -713,7 +719,22 @@ public abstract class JdbcValueHandler {
}
@Override
protected JdbcValueHandler newInstance(int sqlType) {
- return new TimestampJdbcValueHandler(sqlType);
+ if (sqlType == Types.CHAR) {
+ return new TimestampJdbcValueHandler(sqlType) {
+ protected void castAndSetValue(PreparedStatement ps, int
parameterIndex, Object obj) throws SQLException {
+ ps.setString(parameterIndex, ((java.sql.Timestamp)
obj).toString());
+ }
+ public Object getValue(ResultSet rs, int columnIndex)
throws SQLException {
+ String str = rs.getString(columnIndex);
+ if (str == null) {
+ return null;
+ }
+ return Timestamp.valueOf(str);
+ }
+ };
+ } else {
+ return new TimestampJdbcValueHandler(sqlType);
+ }
}
}
}