RE: Query Examples Needed Expression.fromString() syntax?

From: Gentry, Michael \(Contractor\) ("Gentry,)
Date: Wed May 24 2006 - 13:51:13 EDT

  • Next message: Joshua Pyle: "Re: Query Examples Needed – Expression.fromString() syntax?"

    I almost always use Expression.fromString(). Some quick examples:

    Expression expression;
    Map parameters;
    SelectQuery query; // All based off of "Customer" ...

    ...

    expression = Expression.fromString("lastName like $lastName%");
    parameters.put("lastName", "Gentry");
    query = new SelectQuery(Customer.class,
    expression.expWithParameters(parameters));

    ...

    expression = Expression.fromString("firstName = $firstName and lastName
    = $lastName");
    parameters.put("firstName", "Michael");
    parameters.put("lastName", "Gentry");

    ...

    expression = Expression.fromString("orders.lineItems.item.name =
    $name");
    parameters.put("name", "Fireman's Helmet (red)");

    ...

    Note that parameters are optional. In the second example, if you had
    left out the first name portion, Cayenne would automatically make the
    query "lastName = $lastName" for you (great feature when working with
    UI-based fields).

    In the third example, Cayenne fully supports a dotted notation for
    specifying relationships and will construct the joins for you. So
    Customer ->> Order ->> Line Items -> Item's name attribute.

    /dev/mrg

    PS. If your expression never changes, make it final for just a tad of a
    speed boost.

    -----Original Message-----
    From: Eric Lazarus [mailto:ericllazaru..ahoo.com]
    Sent: Wednesday, May 24, 2006 12:52 PM
    To: cayenne user
    Subject: Query Examples Needed Expression.fromString() syntax?

    Would anyone reply with some nice examples of
    SelectQuery beside the simple one here:

    http://www.objectstyle.org/cayenne/userguide/dataobjects/dataobjects-exa
    mple.html

    Expression e =
      ExpressionFactory.greaterExp("estimatedPrice",
                            new BigDecimal(100000.0))

    I need to do things with ANDS and ORs and I'm not sure
    how to use expression factory (or should I use
    Expression.fromString() ?) to do it.

    Right now my immediate need is to be able to construct
    a query with an arbitary number of possible "and"
    clauses depending on what the user types.

    Seems like fromString() is going to be easy and
    powerful but I can't find an example of the syntax
    using google.

    Even just a few nice examples would be helpful.

    Can I look at the attributes of sub objects using this
    syntax? How?

    Thanks!

    Eric

    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com



    This archive was generated by hypermail 2.0.0 : Wed May 24 2006 - 13:51:39 EDT