What a query result can be [Was: performing count]

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Jun 22 2007 - 09:09:20 EDT

  • Next message: Andrus Adamchik: "How about 3.0 M1?"

    Just added another JPA extension to Cayenne Classic - a mechanism to
    return scalars from SQLTemplate. Let me demonstrate how this works.
    Take a normal query execution example:

        List results = context.performQuery(query);

      * In Cayenne Classic each element in the "results" list is either a
    Persistent or a DataRow.
      * In JPA each element is either a Persistent or a scalar (such as
    java.lang.Integer), or an Object[] containing a mix of the previous
    two types.

    So the JPA difference is that (a) scalars are first-class citizens as
    far as query result is concerned; (b) multiple scalar/objects per row
    can be a part of the same result; [(c) no concept of DataRow, but
    that's not relevant for this discussion].

    So what I did now is adding support for scalars and arrays of scalars
    to the "raw" queries. So now a count can be done as SQLTemplate in a
    following manner:

        // a bit involved query construction procedure
        String sql = "SELECT count(1) AS C FROM ARTIST";
        SQLTemplate query = new SQLTemplate(Artist.class, sql);
        query.setColumnNamesCapitalization
    (SQLTemplate.UPPERCASE_COLUMN_NAMES);

        SQLResultSetMapping rsMap = new SQLResultSetMapping();
        rsMap.addColumnResult("C");
        query.setResultSetMapping(rsMap);

        // very simple scalar result set retrieval
        Number count = (Number) DataObjectUtils.objectForQuery(context,
    query);

    We can hide SQLTemplate creation internals inside a CountQuery, but a
    user can easily take care of result processing without extra utility
    API (see the last line in the code above - no need to unwrap a Map or
    anything).

    Regarding the earlier concern that DataObjectUtils is dealing with
    non-DataObjects, looks like a conceptual shift of "what a query
    result is" solves that as well. Now we consider any
    "java.lang.Object" a valid result, only some objects are persistent
    and have identity, and others - do not. Comments?

    Andrus

    On Jun 6, 2007, at 4:15 PM, Michael Gentry wrote:
    > I don't mind resurrecting QueryUtils, but is a count method the
    > only thing
    > that would go in it?
    >
    > Thanks,
    >
    > /dev/mrg
    >
    >
    > On 6/5/07, Andrus Adamchik <andru..bjectstyle.org> wrote:
    >>
    >> [taking to dev]
    >>
    >> There was an old QueryUtils class that was used for some internal
    >> obscure queries. It's long gone (don't even remember which version
    >> dumped it). So now we can reuse the name for the user-friendly
    >> utility class (if we decide to go this way).
    >>
    >> Andrus
    >>
    >>
    >> On Jun 5, 2007, at 4:54 PM, Michael Gentry wrote:
    >>
    >> > Hasn't QueryUtils gone poof in 3.0?
    >> >
    >> > /dev/mrg
    >> >
    >> >
    >> > On 6/5/07, Andrus Adamchik <andru..bjectstyle.org> wrote:
    >> >>
    >> >> So where do we put it then? QueryUtils?
    >> >>
    >> >> Andrus
    >> >>
    >> >>
    >>
    >>



    This archive was generated by hypermail 2.0.0 : Fri Jun 22 2007 - 09:09:45 EDT