RQL (Rapid Query Language) is an object oriented query language developed for REF framework. It is the query flavour which is used under the hood by internals of Rapid itself. I will be showing you a quick overview of RQL in this section.

If the rest of the text bore you, you can jump to the following links
RQL Examples
  1. Simple Select
  2. Sample Conditional Clauses
  3. Sample Order Bys
  4. Sample Group By
  5. Correlated Subquery
  6. Joins

Over the years , Rapid entity framework has been improved based on experience and feedbacks. In the struggle for compliance to ORM semantics and bringing REF users up-to date on ORM standards , we have improved our query strategies and engine to surpass that of LINQ and other ORM technologies in the industry. This breakthrough is one of the successful aspects of REF.

Simple RQL Sample using the Good old Pubs Database

using ( EntityManager manager = new EntityManager() )
{
   manager.OpenDatabaseSession();

   var t1 = manager.AsAlias<Publisher>();
   var results = new From( typeof( Publisher ) ).As( t1 )
                                  .SelectNew( ( ) => t1 ).Execute( );
}

Generated SQL
 SELECT  [t0].[pub_name],
         [t0].[city],
         [t0].[state],
         [t0].[country],
         [t0].[pub_id] 
FROM  [publishers] [t0] 

Code Review
The query returns all Publishers, the objects returned is the Publisher entity.
Before i start explainations, notice that, the new RQL semantics starts from instantiating the From classs, the previous versions of RQL starts from instantiating the Select classs .

The code above makes a call to the AsAlias method from the EntityManager instance, this method creates an alias of the Entity class specified in the generic parameter. For example the following code creates an alias of the Employee entitiy:


 var t5 = manager.AsAlias<Employee>( );


The code snippet above tells REF's CallStack or call it CallSite engine to create an alias query proxy, that monitors calls to properties, this enable the REF runtime engine to keep track of properties which are used in the query. Using string based property name like "t0.Price" is still supported. But the statically bound property provided by the alias to the entity is more prefereable because of compile time check of the properties. This idea is another innovation from the Rapid team.

Call to SelectNew(()=> t0 ).Execute()

The SelectNew method of the Query is called to define the projections of the query, the query above projects the actual entity, this query returns all Publishers. You can do other projetions (anonymous, Projection class , property etc) which will be covered in a seperate section.

.SelectNew( ( ) => t1 ).Execute( );

The above snippet returns an instance of a DeferedExecution<T> class . This class Defers the execution of the query until any of its query operator methods is called. For example, a call to the Execute() method will execute the query and return elements that are projected.

Another example

This example counts all Titles whose Price property is not null using the following RQL query. Note the Count() method is called to retirve the count of the projection.

using ( EntityManager manager = new EntityManager() )
{
   manager.OpenDatabaseSession();

   var t6 = manager.AsAlias<Title>( );
   var results2 = new From( typeof( Title ) ).As( t6 )
                           .Where( t6.Price ).IsNotNull( )
                           .SelectNew( ( ) => t6 ).Count( );

Generated SQL
SELECT  COUNT(1) FROM  [titles] [t0]  
         WHERE  [t0].[price]  IS NOT NULL 

Results : 16

Last edited Apr 1, 2010 at 9:21 AM by ahmedsalako, version 22

Comments

No comments yet.