summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorSteven Spungin <steven@spungin.tv>2015-05-28 09:35:08 -0400
committerVaadin Code Review <review@vaadin.com>2015-06-08 05:50:12 +0000
commit7456b6cf0c2c7e66b7040af2e793566607d2a447 (patch)
tree3abb6eb4bf35c3cc7ee67d5ea65f98040330b8f5 /server
parentbaf00a3368eeee678d8ccc6ac64096a159b8b703 (diff)
downloadvaadin-framework-7456b6cf0c2c7e66b7040af2e793566607d2a447.tar.gz
vaadin-framework-7456b6cf0c2c7e66b7040af2e793566607d2a447.zip
Return 0 instead of throwing exception if count query returns nothing (#18043)
Change-Id: If01c0653021efc85a26d9d5896a4da9d155cf777
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.java18
-rw-r--r--server/tests/src/com/vaadin/data/util/sqlcontainer/query/FreeformQueryTest.java98
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<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());
+ }
}