openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r892947 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
Date Mon, 21 Dec 2009 19:55:59 GMT
Author: ppoddar
Date: Mon Dec 21 19:55:58 2009
New Revision: 892947

URL: http://svn.apache.org/viewvc?rev=892947&view=rev
Log:
OPENJPA-1440: Allow COUNT(*) syntax when DBDictionary.useWildCardForCount=true

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
  (with props)
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=892947&r1=892946&r2=892947&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
Mon Dec 21 19:55:58 2009
@@ -247,6 +247,7 @@
     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 @@
             // 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/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java?rev=892947&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
Mon Dec 21 19:55:58 2009
@@ -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.
+ * <br>
+ * Further details available at <br>
+ * <A HREF="https://issues.apache.org/jira/browse/OPENJPA-1440">OPENJPA-1440</HREF>
+ * 
+ * @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(*)");
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestWildCardCount.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message