cayenne-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Type-safe qualifiers (and queries)
Date Wed, 30 Dec 2009 21:54:08 GMT

On Dec 30, 2009, at 9:59 PM, Andrus Adamchik wrote:

> I'd like to see if a similar approach can be extended to query  
> building. Although that'll be a entirely different level of effort.

Or maybe it is not that hopeless and we can use a set of user-facing  
query "builders" without affecting our backend implementations. Just  
prototyped a builder that can create EJBQL/SelectQuery with  
parameterized result type that can be tied to  
ObjectContext.performQuery()...

public interface Query<T> ...
public class Select<T> implements Query<T>...

Here is now we can build correctly parameterized queries for different  
result types. Also shows other EJBQL features, like subqueries. While  
it adds some more terse API compared to SelectQuery, parameterization  
is the main point here. I guess this is a good way to make EJBQLQuery  
finally usable:

void query_newAPI_EntityResult() {

	Select<Artist> query = Select.entity(Artist.class);
	query.setDistinct();

	Select<Painting> subquery = Select.entity(Painting.class);
	subquery.setWhere(Painting.NAME.like("X%"));

	Expression clause1 = Artist.NAME.eq("X");
	Expression clause2 = Artist.PAINTINGS.dot(Painting.NAME).eq("Y");
	Expression clause3 = Exists.in(subquery);
	query.setWhere(All.of(clause1, clause2, clause3));

	query.addOrderByAscending(Artist.NAME);
	query.addOrderByAscending(Artist.PAINTINGS.dot(Painting.NAME));

	query.addDisjointPrefetch(Artist.PAINTINGS);
}

void query_newAPI_ScalarResult() {

	Select<Date> query = Select.scalar(Artist.class, Artist.DATE_OF_BIRTH);

	Expression clause1 = Artist.NAME.eq("X");
	Expression clause2 = Artist.PAINTINGS.dot(Painting.NAME).eq("Y");
	query.setWhere(Any.of(clause1, clause2));
}

void query_newAPI_MixedResult() {

	// fetching Artist entity plus paintings count
	Select<Object[]> query = Select.mixed(Artist.class,
			new Key<Artist>(""), Artist.PAINTINGS.count());

	Expression clause1 = Artist.NAME.eq("X");
	Expression clause2 = Artist.PAINTINGS.dot(Painting.NAME).eq("Y");
	query.setWhere(Any.of(clause1, clause2));
}

void query_newAPI_GenericObject() {

	Select<Persistent> query = Select.generic(Persistent.class,
			"MyGenericEntity");

	Expression clause1 = new Key<String>("name").eq("X");
	Expression clause2 = new Key<Integer>("age").eq(5);
	query.setWhere(All.of(clause1, clause2));
}

void query_newAPI_DataRow() {

	Select<DataRow> query = Select.dataRow(Artist.class);

	Expression clause1 = Artist.NAME.eq("X");
	Expression clause2 = Artist.PAINTINGS.dot(Painting.NAME).eq("Y");
	query.setWhere(All.of(clause1, clause2));
}



Andrus

Mime
View raw message