List:Commits« Previous MessageNext Message »
From:tikeda Date:February 22 2006 1:29am
Subject:Connector/J commit: r4970 - in branches/branch_5_1/connector-j/src: com/mysql/jdbc testsuite/simple
View as plain text  
Modified:
   branches/branch_5_1/connector-j/src/com/mysql/jdbc/ServerPreparedStatement.java
   branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java
Log:
added ServerPreparedStatement.setNClob() and a testcase

Modified: branches/branch_5_1/connector-j/src/com/mysql/jdbc/ServerPreparedStatement.java
===================================================================
--- branches/branch_5_1/connector-j/src/com/mysql/jdbc/ServerPreparedStatement.java	2006-02-22 01:01:43 UTC (rev 4969)
+++ branches/branch_5_1/connector-j/src/com/mysql/jdbc/ServerPreparedStatement.java	2006-02-22 01:29:40 UTC (rev 4970)
@@ -42,6 +42,7 @@
 import java.sql.Array;
 import java.sql.Blob;
 import java.sql.Clob;
+import java.sql.NClob;
 import java.sql.Date;
 import java.sql.ParameterMetaData;
 import java.sql.Ref;
@@ -1652,7 +1653,70 @@
 			}
 		}
 	}
+    
+    /**
+     * @see java.sql.PreparedStatement#setNClob(int, java.sql.NClob)
+     */
+    public void setNClob(int parameterIndex, NClob x) throws SQLException {
+        // can't take if characterEncoding isn't utf8
+        if (!this.charEncoding.equalsIgnoreCase("UTF-8")
+                && !this.charEncoding.equalsIgnoreCase("utf8")) {
+            throw SQLError.createSQLException(
+                "Can not call setNClob() when connection character set isn't UTF-8");
+        }
+        
+        checkClosed();
+        
+        if (x == null) {
+            setNull(parameterIndex, java.sql.Types.NCLOB);
+        } else {
+            BindValue binding = getBinding(parameterIndex, true);
+            setType(binding, MysqlDefs.FIELD_TYPE_BLOB);
 
+            binding.value = x.getCharacterStream();
+            binding.isNull = false;
+            binding.isLongData = true;
+
+            if (this.connection.getUseStreamLengthsInPrepStmts()) {
+                binding.bindLength = x.length();
+            } else {
+                binding.bindLength = -1;
+            }
+        }
+    }
+    
+    /**
+     * @see java.sql.PreparedStatement#setNClob(int, java.io.Reader, long)
+     */
+    public void setNClob(int parameterIndex, Reader reader, long length)
+            throws SQLException {
+        // can't take if characterEncoding isn't utf8
+        if (!this.charEncoding.equalsIgnoreCase("UTF-8")
+                && !this.charEncoding.equalsIgnoreCase("utf8")) {
+            throw SQLError.createSQLException(
+                "Can not call setNClob() when connection character set isn't UTF-8");
+        }
+        
+        checkClosed();
+        
+        if (reader == null) {
+            setNull(parameterIndex, java.sql.Types.NCLOB);
+        } else {
+            BindValue binding = getBinding(parameterIndex, true);
+            setType(binding, MysqlDefs.FIELD_TYPE_BLOB);
+
+            binding.value = reader;
+            binding.isNull = false;
+            binding.isLongData = true;
+
+            if (this.connection.getUseStreamLengthsInPrepStmts()) {
+                binding.bindLength = length;
+            } else {
+                binding.bindLength = -1;
+            }
+        }
+    }
+
 	protected void setClosed(boolean flag) {
 		this.isClosed = flag;
 	}

Modified: branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java
===================================================================
--- branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java	2006-02-22 01:01:43 UTC (rev 4969)
+++ branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java	2006-02-22 01:29:40 UTC (rev 4970)
@@ -28,6 +28,7 @@
 import java.io.CharArrayReader;
 import java.io.Reader;
 import java.io.StringReader;
+import java.lang.reflect.Method;
 import java.sql.CallableStatement;
 import java.sql.Connection;
 import java.sql.NClob;
@@ -1647,10 +1648,9 @@
         pstmt1.setNClob(1, null);
         NClob nclob2 = conn1.createNClob();
         nclob2.setString(1, "aaa");
-        pstmt1.setNClob(2, nclob2);
-        NClob nclob3 = conn1.createNClob();
-        nclob3.setString(1, "\'aaa\'");
-        pstmt1.setNClob(3, nclob3);
+        pstmt1.setNClob(2, nclob2);                   // for setNClob(int, NClob)
+        Reader reader3 = new StringReader("\'aaa\'");
+        pstmt1.setNClob(3, reader3, 5);               // for setNClob(int, Reader, long)
         pstmt1.execute();
         ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNClob");
         rs1.next();
@@ -1673,10 +1673,9 @@
         pstmt2.setNClob(1, null);
         nclob2 = conn2.createNClob();
         nclob2.setString(1, "aaa");
-        pstmt2.setNClob(2, nclob2);
-        nclob3 = conn2.createNClob();
-        nclob3.setString(1, "\'aaa\'");
-        pstmt2.setNClob(3, nclob3);
+        pstmt2.setNClob(2, nclob2);             // for setNClob(int, NClob)
+        reader3 = new StringReader("\'aaa\'");
+        pstmt2.setNClob(3, reader3, 5);         // for setNClob(int, Reader, long)
         pstmt2.execute();
         ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2, c3 FROM testSetNClob");
         rs2.next();
@@ -1687,4 +1686,64 @@
         pstmt2.close();
         conn2.close();
     }
+    
+    
+    /**
+     * Tests for ServerPreparedStatement.setNClob()
+     * 
+     * @throws Exception
+     */
+    public void testSetNClobServer() throws Exception {
+        createTable("testSetNClobServer", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
+        Properties props1 = new Properties();
+        props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
+        props1.put("useUnicode", "true");
+        props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
+        Connection conn1 = getConnectionWithProps(props1);
+        com.mysql.jdbc.ServerPreparedStatement pstmt1 = (com.mysql.jdbc.ServerPreparedStatement)
+            conn1.prepareStatement("INSERT INTO testSetNClobServer (c1, c2) VALUES (?, ?)");
+        NClob nclob1 = conn1.createNClob();
+        nclob1.setString(1, "aaa");
+        Reader reader2 = new StringReader("aaa");
+        try {
+            pstmt1.setNClob(1, nclob1);
+            fail();
+        } catch (SQLException e) {
+            // ok
+            assertEquals("Can not call setNClob() when connection character set isn't UTF-8",
+                e.getMessage());  
+        }
+        try {
+            pstmt1.setNClob(2, reader2, 3);
+            fail();
+        } catch (SQLException e) {
+            // ok
+            assertEquals("Can not call setNClob() when connection character set isn't UTF-8",
+                e.getMessage());  
+        }
+        pstmt1.close();
+        conn1.close();
+        
+        createTable("testSetNClobServer", "(c1 NATIONAL CHARACTER(10), c2 LONGTEXT charset utf8)");
+        Properties props2 = new Properties();
+        props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
+        props2.put("useUnicode", "true");
+        props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
+        Connection conn2 = getConnectionWithProps(props2);
+        com.mysql.jdbc.ServerPreparedStatement pstmt2 = (com.mysql.jdbc.ServerPreparedStatement)
+            conn2.prepareStatement("INSERT INTO testSetNClobServer (c1, c2) VALUES (?, ?)");
+        nclob1 = conn2.createNClob();
+        nclob1.setString(1, "aaa");
+        pstmt2.setNClob(1, nclob1);
+        pstmt2.setNClob(2, new StringReader(
+                new String(new char[81921])), 81921); // 10 Full Long Data Packet's chars + 1 char
+        pstmt2.execute();
+        ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2 FROM testSetNClobServer");
+        rs2.next();
+        assertEquals("aaa", rs2.getString(1));
+        assertEquals(new String(new char[81921]), rs2.getString(2));
+        rs2.close();
+        pstmt2.close();
+        conn2.close();
+    }
 }

Thread
Connector/J commit: r4970 - in branches/branch_5_1/connector-j/src: com/mysql/jdbc testsuite/simpletikeda22 Feb