data reading & Xtirer – part 3: Select
Weekend is over, so let’s get it on; After explaining the xml_object | xml_attribute in detail in part 2 of the Xtirer 0.2 Introduction, it is time to get hands on some fine tuning of row selection.
Obviously, the WHERE clause is missing, so to put it simple: with the selector tag you can define the value of columns for the rows to be selected.
Definition:
1: <selector
2: variable="XTIRER_COLUMN_REFERENCE"
3: value|array="STATIC_VALUE"
4: valueof|arrayof="XTIRER_COLUMN_REFERENCE"
5: http="false|true"
6: compare="eq|neq|gr|sm|gre|sme"
7: type="number|string"
8: boolean="AND|OR|NOR|XOR|NAND">
- variable: Column name which should be set/defined.
- value|array: static value or comma seperated array.
- valueof|arrayof: Column name of a parent’s query result, which to use for this selection
- http: if true, valueof/arrayof is interpreted as a http variable name and that content is used
- compare: Comparison method between variable and value
- type: string values (default) are always quoted; numbers not.
- boolean: if multiple selectors are used they can be combined with different logical operators; AND is default.
The unique feature of the selector object is that it does not inherit the data source from the parent. It roots itself in the grandparent. But let’s see some examples for explanation:
<container>
<xml_object name="animal" source="TABLE.animals">
<selector variable="COLUMN.id" value="httpid" http="true" type="number"/>
<xml_object name="name" valueof="COLUMN.name"/>
<xml_attribute name="birth" valueof="COLUMN.birth"/>
</xml_object>
</container>
What will happen? Yep, you’re right:
Xtirer tries to read a variable called httpid from the http variables and will build the following query:
SELECT name,birth FROM animals WHERE id = [httpid];
Output and the rest should be pretty straight forward.
If you use arrayof instead of valueof Xtirer assumes you use a comma-seperated array and the above query would look like:
SELECT name,birth FROM animals WHERE id IN ([httpid])
Ok, next – this:
<container>
<xml_object name="animal" source="TABLE.animals">
<xml_object name="name" valueof="COLUMN.name"/>
<xml_attribute name="birth" valueof="COLUMN.birth"/>
<xml_object name="funky" source="TABLE.funkystuff">
<selector variable="COLUMN.id" valueof="COLUMN.id" type="number"/>
<xml_attribute name="num" valueof="COLUMN.number"/>
</xml_object>
</xml_object>
</container>
Here it gets obvious, that the selector object has to inherit from the grandparent; Otherwise the Where-Clause of the secound SQL-Query would make no sense.
First, Xtirer will execute the standard query:
SELECT name, birth, id FROM animals
And then for every result row the following gets read from the database:
SELECT num FROM funkystuff WHERE id <=> [id from animals]
The Xtirer_COLUMN_Reference in the valueof attribute is adressing the animals-table, whereas the variable attribute references the id column of the funkystuff-table.
If you use arrayof instead of valueof the result from the database is treaded as an array. This one advantage of Xtirer above a standard SQL JOIN.
To make it a bit more confusing, but shorter you could simply add the funkystuff-number to the animal object by replacing the
<xml_object name="funky"...
with:
<xml_attribute name="funky" source="TABLE.funkystuff" valueof="COLUMN.id">
<selector variable="COLUMN.id" valueof="COLUMN.id" type="number"/>
</xml_attribute>
To not be confused you could also write:
<selector variable="COLUMN.id" valueof="TABLE.animals.COLUMN.id" type="number"/>
but you don’t have to.
I hope, my explanations are intelligible to all and you still like Xtirer.
Next, i’ll gonna turn to data writing and deleting. So check back soon, cheers!
on July 10th, 2008 at 08:43
[...] my last post, i’ve shown how to finetune data reading by using Xtirer’s selector object. So you [...]
on July 15th, 2008 at 20:26
[...] Finetuning your database reading results with the selector object [...]