Changeset 4058


Ignore:
Timestamp:
Dec 13, 2007, 3:19:20 PM (15 years ago)
Author:
Martin Svensson
Message:

References #762 Added four new Operators and started to implement them in the web client.

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/Base.java

    r4026 r4058  
    520520            {
    521521              boolean hasWildcard = value.indexOf('%') >= 0;
     522              boolean isList = value.indexOf('|') >= 0;
    522523              Type valueType = Type.valueOf(name.substring(7, colonIndex));
    523524 
     
    526527              filter.setValueType(valueType);
    527528             
    528               Operator op = hasWildcard ? Operator.LIKE : Operator.EQ;
     529              Operator op = hasWildcard ?
     530                  (isList ? Operator.LIKE_IN : Operator.LIKE) :
     531                    (isList ? Operator.IN : Operator.EQ);
    529532              if (value.startsWith("<>") || value.startsWith("!="))
    530533              {
    531                 op = hasWildcard ? Operator.NOTLIKE : Operator.NEQ;
     534                op = hasWildcard ?
     535                    (isList ? Operator.NOTLIKE_IN : Operator.NOTLIKE) :
     536                      (isList ? Operator.NOTIN : Operator.NEQ);
     537                value = value.substring(2);
     538              }
     539              else if (value.startsWith("=="))
     540              {
     541                op = Operator.EQ;
    532542                value = value.substring(2);
    533543              }
     
    554564              else if (value.startsWith("="))
    555565              {
    556                 op = hasWildcard ? Operator.LIKE : Operator.EQ;
     566                op = hasWildcard ?
     567                    (isList ? Operator.LIKE_IN : Operator.LIKE) :
     568                      (isList ? Operator.IN : Operator.EQ);
    557569                value = value.substring(1);
    558570              }
  • trunk/src/core/net/sf/basedb/core/Operator.java

    r4040 r4058  
    3333
    3434/**
    35   @author Nicklas
     35  @author Nicklas, Martin
    3636  @version 2.0
    3737*/
    3838public enum Operator
    3939{
    40   AND(1, "AND", "AND", false)
    41   {
    42     public Restriction getRestriction(Expression lvalue, Expression rvalue)
    43       throws InvalidDataException
    44     {
    45       throw new InvalidDataException("Not allowed");
    46     }
    47   },
    48 
    49   OR(2, "OR", "OR", false)
    50   {
    51     public Restriction getRestriction(Expression lvalue, Expression rvalue)
    52       throws InvalidDataException
    53     {
    54       throw new InvalidDataException("Not allowed");
    55     }
    56   },
    57 
    58   NOT(3, "NOT", "NOT", false)
    59   {
    60     public Restriction getRestriction(Expression lvalue, Expression rvalue)
    61       throws InvalidDataException
    62     {
    63       throw new InvalidDataException("Not allowed");
    64     }
    65   },
    66 
    67   EQ(4, "=", "=", true)
     40  AND(1, "AND", "AND", false, false)
     41  {
     42    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     43      throws InvalidDataException
     44    {
     45      throw new InvalidDataException("Not allowed");
     46    }
     47   
     48    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     49      throws InvalidDataException
     50    {
     51      throw new InvalidDataException("Not allowed");
     52    }
     53  },
     54
     55  OR(2, "OR", "OR", false, false)
     56  {
     57    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     58      throws InvalidDataException
     59    {
     60      throw new InvalidDataException("Not allowed");
     61    }
     62   
     63    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     64      throws InvalidDataException
     65    {
     66      throw new InvalidDataException("Not allowed");
     67    }
     68  },
     69
     70  NOT(3, "NOT", "NOT", false, false)
     71  {
     72    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     73      throws InvalidDataException
     74    {
     75      throw new InvalidDataException("Not allowed");
     76    }
     77   
     78    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     79      throws InvalidDataException
     80    {
     81      throw new InvalidDataException("Not allowed");
     82    }
     83  },
     84
     85  EQ(4, "=", "=", true, false)
    6886  {
    6987    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    7290      return Restrictions.eq(lvalue, rvalue);
    7391    }
    74   },
    75 
    76   NEQ(5, "!=", "!=", true)
     92
     93    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     94      throws InvalidDataException
     95    {
     96      throw new InvalidDataException("Not allowed");
     97    }
     98  },
     99
     100  NEQ(5, "!=", "!=", true, false)
    77101  {
    78102    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    81105      return Restrictions.neq(lvalue, rvalue);
    82106    }
    83   },
    84 
    85   LT(6, "<", "<", true)
     107   
     108    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     109      throws InvalidDataException
     110    {
     111      throw new InvalidDataException("Not allowed");
     112    }
     113  },
     114
     115  LT(6, "<", "<", true, false)
    86116  {
    87117    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    90120      return Restrictions.lt(lvalue, rvalue);
    91121    }
    92   },
    93 
    94   LTEQ(7, "<=", "<=", true)
     122   
     123    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     124      throws InvalidDataException
     125    {
     126      throw new InvalidDataException("Not allowed");
     127    }
     128  },
     129
     130  LTEQ(7, "<=", "<=", true, false)
    95131  {
    96132    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    99135      return Restrictions.lteq(lvalue, rvalue);
    100136    }
    101   },
    102 
    103   GT(8, ">", ">", true)
     137   
     138    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     139      throws InvalidDataException
     140    {
     141      throw new InvalidDataException("Not allowed");
     142    }
     143  },
     144
     145  GT(8, ">", ">", true, false)
    104146  {
    105147    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    108150      return Restrictions.gt(lvalue, rvalue);
    109151    }
    110   },
    111 
    112   GTEQ(9, ">=", ">=", true)
     152
     153    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     154      throws InvalidDataException
     155    {
     156      throw new InvalidDataException("Not allowed");
     157    }
     158  },
     159
     160  GTEQ(9, ">=", ">=", true, false)
    113161  {
    114162    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    117165      return Restrictions.gteq(lvalue, rvalue);
    118166    }
    119   },
    120 
    121   LIKE(10, "LIKE", "LIKE", true)
     167
     168    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     169      throws InvalidDataException
     170    {
     171      throw new InvalidDataException("Not allowed");
     172    }
     173  },
     174
     175  LIKE(10, "LIKE", "LIKE", true, false)
    122176  {
    123177    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    126180      return Restrictions.like(lvalue, rvalue);
    127181    }
    128   },
    129  
    130   NOTLIKE(11, "NOT LIKE", "NOT LIKE", true)
     182
     183    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     184      throws InvalidDataException
     185    {
     186      throw new InvalidDataException("Not allowed");
     187    }
     188  },
     189 
     190  NOTLIKE(11, "NOT LIKE", "NOT LIKE", true, false)
    131191  {
    132192    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     
    134194    {
    135195      return Restrictions.not(Restrictions.like(lvalue, rvalue));
     196    }
     197
     198    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     199      throws InvalidDataException
     200    {
     201      throw new InvalidDataException("Not allowed");
     202    }
     203  },
     204 
     205  /**
     206    @since 2.5
     207   */
     208  IN(12, "IN", "IN", true, true)
     209  {
     210    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     211      throws InvalidDataException
     212    {
     213      Expression[] expressionArray = {rvalue};
     214      return getRestriction(lvalue, expressionArray);
     215    }
     216
     217    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     218      throws InvalidDataException
     219    {
     220      return Restrictions.in(lvalue, rvalues);
     221    }
     222  },
     223 
     224  /**
     225    @since 2.5
     226   */
     227  NOTIN(13, "NOT IN", "NOT IN", true, true)
     228  {
     229    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     230      throws InvalidDataException
     231    {
     232      Expression[] expressionArray = {rvalue};
     233      return getRestriction(lvalue, expressionArray);
     234    }
     235
     236    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     237      throws InvalidDataException
     238    {
     239      return Restrictions.not(Restrictions.in(lvalue, rvalues));
     240    }
     241  },
     242 
     243  /**
     244    @since 2.5
     245   */
     246  LIKE_IN(14, "LIKE", "LIKE", true, true)
     247  {
     248    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     249      throws InvalidDataException
     250    {
     251      Expression[] expressionArray = {rvalue};
     252      return getRestriction(lvalue, expressionArray);
     253    }
     254
     255    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     256      throws InvalidDataException
     257    {
     258      return Restrictions.like_in(lvalue, rvalues);
     259    }
     260  },
     261
     262  /**
     263    @since 2.5
     264   */
     265  NOTLIKE_IN(15, "LIKE", "LIKE", true, true)
     266  {
     267    public Restriction getRestriction(Expression lvalue, Expression rvalue)
     268      throws InvalidDataException
     269    {
     270      Expression[] expressionArray = {rvalue};
     271      return getRestriction(lvalue, expressionArray);
     272    }
     273
     274    public Restriction getRestriction(Expression lvalue, Expression rvalues[])
     275      throws InvalidDataException
     276    {
     277      return Restrictions.not(Restrictions.like_in(lvalue, rvalues));
    136278    }
    137279  };
     
    152294  private final String sqlSymbol;
    153295  private final boolean expressionOperator;
    154 
    155   private Operator(int value, String symbol, String sqlSymbol, boolean expressionOperator)
     296  private final boolean isListOperator;
     297
     298  private Operator(int value, String symbol, String sqlSymbol, boolean expressionOperator, boolean isListOperator)
    156299  {
    157300    this.value = value;
     
    159302    this.sqlSymbol = sqlSymbol;
    160303    this.expressionOperator = expressionOperator;
     304    this.isListOperator = isListOperator;
    161305  }
    162306
     
    202346    throws InvalidDataException;
    203347
     348  public abstract Restriction getRestriction(Expression lvalue, Expression rvalues[])
     349    throws InvalidDataException;
     350
     351 
    204352  /**
    205353    Get the <code>Operator</code> object when you know the integer code.
     
    214362  }
    215363
     364  /**
     365    Gets if the operator should be used with a list or not.
     366    The list items should be separated with '|'.
     367    @return TRUE if it should, FALSE otherwise.
     368    @since 2,6
     369   */
     370  public boolean isListOperator()
     371  {
     372    return isListOperator;
     373  }
    216374}
  • trunk/src/core/net/sf/basedb/core/query/Restrictions.java

    r3679 r4058  
    2929import net.sf.basedb.core.InvalidUseOfNullException;
    3030
     31import java.util.ArrayList;
     32import java.util.List;
     33
    3134/**
    3235  A factory class to create restrictions.
     
    252255
    253256  /**
     257    Check if one expression matches any expressions in an array:
     258    (e1 LIKE e2) OR (e1 LIKE e3) OR ....
     259    @param e1 The left expression
     260    @param e2 The array with expression to match against.
     261    @return The new restriction.
     262    @throws InvalidDataException If any of the expressions are null.
     263    @since 2.6
     264   */
     265  public static Restriction like_in(Expression e1, Expression...e2)
     266  {
     267    if (e1 == null) throw new InvalidUseOfNullException("e1");
     268    int firstNull = getFirstNull(e2);
     269    if (firstNull >= 0 || e2 == null)
     270    {
     271      throw new InvalidUseOfNullException("e2[" + firstNull + "]");
     272    }
     273    if (e2.length < 1) throw new InvalidDataException("'e2' must contain at least one element");
     274    List<Restriction> restrictions = new ArrayList<Restriction>(e2.length);   
     275    for (Expression e : e2)
     276    {
     277      restrictions.add(Restrictions.like(e1, e));
     278    }   
     279    return Restrictions.or(restrictions.toArray(new Restriction[restrictions.size()]));
     280  }
     281 
     282  /**
    254283    Test if there is null in the array.
    255284    @return The index of the first null element,
Note: See TracChangeset for help on using the changeset viewer.