mip’s blog


Xtirer helper tags: condition, sql & dirreader

Posted in Xtirer by admin on the July 15th, 2008

My introductory to Xtirer 0.2 is almost finished – so this time it’s all about the helper objects, that went handy during my latest development projects. Check out the posts about reading data from MYSQL via Xtirer: Intro, part 2 & part 3 and the guide how to write data to the database.

Let’s start with the sql object:

<sql
 function="SQL_FUNCTION_NAME"
 param="SQL_FUNCTION_PARAMETERS">

This helper is quite straight forward in terms of setup and it’s effect on the MYSQL query. Take a look at the example below:

<?xml version="1.0" encoding="UTF-8"?>
<test>
 <xml_object name="simple_sql">
    <sql param="1+1"/>
  </xml_object>
  <xml_object name="other_simple_sql">
    <sql function="CONCAT" param="'hello','world'"/>
  </xml_object>
  <xml_object name="more_sql" source="TABLE.animals">
    <xml_object name="owner" valueof="COLUMN.owner">
      <sql function="CONCAT" param="'Owner:',#this"/>
    </xml_object>
    <xml_attribute name="namelength" valueof="COLUMN.owner">
      <sql function="LENGTH" param="#this"/>
    </xml_attribute>
  </xml_object>
</test>

SQL would be:

SELECT (1+1)
SELECT CONCAT('hello','world')
SELECT CONCAT('Owner:',owner), LENGTH(owner) FROM animals

Ok, i think you get the idea. Function defines the SQL Function to use (see the MYSQL function section).
Param defines the parameter list. Please use single quotes to enclose strings. You may use “#this” for including the parent’s sql reference. 

Next helper: condition

<condition
 variable="XTIRER_EXPRESSION"
 value="XTIRER_EXPRESSION"
 compare="eq|neq|gr|sm|gre|sme">

Some sample code:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <xml_object name="save" value="2" type="integer"/>
  <condition variable="$parent.save" value="1" compare="gre">
    <message>success!!</message>
  </condition>
  <condition variable="$parent.save" value="0">
    <message>error!</message>
  </condition>
</test>

The condition object enables conditional output, which is especially helpful when writing data to the database. The db_value object always returns the insert id (except of delete actions), so checking for successful inserts is quite easy.

Variable and value-path again can be Xtirer expressions; Starting with # or $ for the SQL Name or the nodes value you can reference objects throughout the xml-tree. But please be forgiving – this is one of the beta things.

Note: If the condition object contains some SQL action (aka. xml_object, xml_attribute, db_value, etc.) these commands are executed regardless the result of the condition, only the output is filtered!

Last tag for this post: dirreader

<dir_reader
 name="SOME_NAME"
 path="DIRECTORY_PATH"
 http="true|false"
 subdir="true|false"
 limit="NUMBER"
 offset="NUMBER"
 order="ascend|descend|alphabetuc"
 output="[name|type|size|owner|group|date|dir|full|number]"
 excludeon="name|type|size|owner|group|date|dir|full|number">

As the name already suggests it, this has nothing to do with SQL, but in a lot of web projects you sometimes need directory listings. So this object gives you the listing in XML formatted ways. Consider the following prototype files:

<?xml version="1.0" encoding="utf-8"?>
<list>
  <dir_reader name="file" path="dir" http="true" order="numeric" output="name,type,number"/>
</list>

Output for dir being some path:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<list>
 <file type="DS_Store" number="1">
  <name><![CDATA[.DS_Store]]></name>
</file> <file type="php" number="2">   <name><![CDATA[array_splice_test.php]]></name>  </file>  <file type="directory" number="3">   <name><![CDATA[classes]]></name> 
 <file type="php" number="4">
 <name><![CDATA[config.example.inc.php]]></name>
</file>
<file type="php" number="5">
<name><![CDATA[config.inc.php]]></name>
</file>
<file type="html" number="6">
<name><![CDATA[debugfooter.html]]></name> 
 </file>
</list>

The function of most of the parameters is quite obvious, so let’s discuss the two main attributes you might need: output and excludeon. The output attribute takes one or more values delimited by coma and control the output per file found. With excludeon you might define one value on which Xtirer should filter the files.

Let’s consider the following: You have a table in your database containing all files of a gallery with their path and you want to provide a file upload to add additional pics. The user selects the uploaded and adds it to the database. So you would need a list of files which are in the upload-directory but are not present in the database. So you would use something like this:

<?xml version="1.0" encoding="utf-8"?>
<list>
  <dir_reader name="file" path="dir" http="true" order="numeric" excludeon="full" output="name,type,size,date,full">
    <xml_object name="exclude" source="TABLE.gallery" valueof="COLUMN.Filename"/>
  </dir_reader>
</list>

Xtirer reads the file list from the directory specified by dir and excludes all files that are stored in the table gallery via comparing the full file path to the entries in the table. The child node has to be named “exclude”!

I think there is enough to explore in Xtirer and i hope you find something useful for ya. Every comment and feedback welcome!

One Response to 'Xtirer helper tags: condition, sql & dirreader'

Subscribe to comments with RSS or TrackBack to 'Xtirer helper tags: condition, sql & dirreader'.


  1. on July 15th, 2008 at 20:27

    [...] And finally some helpers You’ll find Xtirer at http://sourceforge.net/projects/xtirer In case of any questions lieve a comment or post on the xtirer forum at sourceforge. [...]

Leave a Reply