Changeset 5401


Ignore:
Timestamp:
Sep 3, 2010, 1:58:11 PM (12 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1504: Add support for LIKE expressions to JEP parser

The parse will detect situations were the right hand argument of expressions like foo == 'bar%' is a string constant that includes a '%'. If so, the parser will create a LIKE-restriction instead of an EQUALS-restriction.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/jep/convert/EqualsConverter.java

    r5385 r5401  
    2222package net.sf.basedb.util.jep.convert;
    2323
     24import net.sf.basedb.core.query.Expression;
    2425import net.sf.basedb.core.query.Restriction;
    2526import net.sf.basedb.core.query.Restrictions;
    2627import net.sf.basedb.util.jep.Jep;
    2728
     29import org.nfunk.jep.ASTConstant;
    2830import org.nfunk.jep.Node;
    2931
     
    6668  }
    6769  /**
    68     Convert to restriction by calling {@link Restrictions#eq(net.sf.basedb.core.query.Expression, net.sf.basedb.core.query.Expression)}.
     70    Convert to restriction by calling {@link Restrictions#eq(Expression, Expression)}.
     71    If the right-hand node is string constant containing '%'
     72    {@link Restrictions#like(Expression, Expression)} is used instead (since 2.16).
    6973  */
    7074  @Override
    7175  public Restriction toRestriction(Node node)
    7276  {
    73     return Restrictions.eq(
    74         Jep.nodeToExpression(node.jjtGetChild(0)),
    75         Jep.nodeToExpression(node.jjtGetChild(1))
    76       );
     77    Node lNode = node.jjtGetChild(0);
     78    Node rNode = node.jjtGetChild(1);
     79    Expression left = Jep.nodeToExpression(lNode);
     80    Expression right = Jep.nodeToExpression(rNode);
     81    boolean useLike = false;
     82    if (rNode instanceof ASTConstant)
     83    {
     84      ASTConstant constNode = (ASTConstant)rNode;
     85      Object constValue = constNode.getValue();
     86      if (constValue instanceof String)
     87      {
     88        useLike = ((String)constValue).contains("%");
     89      }
     90    }
     91    return useLike ? Restrictions.like(left, right) : Restrictions.eq(left, right);
    7792  }
    7893  // -------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.