Bulk Updates


Bulk updates help keep our Ajax applications running more efficiently by keeping the number of database requests down. This is because large amounts of data can be sent as an array, XML, or JSON rather than separate XHRs. This data can be deleted from, saved to, or inserted into the database as one SQL query. Imagine that we have a number of items we want to save to the database. These items could be user attributes, for example, such as names, descriptions, and so on. In most examples, requests are made with each of these attributes as separate key/value pairs. This solution works fine for simple XHRs that need to send only small amounts of data, but if we wanted to send data for multiple users, it would be much more efficient to send it as a bulk update. Let's look at three different ways that we could send bulk data to the server via an XHR.

Sending Arrays

Sending bulk updates in the form of an array follows the same pattern as the other Ajax requests that we have made throughout the book. The difference with passing arrays is that instead of sending one value per key, we will send an array (or comma-delimited list) of data per key. In the user data example, which I introduced previously, we may want to update multiple users through one request. In order to do this, we could pass keys with arrays as the key values. For example, we could pass an array of names paired with a name key or an array of descriptions paired with a description key. This part is fairly simplethe data simply needs to be pushed into an array on the client side with JavaScript, concatenated into a query string, and sent via an XHR to the server. Our first hurdle arises because a JavaScript array does not translate directly to an array in a server-side language, but this can be easily fixed. By taking what is now a comma-delimited list in our server-side language of choice, we can split the data into a new array and iterate the values in order to add, update, or remove them from the database. By approaching the request in this fashion, we can see that there are some direct correlations between a query string and a SQL queryit is simply a matter of how we are applying the query that counts. Let's use the following HTTP query as an example.

serviceConnector.php?object=DatabaseManager&method=parseArray& name=Grace,Pi&description=Hopper,Sheng


As you can see, we have a name and a description key with corresponding values. As an example, we will act as if we are going to insert this data into a database with a method, which simply makes queries directly on our database based on the HTTP queries that were sent to it. We will first need to abstract our serviceConnector.php file a bit in order to simply send the HTTP query data directly to our handling method, rather than send the data as we did in the previous chapter.

<?php header("Content-Type: application/xml; charset=UTF-8"); require_once("classes/UserManager.class.php"); $o = new $_GET['object'](); echo $o->$_GET['method']( $_GET ); ?>


We are simply passing the query data as a whole rather than splitting it or sending a specific key. The following is an example of how we would write the method that will make the connection between our Ajax engine and our server-side object.

[View full width]

<?php class DatabaseManager { public function parseArray($q) { $names = split(",", $q['name']); $descriptions = split(",", $q['description']); // Open database connection for($i=0; $i<count($names); $i++) { $query = "INSERT INTO myTable SET name='$names[$i]', description='$description s[$i]'"; // Close database connection } return "<response>". $q['name'] ." and ". $q['description'].": were successfully retrieved!</response>"; } } ?>


This option is very easy to manage and, even though it is not the best solution, it can provide us with some powerful ways of manipulating bulk data.

Sending XML

Sending XML or JSON is slightly different from sending a simple array because the data can be much more complex and therefore achieve results on a larger scale. In this section, we will focus on sending XML as our request data and cover some of the possibilities that can be reached with this approach. As we have seen throughout the samples in this book, XML is a flexible language that can be used to encapsulate static or dynamic data as any custom structure that we define. This, of course, leaves a lot of room for interpretation and allows complete flexibility in our code, which enables us to exchange complex data types between different languages. This is why XML lends much more power to our requests than passing a simple array or other custom character-delimited lists. The following is an example of a simple query that can be sent from any application to pass data as XML to be used on our server side based on the object and method specified.

serviceConnector.php?object=DatabaseManager&method=parseXML&xml=<names> <name>Kris Hadlock</name></names>


After the sample is set on your server, you can simply paste this query on the end of your URL and you will receive the response that we will now construct.

[View full width]

<?php class DatabaseManager { public function parseXML($q) { $doc = new DOMDocument(); $doc->loadXML($q['xml']); // Manipulate the database return "<response>". $doc->getElementsByTagName('name')->item(0)->nodeValue .": was successfully retrieved!</response>"; } } ?>


This method will have been called based on the query that took place via the serviceConnector. In this method, I am simply showing you how to load the XML and parse the data we have sent to it. Of course, this method can be much more complex because it can receive XML, save, insert, or delete node values in the database, and so on. After the data has been used to manipulate the database, we can take the values and respond to the requestor.

Sending JSON

JSON is a great solution for JavaScript and lends a lot of power to Ajax requests, but it is not as easy to manage on the server side because we must implement or create our own parser depending on the language we choose to use. Of course, our job is made much easier if we implement one of the parsers available at http://www.json.org. If you are interested in using JSON as your data-interchange format with Ajax, I recommend taking a look at the examples found on the download sites to really get an idea of the functionality that can be accomplished.



Ajax for Web Application Developers
Ajax for Web Application Developers
ISBN: 0672329123
EAN: 2147483647
Year: 2007
Pages: 129
Authors: Kris Hadlock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net