You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by be...@apache.org on 2007/06/14 12:27:15 UTC
svn commit: r547203 - in /db/derby/code/trunk/java:
engine/org/apache/derby/impl/jdbc/
testing/org/apache/derbyTesting/functionTests/tests/jdbc4/
Author: bernt
Date: Thu Jun 14 03:27:08 2007
New Revision: 547203
URL: http://svn.apache.org/viewvc?view=rev&rev=547203
Log:
DERBY-2730 Embedded method Clob.getCharacterStream. Submitted by V.Narayanan
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClobTest.java
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java?view=diff&rev=547203&r1=547202&r2=547203
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/ClobUpdateableReader.java Thu Jun 14 03:27:08 2007
@@ -55,6 +55,11 @@
private boolean materialized;
/** clob object this object is associated */
private final EmbedClob clob;
+ /**
+ * Position in Clob where to stop reading.
+ */
+ private final long maxPos;
+
/**
* Constructs a <code>Reader</code> over a <code>LOBInputStream</code>.
@@ -69,6 +74,9 @@
this.conChild = conChild;
this.stream = stream;
init (stream, 0);
+ //The subset of the Clob has not been requested.
+ //Hence set maxPos to -1.
+ this.maxPos = -1;
}
/**
@@ -86,6 +94,43 @@
long byteLength = clob.getByteLength();
this.stream = clob.getInternalStream ();
init (0, byteLength);
+ //The subset of the Clob
+ //has not been requested.
+ //Hence set maxPos to -1.
+ this.maxPos = -1;
+ }
+
+ /**
+ * Construct an <code>ClobUpdateableReader<code> using the
+ * <code>EmbedClob</code> received as parameter. The initial
+ * position in the stream is set to <code>pos</code> and the
+ * stream is restricted to a length of <code>len</code>.
+ *
+ * @param clob EmbedClob this stream is associated with.
+ * @param pos initial position. The position starts from 0.
+ * @param len The length to which the underlying <code>InputStream</code>
+ * has to be restricted.
+ * @throws IOException
+ * @throws SQLException
+ */
+ ClobUpdateableReader (EmbedClob clob, long pos, long len)
+ throws IOException, SQLException {
+ materialized = clob.isWritable();
+ this.clob = clob;
+ this.conChild = clob;
+ //Get the Byte length from the Clob which can be
+ //passes to the init method.
+ long byteLength = clob.getByteLength();
+ //Initialize the InputStream with the underlying
+ //InputStream of the Clob.
+ this.stream = clob.getInternalStream ();
+ //position the stream on pos using the init method.
+ init (pos, byteLength);
+ //The length requested cannot exceed the length
+ //of the underlying Clob object. Hence chose the
+ //minimum of the length of the underlying Clob
+ //object and requested length.
+ maxPos = Math.min(clob.length(), pos + len);
}
/**
@@ -99,7 +144,26 @@
*/
public int read(char[] cbuf, int off, int len) throws IOException {
updateIfRequired();
- int ret = streamReader.read (cbuf, off, len);
+
+ //If maxPos is not invalid and the current position inside the
+ //stream has exceeded maxPos the read sould return -1 signifying
+ //end of stream.
+ if (maxPos != -1 && pos >= maxPos) {
+ return -1;
+ }
+
+ int actualLength = 0;
+ //If maxPos is not invalid then ensure that the length(len)
+ //that is requested falls within the restriction set by maxPos.
+ if(maxPos != -1) {
+ actualLength
+ = (int )Math.min(len, maxPos - pos);
+ }
+ else {
+ //maxPos has not been set. Make maxPos the length requested.
+ actualLength = len;
+ }
+ int ret = streamReader.read (cbuf, off, actualLength);
if (ret >= 0) {
pos += ret;
}
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java?view=diff&rev=547203&r1=547202&r2=547203
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java Thu Jun 14 03:27:08 2007
@@ -958,10 +958,14 @@
new Long(pos), new Long(length));
}
- return new UpdateableBlobStream(this,
- getBinaryStream(),
- pos-1,
- length);
+ try {
+ return new UpdateableBlobStream(this,
+ getBinaryStream(),
+ pos-1,
+ length);
+ } catch (IOException ioe) {
+ throw Util.setStreamFailure(ioe);
+ }
}
/*
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java?view=diff&rev=547203&r1=547202&r2=547203
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedClob.java Thu Jun 14 03:27:08 2007
@@ -666,9 +666,54 @@
}
}
+ /**
+ * Returns a <code>Reader</code> object that contains a partial
+ * <code>Clob</code> value, starting with the character specified by pos,
+ * which is length characters in length.
+ *
+ * @param pos the offset to the first character of the partial value to
+ * be retrieved. The first character in the Clob is at position 1.
+ * @param length the length in characters of the partial value to be
+ * retrieved.
+ * @return <code>Reader</code> through which the partial <code>Clob</code>
+ * value can be read.
+ * @throws SQLException if pos is less than 1 or if pos is greater than the
+ * number of
+ * characters in the <code>Clob</code> or if pos + length is greater than
+ * the number of
+ * characters in the <code>Clob</code>
+ *
+ * @throws SQLException.
+ */
public java.io.Reader getCharacterStream(long pos, long length)
throws SQLException {
- throw Util.notImplemented();
+ //call checkValidity to exit by throwing a SQLException if
+ //the Clob object has been freed by calling free() on it
+ checkValidity();
+
+ if (pos <= 0) {
+ throw Util.generateCsSQLException(
+ SQLState.BLOB_BAD_POSITION,
+ new Long(pos));
+ }
+ if (length < 0) {
+ throw Util.generateCsSQLException(
+ SQLState.BLOB_NONPOSITIVE_LENGTH,
+ new Long(length));
+ }
+ if (length > (this.length() - pos)) {
+ throw Util.generateCsSQLException(
+ SQLState.POS_AND_LENGTH_GREATER_THAN_LOB,
+ new Long(pos), new Long(length));
+ }
+
+ try {
+ return new ClobUpdateableReader(this,
+ pos-1,
+ length);
+ } catch (IOException ioe) {
+ throw Util.setStreamFailure(ioe);
+ }
}
/*
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java?view=diff&rev=547203&r1=547202&r2=547203
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdateableBlobStream.java Thu Jun 14 03:27:08 2007
@@ -82,9 +82,10 @@
* @param len The length to which the underlying <code>InputStream</code>
* has to be restricted.
* @throws IOException
+ * @throws SQLException
*/
UpdateableBlobStream (EmbedBlob blob, InputStream is, long pos, long len)
- throws SQLException {
+ throws IOException, SQLException {
this(blob, is);
//The length requested cannot exceed the length
//of the underlying Blob object. Hence chose the
@@ -92,20 +93,9 @@
//object and requested length.
maxPos = Math.min(blob.length(), pos + len);
- try {
- //Skip to the requested position
- //inside the stream.
- skip(pos);
- }
- catch(IOException ioe) {
- //Skip throws an IOException. Wrap the
- //exception inside a SQLException and
- //throw it to the caller.
-
- SQLException sqle = new SQLException();
- sqle.initCause(ioe);
- throw sqle;
- }
+ //Skip to the requested position
+ //inside the stream.
+ skip(pos);
}
/**
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClobTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClobTest.java?view=diff&rev=547203&r1=547202&r2=547203
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClobTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ClobTest.java Thu Jun 14 03:27:08 2007
@@ -375,28 +375,11 @@
}
/**
- * Tests the getCharacterStream(long pos, long length) on the
- * Embedded side.
- *
- * @throws SQLException
- */
- public void embeddedTestGetCharacterStreamLongNotImplemented()
- throws SQLException {
- try {
- clob.getCharacterStream(5l, 10l);
- fail("Clob.getCharacterStream(long,long)" +
- "should not be implemented");
- } catch (SQLFeatureNotSupportedException sfnse) {
- // Do nothing, we are fine
- }
- }
-
- /**
- * Tests the implementation of getCharacterStream(long pos, long length)
- * in the NetworkClient.
+ * Tests the implementation of getCharacterStream(long pos, long length).
+ *
* @throws Exception
*/
- public void clientTestGetCharacterStreamLong()
+ public void testGetCharacterStreamLong()
throws Exception {
String str1 = "This is a test String. This is a test String";
@@ -437,7 +420,7 @@
*
* @throws SQLException.
*/
- public void clientTestGetCharacterStreamLongExceptionConditions()
+ public void testGetCharacterStreamLongExceptionConditions()
throws SQLException {
String str1 = "This is a test String. This is a test String";
@@ -518,23 +501,10 @@
* Create test suite for this test.
*/
public static Test suite() {
- TestSuite ctSuite = new TestSuite("ClobTest suite");
-
- TestSuite embedded = new TestSuite("ClobTest:embedded");
- embedded.addTestSuite(ClobTest.class);
- embedded.addTest(new ClobTest(
- "embeddedTestGetCharacterStreamLongNotImplemented"));
- ctSuite.addTest(new BlobClobTestSetup(embedded));
-
- TestSuite client = new TestSuite("ClobTest:client");
- client.addTestSuite(ClobTest.class);
- client.addTest(new ClobTest("clientTestGetCharacterStreamLong"));
- client.addTest(new ClobTest("clientTestGetCharacterStreamLong" +
- "ExceptionConditions"));
- ctSuite.addTest(TestConfiguration.clientServerDecorator(
- new BlobClobTestSetup(client)));
-
- return ctSuite;
+ return new BlobClobTestSetup(
+ TestConfiguration.defaultSuite(
+ ClobTest.class,
+ false));
}
} // End class ClobTest