]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix updatable ResultSets stream methods (ascii, character, binary).
authorKris Jurka <books@ejurka.com>
Mon, 21 Jun 2004 02:01:12 +0000 (02:01 +0000)
committerKris Jurka <books@ejurka.com>
Mon, 21 Jun 2004 02:01:12 +0000 (02:01 +0000)
The existing code didn't correctly allocate data arrays, and it
failed to loop when a stream didn't provide the full amount of
data requested of it.

Reported by Jan de Visser.

src/interfaces/jdbc/org/postgresql/errors.properties
src/interfaces/jdbc/org/postgresql/errors_pt_BR.properties
src/interfaces/jdbc/org/postgresql/errors_ru.properties
src/interfaces/jdbc/org/postgresql/errors_zh_TW.properties
src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java

index 468ef71aa8b16781eaf06d8321fdb9231d16acb1..a0b7ddee3e2463efabe6372952e5c3e471285fd1 100644 (file)
@@ -93,7 +93,6 @@ postgresql.updateable.emptydelete:Can't deleteRow() on empty result set
 postgresql.updateable.beforestartdelete:Before start of result set. Can not call deleteRow().
 postgresql.updateable.afterlastdelete:After end of result set. Can not call deleteRow().
 postgresql.updateable.notoninsertrow:Not on insert row.
-postgresql.updateable.inputstream:Input Stream is null.
 postgresql.updateable.ioerror:Input Stream Error - {0}
 postgresql.call.noreturntype:A CallableStatement Function was declared but no call to 'registerOutParameter (1, <some_type>)' was made.
 postgresql.call.noinout:PostgreSQL only supports function return value [@ 1] (no OUT or INOUT arguments)
index 1f6c1dd4f82c13ff2cb3e8f9d4ec90c6309af951..73bb2b3adff25d40461388416f53577943ace823 100644 (file)
@@ -95,7 +95,6 @@ postgresql.updateable.emptydelete:N
 postgresql.updateable.beforestartdelete:Antes do início do conjunto de resultados. Não pode chamar deleteRow().
 postgresql.updateable.afterlastdelete:Depois do fim do conjunto de resultados. Não pode chamar deleteRow().
 postgresql.updateable.notoninsertrow:Não está inserindo um registro.
-postgresql.updateable.inputstream:Fluxo de Entrada é nulo.
 postgresql.updateable.ioerror:Erro de Fluxo de Entrada - {0}
 postgresql.call.noreturntype:Uma Instrução Executável foi declarada mas nenhuma chamada a 'registerOutParameter (1, <algum_tipo>)' foi feita.
 postgresql.call.noinout:PostgreSQL só suporta função que retorna valor [@ 1] (nenhum argumento OUT ou INOUT)
index 51271b62b365c083f29d0b1d8829c40dac2271a8..79eb009ef31f46550dae71f8b20e0dbdff976fb3 100644 (file)
@@ -99,7 +99,6 @@ postgresql.updateable.emptydelete:Can't deleteRow() on empty result set
 postgresql.updateable.beforestartdelete:Before start of result set. Can not call deleteRow().
 postgresql.updateable.afterlastdelete:After end of result set. Can not call deleteRow().
 postgresql.updateable.notoninsertrow:Not on insert row.
-postgresql.updateable.inputstream:Input Stream is null.
 postgresql.updateable.ioerror:ошибка Input Stream - {0}
 postgresql.call.noreturntype:A CallableStatement Function was declared but no call to 'registerOutParameter (1, <some_type>)' was made.
 postgresql.call.noinout:PostgreSQL only supports function return value [@ 1] (no OUT or INOUT arguments)
index eed4a40e86c6038a6d342b595bf644f1aced6f45..8b26e558e995e6b26e52ceb6f72589ff61703ac0 100644 (file)
@@ -93,7 +93,6 @@ postgresql.updateable.emptydelete:\u4e0d\u884c\u5728\u7a7a\u7684ResultSet\u4f7f\
 postgresql.updateable.beforestartdelete:\u4e0d\u884c\u5728ResultSet\u7684\u7b2c\u4e00\u7b46\u8cc7\u6599\u4e4b\u524d\u547c\u53ebdeleteRow()\u65b9\u6cd5\u3002
 postgresql.updateable.afterlastdelete:\u4e0d\u884c\u5728ResultSet\u7684\u6700\u5f8c\u4e00\u7b46\u8cc7\u6599\u4e4b\u5f8c\u547c\u53ebdeleteRow()\u65b9\u6cd5\u3002
 postgresql.updateable.notoninsertrow:\u4e0d\u5728\u65b0\u589e\u7684\u8cc7\u6599\u5217\u4e0a\u3002
-postgresql.updateable.inputstream:InputStream\u662fnull\u3002
 postgresql.updateable.ioerror:InputStream\u932f\u8aa4\u3002
 postgresql.call.noreturntype:\u5df2\u7d93\u5ba3\u544aCallableStatement\u51fd\u5f0f\uff0c\u4f46\u662f\u5c1a\u672a\u547c\u53eb'registerOutParameter (1, <some_type>)'\u3002
 postgresql.call.noinout:PostgreSQL\u53ea\u652f\u63f4\u50b3\u56de\u503c\u70ba[@ 1]\u7684\u51fd\u5f0f(\u6c92\u6709OUT\u6216INPUT\u5f15\u6578)\u3002
index 6a479e7cd012128f96ccd93f481e796d7aeace4a..b44e301854d77a2ba17d0860f5dc7241dbf2e161 100644 (file)
@@ -9,7 +9,7 @@
  * Copyright (c) 2003, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.6 2004/06/16 05:11:44 jurka Exp $
+ *       $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc2/Attic/AbstractJdbc2ResultSet.java,v 1.25.2.7 2004/06/21 02:01:11 jurka Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -18,6 +18,8 @@ package org.postgresql.jdbc2;
 import java.io.CharArrayReader;
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
 import java.sql.*;
 import java.util.Enumeration;
@@ -210,6 +212,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
 
                rowBuffer = new byte[this_row.length][];
                System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length);
+               onInsertRow = false;
 
                return true;
        }
@@ -220,6 +223,8 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                final int rows_size = rows.size();
                if (rows_size > 0)
                        current_row = rows_size;
+
+               onInsertRow = false;
        }
 
 
@@ -227,6 +232,8 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
        {
                if (rows.size() > 0)
                        current_row = -1;
+
+               onInsertRow = false;
        }
 
 
@@ -235,12 +242,12 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                if (rows.size() <= 0)
                        return false;
 
-               onInsertRow = false;
                current_row = 0;
                this_row = (byte[][]) rows.elementAt(current_row);
 
                rowBuffer = new byte[this_row.length][];
                System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length);
+               onInsertRow = false;
 
                return true;
        }
@@ -483,6 +490,7 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
 
                rowBuffer = new byte[this_row.length][];
                System.arraycopy(this_row, 0, rowBuffer, 0, this_row.length);
+               onInsertRow = false;
 
                return true;
        }
@@ -762,21 +770,39 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                                                                                          )
        throws SQLException
        {
-               byte[] theData = null;
+               if (x == null)
+               {
+                       updateNull(columnIndex);
+                       return;
+               }
+
                try
                {
-                       x.read(theData, 0, length);
+                       InputStreamReader reader = new InputStreamReader(x, "ASCII");
+                       char data[] = new char[length];
+                       int numRead = 0;
+                       while (true)
+                       {
+                               int n = reader.read(data, numRead, length - numRead);
+                               if (n == -1)
+                                       break;
+
+                               numRead += n;
+
+                               if (numRead == length)
+                                       break;
+                       }
+                       updateString(columnIndex, new String(data, 0, numRead));
                }
-               catch (NullPointerException ex )
+               catch (UnsupportedEncodingException uee)
                {
-                       throw new PSQLException("postgresql.updateable.inputstream");
+                       throw new PSQLException("postgresql.unusual", PSQLState.UNEXPECTED_ERROR, uee);
                }
                catch (IOException ie)
                {
                        throw new PSQLException("postgresql.updateable.ioerror", ie);
                }
 
-               updateValue(columnIndex, theData);
        }
 
 
@@ -794,21 +820,46 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                                                                                           )
        throws SQLException
        {
-               byte[] theData = null;
-               try
+               if (x == null)
                {
-                       x.read(theData, 0, length);
-
+                       updateNull(columnIndex);
+                       return;
                }
-               catch ( NullPointerException ex )
+
+               byte data[] = new byte[length];
+               int numRead = 0;
+               try
                {
-                       throw new PSQLException("postgresql.updateable.inputstream");
+                       while (true)
+                       {
+                               int n = x.read(data, numRead, length - numRead);
+                               if (n == -1)
+                                       break;
+
+                               numRead += n;
+
+                               if (numRead == length)
+                                       break;
+                       }
                }
                catch (IOException ie)
                {
                        throw new PSQLException("postgresql.updateable.ioerror", ie);
                }
-               updateValue(columnIndex, theData);
+
+               if (numRead == length)
+               {
+                       updateBytes(columnIndex, data);
+               }
+               else
+               {
+                       // the stream contained less data than they said
+                       // perhaps this is an error?
+                       byte data2[] = new byte[numRead];
+                       System.arraycopy(data, 0, data2, 0, numRead);
+                       updateBytes(columnIndex, data2);
+               }
+
        }
 
 
@@ -841,21 +892,33 @@ public abstract class AbstractJdbc2ResultSet extends org.postgresql.jdbc1.Abstra
                                                                                                  )
        throws SQLException
        {
-               char[] theData = null;
-               try
+               if (x == null)
                {
-                       x.read(theData, 0, length);
-
+                       updateNull(columnIndex);
+                       return;
                }
-               catch (NullPointerException ex)
+
+               try
                {
-                       throw new PSQLException("postgresql.updateable.inputstream");
+                       char data[] = new char[length];
+                       int numRead = 0;
+                       while (true)
+                       {
+                               int n = x.read(data, numRead, length - numRead);
+                               if (n == -1)
+                                       break;
+
+                               numRead += n;
+
+                               if (numRead == length)
+                                       break;
+                       }
+                       updateString(columnIndex, new String(data, 0, numRead));
                }
                catch (IOException ie)
                {
                        throw new PSQLException("postgresql.updateable.ioerror", ie);
                }
-               updateValue(columnIndex, theData);
        }
 
 
index e3a932136a1f2663ade8072f8f748a0d5eaed123..b3284d4df28149e698141a24254fe92b96748ca8 100644 (file)
@@ -3,6 +3,12 @@ package org.postgresql.test.jdbc2;
 import java.sql.*;
 import junit.framework.TestCase;
 
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+
 import org.postgresql.test.TestUtil;
 /**
  * <p>Title: </p>
@@ -27,6 +33,7 @@ public class UpdateableResultTest extends TestCase
                con = TestUtil.openDB();
                TestUtil.createTable(con, "updateable", "id int primary key, name text, notselected text");
                TestUtil.createTable(con, "second", "id1 int primary key, name1 text");
+               TestUtil.createTable(con, "stream", "id int primary key, asi text, chr text, bin bytea");
 
                // put some dummy data into second
                Statement st2 = con.createStatement();
@@ -39,6 +46,7 @@ public class UpdateableResultTest extends TestCase
        {
                TestUtil.dropTable(con, "updateable");
                TestUtil.dropTable(con, "second");
+               TestUtil.dropTable(con, "stream");
                TestUtil.closeDB(con);
        }
 
@@ -110,6 +118,52 @@ public class UpdateableResultTest extends TestCase
                st.close();
        }
 
+       public void testUpdateStreams() throws SQLException, UnsupportedEncodingException
+       {
+               Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
+               ResultSet rs = stmt.executeQuery("SELECT id, asi, chr, bin FROM stream");
+
+               rs.moveToInsertRow();
+               rs.updateInt(1, 1);
+               rs.updateAsciiStream("asi", null, 17);
+               rs.updateCharacterStream("chr", null, 81);
+               rs.updateBinaryStream("bin", null, 0);
+               rs.insertRow();
+
+               rs.beforeFirst();
+               rs.next();
+
+               assertEquals(1, rs.getInt(1));
+               assertNull(rs.getString(2));
+               assertNull(rs.getString(3));
+               assertNull(rs.getBytes(4));
+
+               String string = "Hello";
+               InputStream asi = new ByteArrayInputStream(string.getBytes("US-ASCII"));
+               Reader chr = new StringReader(string);
+               InputStream bin = new ByteArrayInputStream(string.getBytes("US-ASCII"));
+
+               rs.updateInt("id", 2);
+               rs.updateAsciiStream("asi", asi, 5);
+               rs.updateCharacterStream("chr", chr, 5);
+               rs.updateBinaryStream("bin", bin, 5);
+               rs.updateRow();
+
+               assertEquals(2, rs.getInt(1));
+               assertEquals(string, rs.getString(2));
+               assertEquals(string, rs.getString(3));
+               assertEquals(string, rs.getString(4));
+
+               rs.refreshRow();
+
+               assertEquals(2, rs.getInt(1));
+               assertEquals(string, rs.getString(2));
+               assertEquals(string, rs.getString(3));
+               assertEquals(string, rs.getString(4));
+
+               rs.close();
+               stmt.close();
+       }
 
 
        public void testUpdateable() throws SQLException