Enhancement 169 : ENHANCEMENT: Add support for intercepting Queries
Priority 
High
Reported Version 
 
Logged By 
Rob
Status 
Fixed
Fixed Version 
2.2.0
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
30/10/2009
Updated 
30/10/2009
Type 
Enhancement
 
Attachments 
No attachments

Is there also a way to intercept all queries and saves?

... we add the company id to almost every query to limit the load on the database and prevent companies from seeing each others data.

 
Rob 30 Oct 11:31
BeanQueryAdapter ...

Just adding a BeanQueryAdapter which will allow you to do this.

Rob 30 Oct 11:33
BeanQueryAdapter interface
/**
 * Return true if this adapter is interested in queries for the given entity
 * type.
 */
public boolean isRegisterFor(Class<?> cls);

/**
 * Returns an int to to control the order in which BeanQueryAdapter are
 * executed when there is multiple of them registered for a given entity
 * type (class).
 */
public int getExecutionOrder();

/**
 * Modify the associated query prior to it being executed.
 */
public void preQuery(BeanQueryRequest<?> request);

Rob 30 Oct 11:36
Typical usage would be...

So typically usage would be to register for any/all bean types that you want to adapt the queries for.

public void preQuery(BeanQueryRequest<?> request) {
        
  Query<?> query = request.getQuery();
        
  // can get the type of query Bean, List, RowCount, Id's etc
  //Type queryType = query.getType();
        
  // typically add extra predicates based on the current user
  // (probably get the current user from a TheadLocal)

  // ... for this example add a silly 1=1 predicate
  query.where().raw("1=1");
}

Rob 30 Oct 11:42
Fixed in HEAD

The code to implement this is in head.

Refer to com.avaje.ebean.event.BeanQueryAdapter

woResponse

Upload a file