From 97a94847f6c6620b934255f2ecf30bb9493e11c5 Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Wed, 11 Jun 2008 23:46:28 +0000 Subject: [PATCH] OPENJPA-130. Merge from ../branches/1.1.x. svn merge -c 653008 ../branches/1.1.x git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@666905 13f79535-47bb-0310-9956-ffa450edef68 --- .../jdbc/meta/strats/LobFieldStrategy.java | 44 ++++++++++++------- .../apache/openjpa/jdbc/sql/DBDictionary.java | 20 ++++++--- .../openjpa/jdbc/sql/OracleDictionary.java | 8 +++- .../jdbc/meta/strats/AbstractLobTest.java | 36 +++++++++++++++ .../jdbc/meta/strats/ReaderLobEntity.java | 4 +- 5 files changed, 85 insertions(+), 27 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/LobFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/LobFieldStrategy.java index 5ab815946..6f06bdd04 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/LobFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/LobFieldStrategy.java @@ -80,18 +80,12 @@ public class LobFieldStrategy extends AbstractFieldStrategy { (field.getIndex()), store); Row row = field.getRow(sm, store, rm, Row.ACTION_INSERT); if (field.getColumnIO().isInsertable(0, ob == null)) { - if (ob != null) { - if (isBlob()) { - store.getDBDictionary().insertBlobForStreamingLoad - (row, field.getColumns()[0]); - } else { - store.getDBDictionary().insertClobForStreamingLoad - (row, field.getColumns()[0]); - } + if (isBlob()) { + store.getDBDictionary().insertBlobForStreamingLoad + (row, field.getColumns()[0], ob); } else { - Column col = field.getColumns()[0]; - col.setType(Types.OTHER); - row.setNull(col); + store.getDBDictionary().insertClobForStreamingLoad + (row, field.getColumns()[0], ob); } } } @@ -113,11 +107,31 @@ public class LobFieldStrategy extends AbstractFieldStrategy { } } } + + public Boolean isCustomUpdate(OpenJPAStateManager sm, JDBCStore store) { + return null; + } public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm) throws SQLException { Object ob = toDataStoreValue(sm.fetchObjectField - (field.getIndex()), store); + (field.getIndex()), store); + if (field.getColumnIO().isUpdatable(0, ob == null)) { + Row row = field.getRow(sm, store, rm, Row.ACTION_UPDATE); + if (isBlob()) { + store.getDBDictionary().insertBlobForStreamingLoad + (row, field.getColumns()[0], ob); + } else { + store.getDBDictionary().insertClobForStreamingLoad + (row, field.getColumns()[0], ob); + } + } + } + + public void customUpdate(OpenJPAStateManager sm, JDBCStore store) + throws SQLException { + Object ob = toDataStoreValue(sm.fetchObjectField + (field.getIndex()), store); if (field.getColumnIO().isUpdatable(0, ob == null)) { if (ob != null) { Select sel = createSelect(sm, store); @@ -128,11 +142,6 @@ public class LobFieldStrategy extends AbstractFieldStrategy { store.getDBDictionary().updateClob (sel, store, (Reader)ob); } - } else { - Row row = field.getRow(sm, store, rm, Row.ACTION_UPDATE); - Column col = field.getColumns()[0]; - col.setType(Types.OTHER); - row.setNull(col); } } } @@ -191,4 +200,5 @@ public class LobFieldStrategy extends AbstractFieldStrategy { sel.setLob(true); return sel; } + } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index 19ec02a30..42a5d1be5 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -127,8 +127,8 @@ public class DBDictionary public static final String CONS_NAME_MID = "mid"; public static final String CONS_NAME_AFTER = "after"; - public int blobBufferSize = 50; - public int clobBufferSize = 50; + public int blobBufferSize = 50000; + public int clobBufferSize = 50000; protected static final int RANGE_POST_SELECT = 0; protected static final int RANGE_PRE_DISTINCT = 1; @@ -4189,16 +4189,24 @@ public class DBDictionary return column.toString(); } - public void insertBlobForStreamingLoad(Row row, Column col) + public void insertBlobForStreamingLoad(Row row, Column col, Object ob) throws SQLException { - row.setBinaryStream(col, - new ByteArrayInputStream(new byte[0]), 0); + if (ob != null) { + row.setBinaryStream(col, + new ByteArrayInputStream(new byte[0]), 0); + } else { + row.setNull(col); + } } - public void insertClobForStreamingLoad(Row row, Column col) + public void insertClobForStreamingLoad(Row row, Column col, Object ob) throws SQLException { + if (ob != null) { row.setCharacterStream(col, new CharArrayReader(new char[0]), 0); + } else { + row.setNull(col); + } } public void updateBlob(Select sel, JDBCStore store, InputStream is) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java index ee86d04a6..872d96488 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/OracleDictionary.java @@ -1096,13 +1096,17 @@ public class OracleDictionary buf.append("')"); } - public void insertBlobForStreamingLoad(Row row, Column col) + public void insertBlobForStreamingLoad(Row row, Column col, Object ob) throws SQLException { + if (ob == null) + col.setType(Types.OTHER); row.setNull(col); } - public void insertClobForStreamingLoad(Row row, Column col) + public void insertClobForStreamingLoad(Row row, Column col, Object ob) throws SQLException { + if (ob == null) + col.setType(Types.OTHER); row.setNull(col); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/AbstractLobTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/AbstractLobTest.java index ab93d6888..65158ae53 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/AbstractLobTest.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/AbstractLobTest.java @@ -20,6 +20,7 @@ package org.apache.openjpa.jdbc.meta.strats; import java.io.IOException; + import javax.persistence.EntityManager; import javax.persistence.Query; @@ -117,6 +118,41 @@ public abstract class AbstractLobTest extends SingleEMFTestCase { em.close(); } + public void testUpdateWithNull() { + if (!isDatabaseSupported()) return; + insert(newLobEntity("oOOOOOo", 1)); + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1); + entity.setStream(null); + em.getTransaction().commit(); + em.close(); + em = emf.createEntityManager(); + em.getTransaction().begin(); + entity = (LobEntity) em.find(getLobEntityClass(), 1); + assertNull(entity.getStream()); + em.getTransaction().commit(); + em.close(); + } + + public void testUpdateANullObjectWithoutNull() throws IOException { + if (!isDatabaseSupported()) return; + insert(newLobEntity(null, 1)); + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + LobEntity entity = (LobEntity) em.find(getLobEntityClass(), 1); + String string = "iIIIIIi"; + changeStream(entity, string); + em.getTransaction().commit(); + em.close(); + em = emf.createEntityManager(); + em.getTransaction().begin(); + entity = (LobEntity) em.find(getLobEntityClass(), 1); + assertEquals(string, getStreamContentAsString(entity.getStream())); + em.getTransaction().commit(); + em.close(); + } + public void testDelete() { if (!isDatabaseSupported()) return; insert(newLobEntity("oOOOOOo", 1)); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/ReaderLobEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/ReaderLobEntity.java index 84afe9e63..64adc29f2 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/ReaderLobEntity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/strats/ReaderLobEntity.java @@ -35,10 +35,10 @@ import org.apache.openjpa.persistence.Persistent; public class ReaderLobEntity implements LobEntity { @Id - int id; + private int id; @Persistent - Reader stream; + private Reader stream; public int getId() { return id;