db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r231110 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/dynabean/ java/org/apache/ddlutils/util/ test/org/apache/ddlutils/dynabean/
Date Tue, 09 Aug 2005 20:55:39 GMT
Author: tomdz
Date: Tue Aug  9 13:55:22 2005
New Revision: 231110

URL: http://svn.apache.org/viewcvs?rev=231110&view=rev
Log:
Fixed error in DynaSqlIterator that advanced the result set too early
Added a dynasql test containing a query with a join

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSql.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSqlIterator.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/util/DDLExecutor.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/dynabean/TestDynaSqlQueries.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSql.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSql.java?rev=231110&r1=231109&r2=231110&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSql.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSql.java Tue Aug  9 13:55:22
2005
@@ -146,7 +146,8 @@
             // otherwise we're leaving it open for the iterator
             if (answer == null)
             {
-                closeStatementAndConnection(statement);
+                closeStatement(statement);
+                returnConnection(connection);
             }
         }
     }
@@ -181,10 +182,11 @@
         }
         finally
         {
-            // if any exceptions are thrown, close things down
+            // if any exceptions are thrown (no answer), close things down
             if (answer == null)
             {
-                closeStatementAndConnection(statement);
+                closeStatement(statement);
+                returnConnection(connection);
             }
         }
     }
@@ -254,7 +256,8 @@
         catch (SQLException ex)
         {
             // any other exception comes from the iterator which closes the resources automatically
-            closeStatementAndConnection(statement);
+            closeStatement(statement);
+            returnConnection(connection);
         }
         return result;
     }
@@ -305,7 +308,8 @@
         catch (SQLException ex)
         {
             // any other exception comes from the iterator which closes the resources automatically
-            closeStatementAndConnection(statement);
+            closeStatement(statement);
+            returnConnection(connection);
         }
         return result;
     }
@@ -486,7 +490,7 @@
         }
         finally
         {
-            closeStatementAndConnection(statement);
+            closeStatement(statement);
             returnConnection(connection);
         }
     }
@@ -562,6 +566,7 @@
     /**
      * Inserts the bean. If one of the columns is an auto-incremented column, then the
      * dyna bean will also be updated with the column value generated by the database.
+     * Note that the connection will not be closed by this method.
      * 
      * @param dynaBean   The bean
      * @param connection The database connection
@@ -629,7 +634,7 @@
         }
         finally
         {
-            closeStatementAndConnection(statement);
+            closeStatement(statement);
         }
         if (queryIdSql != null)
         {
@@ -670,7 +675,8 @@
                 {
                     lastInsertedIds.close();
                 }
-                closeStatementAndConnection(queryStmt);
+                closeStatement(statement);
+                returnConnection(connection);
             }
         }
     }
@@ -726,7 +732,8 @@
         }
         finally
         {
-            closeStatementAndConnection(statement);
+            closeStatement(statement);
+            returnConnection(connection);
         }
     }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSqlIterator.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSqlIterator.java?rev=231110&r1=231109&r2=231110&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSqlIterator.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/dynabean/DynaSqlIterator.java Tue Aug 
9 13:55:22 2005
@@ -32,6 +32,8 @@
     private DynaClass _dynaClass;
     /** Maps column names to properties */
     private Map _columnsToProperties = new ListOrderedMap();
+    /** Whether the next call to hasNext or next needs advancement */
+    private boolean _needsAdvancing = true;
     /** Whether we're already at the end of the result set */
     private boolean _isAtEnd = false;
     /** Whether to close the statement and connection after finishing */
@@ -134,31 +136,8 @@
      */
     public boolean hasNext() throws DynaSqlException
     {
-        if (_isAtEnd)
-        {
-            return false;
-        }
-        else
-        {
-            try
-            {
-                if (_resultSet.next())
-                {
-                    return true;
-                }
-                else
-                {
-                    _isAtEnd = true;
-                    cleanUp();
-                    return false;
-                }
-            }
-            catch (SQLException ex)
-            {
-                cleanUp();
-                throw new DynaSqlException("Exception while advancing the resultset cursor",
ex);
-            }
-        }
+        advanceIfNecessary();
+        return !_isAtEnd;
     }
 
     /* (non-Javadoc)
@@ -166,6 +145,7 @@
      */
     public Object next() throws DynaSqlException
     {
+        advanceIfNecessary();
         if (_isAtEnd)
         {
             throw new NoSuchElementException("No more elements in the resultset");
@@ -182,6 +162,7 @@
 
                     bean.set((String)entry.getValue(), _resultSet.getObject((String)entry.getKey()));
                 }
+                _needsAdvancing = true;
                 return bean;
             }
             catch (Exception ex)
@@ -192,6 +173,30 @@
         }
     }
 
+    /**
+     * Advances the result set if necessary.
+     */
+    private void advanceIfNecessary() throws DynaSqlException
+    {
+        if (_needsAdvancing && !_isAtEnd)
+        {
+            try
+            {
+                _isAtEnd        = !_resultSet.next();
+                _needsAdvancing = false;
+            }
+            catch (SQLException ex)
+            {
+                cleanUp();
+                throw new DynaSqlException("Could not retrieve next row from result set",
ex);
+            }
+            if (_isAtEnd)
+            {
+                cleanUp();
+            }
+        }
+    }
+    
     /* (non-Javadoc)
      * @see java.util.Iterator#remove()
      */

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/DDLExecutor.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/DDLExecutor.java?rev=231110&r1=231109&r2=231110&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/DDLExecutor.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/DDLExecutor.java Tue Aug  9 13:55:22
2005
@@ -201,8 +201,9 @@
             }
             log.info( "Executed: "+ commandCount + " statement(s) with " + errors + " error(s)"
);
         }
-        finally {
-            closeStatementAndConnection(statement);
+        finally
+        {
+            closeStatement(statement);
             returnConnection(connection);
         }
 

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java?rev=231110&r1=231109&r2=231110&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/JdbcSupport.java Tue Aug  9 13:55:22
2005
@@ -105,7 +105,7 @@
      * 
      * @param statement The statement
      */
-    public void closeStatementAndConnection(Statement statement)
+    public void closeStatement(Statement statement)
     {
         if (statement != null)
         {
@@ -116,11 +116,6 @@
                 if ((conn != null) && !conn.isClosed())
                 {
                     statement.close();
-                    // this might have closed the connection ?
-                    if (!conn.isClosed())
-                    {
-                        returnConnection(conn);
-                    }
                 }
             }
             catch (Exception e)

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/dynabean/TestDynaSqlQueries.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/dynabean/TestDynaSqlQueries.java?rev=231110&r1=231109&r2=231110&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/dynabean/TestDynaSqlQueries.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/dynabean/TestDynaSqlQueries.java Tue Aug
 9 13:55:22 2005
@@ -31,6 +31,8 @@
         DynaBean        bean    = null;
 
         assertTrue(it.hasNext());
+        // we call the method a second time to assert that the result set does not get advanced
twice
+        assertTrue(it.hasNext());
 
         bean = (DynaBean)it.next();
 
@@ -107,4 +109,47 @@
         assertEquals("Text 3",
                      bean.get("text"));
     }
+
+    public void testJoinQuery() throws Exception
+    {
+        createDatabase(
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='ddlutils'>\n"+
+            "  <table name='testtable1'>\n"+
+            "    <column name='id1' type='INTEGER' primaryKey='true'/>\n"+
+            "    <column name='id2' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "  <table name='testtable2'>\n"+
+            "    <column name='id'   type='INTEGER' primaryKey='true'/>\n"+
+            "    <column name='text' type='VARCHAR' size='15'/>\n"+
+            "  </table>\n"+
+            "</database>");
+
+        insertData(
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<data>\n"+
+            "  <testtable1 id1='1'/>\n"+
+            "  <testtable1 id1='2' id2='3'/>\n"+
+            "  <testtable2 id='1' text='Text 1'/>\n"+
+            "  <testtable2 id='2' text='Text 2'/>\n"+
+            "  <testtable2 id='3' text='Text 3'/>"+
+            "</data>");
+
+        DynaSql         dynaSql = new DynaSql(getBuilder(), getDataSource(), getModel());
+        DynaSqlIterator it      = (DynaSqlIterator)dynaSql.query("SELECT id1, text FROM testtable1,
testtable2 WHERE id2 = id");
+        DynaBean        bean    = null;
+
+        assertTrue(it.hasNext());
+
+        bean = (DynaBean)it.next();
+
+        assertEquals(new Integer(2),
+                     bean.get("id1"));
+        assertEquals("Text 3",
+                     bean.get("text"));
+
+        assertFalse(it.hasNext());
+        assertFalse(it.isConnectionOpen());
+    }
+
 }



Mime
View raw message