Adapter Class
Ok, here comes most important part, that is explanation of “Adapter” class, which actually executes queries and following section enlists all possible ways.
The most important part, as && and || operators are not overridable, the SQL AND is replaced by single “&” operand and OR is replaced by single “|” operand in order to build the query expression
Throught this section, we will use following schema,
Table: Customers
Fields: CustomerID, FirstName, LastName, IsActive
Table: Invoices
Fields: InvoiceID, InvoiceDate, Amount, CustomerID
and now note that, CustomerID of Invoices table refers to CustomerID of Customers.
Query Single Object
Invoice.Adapter.Get( 2312 );
Just by specifying the “Long” ID.
Query Single Object by “WHERE”
Invoice.Adapter.Get( Invoice.Schema.InvoiceID == 2312 );
This is an expression tree, and it is built by schema access.
Invoice.Adapter.Get( Customer.Schema.IsActive == true , Customer.Schema.CustomerID.SortDesc());
This expression creates an Auto Join and also sorts CustomerID in descending order, giving you the invoice of last added customer
Compound WHERE Expression by “AND”
Invoice.Adapter.GetAll( Invoice.Schema.InvoiceDate >= new DateTime(2001,1,1) & Invoice.Schema.Status != "PAID");
This compond expression contains two expressions and “AND” operator.
Dynamic Expression Tree for WHERE
Consider following sample of searching customer,
private CustomerList Search(string name, string email, string phone, int start, int size) { ErpExpression exp = null; if(!String.IsNullOrEmpty(name)) exp &= Customer.Schema.FirstName.Contains(name); if(!String.IsNullOrEmpty(email)) exp &= Customer.Schema.EmailAddress.Contains(email); if(!String.IsNullOrEmpty(phone)) exp &= Customer.Schema.Phone.Contains(phone); // note this exp Expression is dynamic depending upon // values specified at run time return Customer.Adapter.GetAll(exp, start, size); }
The above function returns all Customers if all parameters are either null or empty. This search functionality is very essential in order to build dynamic query expression, which is not offered in any of the ORML software existing till date.
You can sure use & or | respectively for AND or OR operators, however please try to visualize the expression tree in order to avoid any confusion.
