mip’s blog


store data to MYSQL with Xtirer

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

In my last post, i’ve shown how to finetune data reading by using Xtirer’s selector object. So you should already be able to get your values from the MYSQL database. Today, it’s all about writing and deleting data to the tables. Let’s assume we like to store data to a table containing the team member’s data. So your Xtirer prototype file could look somewhat like this:

<?xml version="1.0" encoding="utf-8"?>
 1: <datastore>
 2: <db_value field="TABLE.teamlist" name="save" type="integer" action="REPLACE">
 3:  <db_value field="COLUMN.id" http="personid" type="integer"/>
 4:  <db_value field="COLUMN.name" http="name"/>
 5:  <db_value field="COLUMN.position" http="position"/>
 6:  <db_value field="COLUMN.email" http="email"/>
 7:  <db_value field="COLUMN.description" http="description"/>
 8:  <db_value field="COLUMN.image" http="img_id" type="integer"/>
 9: </db_value>
10: </datastore>

And here goes the definition of the db_value object:

<db_value
 name="SOME_NAME"
 field="XTIRER_TABLE_REFERENCE|XTIRER_COLUMN_REFERENCE"
 action="REPLACE|UPDATE|DELETE"
 type="number|string"
 http="HTTP_VARIABLE_NAME"
 value="SOME_VALUE">

I think it is quite obvious what is happening there:
Line 1: the datastore tag is just a wrapper – so no functionality
Line 2: the first db_value object – the parent- should define the table to write to
Line 3-8: database columns to write to and http variable names to get the data from.

Things to note:
the type attribute triggers quoting or not for the value: 1 vs. ‘1′
adjust the desired write action by changing the action attribute of the parent db_value object
there is no “INSERT” command ’cause “REPLACE” does the same for new entries, but can be used to change data of already existing rows when a unique key you like to write already exists. That’s why you usually don’t need a selector object.

Ok, but what’s the notion behind the name- and type-attribute of the parent db_value object in line 2!?
Of course in most cases you should be interested of the result of the writing action. So the db_value returns the LAST_INSERT_ID. Hm, and there should be some way to exploit this information.

So, next code shows how my writing prototype files usually look like:

<?xml version="1.0" encoding="utf-8"?>
 <datastore>
 <db_value field="TABLE.teamlist" name="save" type="integer" action="REPLACE">
  <db_value field="COLUMN.id" http="personid" type="integer"/>
  <db_value field="COLUMN.name" http="name"/>
  <db_value field="COLUMN.position" http="position"/>
  <db_value field="COLUMN.email" http="email"/>
  <db_value field="COLUMN.description" http="description"/>
  <db_value field="COLUMN.image" http="img_id"/>
 </db_value>
 <condition variable="$parent.save" value="1" method="gre">
  <message>success!!</message>
 </condition>
 <condition variable="$parent.save" value="0">
  <message>error!</message>
 </condition>
</datastore>

I’d like to finish for this post, but i promise to handle the helper objects such as the condition, sql or dirreader object in the next. So, check back next time! Have fun.

Leave a Reply