Commits

Grace Batumbya  committed 343c646

Added support for InputStreams on PreparedStatement
1. setBinaryStream()
2. setAsciiStream()
3. setUnicodeStream()

Fixed issue 54 - Implement PreparedStatement.setBinaryStream()

  • Participants
  • Parent commits 4f17c9f

Comments (0)

Files changed (5)

 Work in progress:
 
+* Added support for InputStreams on PreparedStatement
+    1. setBinaryStream()
+    2. setAsciiStream()
+    3. setUnicodeStream()
+* Issue 54 - Implement PreparedStatement.setBinaryStream()
 
 Release 3.7.15-SNAPSHOT-2
 * Fixed clean target to remove previous driver builds

File src/main/java/org/sqlite/PrepStmt.java

 
 package org.sqlite;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.Reader;
 import java.math.BigDecimal;
 import java.sql.Date;
      * @see java.sql.PreparedStatement#setBigDecimal(int, java.math.BigDecimal)
      */
     public void setBigDecimal(int pos, BigDecimal value) throws SQLException {
-		batch(pos, value == null ? null : value.toString());
-	}
+        batch(pos, value == null ? null : value.toString());
+    }
+
+    /**
+     * Reads given number of bytes from an input stream.
+     * @param istream The input stream.
+     * @param length The number of bytes to read.
+     * @return byte array.
+     * @throws SQLException
+     */
+    private byte[] readBytes(InputStream istream, int length) throws SQLException {
+        if (length < 0) {
+            SQLException exception =
+                new SQLException("Error reading stream. Length should be non-negative");
+
+            throw exception;
+        } 
+        
+        byte[] bytes = new byte[length];
+
+        try 
+        {
+            istream.read(bytes);
+
+            return bytes;
+        } 
+        catch (IOException cause)
+        {
+            SQLException exception = new SQLException("Error reading stream");
+
+            exception.initCause(cause);
+            throw(exception);
+        }
+    }
+
+    /**
+     * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, int)
+     */
+    public void setBinaryStream(int pos, InputStream istream, int length) throws SQLException {
+        if (istream == null && length == 0) {
+            setBytes(pos, null);
+        }
+
+        setBytes(pos, readBytes(istream, length));
+    }
+
+    /**
+     * @see java.sql.PreparedStatement#setAsciiStream(int, java.io.InputStream, int)
+     */
+    public void setAsciiStream(int pos, InputStream istream, int length) throws SQLException {
+        setUnicodeStream(pos, istream, length);
+    }
+
+    /**
+     * @see java.sql.PreparedStatement#setUnicodeStream(int, java.io.InputStream, int)
+     */
+    public void setUnicodeStream(int pos, InputStream istream, int length) throws SQLException {
+        if (istream == null && length == 0) {
+            setString(pos, null);
+        }
+
+        setString(pos, new String(readBytes(istream, length)));
+    }
 
     /**
      * @see java.sql.PreparedStatement#setBoolean(int, boolean)

File src/main/java/org/sqlite/Unused.java

 
     public void setArray(int i, Array x)
         throws SQLException { throw unused(); }
-    public void setAsciiStream(int parameterIndex, InputStream x, int length)
-        throws SQLException { throw unused(); }
 //    public void setBigDecimal(int parameterIndex, BigDecimal x)
 //        throws SQLException { throw unused(); }
-    public void setBinaryStream(int parameterIndex, InputStream x, int length)
-        throws SQLException { throw unused(); }
     public void setBlob(int i, Blob x)
         throws SQLException { throw unused(); }
     public void setClob(int i, Clob x)
         throws SQLException { throw unused(); }
     public void setRef(int i, Ref x)
         throws SQLException { throw unused(); }
-    public void setUnicodeStream(int pos, InputStream x, int length)
-        throws SQLException { throw unused(); }
     public void setURL(int pos, URL x)
         throws SQLException { throw unused(); }
 

File src/test/java/org/sqlite/ConnectionTest.java

         rs = stmt4.executeQuery("select * from tbl2");
         assertTrue(rs.next());
         assertEquals(200, rs.getInt(1));
-        
+        rs.close();
+        stmt4.close();
+        conn4.close();
     }
 }

File src/test/java/org/sqlite/PrepStmtTest.java

 
 import static org.junit.Assert.*;
 
+import java.io.ByteArrayInputStream;
+import java.io.UnsupportedEncodingException;
 import java.sql.Connection;
 import java.sql.Date;
 import java.sql.DriverManager;
 import java.sql.Statement;
 import java.util.StringTokenizer;
 
+import javax.naming.BinaryRefAddr;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.BeforeClass;
     }
 
     @Test
-    public void set() throws SQLException {
+    public void set() throws SQLException, UnsupportedEncodingException {
         ResultSet rs;
         PreparedStatement prep = conn.prepareStatement("select ?, ?, ?;");
 
         assertArrayEq(rs.getBytes(3), b3);
         assertFalse(rs.next());
         rs.close();
+
+        // streams
+        ByteArrayInputStream inByte = new ByteArrayInputStream(b1);
+        prep.setBinaryStream(1, inByte, b1.length);
+        ByteArrayInputStream inAscii = new ByteArrayInputStream(b2);
+        prep.setAsciiStream(2, inAscii, b2.length);
+        byte[] b3 = utf08.getBytes("UTF-8");
+        ByteArrayInputStream inUnicode = new ByteArrayInputStream(b3);
+        prep.setUnicodeStream(3, inUnicode, b3.length);
+
+        rs = prep.executeQuery();
+        assertTrue(rs.next());
+        assertArrayEq(b1, rs.getBytes(1));
+        assertEquals(new String(b2), rs.getString(2));
+        assertEquals(new String(b3), rs.getString(3));
+        assertFalse(rs.next());
+        rs.close();
     }
 
     @Test