Update on Named Query support;

Since I received no feedback, I went ahead with the following approach;

The NamedEntityFinder service is getting (during bootstrap) a
meta-info class called NamedQueries, which is essentially a map of
named queries, string to NamedQueryDescriptor.

The NamedQueryDescriptor is responsible to create a valid query upon
call to its compose() method, which takes the arguments that are
modifiable in the Query instance, i.e. Variables, First, Max and
OrderBy.

During client code's runtime, one calls the following method in
QueryBuilderFactory;

<T> Query<T> newNamedQuery( Class<T> resultType, UnitOfWork
unitOfWork, String name );

where name, refers to a name in the NamedQueries meta-info of the
NamedEntityFinder service.

Implementation-wise, I have added the NamedEntityFinder to the
RdfQueryService and provided a implementation mixin.

Also, there is a NamedSparqlDescriptor class that implements the
NamedQueryDescriptor, so that during bootstrap, one does something
like this;

    NamedQueries namedQueries = new NamedQueries();
    namedQueries.add( "findPredatorsOfFlyingAnimals", new
NamedSparqlDescriptor( "PREFIX ... SELECT DISTINCT.... WHERE { ... }"
) );
    :
    :
    module.addServices( RdfQueryService.class ).setMetaInfo( namedQueries );


To use it, it would look like;

   UnitOfWork uow = uowf.currentUnitOfWork();
   Query<Predator> query = qbf.newNamedQuery( Predator.class, uow,
"findPredatorsOfFlyingAnimals" );
   for( Predator predator : query )
   {
     ...
   }

Now, it is for the Sparql language easy to support the firstResult()
and maxResults() in the Query. It is just appended at the end of the
sparql query.

But for OrderBy, the story is much more complicated, and I have
currently not reached a satisfactory solution. The problem is somewhat
two fold;

1. The Query Runtime is pretty tightly tied into the Property model
for picking the sort keys. I don't want to modify the Query (and half
a dozen internal classes) to support "orderBy( String name )". The
good news is that the templateFor() to obtain sorting properties are
QueryBuilder agnostic, so that works.

2. But, since the query is handwritten, it is really(!) hard to figure
out which Sparql 'variable' that a particular property refers to.


So, I don't know what I should do...

I have so far added very limited support. The query string in the
NamedSparqlDescriptor contains the "ORDER BY" clause, and the
Query.orderBy() is required to have the same properties for in the
same order, and only the ASC or DESC is modified in the query string.

In the testcase I have this;

   Person person = templateFor( Person.class );
   final Query<Person> query = qbf.newNamedQuery( Person.class,
unitOfWork, "script21" );
   query.orderBy( orderBy( person.placeOfBirth().get().name() ),
                       orderBy( person.yearOfBirth() ) );

where the "script21" is the following query;

    "PREFIX ns1: <urn:qi4j:type:org.qi4j.index.rdf.model.Person#> \n" +
    "PREFIX ns2: <urn:qi4j:type:org.qi4j.index.rdf.model.Nameable#> \n" +
    "PREFIX ns0: <urn:qi4j:type:org.qi4j.api.entity.Identity#> \n" +
    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
    "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" +
    "SELECT DISTINCT ?entityType ?identity\n" +
    "WHERE {\n" +
    "?entityType rdfs:subClassOf
<urn:qi4j:type:org.qi4j.index.rdf.model.Person>. \n" +
    "?entity rdf:type ?entityType. \n" +
    "?entity ns0:identity ?identity. \n" +
    "?entity ns1:placeOfBirth ?v0. \n" +
    "?v0 ns2:name ?v1. \n" +
    "?entity ns1:yearOfBirth ?v2. \n" +
    "\n" +
    "}  ORDER BY DESC(?v1) DESC(?v2)", //script21

So since the code says that the order should be Ascending (the
default), the DESC() declarations are changed to ASC() when the query
is executed. BUT if the query.orderBy() sends in a directives of
either other properties, or those in a different order, the wrong
result will occur.


The only other choice I can think of, is to not support orderBy
changes in the Query at all.


Feedback requested.


Cheers
-- 
Niclas Hedhman, Software Developer
http://www.qi4j.org - New Energy for Java

I  live here; http://tinyurl.com/2qq9er
I  work here; http://tinyurl.com/2ymelc
I relax here; http://tinyurl.com/2cgsug

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to