Author: mikedd
Date: Wed Sep 29 23:51:16 2010
New Revision: 1002897
URL: http://svn.apache.org/viewvc?rev=1002897&view=rev
Log:
OPENJPA-712: Handle the having claus in aggregate functions.
Submitted By: Heath Thomann, based on Fay Wang's patch for trunk.
Added:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java
(with props)
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java
(with props)
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java
(with props)
Modified:
openjpa/branches/1.2.x/openjpa-kernel/src/main/jjtree/org/apache/openjpa/kernel/jpql/JPQL.jjt
Modified:
openjpa/branches/1.2.x/openjpa-kernel/src/main/jjtree/org/apache/openjpa/kernel/jpql/JPQL.jjt
URL:
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-kernel/src/main/jjtree/org/apache/openjpa/kernel/jpql/JPQL.jjt?rev=1002897&r1=1002896&r2=1002897&view=diff
==============================================================================
---
openjpa/branches/1.2.x/openjpa-kernel/src/main/jjtree/org/apache/openjpa/kernel/jpql/JPQL.jjt
(original)
+++
openjpa/branches/1.2.x/openjpa-kernel/src/main/jjtree/org/apache/openjpa/kernel/jpql/JPQL.jjt
Wed Sep 29 23:51:16 2010
@@ -729,7 +729,7 @@ void like_expression() #LIKE : { }
void null_comparison_expression() #ISNULL : { }
{
- (input_parameter() | path())
+ (input_parameter() | path() | aggregate_select_expression())
<IS> [<NOT> { jjtThis.not = true; }] <NULL>
}
@@ -931,7 +931,7 @@ void datetime_expression() : { }
void datetime_primary() : { }
{
- path() | functions_returning_datetime() | input_parameter()
+ path() | functions_returning_datetime() | input_parameter() |
aggregate_select_expression()
}
Added:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java
URL:
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java?rev=1002897&view=auto
==============================================================================
---
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java
(added)
+++
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java
Wed Sep 29 23:51:16 2010
@@ -0,0 +1,177 @@
+/*
+ * 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.jdbc.query;
+
+import java.sql.Timestamp;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.jdbc.query.domain.Magazine;
+import org.apache.openjpa.persistence.jdbc.query.domain.Publisher;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests multiple entities in projection are returned with both inner and outer
+ * joins.
+ */
+public class TestMultipleEntityProjection extends SingleEMFTestCase {
+ private static boolean INNER_JOIN = true;
+ private static String[][] MAGAZINE_PUBLISHER_NAME_PAIRS = {
+ new String[] {"Times", "Publisher-0"},
+ new String[] {"Times Life", "Publisher-1"},
+ new String[] {"Times Kid", null},
+ new String[] {"Newsweek", "Publisher-3"},
+ new String[] {"People", null},
+ new String[] {"Nature", "Publisher-5"},
+ new String[] {"MIT Review", "Publisher-6"},
+ new String[] {"Wired", "Publisher-7"},
+
+ };
+
+ public void setUp() {
+ super.setUp(CLEAR_TABLES, Magazine.class, Publisher.class);
+ createData();
+ }
+
+ /**
+ * Create Magazine and associate Publisher unless its name is null.
+ */
+ void createData() {
+ EntityManager em = emf.createEntityManager();
+ em.getTransaction().begin();
+ for (int i = 0; i < MAGAZINE_PUBLISHER_NAME_PAIRS.length; i++) {
+ String magName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][0];
+ String pubName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][1];
+ Magazine mag = new Magazine();
+ mag.setName(magName);
+ if (pubName != null) {
+ Publisher pub = new Publisher();
+ pub.setName(pubName);
+ mag.setPublisher(pub);
+ try {
+ DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
+ Date date = df.parse("2001-01-01");
+ mag.setDatePublished(date);
+ } catch (ParseException e) {
+ mag.setDatePublished(null);
+ }
+ mag.setTsPublished(new Timestamp(System.currentTimeMillis() -
100000));
+
+ em.persist(pub);
+ }
+ em.persist(mag);
+ }
+ em.getTransaction().commit();
+ }
+
+ public void testMultipleEntitiesInProjectionUsingOuterJoin() {
+ String jpql = "select m, p " +
+ "from Magazine m left outer join m.publisher p " +
+ "where ((:useName = false) or (m.name like :name))";
+ EntityManager em = emf.createEntityManager();
+ Query query = em.createQuery(jpql);
+
+ String magKey = "Times";
+ query.setParameter("useName", true);
+ query.setParameter("name", '%'+magKey+'%');
+
+ List result = query.getResultList();
+
+ int expecetedCount = getExpecetedResultCount(magKey, !INNER_JOIN);
+ assertFalse(result.isEmpty());
+ assertEquals(expecetedCount, result.size());
+ for (Object o : result) {
+ assertTrue(o instanceof Object[]);
+ Object[] row = (Object[])o;
+ assertEquals(2, row.length);
+ assertTrue(row[0] instanceof Magazine);
+ assertTrue(row[1] == null || row[1] instanceof Publisher);
+ assertNotNull(row[0]);
+ assertEquals(((Magazine)row[0]).getPublisher(), row[1]);
+ }
+ }
+
+ public void testMultipleEntitiesInProjectionUsingInnerJoin() {
+ String jpql = "select m, p " +
+ "from Magazine m inner join m.publisher p " +
+ "where ((:useName = false) or (m.name like :name))";
+ EntityManager em = emf.createEntityManager();
+ Query query = em.createQuery(jpql);
+
+ String magKey = "Times";
+ query.setParameter("useName", true);
+ query.setParameter("name", '%'+magKey+'%');
+
+ List result = query.getResultList();
+
+ int expecetedCount = getExpecetedResultCount(magKey, INNER_JOIN);
+ assertFalse(result.isEmpty());
+ assertEquals(expecetedCount, result.size());
+ for (Object o : result) {
+ assertTrue(o instanceof Object[]);
+ Object[] row = (Object[])o;
+ assertEquals(2, row.length);
+ assertTrue(row[0] instanceof Magazine);
+ assertTrue(row[1] instanceof Publisher);
+ assertNotNull(row[0]);
+ assertNotNull(row[1]);
+ assertEquals(((Magazine)row[0]).getPublisher(), row[1]);
+ }
+ }
+
+ public void testAggregateExpressionInHavingExpression() {
+ String jpql = "select m.publisher, max(m.datePublished) " +
+ "from Magazine m group by m.publisher " +
+ "having max(m.datePublished) is null";
+
+ EntityManager em = emf.createEntityManager();
+ Query query = em.createQuery(jpql);
+ List result = query.getResultList();
+ assertTrue(result.isEmpty());
+
+ jpql = "select m.publisher, max(m.datePublished) " +
+ "from Magazine m group by m.publisher " +
+ "having max(m.tsPublished) = CURRENT_TIMESTAMP";
+ query = em.createQuery(jpql);
+ result = query.getResultList();
+ assertTrue(result.isEmpty());
+ }
+
+ /**
+ * Count number of expected result based on inner/outer join condition and
+ * the name of the Magazine.
+ */
+ private int getExpecetedResultCount(String key, boolean innerJoin) {
+ int count = 0;
+ for (int i = 0; i < MAGAZINE_PUBLISHER_NAME_PAIRS.length; i++) {
+ String magName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][0];
+ String pubName = MAGAZINE_PUBLISHER_NAME_PAIRS[i][1];
+ if (magName.indexOf(key) != -1)
+ if (!innerJoin || (innerJoin && pubName != null))
+ count++;
+ }
+ return count;
+ }
+}
Propchange:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestMultipleEntityProjection.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java
URL:
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java?rev=1002897&view=auto
==============================================================================
---
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java
(added)
+++
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java
Wed Sep 29 23:51:16 2010
@@ -0,0 +1,82 @@
+/*
+ * 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.jdbc.query.domain;
+
+import java.sql.Timestamp;
+import java.util.Date;
+
+import javax.persistence.*;
+
+/**
+ * Simple persistent entity as a target of bi-directional one-to-one
+ * association.
+ */
+...@entity
+...@table(name="MAG_DOMAIN")
+public class Magazine {
+ @Id
+ @GeneratedValue
+ private long id;
+
+ private String name;
+
+ @OneToOne
+ private Publisher publisher;
+
+ private Date datePublished;
+
+ private Timestamp tsPublished;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public Publisher getPublisher() {
+ return publisher;
+ }
+
+ public void setPublisher(Publisher publisher) {
+ this.publisher = publisher;
+ }
+
+ public Date getDatePublished() {
+ return datePublished;
+ }
+
+ public void setDatePublished(Date datePublished) {
+ this.datePublished = datePublished;
+ }
+
+ public Date getTsPublished() {
+ return tsPublished;
+ }
+
+ public void setTsPublished(Timestamp tsPublished) {
+ this.tsPublished = tsPublished;
+ }
+
+}
Propchange:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Magazine.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java
URL:
http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java?rev=1002897&view=auto
==============================================================================
---
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java
(added)
+++
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java
Wed Sep 29 23:51:16 2010
@@ -0,0 +1,48 @@
+/*
+ * 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.jdbc.query.domain;
+
+import javax.persistence.*;
+
+/**
+ * Simple persistent entity as a target of uni-directional one-to-one
+ * association.
+ */
+...@entity
+...@table(name="PUB_DOMAIN")
+public class Publisher {
+ @Id
+ @GeneratedValue
+ private long id;
+
+ private String name;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+}
Propchange:
openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/domain/Publisher.java
------------------------------------------------------------------------------
svn:eol-style = native