Changeset 7911


Ignore:
Timestamp:
Feb 17, 2021, 12:56:26 PM (10 months ago)
Author:
Nicklas Nordborg
Message:

References #2237: Implement extension mechanism for query filtering

The IdListRestriction now got support for NOT IN (...).

Location:
trunk/src/core/net/sf/basedb/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/ItemContext.java

    r7895 r7911  
    23762376        if (rowIdList != null)
    23772377        {
    2378           rowIdList.intersect((IdListRestriction)r);
     2378          rowIdList.mergeAnd((IdListRestriction)r);
    23792379          r = null;
    23802380        }
  • trunk/src/core/net/sf/basedb/core/query/IdListRestriction.java

    r7772 r7911  
    3434 
    3535  id IN (a, b, c, ....)
     36  id NOT IN (a, b, c, ....)
    3637 
    3738  @author nicklas
     
    4243{
    4344
    44   private final Set<Integer> idList;
     45  private boolean notIn;
     46  private Set<Integer> idList;
    4547  private final Expression idExpression;
    4648 
    4749  public IdListRestriction()
    4850  {
    49     this(new HashSet<>());
     51    this(false, new HashSet<>());
     52  }
     53 
     54  /**
     55    @since 3.18
     56  */
     57  public IdListRestriction(boolean notIn)
     58  {
     59    this(notIn, new HashSet<>());
    5060  }
    5161 
    5262  public IdListRestriction(Set<Integer> idList)
    5363  {
     64    this(false, idList);
     65  }
     66 
     67  /**
     68    @since 3.18
     69  */
     70  public IdListRestriction(boolean notIn, Set<Integer> idList)
     71  {
     72    this.notIn = notIn;
    5473    this.idList = new HashSet<>(idList);
    5574    this.idExpression = Hql.property("id");
     
    6079  {
    6180    String idString = idList.size() == 0 ? "0" : Values.getString(idList, ", ", true);
    62     return "(" + idExpression.toQl(query, dc) + " IN (" + idString + "))";
     81    return "(" + idExpression.toQl(query, dc) + (notIn ? " NOT IN " : " IN ") +"(" + idString + "))";
    6382  }
    6483
     
    7291  {
    7392    String idString = idList.size() == 0 ? "0" : Values.getString(idList, ", ", true);
    74     return idExpression.toString() + " IN (" + idString + ")";
     93    return idExpression.toString() + (notIn ? " NOT IN " : " IN ") + "(" + idString + ")";
    7594  }
    7695
     
    7897    Merge this id list with another list. This list will now
    7998    contain the combined set of ids from both lists.
     99    NOTE! This method ignore the 'notIn' setting for both lists.
    80100  */
    81101  public void union(IdListRestriction other)
     
    87107    Merge this id list with another list. This list will
    88108    now contain only the ids that are found in both lists.
     109    NOTE! This method ignore the 'notIn' setting for both lists.
    89110  */
    90111  public void intersect(IdListRestriction other)
     
    93114  }
    94115 
     116  /**
     117    Merge this id list with another list by removing
     118    ids that are found in the other list.
     119    NOTE! This method ignore the 'notIn' setting for both lists.
     120  */
     121  public void remove(IdListRestriction other)
     122  {
     123    this.idList.removeAll(other.idList);
     124  }
     125 
     126  /**
     127    Merge this list with another list with AND logic respecting
     128    the notIn setting. This may cause the list of ids and notIn
     129    setting of this list to change.
     130    @since 3.18
     131  */
     132  public void mergeAnd(IdListRestriction other)
     133  {
     134    if (notIn == other.notIn)
     135    {
     136      if (notIn)
     137      {
     138        // NOT IN(A,B) AND NOT IN(B,C) --> NOT IN(A,B,C)
     139        // Merge with UNION and keep notIn setting
     140        union(other);
     141      }
     142      else
     143      {
     144        // IN(A,B) AND IN(B,C) --> IN(B)
     145        // Merge with INTERSECT and keep notIn setting
     146        intersect(other);
     147      }
     148    }
     149    else
     150    {
     151      if (notIn)
     152      {
     153        // NOT IN(A,B) AND IN(B,C) --> IN(C)
     154        // Swap, merge with REMOVE and change notIn setting
     155        Set<Integer> tmp = new HashSet<>(other.idList);
     156        tmp.removeAll(idList);
     157        idList = tmp;
     158        notIn = false;
     159      }
     160      else
     161      {
     162        // IN(A,B) AND NOT IN(B,C) --> IN(A)
     163        // Merge with REMOVE and keep notIn setting
     164        remove(other);
     165      }
     166    }
     167  }
    95168 
    96169}
Note: See TracChangeset for help on using the changeset viewer.