cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dkazimirc...@apache.org
Subject [01/16] cayenne git commit: CAY-2060 Ongoing work on replacing actual query objects stored in the DataMap with query descriptors.
Date Fri, 19 Feb 2016 14:07:25 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master faa35546a -> a6c438354


http://git-wip-us.apache.org/repos/asf/cayenne/blob/a265b3ab/modeler/cayenne-modeler/src/main/resources/pref/Preferences.map.xml
----------------------------------------------------------------------
diff --git a/modeler/cayenne-modeler/src/main/resources/pref/Preferences.map.xml b/modeler/cayenne-modeler/src/main/resources/pref/Preferences.map.xml
index 67e6cb8..1011c91 100644
--- a/modeler/cayenne-modeler/src/main/resources/pref/Preferences.map.xml
+++ b/modeler/cayenne-modeler/src/main/resources/pref/Preferences.map.xml
@@ -45,12 +45,12 @@
 	<obj-relationship name="preferences" source="Domain" target="DomainPreference" deleteRule="Cascade"
db-relationship-path="preferences"/>
 	<obj-relationship name="subdomains" source="Domain" target="Domain" deleteRule="Cascade"
db-relationship-path="subdomains"/>
 	<obj-relationship name="domain" source="DomainPreference" target="Domain" deleteRule="Nullify"
db-relationship-path="domain"/>
-	<query name="DomainPreferenceForKey" factory="org.apache.cayenne.map.SelectQueryBuilder"
root="obj-entity" root-name="DomainPreference">
+	<query name="DomainPreferenceForKey" type="SelectQuery" root="obj-entity" root-name="DomainPreference">
 		<property name="cayenne.GenericSelectQuery.fetchLimit" value="1"/>
 		<qualifier><![CDATA[(key = $key) and (domain = $domain)]]></qualifier>
 		<ordering descending="true"><![CDATA[domain.level]]></ordering>
 	</query>
-	<query name="TopLevelDomain" factory="org.apache.cayenne.map.SelectQueryBuilder" root="obj-entity"
root-name="Domain">
+	<query name="TopLevelDomain" type="SelectQuery" root="obj-entity" root-name="Domain">
 		<qualifier><![CDATA[(name = $name) and (level = 0)]]></qualifier>
 	</query>
 </data-map>

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a265b3ab/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
b/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
index 4797b43..179dae2 100644
--- a/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
+++ b/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOModelProcessor.java
@@ -41,12 +41,12 @@ import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.map.naming.DefaultUniqueNameGenerator;
 import org.apache.cayenne.map.naming.NameCheckers;
-import org.apache.cayenne.query.AbstractQuery;
-import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.*;
 import org.apache.cayenne.wocompat.parser.Parser;
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.Predicate;
 import org.apache.commons.collections.PredicateUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -238,7 +238,7 @@ public class EOModelProcessor {
 	 * 
 	 * @since 1.1
 	 */
-	protected Query makeQuery(EOModelHelper helper, EOObjEntity entity, String queryName) {
+	protected QueryDescriptor makeQuery(EOModelHelper helper, EOObjEntity entity, String queryName)
{
 
 		DataMap dataMap = helper.getDataMap();
 		Map queryPlist = helper.queryPListMap(entity.getName(), queryName);
@@ -246,18 +246,106 @@ public class EOModelProcessor {
 			return null;
 		}
 
-		AbstractQuery query;
+		QueryDescriptor query;
 		if (queryPlist.containsKey("hints")) { // just a predefined SQL query
-			query = new EOSQLQuery(entity, queryPlist);
+			query = makeEOSQLQueryDescriptor(entity, queryPlist);
 		} else {
-			query = new EOQuery(entity, queryPlist);
+			query = makeEOQueryDescriptor(entity, queryPlist);
 		}
 		query.setName(entity.qualifiedQueryName(queryName));
-		dataMap.addQuery(query);
+		dataMap.addQueryDescriptor(query);
 
 		return query;
 	}
 
+	protected QueryDescriptor makeEOQueryDescriptor(ObjEntity root, Map plistMap) {
+		SelectQueryDescriptor descriptor = QueryDescriptor.selectQueryDescriptor();
+		descriptor.setRoot(root);
+
+		descriptor.setDistinct("YES".equalsIgnoreCase((String) plistMap.get("usesDistinct")));
+
+		Object fetchLimit = plistMap.get("fetchLimit");
+		if (fetchLimit != null) {
+			try {
+				if (fetchLimit instanceof Number) {
+					descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY,
+							String.valueOf(((Number) fetchLimit).intValue()));
+				} else if (StringUtils.isNumeric(fetchLimit.toString())) {
+					descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, fetchLimit.toString());
+				}
+			} catch (NumberFormatException nfex) {
+				// ignoring...
+			}
+		}
+
+		// sort orderings
+		List<Map<String, String>> orderings = (List<Map<String, String>>)
plistMap.get("sortOrderings");
+		if (orderings != null && !orderings.isEmpty()) {
+			for (Map<String, String> ordering : orderings) {
+				boolean asc = !"compareDescending:".equals(ordering.get("selectorName"));
+				String key = ordering.get("key");
+				if (key != null) {
+					descriptor.addOrdering(new Ordering(key, asc ? SortOrder.ASCENDING : SortOrder.DESCENDING));
+				}
+			}
+		}
+
+		// qualifiers
+		Map<String, ?> qualifierMap = (Map<String, ?>) plistMap.get("qualifier");
+		if (qualifierMap != null && !qualifierMap.isEmpty()) {
+			descriptor.setQualifier(EOQuery.EOFetchSpecificationParser.makeQualifier((EOObjEntity)
root, qualifierMap));
+		}
+
+		// prefetches
+		List prefetches = (List) plistMap.get("prefetchingRelationshipKeyPaths");
+		if (prefetches != null && !prefetches.isEmpty()) {
+			Iterator it = prefetches.iterator();
+			while (it.hasNext()) {
+				descriptor.addPrefetch((String) it.next());
+			}
+		}
+
+		// data rows - note that we do not support fetching individual columns
+		// in the
+		// modeler...
+		if (plistMap.containsKey("rawRowKeyPaths")) {
+			descriptor.setProperty(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY, String.valueOf(true));
+		}
+
+		return descriptor;
+	}
+
+	protected QueryDescriptor makeEOSQLQueryDescriptor(ObjEntity root, Map plistMap) {
+		SQLTemplateDescriptor descriptor = QueryDescriptor.sqlTemplateDescriptor();
+		descriptor.setRoot(root);
+
+		Object fetchLimit = plistMap.get("fetchLimit");
+		if (fetchLimit != null) {
+			try {
+				if (fetchLimit instanceof Number) {
+					descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY,
+							String.valueOf(((Number) fetchLimit).intValue()));
+				} else if (StringUtils.isNumeric(fetchLimit.toString())) {
+					descriptor.setProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, fetchLimit.toString());
+				}
+			} catch (NumberFormatException nfex) {
+				// ignoring...
+			}
+		}
+
+		//query
+		// TODO: doesn't work with Stored Procedures.
+		Map hints = (Map) plistMap.get("hints");
+		if (hints != null && !hints.isEmpty()) {
+			String sqlExpression = (String) hints.get("EOCustomQueryExpressionHintKey");
+			if (sqlExpression != null) {
+				descriptor.setSql(sqlExpression);
+			}
+		}
+
+		return descriptor;
+	}
+
 	/**
 	 * Creates and returns a new ObjEntity linked to a corresponding DbEntity.
 	 */

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a265b3ab/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOObjEntity.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOObjEntity.java
b/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOObjEntity.java
index 7689074..240682d 100644
--- a/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOObjEntity.java
+++ b/modeler/cayenne-wocompat/src/main/java/org/apache/cayenne/wocompat/EOObjEntity.java
@@ -34,7 +34,6 @@ import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.map.Entity;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.Relationship;
-import org.apache.cayenne.query.Query;
 import org.apache.commons.collections.Transformer;
 
 /**
@@ -56,20 +55,6 @@ public class EOObjEntity extends ObjEntity {
     }
 
     /**
-     * Returns stored EOQuery.
-     * 
-     * @since 1.1
-     */
-    public EOQuery getEOQuery(String queryName) {
-        Query query = getDataMap().getQuery(qualifiedQueryName(queryName));
-        if (query instanceof EOQuery) {
-            return (EOQuery) query;
-        }
-
-        return null;
-    }
-
-    /**
      * Overrides super to support translation of EO attributes that have no ObjAttributes.
      * 
      * @since 1.2
@@ -118,7 +103,7 @@ public class EOObjEntity extends ObjEntity {
      */
     public Collection getEOQueries() {
         if (filteredQueries == null) {
-            Collection queries = getDataMap().getQueries();
+            Collection queries = getDataMap().getQueryDescriptors();
             if (queries.isEmpty()) {
                 filteredQueries = Collections.EMPTY_LIST;
             }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a265b3ab/modeler/cayenne-wocompat/src/test/java/org/apache/cayenne/wocompat/EOModelProcessorTest.java
----------------------------------------------------------------------
diff --git a/modeler/cayenne-wocompat/src/test/java/org/apache/cayenne/wocompat/EOModelProcessorTest.java
b/modeler/cayenne-wocompat/src/test/java/org/apache/cayenne/wocompat/EOModelProcessorTest.java
index 55555c8..f511d18 100644
--- a/modeler/cayenne-wocompat/src/test/java/org/apache/cayenne/wocompat/EOModelProcessorTest.java
+++ b/modeler/cayenne-wocompat/src/test/java/org/apache/cayenne/wocompat/EOModelProcessorTest.java
@@ -26,8 +26,8 @@ import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
-import org.apache.cayenne.query.Query;
-import org.apache.cayenne.query.SelectQuery;
+import org.apache.cayenne.query.QueryDescriptor;
+import org.apache.cayenne.query.SelectQueryDescriptor;
 import org.apache.cayenne.util.XMLEncoder;
 import org.junit.Before;
 import org.junit.Test;
@@ -130,21 +130,13 @@ public class EOModelProcessorTest {
     protected void assertLoadedQueries(DataMap map) throws Exception {
 
         // queries
-        Query query = map.getQuery("ExhibitType_TestQuery");
+        QueryDescriptor query = map.getQueryDescriptor("ExhibitType_TestQuery");
 
         assertNotNull(query);
-        assertTrue(query instanceof SelectQuery);
-        assertTrue(query instanceof EOQuery);
-        EOQuery eoQuery = (EOQuery) query;
-
-        assertSame(map.getObjEntity("ExhibitType"), eoQuery.getRoot());
-
-        Collection<?> bindings = eoQuery.getBindingNames();
-        assertNotNull(bindings);
-        assertEquals(3, bindings.size());
-        assertEquals("java.lang.String", eoQuery.bindingClass("x"));
-        assertEquals("java.lang.String", eoQuery.bindingClass("y"));
-        assertEquals("java.lang.Object", eoQuery.bindingClass("z"));
+        assertEquals(QueryDescriptor.SELECT_QUERY, query.getType());
+        assertTrue(query instanceof SelectQueryDescriptor);
+
+        assertSame(map.getObjEntity("ExhibitType"), query.getRoot());
     }
 
     protected void assertLoadedCustomTypes(DataMap map) throws Exception {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/a265b3ab/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
b/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
index 8e683c1..87a3156 100644
--- a/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
+++ b/plugins/maven-cayenne-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
@@ -230,7 +230,7 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 			// attributes
 			generator.addEmbeddables(dataMap.getEmbeddables());
 			// TODO add the "includeQueries" and "excludeQueries" attributes
-			generator.addQueries(dataMap.getQueries());
+			generator.addQueries(dataMap.getQueryDescriptors());
 			generator.execute();
 		} catch (Exception e) {
 			throw new MojoExecutionException("Error generating classes: ", e);


Mime
View raw message