<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dimuthu's Blog &#187; SQL</title>
	<atom:link href="http://www.dimuthu.org/catagory/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>WSO2 Releases Goernance Registry 3.0.1, ESB 2.1.1, WSAS 3.1.1, IS 2.0.1 AND Mashup Server 2.0.0</title>
		<link>http://www.dimuthu.org/blog/2009/10/13/wso2-releases-goernance-registry-3-0-1-esb-2-1-1-wsas-3-1-1-is-2-0-1-and-mashup-server-2-0-0/</link>
		<comments>http://www.dimuthu.org/blog/2009/10/13/wso2-releases-goernance-registry-3-0-1-esb-2-1-1-wsas-3-1-1-is-2-0-1-and-mashup-server-2-0-0/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 02:08:07 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[carbon]]></category>
		<category><![CDATA[esb]]></category>
		<category><![CDATA[Governance]]></category>
		<category><![CDATA[Governance Registry]]></category>
		<category><![CDATA[mashup server]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsas]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[identity]]></category>
		<category><![CDATA[mashup]]></category>
		<category><![CDATA[registry]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=1022</guid>
		<description><![CDATA[WSO2 announced an another round of release of their famous SOA products. WSO2 Governance Registry 3.0.1 WSO2 ESB 2.1.1 WOS2 Web Services Application Server (WSAS) 3.1.1 WSO2 Identity Server (IS) 2.0.1 WSO2 Mashup Server 2.0.0 Although the version numbers say this is minor patch release (Other than the Mashup Server which is shipping as a [...]]]></description>
			<content:encoded><![CDATA[<p>WSO2 announced an another round of release of their famous SOA products.</p>
<ul>
<li><a href="http://wso2.org/projects/governance-registry">WSO2 Governance Registry 3.0.1</a></li>
<li><a href="http://wso2.org/projects/esb/java">WSO2 ESB 2.1.1</a></li>
<li><a href="http://wso2.org/projects/wsas/java">WOS2 Web Services Application Server (WSAS) 3.1.1</a></li>
<li><a href="http://wso2.org/projects/identity">WSO2 Identity Server (IS) 2.0.1</a></li>
<li><a href="http://wso2.org/projects/mashup">WSO2 Mashup Server 2.0.0</a></li>
</ul>
<p>Although the version numbers say this is minor patch release (Other than the Mashup Server which is shipping as a major release), in fact there are new features and improvements. Some basic new features shares among all of these products are</p>
<ol>
<li>Improved registry level transaction Support.</li>
<li>Improved Support for deploying on top of Application Servers other than tomcat like WebSphere, WebLogic, and<br />
JBoss.</li>
<li>Support for Eclipse P2 based provisioning. (Yes, you can add/remove features from these WSO2 products , see <a href="https://wso2.org/wiki/display/carbon/p2-based-provisioning-support">https://wso2.org/wiki/display/carbon/p2-based-provisioning-support</a> for more details)</li>
<li>Improved Remote Registry model</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/10/13/wso2-releases-goernance-registry-3-0-1-esb-2-1-1-wsas-3-1-1-is-2-0-1-and-mashup-server-2-0-0/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Making Good SOA Great</title>
		<link>http://www.dimuthu.org/blog/2009/01/31/making-good-soa-great/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/31/making-good-soa-great/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 13:36:10 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[carbon]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[enterprise]]></category>
		<category><![CDATA[OSGI]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=912</guid>
		<description><![CDATA[WSO2 is preparing for the first major release of their enterprise java product series after adapting the OSGI technology. You can already try out the betas from the wso2.org site. WSO2 Web Services Application Server (WSAS) WSO2 Enterprise Service Bus (ESB) WSO2 Registry WSO2 Business Process Server (BPS) With the power of OSGI you will [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wso2.com/">WSO2</a> is preparing for the first major release of their enterprise java product series after adapting the OSGI technology. You can already try out the betas from the <a href="http://wso2.org/projects/carbon">wso2.org</a> site.</p>
<ol>
<li><a href="http://wso2.org/projects/wsas/java">WSO2 Web Services Application Server (WSAS)<br />
</a></li>
<li><a href="http://wso2.org/projects/esb/java">WSO2 Enterprise Service Bus (ESB) </a></li>
<li><a href="http://wso2.org/projects/registry">WSO2 Registry </a></li>
<li><a href="http://wso2.org/projects/bps">WSO2 Business Process Server (BPS) </a></li>
</ol>
<p>With the power of OSGI you will be able to customize these products for your need just by mixing and matching the components within these products. If you like to learn more about this, just have a loot at the following ebook released by WSO2.</p>
<div id="attachment_913" class="wp-caption aligncenter" style="width: 310px"><a href="http://wso2.org/project/carbon/making_good_soa_great.pdf"><img class="size-medium wp-image-913" title="Making Good SOA Great" src="http://www.dimuthu.org/wp-content/uploads/2009/01/ebook-300x249.png" alt="Making Good SOA Great" width="300" height="249" /></a><p class="wp-caption-text">Making Good SOA Great</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/31/making-good-soa-great/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Article Introducing PHP Data Services</title>
		<link>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 13:06:55 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[WS-*]]></category>
		<category><![CDATA[ws-security]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=900</guid>
		<description><![CDATA[Now you can view the article I wrote titling &#8220;Introduction to PHP Data Services&#8220;. There I explain how you can design and implement Data Services in PHP using WSF/PHP Data Services Library. This article covers, Designing your Data Service API. Writing the Data Service. Deploying and Testing Data Service. Make the Data Service available in [...]]]></description>
			<content:encoded><![CDATA[<p>Now you can view the article I wrote titling &#8220;<a href="http://wso2.org/library/articles/introduction-php-data-services">Introduction to PHP Data Services</a>&#8220;. There I explain how you can design and implement Data Services in PHP using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> Data Services Library.</p>
<p>This article covers,</p>
<ol>
<li>Designing your Data Service API.</li>
<li>Writing the Data Service.</li>
<li>Deploying and Testing Data Service.</li>
<li>Make the Data Service available in both SOAP and RESTful form.</li>
<li>Use of WS-* features in your Data Service.</li>
</ol>
<p>If you are thinking of adapting SOA in to your database backed PHP applications, this article will be a good starting point.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/05/article-introducing-php-data-services/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Services Best Practices</title>
		<link>http://www.dimuthu.org/blog/2008/12/18/data-services-best-practices/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/18/data-services-best-practices/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 17:39:48 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[data services]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[best practises]]></category>
		<category><![CDATA[coupling]]></category>
		<category><![CDATA[granularity]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[wsf/php]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=803</guid>
		<description><![CDATA[In simple term, data services are exposing data as web services. Anyway it is not a complete definition. Actually there are situations where we use data services not only to read data, but also to create, update or delete data. So it is better say data services are doing CRUD (Create, Read, Update, Delete) operations [...]]]></description>
			<content:encoded><![CDATA[<p>In simple term, data services are exposing data as web services. Anyway it is not a complete definition. Actually there are situations where we use data services not only to read data, but also to create, update or delete data. So it is better say data services are doing CRUD (Create, Read, Update, Delete) operations for data through web services. Simply it is like providing a web service interface for the database.</p>
<p>Anyway exposing  a database directly as a web service is like violating the first principles of software engineering. It will tightly couple the database structure with the interface, so whenever you do a simple change to the database schema, you will have to change the web service interface which will no doubt break all the clients depending on it.</p>
<p>So first of all, you have to design the service interface independent of the database schema you have. Most of the time you will able to find some query that would map the service interface to the database schema.</p>
<p>For an example think of publishing data in database table (say for table name &#8220;Games&#8221;) like this.</p>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="7">Teams</th>
</tr>
<tr style="background:#cccccc">
<td>GameID</td>
<td>Venue</td>
<td>Date</td>
<td>Team1</td>
<td>Team2</td>
<td>Team1Score</td>
<td>Team2Score</td>
</tr>
<tr>
<td>1</td>
<td>xxx stadium</td>
<td>2008-12-18</td>
<td>Italy</td>
<td>Sweden</td>
<td>34</td>
<td>33</td>
</tr>
<tr>
<td>2</td>
<td>yyy stadium</td>
<td>2008-12-19</td>
<td>France</td>
<td>Spain</td>
<td>51</td>
<td>50</td>
</tr>
</tbody>
</table>
<p>You will directly able to map these data to data service. So the response payload for a &#8220;getGames&#8221; operation would be something like,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;getGamesResponse<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         xxx stadium
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         2008-12-18
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Italy
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Sweden
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          34
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          33
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         yyy stadium
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         2008-12-19
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          France
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Spain
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          51
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          50
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/</span></span><span><span><span style="color: #009900;"><span style="font-weight: bold; color: black;">getGamesResponse</span></span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;"><span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You can get this done with a SQL query simply as this,</p>
<pre class="sql"><span style="font-weight: bold; color: #993333;">SELECT</span> * <span style="font-weight: bold; color: #993333;">FROM</span> <span style="color: #ff0000;">`Games`</span></pre>
<p>Say later if you decided to restructure the database table so the new database schema would be like this,</p>
<pre>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="3">Games</th>
</tr>
<tr style="background:#cccccc">
<td>GameId</td>
<td>Venue</td>
<td>Date</td>
</tr>
<tr>
<td>1</td>
<td>xxx stadium</td>
<td>2008-12-18</td>
</tr>
<tr>
<td>1</td>
<td>yyy stadium</td>
<td>2008-12-19</td>
</tr>
</tbody>
</table>
</pre>
<pre>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="3">GamesTeams</th>
</tr>
<tr style="background:#cccccc">
<td>GameId</td>
<td>TeamId</td>
<td>score</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>70</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>33</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>51</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td>50</td>
</tr>
</tbody>
</table>
</pre>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="3">Teams</th>
</tr>
<tr style="background:#cccccc">
<td>TeamId</td>
<td>Name</td>
<td>Coach</td>
</tr>
<tr>
<td>1</td>
<td>Italy</td>
<td>Mr. ABC</td>
</tr>
<tr>
<td>1</td>
<td>Canada</td>
<td>Mr. PQR</td>
</tr>
<tr>
<td>2</td>
<td>Spain</td>
<td>Mr. XYZ</td>
</tr>
</tbody>
</table>
<p><em>(Note here the Games and Teams are associated in the GamesTeams table.)</em></p>
<p>You can still use a query like the one in following to provide the same service interface, because it returns the same result set as the earlier one.</p>
<pre class="sql">   <span style="font-weight: bold; color: #993333;">SELECT</span> Games.Venue,
              Games.Date,
              Team1.Name <span style="font-weight: bold; color: #993333;">AS</span> Team1,
              Team2.Name <span style="font-weight: bold; color: #993333;">AS</span> Team2,
              GameTeam1.Score <span style="font-weight: bold; color: #993333;">AS</span> Score1,
              GameTeam2.Score <span style="font-weight: bold; color: #993333;">AS</span> Score2
         <span style="font-weight: bold; color: #993333;">FROM</span> Teams Team1,
              Teams Team2,
              GamesTeams GameTeam1,
              GamesTeams GameTeam2,
              Games
        <span style="font-weight: bold; color: #993333;">WHERE</span> GameTeam1.gameId = Games.gameId  <span style="font-weight: bold; color: #993333;">AND</span>
              GameTeam2.gameId = Games.gameId <span style="font-weight: bold; color: #993333;">AND</span>
              GameTeam1.teamId = Team1.teamId <span style="font-weight: bold; color: #993333;">AND</span>
              GameTeam2.teamId = Team2.teamId <span style="font-weight: bold; color: #993333;">AND</span>
              Team1.teamId &lt;&gt; Team2.teamId <span style="font-weight: bold; color: #993333;">AND</span>
              Team1.name=?</pre>
<p>This allows you to keep the service interface unchanged, regardless of the changes you done to the database schema.</p>
<p>In addition to that, we can uses the features of the data service libraries to give meaningful names for the response elements. If we take above example itself, say you want to rename &#8216;Score1&#8242; to &#8216;Team1-Score&#8217; and &#8216;Score2&#8242; to &#8216;Team2-Score&#8217;. But the dash character (&#8216;-&#8217;) cannot be used as a variable in a database query. But you can provide that in the map of sql name to element name when you are writing the data service.</p>
<p>If you are using <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> php data services Here is how you provide that mapping,</p>
<pre class="php"><span style="color: #0000ff;">$outputFormat</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"resultElement"</span> =&gt; <span style="color: #ff0000;">"getGamesResponse"</span>,
                      <span style="color: #ff0000;">"rowElement"</span> =&gt; <span style="color: #ff0000;">"game"</span>,  <span style="font-style: italic; color: #808080;">// this is the repeating wrapper element for each game</span>
                      <span style="color: #ff0000;">"elements"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span> <span style="color: #ff0000;">"Venue"</span> =&gt; <span style="color: #ff0000;">"Venue"</span>, <span style="font-style: italic; color: #808080;">// this is the mapping of xml name =&gt; sql name</span>
                                           <span style="color: #ff0000;">"Date"</span> =&gt; <span style="color: #ff0000;">"Date"</span>,
                                           <span style="color: #ff0000;">"Team1"</span> =&gt; <span style="color: #ff0000;">"Team1"</span>,
                                           <span style="color: #ff0000;">"Team2"</span> =&gt; <span style="color: #ff0000;">"Team2"</span>,
                                           <span style="color: #ff0000;">"Team1-score"</span> =&gt; <span style="color: #ff0000;">"Score1"</span>, <span style="font-style: italic; color: #808080;">// we are using different names for sql and xml</span>
                                           <span style="color: #ff0000;">"Team2-score"</span> =&gt; <span style="color: #ff0000;">"Score2"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>If you write it using <a href="http://wso2.org/projects/solutions/data-services/java">Java Data Services</a>, you can use do this with the following configuration xml,</p>
<pre class="xml">        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;result</span> <span style="color: #000066;">element</span>=<span style="color: #ff0000;">"getGamesResponse"</span> <span style="color: #000066;">rowName</span>=<span style="color: #ff0000;">"game"</span><span style="font-weight: bold; color: black;">&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Venue"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Venu"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Date"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Date"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team1"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Team1"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team2"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Team2"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team1-Score"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Score1"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
            <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Team2-Score"</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">"Score2"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/result<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Another consideration, when talking about the best practices of designing web services or data services is the granularity of the service interface. We say a web service is fine grained if the service contains tons of small operations. The opposite of that is the coarse grained services, which contains large but few operations. And it is recommended to use the later approach, you can see why from the following example.</p>
<p>Lets say you are developing a web service to upload some information. In order to upload the information first the user have to be authenticated and then the content should be validated. Then only he can submit the actual information. Say you design a fine grained web service for that. So it has three operations.</p>
<ol>
<li>authenticateUser</li>
<li>validateContent</li>
<li>submitInformation</li>
</ol>
<p>If you design a coarse grained service interface it will be just the &#8216;uploadInformation&#8217; operation. And all the three operation defined earlier will be called within the service logic and the clients will not be aware of that.</p>
<p>So here are some disadvantages that I see in the fine grained interface design compared with coarse grained design.</p>
<ol>
<li>The coupling is too high. Since the client is linked with the service in three adapters. Say you decided to change the service so that you first validate the content and depending on the content it sometime bypasses the authentication. You can&#8217;t do this with the former approach (fine grained approach) without changing the clients. But if it were designed as a coarse grained service (just one uploadInformation operation), the client need not to be changed.</li>
<li>Two much time consumed for the transmission. Since we used three web service calls to do a single task the latency of the operation will largely depend on the network latency which will no doubt is comparatively very low. So the performance of the operation is degraded.</li>
<li>The clients can bypass some steps!, Say in your fine grained service, some client bypass the authentication and validateContent steps and jump directly to the sumbitContent operation. In fact you have to write special code to make sure the clients call the service in the correct sequence, otherwise it will be a big security hole.</li>
</ol>
<p>So I think these three points will be enough to explain why you should try to design a coarse grained interface for your web service.</p>
<p>You can apply these principle when you write data services as well. Lets take the same example explained above. Say there is another table that keep the scoring shots of each game like the one in following.</p>
<table border="1">
<tbody>
<tr>
<th style="background:#cccccc" colspan="7">ScoringShots</th>
</tr>
<tr style="background:#cccccc">
<td>GameID</td>
<td>Time</td>
<td>ScoringTeam</td>
<td>ScoringPlayer</td>
</tr>
</tbody>
</table>
<p>Here also you can keep the operation like &#8216;getGames&#8217; which we defined in the above section. That operation only provide the basic information like the winner and the scores. So if the clients want to know about scoring shots as well, he have to call another operation, say getScoringShots(game) that will return the scoring shots results for a given game. If in practice the clients only need to know about scoring shots of few selected games, then this approach is ok.<br />
But say normally clients need to know about the scoring shots of each and every games. Then they have to call the operation &#8216;getScoringShots&#8217; multiple times. That&#8217;s when the lesson we just learn about the granularity can be applied. We can actually provide another operation, say &#8216;getGamesDetailed&#8217; that actually bundle the details of scoring shots for all games with other information about the game. Here is snip of the response XML, I&#8217;m talking about.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;getGamesDetailedResponse<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         xxx stadium
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Venue<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
         2008-12-18
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/date<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Italy
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          Sweden
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          34
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team1Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          33
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Team2Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- additionally we have ScoringShots element --&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringShots<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Time<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Time<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Score<span style="font-weight: bold; color: black;">&gt;</span></span></span> xxx <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Time<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Time<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringPlayer<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringTeam<span style="font-weight: bold; color: black;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Score<span style="font-weight: bold; color: black;">&gt;</span></span></span> yyy <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Score<span style="font-weight: bold; color: black;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Shot<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ScoringShots<span style="font-weight: bold; color: black;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>

   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- Another game details are mentioned here --&gt;</span></span>
   <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Game<span style="font-weight: bold; color: black;">&gt;</span></span></span>

   <span style="color: #009900;"><span style="font-style: italic; color: #808080;">&lt;!-- More Games --&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/getGamesDetailedResponse<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>You can generate this kind of response using the nested query support of the data services libraries.<br />
You can checkout more details about nested queries in php data services from <a href="http://www.dimuthu.org/blog/2008/08/04/nested-queries-with-php-data-services/">my old post about php data service API.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/18/data-services-best-practices/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP Data Services with PostgreSQL</title>
		<link>http://www.dimuthu.org/blog/2008/12/07/php-data-services-with-postgresql/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/07/php-data-services-with-postgresql/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 12:56:19 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[2 minutes guide]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[WSDL]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[data services]]></category>
		<category><![CDATA[DataService]]></category>
		<category><![CDATA[pdo_pgsql]]></category>
		<category><![CDATA[pgsql]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=771</guid>
		<description><![CDATA[LAMP (Linux + Apache + Mysql + PHP) stack powers many servers in the Internet today. For a LAMP  server, PostgreSQL could be the first alternative to Mysql. Similar to PHP + MySQL,  PHP + PostgreSQL too can be easily used in to host data services. Here are the steps to do it. If you [...]]]></description>
			<content:encoded><![CDATA[<p>LAMP (Linux + Apache + Mysql + PHP) stack powers many servers in the Internet today. For a LAMP  server, PostgreSQL could be the first alternative to Mysql. Similar to PHP + MySQL,  PHP + PostgreSQL too can be easily used in to host data services. Here are the steps to do it.</p>
<ol>
<li> If you already don&#8217;t have Apache + PHP + PostgreSQL download them from the following locations. Apache &#8211; <a href="http://httpd.apache.org/download.cgi">http://httpd.apache.org/download.cgi</a>, PHP &#8211; <a href="http://php.net">http://php.net</a> and PostgreSQL &#8211; <a href="http://www.postgresql.org/download/">http://www.postgresql.org/download/</a></li>
<li> You have to enable the PHP pdo_pgsql, pdo and pgsql plugins. Read <a href="http://www.php.net/manual/en/pgsql.setup.php">here </a>for the instructions to setup these libraries. (For an example: if you are windows you have to set the system &#8216;PATH&#8217; variable to the &lt;postgresql_installed_dir&gt;/bin directory.</li>
<li> If you already don&#8217;t have <a href="http://wso2.org/projects/wsf/php">WSF/PHP,</a> download and install it according to the guidelines provided in <a href="http://wso2.org/project/wsf/php/2.0.0/docs/install_guide.html">wsf/php installation guide</a>.<em>NOTE: You can check pdo_pgsql and wsf/php has properly installed with the help of phpinfo() function.</em></li>
<li> Now lets start with creating a sample Database table. For this example I created a database called &#8216;workshop&#8217;, schema called &#8216;workshop&#8217; and inside there the table &#8216;Employee&#8217; with the following schema.<br />
<table border="1">
<tbody>
<tr style="background: #dddddd">
<td>Column Name</td>
<td>Column Type</td>
</tr>
<tr>
<td>employId</td>
<td>integer</td>
</tr>
<tr>
<td>name</td>
<td>character varying</td>
</tr>
<tr>
<td>email</td>
<td>character varying</td>
</tr>
<tr>
<td>jobTitle</td>
<td>character varying</td>
</tr>
<tr>
<td>project</td>
<td>character varying</td>
</tr>
</tbody>
</table>
<p><em>Note: You can use <a href="http://phppgadmin.sourceforge.net/">phpPgAdmin</a> (web based) or <a href="http://www.pgadmin.org/">pgAdmin III </a>to create tables from GUI </em></li>
<li> Then you can write a small php script to expose the data in the above table as a web service.
<pre class="php"><span style="font-weight: bold; color: #000000;">&lt;?php</span>

<span style="font-style: italic; color: #808080;">//Including the Data Services library</span>
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"wso2/DataServices/DataService.php"</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// Including the connection information (i.e. PGSQUL USERNAME</span>
<span style="font-style: italic; color: #808080;">// and PGSQL_PASSWORD) for my PGSQL Connection</span>
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"constants.php"</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// database configurations</span>
<span style="color: #0000ff;">$config</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
		<span style="color: #ff0000;">"db"</span> =&gt; <span style="color: #ff0000;">"pgsql"</span>,
		<span style="color: #ff0000;">"username"</span> =&gt; PGSQL_USERNAME,
		<span style="color: #ff0000;">"password"</span> =&gt; PGSQL_PASSWORD,
		<span style="color: #ff0000;">"dbname"</span> =&gt; <span style="color: #ff0000;">"workshop"</span>,
		<span style="color: #ff0000;">"dbhost"</span> =&gt; <span style="color: #ff0000;">"localhost"</span>
		<span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$output_format</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
                    <span style="color: #ff0000;">"resultElement"</span> =&gt; <span style="color: #ff0000;">"employees"</span>,
                    <span style="color: #ff0000;">"rowElement"</span> =&gt; <span style="color: #ff0000;">"employee"</span>,
                    <span style="color: #ff0000;">"elements"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
			    <span style="color: #ff0000;">"id"</span> =&gt; <span style="color: #ff0000;">"employeeId"</span>,
                            <span style="color: #ff0000;">"name"</span> =&gt; <span style="color: #ff0000;">"name"</span>,
                            <span style="color: #ff0000;">"email"</span> =&gt; <span style="color: #ff0000;">"email"</span>,
                            <span style="color: #ff0000;">"jobTitle"</span> =&gt; <span style="color: #ff0000;">"jobTitle"</span>,
                            <span style="color: #ff0000;">"project"</span> =&gt; <span style="color: #ff0000;">"project"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"SELECT * FROM workshop.Employees"</span>;

<span style="color: #0000ff;">$get_employees_op</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"outputFormat"</span> =&gt; <span style="color: #0000ff;">$output_format</span>, <span style="color: #ff0000;">"sql"</span> =&gt; <span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$get_employees_url</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"HTTPMethod"</span> =&gt; <span style="color: #ff0000;">"GET"</span>, <span style="color: #ff0000;">"RESTLocation"</span> =&gt; <span style="color: #ff0000;">"employees"</span><span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// list of operations</span>
<span style="color: #0000ff;">$operations</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
                <span style="color: #ff0000;">"getEmployees"</span> =&gt; <span style="color: #0000ff;">$get_employees_op</span>,
                <span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// list of rest url mappping (operation =&gt; url)</span>
<span style="color: #0000ff;">$restmap</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
                <span style="color: #ff0000;">"getEmployees"</span> =&gt; <span style="color: #0000ff;">$get_employees_url</span>,
                <span style="color: #66cc66;">)</span>;

<span style="font-style: italic; color: #808080;">// creating DSService and reply</span>
<span style="color: #0000ff;">$service</span> = <span style="font-weight: bold; color: #000000;">new</span> DataService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
         <span style="color: #ff0000;">"config"</span> =&gt; <span style="color: #0000ff;">$config</span>,
         <span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span>, <span style="color: #ff0000;">"RESTMapping"</span>=&gt;<span style="color: #0000ff;">$restmap</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #0000ff;">$service</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
<pre class="php"><span style="font-weight: bold; color: #000000;">?&gt;</span></pre>
</li>
<li> We just wrote a PostgreSQL Data Services that provides its service as both REST and SOAP form. To deploy this service, We just need to copy this in to the web root directory. And the web URL for the script will be the endpoint to the web service.</li>
<li>We can test the service either by calling its SOAP interface, which we may need to write a small SOAP client or by calling its REST interface, which only need a GET request from the browser. Say my script name is &#8220;my_dataservice.php&#8221; and I&#8217;ve put it in the web root directory, then the URL to call the REST interface of the service is
<pre>http://localhost/my_dataservice.php/employees</pre>
</li>
</ol>
<p><strong>WSDL Generation for PostgreSQL Data Service</strong><br />
You can get the WSDL for the service from the URL formed adding the suffix &#8220;?wsdl&#8221; (or &#8220;?wsdl2&#8243; to wsdl v2.0)  to the service URL,</p>
<pre>http://localhost/my_dataservice.php?wsdl</pre>
<p>Here all the schema data types are shown as xsd:anyType, which may not be the behavior that you want. In fact you can provide the schema data types to the fields from the code itself. Lets change the $outputFormat variable to provide the schema information as well using the following code snip.</p>
<pre class="php"><span style="color: #0000ff;">$output_format</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
                    <span style="color: #ff0000;">"resultElement"</span> =&gt; <span style="color: #ff0000;">"employees"</span>,
                    <span style="color: #ff0000;">"rowElement"</span> =&gt; <span style="color: #ff0000;">"employee"</span>,
                    <span style="color: #ff0000;">"elements"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
			    <span style="color: #ff0000;">"id"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"column"</span> =&gt; <span style="color: #ff0000;">"employeeId"</span>,
		    			  <span style="color: #ff0000;">"xsdType"</span> =&gt; <span style="color: #ff0000;">"xsd:int"</span><span style="color: #66cc66;">)</span>,
			    <span style="color: #ff0000;">"name"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"column"</span> =&gt; <span style="color: #ff0000;">"name"</span>,
		    			  <span style="color: #ff0000;">"xsdType"</span> =&gt; <span style="color: #ff0000;">"xsd:string"</span><span style="color: #66cc66;">)</span>,
			    <span style="color: #ff0000;">"email"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"column"</span> =&gt; <span style="color: #ff0000;">"email"</span>,
		    			  <span style="color: #ff0000;">"xsdType"</span> =&gt; <span style="color: #ff0000;">"xsd:string"</span><span style="color: #66cc66;">)</span>,
			    <span style="color: #ff0000;">"jobTitle"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"column"</span> =&gt; <span style="color: #ff0000;">"jobTitle"</span>,
		    			  <span style="color: #ff0000;">"xsdType"</span> =&gt; <span style="color: #ff0000;">"xsd:string"</span><span style="color: #66cc66;">)</span>,
			    <span style="color: #ff0000;">"project"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"column"</span> =&gt; <span style="color: #ff0000;">"project"</span>,
		    			  <span style="color: #ff0000;">"xsdType"</span> =&gt; <span style="color: #ff0000;">"xsd:string"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>Note that you provide the xsd type for each field explicitly. In fact this change is not needed for mysql pdo extension since it allows identifying field types programatically. Since this feature is not available in all the other pdo drivers, we have to explicitly give xsd type information for them.</p>
<p>If you wan to provide data services with SQLite or MSSQL, You can check my other posts on <a href="http://www.dimuthu.org/blog/2008/10/04/mssqlmicrosoft-sql-data-services-in-php/">MSSQL(Microsoft SQL) Data Services In PHP</a> and <a href="http://www.dimuthu.org/blog/2008/10/05/data-services-with-sqlite-in-php/">Data Services with SQLite in PHP</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/07/php-data-services-with-postgresql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Steps to Convert a SQL Query to a Data Service</title>
		<link>http://www.dimuthu.org/blog/2008/11/27/steps-to-convert-sql-query-to-a-data-service/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/27/steps-to-convert-sql-query-to-a-data-service/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 17:48:31 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[data service]]></category>
		<category><![CDATA[sql query]]></category>
		<category><![CDATA[Web Service]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=741</guid>
		<description><![CDATA[With WSF/PHP Data Service library, you can convert a SQL query to a Data Service very easily in few steps. Decide your SQL query first, For the query you may require some input parameters, and you have to decide what should be returned by the query, Say your query is $sql_query = "SELECT name, age, [...]]]></description>
			<content:encoded><![CDATA[<p>With <a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> Data Service library, you can convert a SQL query to a Data Service very easily in few steps.</p>
<ol>
<li>Decide your SQL query first, For the query you may require some input parameters, and you have to decide what should be returned by the query, Say your query is
<pre><span style="color: #0000ff;">$sql_query</span> = <span style="color: #ff0000;">"SELECT name, age, email FROM users where country = ?"</span>;</pre>
<p>Then the &#8220;country&#8221; is our input parameter and the name, age, email are our return values.</li>
<li>Define the input Format. For the above query it will be something like,
<pre class="php"><span style="color: #0000ff;">$inputFormat</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"country"</span> =&gt; <span style="color: #ff0000;">"INT"</span><span style="color: #66cc66;">)</span>;</pre>
</li>
<li>Define the output format. We are giving the name &#8220;Users&#8221; for the out most wrapper element and the name &#8220;user&#8221; for the wrapper element of each user. Here is how we define it,
<pre class="php"><span style="color: #0000ff;">$outputFormat</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"resultElement"</span> =&gt; <span style="color: #ff0000;">"users"</span>,
                <span style="color: #ff0000;">"rowElement"</span> =&gt; <span style="color: #ff0000;">"user"</span>,
                <span style="color: #ff0000;">"elements"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
                            <span style="color: #ff0000;">"name"</span> =&gt; <span style="color: #ff0000;">"name"</span>,
                            <span style="color: #ff0000;">"age"</span> =&gt; <span style="color: #ff0000;">"age"</span>,
                            <span style="color: #ff0000;">"email"</span> =&gt; <span style="color: #ff0000;">"email"</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
<p>For this output format, our expected result payload would be like,</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;users<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;user<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span>xxx<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;age<span style="font-weight: bold; color: black;">&gt;</span></span></span>23<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/age<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;email<span style="font-weight: bold; color: black;">&gt;</span></span></span>xxx@xxx.xx<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/email<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/user<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;user<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span>yyy<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;age<span style="font-weight: bold; color: black;">&gt;</span></span></span>23<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/age<span style="font-weight: bold; color: black;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;email<span style="font-weight: bold; color: black;">&gt;</span></span></span>yyy@yyy.yy<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/email<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/user<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  ....
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/users<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
</li>
<li> Define your operation using the sql query, input and output formats.
<pre class="php"><span style="color: #0000ff;">$operations</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"getUsersByCountry"</span> =&gt; <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
                              <span style="color: #ff0000;">"inputFormat"</span> =&gt; <span style="color: #0000ff;">$inputFormat</span>,
                              <span style="color: #ff0000;">"sql"</span> =&gt; <span style="color: #0000ff;">$sql_query</span>,
                              <span style="color: #ff0000;">"outputFormat"</span> =&gt; <span style="color: #0000ff;">$outputFormat</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;</pre>
</li>
<li> Define your database configuration in an array like this,
<pre class="php"><span style="color: #0000ff;">$config</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
    <span style="color: #ff0000;">"db"</span> =&gt; <span style="color: #ff0000;">"mysql"</span>, <span style="font-style: italic; color: #808080;">// your db engine</span>
    <span style="color: #ff0000;">"username"</span> =&gt; <span style="color: #ff0000;">"myname"</span>, <span style="font-style: italic; color: #808080;">// name &amp; password for the db server</span>
    <span style="color: #ff0000;">"password"</span> =&gt; <span style="color: #ff0000;">"mypasswd"</span>,
    <span style="color: #ff0000;">"dbname"</span> =&gt; <span style="color: #ff0000;">"db"</span>, <span style="font-style: italic; color: #808080;">// the db</span>
    <span style="color: #ff0000;">"dbhost"</span> =&gt; <span style="color: #ff0000;">"localhost"</span><span style="color: #66cc66;">)</span>;</pre>
</li>
<li> Create a DataService instance using the database configuration and the operations we just created. And call DataServices reply method.
<pre class="php"><span style="color: #0000ff;">$ds_service</span> = <span style="font-weight: bold; color: #000000;">new</span> DataService<span style="color: #66cc66;">(</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span>
               <span style="color: #ff0000;">"config"</span> =&gt; <span style="color: #0000ff;">$config</span>,
               <span style="color: #ff0000;">"operations"</span> =&gt; <span style="color: #0000ff;">$operations</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;

<span style="color: #0000ff;">$ds_service</span>-&gt;<span style="color: #006600;">reply</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;</pre>
</li>
</ol>
<p>That is it. You just exposed your query as a web service. The PHP script URL will be the endpoint URL for the web service.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/27/steps-to-convert-sql-query-to-a-data-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RESTful URL Mapping in WSF/PHP</title>
		<link>http://www.dimuthu.org/blog/2008/11/16/restful-url-mapping-in-wsfphp/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/16/restful-url-mapping-in-wsfphp/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 17:21:49 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/php]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[data services]]></category>
		<category><![CDATA[DataService]]></category>
		<category><![CDATA[RESTfulSchool]]></category>
		<category><![CDATA[URL Mapping]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=621</guid>
		<description><![CDATA[In a RESTful design, choose of URLs for resources are really important. The URL uniquely represents a resource. Service consumers can change some parts in the URL to access different other resources. So it is clear that the URL consists of some constant parts which describe the resource group or catalog in general and some [...]]]></description>
			<content:encoded><![CDATA[<p>In a RESTful design, choose of URLs for resources are really important. The URL uniquely represents a resource. Service consumers can change some parts in the URL to access different other resources. So it is clear that the URL consists of some constant parts which describe the resource group or catalog in general and some variable parts which have different and unique values for different resources.</p>
<p>As an example look at the following URL patterns</p>
<ul>
<li>students/{name} &#8211; The constant &#8216;students&#8217; represent the students group in general and the variable &#8216;name&#8217; is used to identify each student individually.</li>
<li>students/{name}/marks/{subject} &#8211; The constants &#8216;students&#8217; and &#8216;marks&#8217; shows that this resource is a marks of some students, The two variables &#8216;name&#8217; and &#8216;subjects&#8217; addresses which student and marks of which subject is presented in the URL.</li>
</ul>
<p>You can have a look at some of the uses of such mappings from <a href="http://labs.wso2.org/wsf/php/resource_view.php?url=RESTfulSchool">RESTful School demo</a>.</p>
<p><a href="http://wso2.org/projects/wsf/php">WSF/PHP</a> allows you to create RESTful Web Services and further more RESTful Data Services in PHP.</p>
<p>In a RESTful Data Service you expose a database query as a web service. There you can write a prepared statement and feed arguments for the statement through the variable parameters of the URL. For an example take the <a href="http://labs.wso2.org/wsf/php/source_page_old.php?src=solutions%2FRESTFulSchool%2Fschool_service.php">RESTfulSchool Demo Code</a>.</p>
<p>To retrieve a particular student, we can use the following prepared statement and the URL pattern ( This URL Pattern+  HTTP &#8216;GET&#8217; method is matched to execute this query. )</p>
<pre class="php"><span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"SELECT * FROM Students where StudentName = ?"</span>

<span style="color: #0000ff;">$get_students_with_name_url</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"HTTPMethod"</span> =&gt; <span style="color: #ff0000;">"GET"</span>,
                 <span style="color: #ff0000;">"RESTLocation"</span> =&gt; <span style="color: #ff0000;">"students/{name}"</span><span style="color: #66cc66;">)</span>;</pre>
<p>So you can execute this prepared statement with the subject name &#8216;John&#8217; using the following URL.</p>
<pre><a href="http://labs.wso2.org/wsf/php/solutions/RESTFulSchool/school_service.php/students/john">http://labs.wso2.org/wsf/php/solutions/RESTFulSchool/school_service.php/students/john</a></pre>
<p>If your service is not exposing the database directly, then you have to choose the general web service API rather than the data service specific API. In there you will be able to write your business logic for publishing student information in a PHP function and expose it as a web service.</p>
<p>In such a cas,e your function is taking an argument which of type <a href="http://wso2.org/project/wsf/php/2.0.0/docs/api_content.html#message">WSMessage</a>. This structure hold an XML that contains values for all the variable parameters as in the users request URL. For an example for above REST Mapping, we can expect the following XML.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;getSubject<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span>Chemistry<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/getSubject<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>And your function to expose as a service, would be look like this,</p>
<pre class="php"><span style="font-weight: bold; color: #000000;">function</span> getSubject<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$input</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
    <span style="font-style: italic; color: #808080;">/* retrieve the subject name from the
       $input xml using simple xml */</span>

    <span style="color: #0000ff;">$input_xml</span> = <span style="font-weight: bold; color: #000000;">new</span> SimpleXMLElement<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$input</span>-&gt;<span style="color: #006600;">str</span><span style="color: #66cc66;">)</span>;

    <span style="color: #0000ff;">$subject_name</span> = <span style="color: #0000ff;">$input</span>-&gt;<span style="color: #006600;">name</span>;

    <span style="font-style: italic; color: #808080;">/* write the logic to retrieve subject information
      for the $subject_name */</span>

    ....
<span style="color: #66cc66;">}</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/16/restful-url-mapping-in-wsfphp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Use of MySQL &#8216;GROUP BY&#8217; to Derive Statistics</title>
		<link>http://www.dimuthu.org/blog/2008/11/06/use-of-mysql-group-by-to-derive-statistics/</link>
		<comments>http://www.dimuthu.org/blog/2008/11/06/use-of-mysql-group-by-to-derive-statistics/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 18:49:37 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[DataServices]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[GROUP BY]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=579</guid>
		<description><![CDATA[We use &#8216;GROUP BY&#8217; SQL construct to query the data with aggregating some rows according to a field. For an example say if your blog database store your blogs in a table call &#8216;Blog&#8217; and it has &#8216;Date&#8217; as a field. If so SELECT count(*) FROM Blog GROUP BY Date will give you a set [...]]]></description>
			<content:encoded><![CDATA[<p>We use &#8216;GROUP BY&#8217; SQL construct to query the data with aggregating some rows according to a field. For an example say if your blog database store your blogs in a table call &#8216;Blog&#8217; and it has &#8216;Date&#8217; as a field. If so</p>
<pre><span style="font-weight: bold; color: #993333;">SELECT</span> count<span style="color: #66cc66;">(</span>*<span style="color: #66cc66;">)</span> <span style="font-weight: bold; color: #993333;">FROM</span> Blog <span style="font-weight: bold; color: #993333;">GROUP</span> <span style="font-weight: bold; color: #993333;">BY</span> Date</pre>
<p>will give you a set of numbers that represent the number of blog you posted each day.</p>
<pre><span style="font-weight: bold; color: #993333;">SELECT</span> Date, count<span style="color: #66cc66;">(</span>*<span style="color: #66cc66;">)</span> <span style="font-weight: bold; color: #993333;">FROM</span> Blog <span style="font-weight: bold; color: #993333;">GROUP</span> <span style="font-weight: bold; color: #993333;">BY</span> Date</pre>
<p>will give you a map of &#8216;date&#8217; to &#8216;number of blog posted for that date&#8217; without much trouble. <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html"></a></p>
<p>Anyway the problem is most of the databases of blogs don&#8217;t just keep the &#8216;date&#8217; for a blog, rather it keep both &#8216;date and time&#8217; (say in a field called &#8216;Time&#8217;). But you still want to group by date. You may use the MySQL &#8216;DATE&#8217; function to convert the &#8216;Date and Time value&#8217; to just &#8216;Date&#8217; and use it in GROUP BY statement.</p>
<pre><span style="font-weight: bold; color: #993333;">SELECT</span> Date, count<span style="color: #66cc66;">(</span>*<span style="color: #66cc66;">)</span> <span style="font-weight: bold; color: #993333;">FROM</span> Blog <span style="font-weight: bold; color: #993333;">GROUP</span> <span style="font-weight: bold; color: #993333;">BY</span> DATE<span style="color: #66cc66;">(</span>Time<span style="color: #66cc66;">)</span></pre>
<p>If you take Drupal for a blog database, it save the time of the blog entry as a unix timestamp. So you have to derive the Date from the timestamp using the infamous FROM_UNIXTIME mysql function,</p>
<p>In Drupal the database table name to store blog is &#8216;node&#8217; and the field name to store the create time is &#8216;created&#8217; . So your query to get statistics of Drupal would be something like this.</p>
<pre><span style="font-weight: bold; color: #993333;">SELECT</span> DATE<span style="color: #66cc66;">(</span> FROM_UNIXTIME<span style="color: #66cc66;">(</span><span style="color: #ff0000;">`created`</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>, count<span style="color: #66cc66;">(</span>*<span style="color: #66cc66;">)</span>
<span style="font-weight: bold; color: #993333;">FROM</span> <span style="color: #ff0000;">`node`</span>
<span style="font-weight: bold; color: #993333;">GROUP</span> <span style="font-weight: bold; color: #993333;">BY</span> DATE<span style="color: #66cc66;">(</span> FROM_UNIXTIME<span style="color: #66cc66;">(</span><span style="color: #ff0000;">`created`</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span></pre>
<p>Rather than converting the timestamp to SQL Date format, You can convert Date to timestamp and your sql statement may look little mathematical.</p>
<pre class="sql"><span style="font-weight: bold; color: #993333;">SELECT</span> ROUND<span style="color: #66cc66;">(</span> <span style="color: #66cc66;">(</span>
UNIX_TIMESTAMP<span style="color: #66cc66;">(</span> NOW<span style="color: #66cc66;">(</span> <span style="color: #66cc66;">)</span> <span style="color: #66cc66;">)</span> - <span style="color: #ff0000;">`created`</span> <span style="color: #66cc66;">)</span> / <span style="color: #66cc66;">(</span> <span style="color: #cc66cc;">24</span> *<span style="color: #cc66cc;">60</span> *<span style="color: #cc66cc;">60</span> <span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span>, count<span style="color: #66cc66;">(</span> * <span style="color: #66cc66;">)</span>
<span style="font-weight: bold; color: #993333;">FROM</span> <span style="color: #ff0000;">`node`</span>
<span style="font-weight: bold; color: #993333;">GROUP</span> <span style="font-weight: bold; color: #993333;">BY</span> ROUND<span style="color: #66cc66;">(</span> <span style="color: #66cc66;">(</span>
UNIX_TIMESTAMP<span style="color: #66cc66;">(</span> NOW<span style="color: #66cc66;">(</span> <span style="color: #66cc66;">)</span> <span style="color: #66cc66;">)</span> - <span style="color: #ff0000;">`created`</span> <span style="color: #66cc66;">)</span> / <span style="color: #66cc66;">(</span> <span style="color: #cc66cc;">24</span> *<span style="color: #cc66cc;">60</span> *<span style="color: #cc66cc;">60</span> <span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">)</span></pre>
<p>In fact the expression &#8220;UNIX_TIMESTAMP<span style="color: #66cc66;">(</span> NOW<span style="color: #66cc66;">(</span> <span style="color: #66cc66;">)</span> <span style="color: #66cc66;">)</span> &#8211; <span style="color: #ff0000;">`created`</span> <span style="color: #66cc66;">)</span> / <span style="color: #66cc66;">(</span> <span style="color: #cc66cc;">24</span> *<span style="color: #cc66cc;">60</span> *<span style="color: #cc66cc;">60</span> <span style="color: #66cc66;">)</span>&#8221; derives a number that represent the age of the post in days.</p>
<p>So this way you can derive statistics of your data with the use of &#8216;GROUP BY&#8221; construct. The ability to write complex queries in SQL syntax like this is really useful, specially when you access a remote database through a web services (i.e. <a href="http://www.dimuthu.org/tag/data-services/">Data Services</a>) or using database drivers, you have to minimize the number of sql queries to execute as minimum as possible.</p>
<p>Here are some of the other aggregate function that you may use with GROUP BY, <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html">http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/11/06/use-of-mysql-group-by-to-derive-statistics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

