RE: Building expression from Null strings

From: Gentry, Michael (michael_gentr..anniemae.com)
Date: Tue Oct 12 2004 - 10:34:53 EDT

  • Next message: Andrus Adamchik: "Re: Cayenne, Oracle and Date-based queries..."

    It's been my experience that, by default, expressions not supplied are
    factored out.

    Expression expression = Expression.fromString("firstName like
    '%$firstName%' and lastName like '%$lastName%' and userName like
    '%$username%' and email like '%email%'");
    Map parameters = new HashMap();

    if (getSearchFirstName() != null)
      parameters.put("firstName", getSearchFirstName());
    if (getSearchLastName() != null)
      parameters.put("lastName", getSearchLastName());
    if (getSearchUserName() != null)
      parameters.put("username", getSearchUserName());
    if (getSearchEmail() != null)
      parameters.put("email", getSearchEmail());

    SelectQuery query = new SelectQuery(Customer.class,
    expression.expWithParameters(parameters));
    ...

    Of course, if no search criteria is entered, you'll get everything, but
    that might be what you are wanting.

    /dev/mrg

    -----Original Message-----
    From: Cyril Godefroy [mailto:cgodefro..ac.com]
    Sent: Tuesday, October 12, 2004 9:35 AM
    To: cayenne-use..bjectstyle.org
    Subject: Building expression from Null strings

    I'm trying to do something around this:

    Expression qual = generateLikeExp("firstName", getSearchFirstName());
             qual =qual.andExp(generateLikeExp("lastName",
    getSearchLastName()));
             qual =qual.andExp(generateLikeExp("userName",
    getSearchUserName()));
             qual =qual.andExp(generateLikeExp("email", getSearchEmail()));
             SelectQuery query = new SelectQuery(Customer.class, qual);
    ...

        public Expression generateLikeExp(String attribute, String
    searchString){
             if(!searchString.equals("") && searchString != null){
                 return ExpressionFactory.likeIgnoreCaseExp(attribute, "%" +

    searchString + "%");
             } else {
                 return null;
             }
         }

    You see where this leads me... If getFirstName() returns null, then
    andExp() cannot be called on a null object.

    Is there an obvious way to go around this problem that I am not seeing?

    Tia,
    Cyril



    This archive was generated by hypermail 2.0.0 : Tue Oct 12 2004 - 10:35:01 EDT