You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2010/04/30 00:52:44 UTC
svn commit: r939507 - in /openjpa/branches/1.3.x:
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/
Author: mikedd
Date: Thu Apr 29 22:52:44 2010
New Revision: 939507
URL: http://svn.apache.org/viewvc?rev=939507&view=rev
Log:
OPENJPA-1440: Allow COUNT(*) syntax when DBDictionary.useWildCardForCount=true
Submitted By: Heath Thomann, merged from Pinaki's changes on trunk
Added:
openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java (with props)
Modified:
openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java?rev=939507&r1=939506&r2=939507&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java (original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java Thu Apr 29 22:52:44 2010
@@ -18,6 +18,7 @@
*/
package org.apache.openjpa.jdbc.kernel.exps;
+import org.apache.openjpa.jdbc.sql.SQLBuffer;
import org.apache.openjpa.jdbc.sql.Select;
/**
@@ -51,5 +52,20 @@ class Count
public boolean isAggregate() {
return true;
}
+
+ /**
+ * Overrides SQL formation by replacing COUNT(column) by COUNT(*) when specific conditions are met and
+ * DBDictionary configuration <code>useWildCardForCount</code> is set.
+ */
+ @Override
+ public void appendTo(Select sel, ExpContext ctx, ExpState state, SQLBuffer sql, int index) {
+ super.appendTo(sel, ctx, state, sql, index);
+ if (ctx.store.getDBDictionary().useWildCardForCount && state.joins.isEmpty()) {
+ String s = sql.getSQL();
+ if (s.startsWith("COUNT(") && s.endsWith(")")) {
+ sql.replaceSqlString("COUNT(".length(), s.length()-1, "*");
+ }
+ }
+ }
}
Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=939507&r1=939506&r2=939507&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java (original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java Thu Apr 29 22:52:44 2010
@@ -240,6 +240,7 @@ public class DBDictionary
public boolean supportsCaseConversionForLob = false;
public boolean reportsSuccessNoInfoOnBatchUpdates = false;
public boolean supportsSelectFromFinalTable = false;
+ public boolean useWildCardForCount = false;
/**
* Some Databases append whitespace after the schema name
@@ -1820,7 +1821,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);
Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java?rev=939507&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java Thu Apr 29 22:52:44 2010
@@ -0,0 +1,80 @@
+/*
+ * 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.
+ *
+ */
+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(*)");
+ }
+
+}
Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
------------------------------------------------------------------------------
svn:eol-style = native