From ac6dab1d076a47319c82aac23a2ef5413fd1ecbe Mon Sep 17 00:00:00 2001
From: Pinaki Poddar <ppoddar@apache.org>
Date: Mon, 21 Dec 2009 19:57:11 +0000
Subject: [PATCH] OPENJPA-1440: Allow COUNT(*) syntax when
 DBDictionary.useWildCardForCount=true

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@892949 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/openjpa/jdbc/kernel/exps/Count.java  | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
index 0286bc37d..e58ffbbf0 100644
--- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
+++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
@@ -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,21 @@ 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, "*");
+            }
+        }
+    }
+
 }