--
PedroRio - 27 Dec 2010
XeoQL - XEO Query Language ( also known as BOQL )
XEO Query Language (XEOQL a.k.a BOQL - Business Object Query Language) is a language designed to query instances of XEO Object Models. It's very similar to SQL and has many of the same constructors. SQL is designed to return records from a relational database, while XEOQL is designed to return Object Model instances from its datasource (tipically a relational database).
Using XEO's Java API to issue a XEOQL expression against the datasource will produce a
boObjectList instance which can be used to iterate through all elements that are the result of the query. The methods available in
ObjectListManager allow you to create a boObjectList instance from a XEOQL query.The syntax for a XEOQL expression is the following:
SELECT [MY] [USING user/pwd] [attlist FROM] objname [WHERE whereStatement (AND | OR whereStament)* ][ORDER BY attName[ASC|DESC] (,attName [ASC|DESC])*]
Although the syntax may seem complicated usually you'll not use every option in the syntax. Let's start with the simplest of queries, selecting all instances of a given Object Model, for example the system Object Model
Ebo_Perf (which represents a user).
select Ebo_Perf
Executing this query in the XEOQL Console (available in every XEO Application's Administration Viewer, which will be discussed later) provides the following results:
Figure XEOQL.1 - Executing a simple query in the XEOQL console.
In the bottom of figure XEOQL.1 you can see the two instances of Ebo_Perf (a default XEO application has the SYSUSER and ROBOT user) that are the result of this query. The results reflect a default XEO application. See in the following section how to select instances which are related to a current instance (through collection or object attributes). It's possible to select instances which are related with other instance (though a object or collection attribute), see the "dot syntax" section bellow for more information.
The dot syntax
An Object Model A can define an 1:N relation with to Object Model through a collection attribute (named attCollection) or a 1:1 relation through an object attribute. It's possible (using XEOQL) to retrieve instances associated with other instances by using a dot syntax. For example: The LIB_Book Object Model (created for the XEO Library) defines a categories attribute (collection attribute) and a publisher attribute (object attribute). To select all publishers that are associated with a book the following query would be needed:
select LIB_Book.publisher
The preivous query will return a list of all LIB_Publisher instances that are related to a LIB_Book instance (if one wanted only the publisher of a given book, you would need a where clause, which will be discussed in the next section). To select all LIB_BookCategory instances associated to any given book, the following query could be used
select LIB_Book.categories
You can go further down the hierarchy, like selecting all publisher nucleus from publishers that are related to book instance with the following query:
select LIB_Book.publisher.nucleus
Dot syntax
can be used continually as long as the attributes being selected are of type
object or
collection.
Where Clause
In order to restrict the results of a XEOQL expression, and just like the SQL counterpart, XEOQL provides a WHERE clause. The where clause allows to restrict selection by using logical operators, attribute references, functions, arithmetic and literals. The syntax for the where clause is the following:
WHERE whereStatement (AND | OR whereStament)*
The
whereStatament expression encapsulates all the possible combinations for valid expressions which can be created using the following elements:
- Attributes - Reference to an attribute to be compared with some value (ex. Literals)
- Literals - Literal values to be used for comparison
- Functions
- Arithmetic Operator - Allow to make calculations using +,-,*,/ (i.e sum, subtraction, multiplication and divison)
- Aggregate Functions - Sum(), Avg(), etc...
- Relational operators - (
, >, <, >
, <=, like, is null, etc...)
Where Clause - Attributes (and the Dot Syntax)
In a XEOQL expression is possible to retrict the selected instances by making to only instances whose attributes match a certain condition are returned. Recall from the
XEO Library introduction that the LIB_Book Model had a
title attribute (a text attribute). In order to select all book instances which have a particular title (Da Vinci Code, for example), we would use the following XEOQL expression.
select LIB_Book where title = 'Da Vinci Code'
One could also explicitly refer the Object Model being selected in the expression, like the following:
select LIB_Book where LIB_Book.title = 'Da Vinci Code'
One can use attributes from instances with which the first is related, for example to select all books which were published by "HarperCollins".
select LIB_Book WHERE LIB_Book.publisher.name = 'Wrox'
or
select LIB_Book WHERE publisher.name = 'Wrox'
Beware that when selecting object instances through an attribute (i.e. selecting LIB_Publisher instances by using LIB_Book.publisher) the context for the where clause is that of the type of returned objects. To return all Publishers that have published a book with a title like "Java" the temptation would be to use the following (wrong) query
select LIB_Book.publisher where title LIKE 'Java%'
The query is wrong because in reallity that expression represents
select all publishers (related to book instances) which have a title attribute like "Java"" This happens because the where clause is in the context of the LIB_Publisher Object Model (because of LIB_Book.publisher relation), in order to retrieve the correct instance you would have to use the following query:
select LIB_Book.publisher where LIB_Book.title LIKE 'Java%'
Putting "LIB_Book.title" in the where clause would bring the
title attribute to right context (the LIB_Book Object Model context)
aa
aa
Where Clause - Literals
aaa
Where Clause - Functions
aaa
Where Clause - Arithmetic Operators
aaa
Where Clause - Aggregate Functions
aaa
Where Clause - Relational Operators
zzzz
zzz
Order by Clause
aaa
The MY / USING expressions
The MY expression is used to declared
that only instances which were created by the user executing the query should be returned. In reallity, what's being done is make a where clause with SYSUSER = USER_ID????
The USING USER/PASS expression is used to perform queries as another user (using the given username and password, hence the name). If one issues the query:
SELECT USING SYSUSER/ABC Ebo_Perf
The results will be all Ebo_Perf instances that the SYSUSER user would see when executing the
select Ebo_Perf query.
Special Constants
There are a number of special constants which can be used when creating a XEOQL expression.The following table summarizes them.
Table XEOQL.1 - Special constants in XEOQL
a
a
a
a
a
Sample queries
aaa