Re: Queries that should return single objects

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon Jul 04 2005 - 04:51:06 EDT

  • Next message: Todd O'Bryan: "Re: Queries that should return single objects"

    This is indeed a very useful utility. Still I am reluctant to add this to
    DataContext. It is already overloaded with many ways to perform queries...
    In fact we may even trim it down in the future -
    http://objectstyle.org/confluence/display/CAY/2005/07/02

    So I think some sort of QueryUtils (similar to DataObjectUtils) is
    probably a better place. I even started prototyping something like this.
    This includes singleton queries, raw SQL one-liners, some useful generic
    aggregate queries and a few other things. Not ready to sahre it yet
    though...

    Andrus

    > I find myself writing the same four or five lines of code to perform
    > a query, check that it only returned one object, and then grab that
    > object out.
    >
    > Is this common for other people as well, or is it just me.
    >
    > If it is a pretty common occurrence, can I suggest dropping a
    > convenience method in DataContext?
    >
    > Would
    >
    > public CayenneDataObject performSingletonQuery(SelectQuery q) {
    > List l = performQuery(q);
    > if (l.getSize() != 1) {
    > throw new ValidationException("Query expected to return
    > single unique object didn't");
    > } else {
    > return (CayenneDataObject) l.get(0);
    > }
    >
    > be a reasonable addition, or am I just being silly?
    >
    > Thanks,
    > Todd
    >



    This archive was generated by hypermail 2.0.0 : Mon Jul 04 2005 - 04:51:09 EDT