[seam-commits] Seam SVN: r10248 - in trunk/src: test/unit/org/jboss/seam/test/unit and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Mar 31 03:26:12 EDT 2009
Author: dan.j.allen
Date: 2009-03-31 03:26:12 -0400 (Tue, 31 Mar 2009)
New Revision: 10248
Modified:
trunk/src/main/org/jboss/seam/framework/EntityQuery.java
trunk/src/main/org/jboss/seam/framework/Query.java
trunk/src/test/unit/org/jboss/seam/test/unit/QueryTest.java
trunk/src/test/unit/org/jboss/seam/test/unit/testng.xml
Log:
JBSEAM-3032
Modified: trunk/src/main/org/jboss/seam/framework/EntityQuery.java
===================================================================
--- trunk/src/main/org/jboss/seam/framework/EntityQuery.java 2009-03-31 04:56:03 UTC (rev 10247)
+++ trunk/src/main/org/jboss/seam/framework/EntityQuery.java 2009-03-31 07:26:12 UTC (rev 10248)
@@ -11,6 +11,8 @@
import org.jboss.seam.persistence.QueryParser;
import org.jboss.seam.transaction.Transaction;
+import org.jboss.seam.util.Reflections;
+
/**
* A Query object for JPA.
*
@@ -38,6 +40,11 @@
{
throw new IllegalStateException("entityManager is null");
}
+
+ Object delegate = getEntityManager().getDelegate();
+ if (!(Reflections.isClassAvailable("org.hibernate.Session") && delegate instanceof org.hibernate.Session)) {
+ setUseCompliantCountQuerySubject(true);
+ }
}
@Override
Modified: trunk/src/main/org/jboss/seam/framework/Query.java
===================================================================
--- trunk/src/main/org/jboss/seam/framework/Query.java 2009-03-31 04:56:03 UTC (rev 10247)
+++ trunk/src/main/org/jboss/seam/framework/Query.java 2009-03-31 07:26:12 UTC (rev 10248)
@@ -28,7 +28,7 @@
public abstract class Query<T, E>
extends PersistenceController<T> //TODO: extend MutableController!
{
- private static final Pattern SUBJECT_PATTERN = Pattern.compile("^select (\\w+(\\.\\w+)*)\\s+from", Pattern.CASE_INSENSITIVE);
+ private static final Pattern SUBJECT_PATTERN = Pattern.compile("^select (\\w+((\\s+|\\.)\\w+)*)\\s+from", Pattern.CASE_INSENSITIVE);
private static final Pattern FROM_PATTERN = Pattern.compile("(^|\\s)(from)\\s", Pattern.CASE_INSENSITIVE);
private static final Pattern WHERE_PATTERN = Pattern.compile("\\s(where)\\s", Pattern.CASE_INSENSITIVE);
private static final Pattern ORDER_PATTERN = Pattern.compile("\\s(order)(\\s)+by\\s", Pattern.CASE_INSENSITIVE);
@@ -48,6 +48,7 @@
private String groupBy;
+ private boolean useCompliantCountQuerySubject = false;
private DataModel dataModel;
private String parsedEjbql;
@@ -292,13 +293,15 @@
int whereLoc = whereMatcher.find() ? whereMatcher.start(1) : orderLoc;
String subject = "*";
- // TODO to be JPA-compliant, we need to make this query like "select count(u) from User u"
+ // to be JPA-compliant, we need to make this query like "select count(u) from User u"
// however, Hibernate produces queries some databases cannot run when the primary key is composite
-// Matcher subjectMatcher = SUBJECT_PATTERN.matcher(ejbql);
-// if ( subjectMatcher.find() )
-// {
-// subject = subjectMatcher.group(1);
-// }
+ if (useCompliantCountQuerySubject) {
+ Matcher subjectMatcher = SUBJECT_PATTERN.matcher(ejbql);
+ if ( subjectMatcher.find() )
+ {
+ subject = subjectMatcher.group(1);
+ }
+ }
return new StringBuilder(ejbql.length() + 15).append("select count(").append(subject).append(") ").
append(ejbql.substring(fromLoc, whereLoc).replace("join fetch", "join")).
@@ -567,4 +570,12 @@
}
}
+ protected boolean isUseCompliantCountQuerySubject() {
+ return useCompliantCountQuerySubject;
+ }
+
+ protected void setUseCompliantCountQuerySubject(boolean useCompliantCountQuerySubject) {
+ this.useCompliantCountQuerySubject = useCompliantCountQuerySubject;
+ }
+
}
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/QueryTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/QueryTest.java 2009-03-31 04:56:03 UTC (rev 10247)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/QueryTest.java 2009-03-31 07:26:12 UTC (rev 10248)
@@ -41,6 +41,11 @@
query.parseEjbql();
// TODO this should eventually become count(v.person)
assertEquals(query.getCountEjbql(), "select count(*) from Vehicle v left join v.person");
+
+ query = new CompliantUnitQuery();
+ query.setEjbql("select p from Person p");
+ query.parseEjbql();
+ assertEquals(query.getCountEjbql(), "select count(p) from Person p");
}
class UnitQuery extends EntityQuery {
@@ -64,4 +69,12 @@
}
}
+
+ class CompliantUnitQuery extends UnitQuery {
+
+ public CompliantUnitQuery() {
+ setUseCompliantCountQuerySubject(true);
+ }
+
+ }
}
Modified: trunk/src/test/unit/org/jboss/seam/test/unit/testng.xml
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/testng.xml 2009-03-31 04:56:03 UTC (rev 10247)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/testng.xml 2009-03-31 07:26:12 UTC (rev 10248)
@@ -56,6 +56,7 @@
<test name="Seam Unit Tests: Framework">
<classes>
<class name="org.jboss.seam.test.unit.HomeTest" />
+ <class name="org.jboss.seam.test.unit.QueryTest" />
</classes>
</test>
More information about the seam-commits
mailing list