From 7456b6cf0c2c7e66b7040af2e793566607d2a447 Mon Sep 17 00:00:00 2001 From: Steven Spungin Date: Thu, 28 May 2015 09:35:08 -0400 Subject: [PATCH] Return 0 instead of throwing exception if count query returns nothing (#18043) Change-Id: If01c0653021efc85a26d9d5896a4da9d155cf777 --- .../sqlcontainer/query/FreeformQuery.java | 18 +++- .../sqlcontainer/query/FreeformQueryTest.java | 98 +++++++++++++++++++ 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java b/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java index 79a5b6c067..6b800cb965 100644 --- a/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java +++ b/server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java @@ -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); diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java index e193b79df3..bbf083c158 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java @@ -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 orderBys) + throws UnsupportedOperationException { + // Not used in this test + } + + @Override + public void setFilters(List 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()); + } } -- 2.39.5