cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1507814 - in /cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc: ConnectionAwareResultIterator.java SQLTemplateAction.java SelectAction.java
Date Sun, 28 Jul 2013 17:14:49 GMT
Author: aadamchik
Date: Sun Jul 28 17:14:48 2013
New Revision: 1507814

URL: http://svn.apache.org/r1507814
Log:
CAY-1855  Iterated and paginated queries must print result counts

Modified:
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ConnectionAwareResultIterator.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ConnectionAwareResultIterator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ConnectionAwareResultIterator.java?rev=1507814&r1=1507813&r2=1507814&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ConnectionAwareResultIterator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ConnectionAwareResultIterator.java
Sun Jul 28 17:14:48 2013
@@ -27,9 +27,10 @@ import org.apache.cayenne.CayenneRuntime
 import org.apache.cayenne.ResultIterator;
 
 /**
- * A {@link ResultIterator} wrapper that handles closing a connection. Used in
- * iterators that are returned to the end users and are not implicitly managed
- * by Cayenne.
+ * A {@link ResultIterator} wrapper that handles closing a connection. Also
+ * internally counts processed rows, mostly for the benefit of subclasses.
+ * Subclasses are used in iterators that are returned to the end users and are
+ * not implicitly managed by Cayenne.
  * 
  * @since 3.2
  */
@@ -38,6 +39,7 @@ public class ConnectionAwareResultIterat
     private ResultIterator<T> delegate;
     private Connection connection;
     private boolean closed;
+    protected int rowCounter;
 
     public ConnectionAwareResultIterator(ResultIterator<T> delegate, Connection connection)
{
         this.delegate = delegate;
@@ -48,33 +50,35 @@ public class ConnectionAwareResultIterat
     public void close() {
 
         if (!closed) {
+            doClose();
+            closed = true;
+        }
+    }
 
-            StringBuilder errors = null;
+    protected void doClose() {
+        StringBuilder errors = null;
 
-            try {
-                delegate.close();
-            } catch (Exception e1) {
-                if (errors == null) {
-                    errors = new StringBuilder();
-                }
-                errors.append("Error closing ResultSet: " + e1);
+        try {
+            delegate.close();
+        } catch (Exception e1) {
+            if (errors == null) {
+                errors = new StringBuilder();
             }
+            errors.append("Error closing ResultSet: " + e1);
+        }
 
-            try {
-                connection.close();
-            } catch (SQLException e2) {
-                if (errors == null) {
-                    errors = new StringBuilder();
-                }
-
-                errors.append("Error closing connection: " + e2);
+        try {
+            connection.close();
+        } catch (SQLException e2) {
+            if (errors == null) {
+                errors = new StringBuilder();
             }
 
-            if (errors != null) {
-                throw new CayenneRuntimeException("Error closing ResultIterator: " + errors.toString());
-            }
+            errors.append("Error closing connection: " + e2);
+        }
 
-            closed = true;
+        if (errors != null) {
+            throw new CayenneRuntimeException("Error closing ResultIterator: " + errors.toString());
         }
     }
 
@@ -90,6 +94,7 @@ public class ConnectionAwareResultIterat
 
     @Override
     public T nextRow() {
+        rowCounter++;
         return delegate.nextRow();
     }
 

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java?rev=1507814&r1=1507813&r2=1507814&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
Sun Jul 28 17:14:48 2013
@@ -189,7 +189,7 @@ public class SQLTemplateAction implement
     }
 
     protected void processSelectResult(SQLStatement compiled, Connection connection, Statement
statement,
-            ResultSet resultSet, OperationObserver callback, long startTime) throws Exception
{
+            ResultSet resultSet, OperationObserver callback, final long startTime) throws
Exception {
 
         boolean iteratedResult = callback.isIteratedResult();
 
@@ -202,30 +202,34 @@ public class SQLTemplateAction implement
         ResultIterator it = result;
 
         if (iteratedResult) {
-            it = new ConnectionAwareResultIterator(it, connection);
+
+            it = new ConnectionAwareResultIterator(it, connection) {
+                @Override
+                protected void doClose() {
+                    adapter.getJdbcEventLogger().logSelectCount(rowCounter, System.currentTimeMillis()
- startTime);
+                    super.doClose();
+                }
+            };
         }
 
         it = new LimitResultIterator(it, getFetchOffset(), query.getFetchLimit());
 
-        if (!iteratedResult) {
-
-            // note that we are not closing the iterator here, relying on caller
-            // to close
-            // the underlying ResultSet on its own... this is a hack, maybe a
-            // cleaner flow
-            // is due here.
-            List<DataRow> resultRows = (List<DataRow>) it.allRows();
-
-            adapter.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis()
- startTime);
-
-            callback.nextRows(query, resultRows);
-        } else {
+        if (iteratedResult) {
             try {
                 callback.nextRows(query, it);
             } catch (Exception ex) {
                 it.close();
                 throw ex;
             }
+        } else {
+            // note that we are not closing the iterator here, relying on caller
+            // to close the underlying ResultSet on its own... this is a hack,
+            // maybe a cleaner flow is due here.
+            List<DataRow> resultRows = (List<DataRow>) it.allRows();
+
+            adapter.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis()
- startTime);
+
+            callback.nextRows(query, resultRows);
         }
     }
 

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java?rev=1507814&r1=1507813&r2=1507814&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
Sun Jul 28 17:14:48 2013
@@ -62,10 +62,16 @@ public class SelectAction extends BaseSQ
 
     public void performAction(Connection connection, OperationObserver observer) throws SQLException,
Exception {
 
-        long t1 = System.currentTimeMillis();
+        final long t1 = System.currentTimeMillis();
 
-        SelectTranslator translator = createTranslator(connection);
+        final SelectTranslator translator = createTranslator(connection);
         PreparedStatement prepStmt = translator.createStatement();
+
+        // TODO: ugly... 'createSqlString' is already called inside
+        // 'createStatement', but calling it here again to store for logging
+        // purposes
+        final String sqlString = translator.createSqlString();
+
         ResultSet rs;
 
         // need to run in try-catch block to close statement properly if
@@ -87,7 +93,14 @@ public class SelectAction extends BaseSQ
         ResultIterator it = workerIterator;
 
         if (observer.isIteratedResult()) {
-            it = new ConnectionAwareResultIterator(workerIterator, connection);
+            it = new ConnectionAwareResultIterator(it, connection) {
+                @Override
+                protected void doClose() {
+                    // 
+                    adapter.getJdbcEventLogger().logSelectCount(rowCounter, System.currentTimeMillis()
- t1, sqlString);
+                    super.doClose();
+                }
+            };
         }
 
         // wrap result iterator if distinct has to be suppressed
@@ -157,28 +170,24 @@ public class SelectAction extends BaseSQ
         // PreparedStatement in this
         // method, instead of relying on DefaultResultIterator to do that later
 
-        if (!observer.isIteratedResult()) {
-            // note that we don't need to close ResultIterator
-            // since "dataRows" will do it internally
-
+        if (observer.isIteratedResult()) {
+            try {
+                observer.nextRows(translator.getQuery(), it);
+            } catch (Exception ex) {
+                it.close();
+                throw ex;
+            }
+        } else {
             List<DataRow> resultRows;
             try {
-                resultRows = (List<DataRow>) it.allRows();
+                resultRows = it.allRows();
             } finally {
                 it.close();
             }
 
-            adapter.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis()
- t1,
-                    translator.createSqlString());
+            adapter.getJdbcEventLogger().logSelectCount(resultRows.size(), System.currentTimeMillis()
- t1, sqlString);
 
             observer.nextRows(query, resultRows);
-        } else {
-            try {
-                observer.nextRows(translator.getQuery(), it);
-            } catch (Exception ex) {
-                it.close();
-                throw ex;
-            }
         }
     }
 }



Mime
View raw message