Arbitrary Find

The arbitrary find method provides custom device applications the ability to dynamically build queries based on user input.

AttributeTest

In addition to allowing for arbitrary search criteria, the arbitrary find method lets the user specify the ordering of the results and object state criteria. A Query class is included in the client object API’s core classes. The Query class is the single object passed to the arbitrary search methods and consists of search conditions, object/row state filter conditions, and data ordering information.

Define these conditions by setting properties in a query:
  • TestCriteria – criteria used to filter returned data.
  • SortCriteria – criteria used to order returned data.
  • Skip – an integer specifying how many rows to skip. Used for paging.
  • Take – an integer specifying the maximum number of rows to return. Used for paging.

TestCriteria can be an AttributeTest or a CompositeTest.

An AttributeTest defines a filter condition using an MBO attribute, and supports these conditions:

A CompositeTest combines multiple TestCriteria using the logical operators AND, OR and NOT to create a compound filter.

The following example retrieves all log records where mboName=entityName and key=idString:
String entityName = "Customer";
        String idString = "12345";
        com.sybase.persistence.Query query = new 
        com.sybase.persistence.Query();
        com.sybase.persistence.CompositeTest ct = new 
        com.sybase.persistence.CompositeTest();
        ct.setOperator(com.sybase.persistence.CompositeTest.AND);
        ct.add(com.sybase.persistence.AttributeTest.equal("component", 
        entityName));
        ct.add(com.sybase.persistence.AttributeTest.equal("entityKey",idString));
        query.setTestCriteria(ct);
        com.sybase.collections.ObjectList logList = 
        LogRecordImpl.findWithQuery(query);

SortCriteria defines a list of SortOrder, which contains an attribute name and an order type (ASCENDING or DESCENDING).

For example, locate all Customer objects based on the following criteria:

Use code similar to:

 Query props = new Query();
        //define the attribute based conditions
        CompositeTest innerCompTest = new CompositeTest();
        innerCompTest.setCompositionType(TestType.OR);
        innerCompTest.add (
        new AttributeTest ("state", "CA", AttributeTest.EQUAL));
        innerCompTest.add (
        new AttributeTest ("state", "NY", AttributeTest.EQUAL));
        CompositeTest outerCompTest = new CompositeTest();
        outerCompTest.setCompositionType(CompositeTest.AND);
        outerCompTest.add (
        new AttributeTest("fname", "John", AttributeTest.EQUAL));
        outerCompTest.add (
        new AttributeTest("lname", "Doe" ,AttributeTest.EQUAL));
        outerCompTest.add (innerCompTest);
        //define the ordering
        SortCriteria sort = new SortCriteria();
        sort.add ("lname", SortOrderType.ASCENDING);
        sort.add ("fname", SortOrderType.ASCENDING);
        sort.add ("id", SortOrderType.DESCENDING);
        //set the Query object
        props.setTestCriteria(outerCompTest);
        props.setSortCriteria(sort);
        props.setSkip(10);
        props.setTake(5);
        props.setStateCriteria(ObjectState.NEW | ObjectState.UPDATED);
        com.sybase.collections.ObjectList customers = Customer.findWithQuery(props);