<?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>Marcus Povey &#187; query</title>
	<atom:link href="http://www.marcus-povey.co.uk/tag/query/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marcus-povey.co.uk</link>
	<description>Making the world a better place, one byte at a time...</description>
	<lastBuildDate>Mon, 06 Feb 2012 19:13:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
	<atom:link rel='hub' href='http://www.marcus-povey.co.uk/?pushpress=hub'/>
		<item>
		<title>Introducing the Elgg 1 Query object</title>
		<link>http://www.marcus-povey.co.uk/2008/06/04/introducing-the-elgg-1-query-object/</link>
		<comments>http://www.marcus-povey.co.uk/2008/06/04/introducing-the-elgg-1-query-object/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 15:47:07 +0000</pubDate>
		<dc:creator>Marcus Povey</dc:creator>
				<category><![CDATA[elgg]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[query]]></category>

		<guid isPermaLink="false">http://www.marcus-povey.co.uk/?p=40</guid>
		<description><![CDATA[Elgg 1 is introducing a new (to elgg at least) way of executing arbitrary database queries. Basically, SQL is abstracted away into a class that can be used to construct multiple different types of query through a defined interface. This provides two main advantages: The application programmer does not have to write the SQL themselves. [...]]]></description>
			<content:encoded><![CDATA[<p>Elgg 1 is introducing a new (to elgg at least) way of executing arbitrary database queries. Basically, SQL is abstracted away into a class that can be used to construct multiple different types of query through a defined interface.</p>
<p>This provides two main advantages:</p>
<ol>
<li>The application programmer does not have to write the SQL themselves.</li>
<li>It becomes considerably easier to change database back ends later down the line.</li>
</ol>
<p>In addition, we can add free stuff like access controls etc.</p>
<p>From the documentation:</p>
<blockquote><p>Query provides a framework to construct complex queries in a safer environment.</p>
<p>The usage of this class depends on the type of query you are executing, but the basic idea is to construct a query out of pluggable classes.</p>
<p>Once constructed SQL can be generated using the toString method, this should happen automatically if you pass the Query object to get_data or similar.</p>
<p>To construct a query, create a new Query() object and begin populating it with the various classes that define the various aspects of the query.</p>
<p>Notes:</p>
<ul>
<li>You do not have to specify things in any particular order, provided you specify all required components.</li>
<li>With database tables you do not have to specify your db prefix, this will be added automatically.</li>
<li>When constructing your query keep an eye on the error log &#8211; any problems will get spit out here. Note also that __toString won&#8217;t let you throw Exceptions (!!!) so these are caught and echoed to the log instead.</li>
</ul>
</blockquote>
<p>Here is an example of a select statement:</p>
<blockquote><p><code>// Construct the query<br />
$query = new Query();</code></p>
<p><code>// Say which table we're interested in<br />
$query-&gt;addTable(new TableQueryComponent("entities"));</code></p>
<p><code>// What fields are we interested in<br />
$query-&gt;addSelectField(new SelectFieldQueryComponent("entities","*"));</code></p>
<p><code>// Add access control (Default access control uses default fields on entities table.<br />
// Note that it will error without something specified here!<br />
$query-&gt;setAccessControl(new AccessControlQueryComponent());</code></p>
<p><code>// Set a limit and offset, may be omitted.<br />
$query-&gt;setLimitAndOffset(new LimitOffsetQueryComponent(10,0));</code></p>
<p><code>// Specify the order, may be omitted<br />
$query-&gt;setOrder(new OrderQueryComponent("entities", "subtype", "desc"));</code></p>
<p><code>// Construct a where query<br />
//<br />
// This demonstrates a WhereSet which lets you have sub wheres, a<br />
// WhereStatic which lets you compare a table field against a value and a<br />
// Where which lets you compare a table/field with another table/field.<br />
$query-&gt;addWhere(<br />
new WhereSetQueryComponent(<br />
array(<br />
new WhereStaticQueryComponent("entities", "subtype","=", 1),<br />
new WhereQueryComponent("entities","subtype","=", "entities", "subtype")<br />
)<br />
)<br />
);</code></p>
<p><code>get_data($query);</code></p></blockquote>
<p>Constructing a query out of classes in this way provides a lot of scope for expanding the functionality within a common framework, as well as providing a structured way of constructing complicated database queries. </p>
<p>However, if it looks too complicated, you can use about 99% of <code>Query</code>&#8216;s functionality by using the subclass <code>SimpleQuery</code>, which provides methods for performing much of the standard functionality.</p>
<div class="wsbuttons">
	<div class="shareblob facebook">
		<div class="fb-like" data-href="http://www.marcus-povey.co.uk/2008/06/04/introducing-the-elgg-1-query-object/" data-send="false" data-layout="box_count" data-width="60" data-show-faces="false" data-colorscheme="light"></div>
	</div>

	<div class="shareblob google">
		<div class="g-plusone" data-size="tall" data-href="http://www.marcus-povey.co.uk/2008/06/04/introducing-the-elgg-1-query-object/"></div>
	</div>

	<div class="shareblob twitter">
		<div class="twitter">
			<a href="https://twitter.com/share?url=http%3A%2F%2Fwww.marcus-povey.co.uk%2F2008%2F06%2F04%2Fintroducing-the-elgg-1-query-object%2F&count=vertical" class="twitter-share-button" data-lang="en">Tweet</a>
			<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
		</div>
	</div>

</div>
	]]></content:encoded>
			<wfw:commentRss>http://www.marcus-povey.co.uk/2008/06/04/introducing-the-elgg-1-query-object/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

