]> source.dussan.org Git - vaadin-framework.git/commitdiff
Return 0 instead of throwing exception if count query returns nothing (#18043)
authorSteven Spungin <steven@spungin.tv>
Thu, 28 May 2015 13:35:08 +0000 (09:35 -0400)
committerVaadin Code Review <review@vaadin.com>
Mon, 8 Jun 2015 05:50:12 +0000 (05:50 +0000)
Change-Id: If01c0653021efc85a26d9d5896a4da9d155cf777

server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java
server/tests/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java

index 79a5b6c0670b679cfe405d027d54bb0adcafbaef..6b800cb9658c0210c89d25aea9ec4053e523375e 100644 (file)
@@ -155,8 +155,13 @@ public class FreeformQuery extends AbstractTransactionalQuery implements
                     pstmt = c.prepareStatement(sh.getQueryString());
                     sh.setParameterValuesToStatement(pstmt);
                     rs = pstmt.executeQuery();
-                    rs.next();
-                    count = rs.getInt(1);
+                    if (rs.next()) {
+                        count = rs.getInt(1);
+                    } else {
+                        // The result can be empty when using group by and there
+                        // are no matches (#18043)
+                        count = 0;
+                    }
                 } finally {
                     releaseConnection(c, pstmt, rs);
                 }
@@ -175,8 +180,13 @@ public class FreeformQuery extends AbstractTransactionalQuery implements
                 try {
                     statement = conn.createStatement();
                     rs = statement.executeQuery(countQuery);
-                    rs.next();
-                    count = rs.getInt(1);
+                    if (rs.next()) {
+                        count = rs.getInt(1);
+                    } else {
+                        // The result can be empty when using group by and there
+                        // are no matches (#18043)
+                        count = 0;
+                    }
                     return count;
                 } finally {
                     releaseConnection(conn, statement, rs);
index e193b79df3e5bb44abcb6aa5116c987337961184..bbf083c158fad7510797b92ada4c9ac789d30ea9 100644 (file)
@@ -23,6 +23,7 @@ import com.vaadin.data.util.sqlcontainer.SQLContainer;
 import com.vaadin.data.util.sqlcontainer.SQLTestsConstants;
 import com.vaadin.data.util.sqlcontainer.SQLTestsConstants.DB;
 import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
+import com.vaadin.data.util.sqlcontainer.query.generator.StatementHelper;
 
 public class FreeformQueryTest {
 
@@ -892,6 +893,83 @@ public class FreeformQueryTest {
         EasyMock.verify(delegate);
     }
 
+    public static class NonMatchingDelegateWithGroupBy implements
+            FreeformQueryDelegate {
+
+        private String fromWhere = "FROM people p1 LEFT JOIN people p2 ON p2.id = p1.id WHERE p1.\"NAME\" LIKE 'notfound' GROUP BY p1.ID";
+
+        @Override
+        public int storeRow(Connection conn, RowItem row)
+                throws UnsupportedOperationException, SQLException {
+            // Not used in this test
+            return 0;
+        }
+
+        @Override
+        public void setOrderBy(List<OrderBy> orderBys)
+                throws UnsupportedOperationException {
+            // Not used in this test
+        }
+
+        @Override
+        public void setFilters(List<Filter> filters)
+                throws UnsupportedOperationException {
+            // Not used in this test
+        }
+
+        @Override
+        public boolean removeRow(Connection conn, RowItem row)
+                throws UnsupportedOperationException, SQLException {
+            // Not used in this test
+            return false;
+        }
+
+        @Override
+        public String getQueryString(int offset, int limit)
+                throws UnsupportedOperationException {
+            return "SELECT * " + fromWhere;
+        }
+
+        @Override
+        public String getCountQuery() throws UnsupportedOperationException {
+            return "SELECT COUNT(*) " + fromWhere;
+        }
+
+        @Override
+        public String getContainsRowQueryString(Object... keys)
+                throws UnsupportedOperationException {
+            // Not used in this test
+            return null;
+        }
+    }
+
+    public static class NonMatchingStatementDelegateWithGroupBy extends
+            NonMatchingDelegateWithGroupBy implements FreeformStatementDelegate {
+
+        @Override
+        public StatementHelper getQueryStatement(int offset, int limit)
+                throws UnsupportedOperationException {
+            StatementHelper sh = new StatementHelper();
+            sh.setQueryString(getQueryString(offset, limit));
+            return sh;
+        }
+
+        @Override
+        public StatementHelper getCountStatement()
+                throws UnsupportedOperationException {
+            StatementHelper sh = new StatementHelper();
+            sh.setQueryString(getCountQuery());
+            return sh;
+        }
+
+        @Override
+        public StatementHelper getContainsRowQueryStatement(Object... keys)
+                throws UnsupportedOperationException {
+            // Not used in this test
+            return null;
+        }
+    }
+
     @Test
     public void containsRowWithKeys_delegateRegisteredGetContainsRowQueryStringNotImplemented_shouldBuildQueryString()
             throws SQLException {
@@ -909,4 +987,24 @@ public class FreeformQueryTest {
 
         EasyMock.verify(delegate);
     }
+
+    @Test
+    public void delegateStatementCountWithGroupBy() throws SQLException {
+        String dummyNotUsed = "foo";
+        FreeformQuery query = new FreeformQuery(dummyNotUsed, connectionPool,
+                "p1.ID");
+        query.setDelegate(new NonMatchingStatementDelegateWithGroupBy());
+
+        Assert.assertEquals(0, query.getCount());
+    }
+
+    @Test
+    public void delegateCountWithGroupBy() throws SQLException {
+        String dummyNotUsed = "foo";
+        FreeformQuery query = new FreeformQuery(dummyNotUsed, connectionPool,
+                "p1.ID");
+        query.setDelegate(new NonMatchingDelegateWithGroupBy());
+
+        Assert.assertEquals(0, query.getCount());
+    }
 }