Generating Query and Procedure Access Code
------------------------------------------
Key: CAY-1070
URL: https://issues.apache.org/cayenne/browse/CAY-1070
Project: Cayenne
Issue Type: Task
Components: Cayenne Core Library
Affects Versions: 3.0
Reporter: Andrus Adamchik
Assignee: Andrus Adamchik
Fix For: 3.0
The original request for this feature was submitted as CAY-995. I am using a
separate Jira to better formulate what needs to be done and provide the
context. The goal to generate a flexible type-safe code to invoke stored
procedures and mapped queries.
1. This feature should initially target class generation with Ant (via 'cgen'
task [1]), although backend should be done in a way that should be later
reusable with CayenneModeler and Maven plugins (actually all code generation
stuff is done like that, so it won't be hard to follow).
2. The new feature will be activated in the "datamap" cgen "mode" (default mode
is "entity"). Until now "datamap" mode always required a custom template to
make any sense at all. Now it will result in generating a default predefined
Java class (with a superclass if "makepairs" is true) per DataMap.
3. The new standard Velocity templates need to be created for "datamap" mode
under "resources/dotemplates/v1_2", one for singleclass generation and two for
subclass/superclass generation. Just like with persistent Java class templates,
datamap-subclass.vm should be an empty class inheriting from a generated
superclass; datamap-superclass.vm and datamap-singleclass.vm should be
essentially the same thing except for the class name.
4. Generated class name should be derived from DataMap name and use
DataMap.getDefaultPackage() for the package name. All non-Java identifier
characters in the DataMap name should be replaced (with "_" ??) when deriving a
class name. Superclass in a makepairs generation mode should be prefixed with
"_" (same as persistent Java classes).
5. Generated classes must include the following:
* A singleton method that returns a static final instance of the class:
protected static MyDataMap instance = new MyDataMap();
public static MyDataMap getInstance() {
return instance;
}
* A method for each mapped query. For each SelectQuery, a method would look
like this:
public List<EntityClass> performQuery1(ObjectContext context, Object
parameter1, Object parameter2, ...) {
// bind parameters and
// run the query here
}
For each ProcedureQuery and SQLTemplate query, a method would have to return
a generic QueryResponse object, as we don't have enough info to tell whether
the query is selecting or updating or both (although we can do something smart
about SQLTemplates, such as greping for SELECT in the beginning of the query).
public QueryResponse performQuery2(ObjectContext, Object parameter1, Object
parameter2) {
// bind parameters and
// run the query here
}
The number, names and order of parameters must match that of the mapped query
(for SelectQuery that would require parsing qualifier expression to search for
parameter nodes; not yet sure how to do with with SQLTemplate or
ProcedureQuery). In the future we can also do something smart about parameter
types (we can sort of guess them in SelectQuery), but this is not required for
version 1 of the feature.
* For each stored procedure that does not have at least one procedure query,
create a method that builds a procedure query on the fly. Method name is based
on stored procedure name.
public QueryResponse performProcedure2(ObjectContext, Object parameter1, Object
parameter2) {
// Create ProcedureQuery
// bind parameters and
// run the query here
}
[1] http://cayenne.apache.org/doc/cgen.html
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.