From 14a02417927de84ff33c127459d3c5375eedf093 Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Mon, 21 Dec 2009 19:55:58 +0000 Subject: [PATCH] OPENJPA-1440: Allow COUNT(*) syntax when DBDictionary.useWildCardForCount=true git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@892947 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/sql/DBDictionary.java | 3 +- .../persistence/query/TestWildCardCount.java | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index 560b6e857..aa0ce618e 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -247,6 +247,7 @@ public class DBDictionary public boolean supportsSelectFromFinalTable = false; public boolean supportsSimpleCaseExpression = true; public boolean supportsGeneralCaseExpression = true; + public boolean useWildCardForCount = false; /** * Some Databases append whitespace after the schema name @@ -1883,7 +1884,7 @@ public class DBDictionary // if the select has no identifier cols, use COUNT(*) List aliases = (!sel.isDistinct()) ? Collections.EMPTY_LIST : sel.getIdentifierAliases(); - if (aliases.isEmpty()) { + if (useWildCardForCount || aliases.isEmpty()) { selectSQL.append("COUNT(*)"); return toSelect(selectSQL, null, from, where, null, null, null, false, false, 0, Long.MAX_VALUE); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java new file mode 100644 index 000000000..67bd272f4 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java @@ -0,0 +1,93 @@ +/* + * TestMathQueries.java + * + * Created on October 18, 2006, 1:06 PM + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.openjpa.persistence.query; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.persistence.common.apps.RuntimeTest1; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +/** + * Tests usage of COUNT(*) in SQL query. + *
+ * Further details available at
+ * OPENJPA-1440 + * + * @author Pinaki Poddar + * + */ +public class TestWildCardCount extends SQLListenerTestCase { + private EntityManager em; + public void setUp() throws Exception { + super.setUp(RuntimeTest1.class, "openjpa.jdbc.QuerySQLCache", "false"); + em = emf.createEntityManager(); + sql.clear(); + } + + public void testWildCardForCountInSingleProjectTerm() { + String jpql = "select COUNT(p) from RuntimeTest1 p"; + executeAndAssert(jpql); + } + + public void testWildCardForCountInMultipleProjectTerms() { + String jpql = "select COUNT(p.intField),SUM(p.intField) from RuntimeTest1 p GROUP BY p.intField"; + executeAndAssert(jpql); + } + + public void testWildCardForCountInMultipleProjectTermsButCountIsNotFirstTerm() { + String jpql = "select SUM(p.intField),COUNT(p.intField) from RuntimeTest1 p GROUP BY p.intField"; + executeAndAssert(jpql); + } + + void executeAndAssert(String jpql) { + executeAndAssert(true, jpql); + executeAndAssert(false, jpql); + } + + void executeAndAssert(boolean useWildCard, String jpql) { + setWildCardForCount(useWildCard); + sql.clear(); + em.createQuery(jpql).getResultList(); + assertEquals(1, sql.size()); + assertEquals(getWildCardForCount(), usesWildCardForCount(sql.get(0))); + } + + boolean getWildCardForCount() { + return ((JDBCConfiguration)emf.getConfiguration()).getDBDictionaryInstance().useWildCardForCount; + } + + void setWildCardForCount(boolean flag) { + ((JDBCConfiguration)emf.getConfiguration()).getDBDictionaryInstance().useWildCardForCount = flag; + } + + boolean usesWildCardForCount(String sql) { + return sql.contains("COUNT(*)"); + } + +}