Author: arminw
Date: Mon May 1 16:48:13 2006
New Revision: 398740
URL: http://svn.apache.org/viewcvs?rev=398740&view=rev
Log:
add LOB type support
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformWLOracle9iImpl.java
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java?rev=398740&r1=398739&r2=398740&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
Mon May 1 16:48:13 2006
@@ -16,7 +16,9 @@
*/
import java.io.StringReader;
+import java.sql.Blob;
import java.sql.CallableStatement;
+import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
@@ -28,6 +30,8 @@
import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes;
+import org.apache.ojb.broker.lob.BlobHandle;
+import org.apache.ojb.broker.lob.ClobHandle;
import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
import org.apache.ojb.broker.query.LikeCriteria;
import org.apache.ojb.broker.util.SqlHelper;
@@ -51,6 +55,7 @@
protected boolean m_batchUpdatesChecked = false;
protected boolean m_supportsBatchUpdates = false;
+ protected Boolean locatorsUpdateCopy;
public boolean supportsBatchOperations()
{
@@ -234,11 +239,6 @@
public void setObjectForStatement(PreparedStatement ps, int index, Object
value, int sqlType)
throws SQLException
{
- if ((sqlType == Types.LONGVARCHAR) && (value instanceof String))
- {
- String s = (String) value;
- ps.setCharacterStream(index, new StringReader(s), s.length());
- }
/*
PATCH for BigDecimal truncation problem. Seems that several databases
(e.g. DB2, Sybase)
has problem with BigDecimal fields if the sql-type was set. The
problem was discussed here
@@ -255,19 +255,25 @@
But this way maxDB/sapDB does not work correct, so we use the most
flexible solution
and let the jdbc-driver handle BigDecimal objects by itself.
*/
- else if(sqlType == Types.DECIMAL || sqlType == Types.NUMERIC)
+// if(sqlType == Types.DECIMAL || sqlType == Types.NUMERIC)
+// {
+// ps.setObject(index, value);
+// }
+ if ((sqlType == Types.LONGVARCHAR) && (value instanceof String))
{
- ps.setObject(index, value);
+ String s = (String) value;
+ ps.setCharacterStream(index, new StringReader(s), s.length());
+ }
+ else if (sqlType == Types.BLOB)
+ {
+ defaultSetBlob(ps, index, value, sqlType);
+ }
+ else if (sqlType == Types.CLOB)
+ {
+ defaultSetClob(ps, index, value, sqlType);
}
else
{
-// arminw: this method call is done very, very often, so we can improve
performance
-// by comment out this section
-// if (log.isDebugEnabled()) {
-// log.debug("Default setObjectForStatement, sqlType=" +
sqlType +
-// ", value class=" + (value == null ? "NULL!" :
value.getClass().getName())
-// + ", value=" + value);
-// }
ps.setObject(index, value, sqlType);
}
}
@@ -280,6 +286,110 @@
ps.setNull(index, sqlType);
}
+ public void setObjectForStatement(PreparedStatement ps, int index, Object
value) throws SQLException
+ {
+ if (value != null)
+ {
+ ps.setObject(index, value);
+ }
+ else
+ {
+ setNullForStatement(ps, index, Types.NULL);
+ }
+ }
+
+ /**
+ * Default method to set <em>BLOB</em> sql types.
+ *
+ * @param ps The prepared statement
+ * @param index The column index
+ * @param value The LOB object value
+ * @param sqlType The associated sql [EMAIL PROTECTED] java.sql.Types
type}.
+ * Expects the correct sql type, no sql type checks are done
+ */
+ protected void defaultSetBlob(PreparedStatement ps, int index, Object
value, int sqlType) throws SQLException
+ {
+ if(value instanceof byte[])
+ {
+ ps.setObject(index, value, sqlType);
+ }
+ else if(value instanceof Blob)
+ {
+ Blob b = (Blob) value;
+ /*
+ we use method advised by JDBC 3.0 specification (see 16.3.2)
+ */
+ if(value instanceof BlobHandle && ((BlobHandle)
value).isTransient())
+ {
+ int length = (int) b.length();
+ if(b.length() < 0)
+ {
+ /*
+ We need the length of the specified stream to use
#setBinaryStream
+ This workaround seems to work for many DB (hsql, mysql,
maxdb), the
+ stream is read till EOF without throwing an exception
+ */
+ length = Integer.MAX_VALUE;
+ }
+ ps.setBinaryStream(index, b.getBinaryStream(), length);
+ }
+ else if(locatorsUpdateCopy(ps.getConnection()))
+ {
+ ps.setBlob(index, b);
+ }
+ }
+ else
+ {
+ log.error("Can't handle specified LOB object: " + value);
+ }
+ }
+
+ /**
+ * Default method to set <em>CLOB</em> sql types.
+ *
+ * @param ps The prepared statement
+ * @param index The column index
+ * @param value The LOB object value
+ * @param sqlType The associated sql [EMAIL PROTECTED] java.sql.Types
type}.
+ * Expects the correct sql type, no sql type checks are done
+ */
+ protected void defaultSetClob(PreparedStatement ps, int index, Object
value, int sqlType) throws SQLException
+ {
+ if(value instanceof String)
+ {
+ ps.setObject(index, value, sqlType);
+ }
+ else if(value instanceof Clob)
+ {
+ Clob c = (Clob) value;
+ /*
+ we use method advised by JDBC 3.0 specification (see 16.3.2)
+ */
+ if(value instanceof ClobHandle && ((ClobHandle)
value).isTransient())
+ {
+ int length = (int) c.length();
+ if(c.length() < 0)
+ {
+ /*
+ We need the length of the specified stream to use
#setCharacterStream
+ This workaround seems to work for many DB (hsql, mysql,
maxdb), the
+ reader is read till EOF without throwing an exception
+ */
+ length = Integer.MAX_VALUE;
+ }
+ ps.setCharacterStream(index, c.getCharacterStream(), length);
+ }
+ else if(locatorsUpdateCopy(ps.getConnection()))
+ {
+ ps.setClob(index, c);
+ }
+ }
+ else
+ {
+ log.error("Can't handle specified LOB object: " + value);
+ }
+ }
+
/**
* Get join syntax type for this RDBMS - one on of the constants from
JoinSyntaxType interface
*
@@ -471,5 +581,25 @@
protected String getQuotedName(String aString)
{
return '"' + aString + '"';
- }
+ }
+
+ /**
+ * @see Platform#locatorsUpdateCopy(java.sql.Connection)
+ */
+ public boolean locatorsUpdateCopy(Connection con)
+ {
+ if(locatorsUpdateCopy == null)
+ {
+ try
+ {
+ locatorsUpdateCopy = con.getMetaData().locatorsUpdateCopy() ?
Boolean.TRUE : Boolean.FALSE;
+ }
+ catch(SQLException e)
+ {
+ log.error("Can't detect LOB locators support type, use default
setting" , e);
+ locatorsUpdateCopy = Boolean.TRUE;
+ }
+ }
+ return locatorsUpdateCopy.booleanValue();
+ }
}
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java?rev=398740&r1=398739&r2=398740&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformMySQLImpl.java
Mon May 1 16:48:13 2006
@@ -54,7 +54,11 @@
byte buf[] = (byte[]) value;
ByteArrayInputStream inputStream = new
ByteArrayInputStream(buf);
ps.setBinaryStream(index, inputStream, buf.length);
-
+ break;
+ }
+ else
+ {
+ super.setObjectForStatement(ps, index, value, sqlType);
break;
}
@@ -75,12 +79,19 @@
}
else if (value instanceof byte[])
{
- byte buf[] = (byte[]) value;
+ byte[] buf = (byte[]) value;
ByteArrayInputStream inputStream = new
ByteArrayInputStream(buf);
reader = new InputStreamReader(inputStream);
+ length = buf.length;
+ }
+ if(reader != null)
+ {
+ ps.setCharacterStream(index, reader, length);
+ }
+ else
+ {
+ super.setObjectForStatement(ps, index, value, sqlType);
}
-
- ps.setCharacterStream(index, reader, length);
break;
default :
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformWLOracle9iImpl.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformWLOracle9iImpl.java?rev=398740&r1=398739&r2=398740&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformWLOracle9iImpl.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/platforms/PlatformWLOracle9iImpl.java
Mon May 1 16:48:13 2006
@@ -25,9 +25,7 @@
import java.util.Map;
import java.util.WeakHashMap;
-import org.apache.ojb.broker.metadata.JdbcType;
import org.apache.ojb.broker.util.ClassHelper;
-import org.apache.ojb.broker.metadata.JdbcTypesHelper;
/**
* This class is a concrete implementation of <code>Platform</code>. Provides
@@ -77,10 +75,6 @@
protected static final Object[] PARAM_ROW_PREFETCH_SIZE = new Object[]{new
Integer(ROW_PREFETCH_SIZE)};
protected static final Object[] PARAM_STATEMENT_BATCH_SIZE = new
Object[]{new Integer(STATEMENTS_PER_BATCH)};
protected static final Object[] PARAM_BOOLEAN_TRUE = new
Object[]{Boolean.TRUE};
-
- protected static final JdbcType BASE_CLOB =
JdbcTypesHelper.getJdbcTypeByName("clob");
- protected static final JdbcType BASE_BLOB =
JdbcTypesHelper.getJdbcTypeByName("blob");
-
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]