<?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>domas mituzas &#187; query</title>
	<atom:link href="http://mituzas.lt/tag/query/feed/" rel="self" type="application/rss+xml" />
	<link>http://mituzas.lt</link>
	<description></description>
	<lastBuildDate>Thu, 12 Aug 2010 14:09:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>My own database abstraction class</title>
		<link>http://mituzas.lt/2008/01/02/my-own-database-abstraction-class/</link>
		<comments>http://mituzas.lt/2008/01/02/my-own-database-abstraction-class/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 21:40:28 +0000</pubDate>
		<dc:creator>Domas Mituzas</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[wikitech]]></category>
		<category><![CDATA[abstraction]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[query]]></category>

		<guid isPermaLink="false">http://dammit.lt/2008/01/02/my-own-database-abstraction-class/</guid>
		<description><![CDATA[Back in 2006 July, I decided that all other database classes are not worth it, and created my own one, incorporating best features from MySQL and PHP world. It resulted in this brilliant code, which I showed to few colleagues, &#8230; <a href="http://mituzas.lt/2008/01/02/my-own-database-abstraction-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Back in 2006 July, I decided that all other database classes are not worth it, and created my own one, incorporating best features from MySQL and PHP world. It resulted in this brilliant code, which I showed to few colleagues, and got such quote:</p>
<blockquote><p> i like your nonframework. it gives a fuzzy feeling to the poor souls that think they need an abstraction layer. &#8212; <a href='http://kris.koehntopp.de/'>Kristian Köhntopp</a></p></blockquote>
<p>This was written using <a href='http://en.wikipedia.org/wiki/Tongue-in-cheek'>TIC pattern</a> and can be used in variety of applications:</p>
<pre>
class MyDB {
  var $conn = null;

  function MyDB($database=null,$user='root',
                $password='',$host='localhost') {
    $this-&gt;conn=mysql_connect($host,$user,$password) and
    $database?mysql_select_db($database, $this-&gt;conn):null;
  }

  function _escape($s) {
    return mysql_real_escape_string($s,$this-&gt;conn);
  }

  function _quote($s) {
    return &quot;'&quot; . $this-&gt;_escape($s) . &quot;'&quot;;
  }

  function __call($method,$arguments) {
    $query=preg_replace_callback('([A-Z]|\d+)',
      create_function('$s',
         'return &quot; &quot;.strtolower($s[0]);'),
         $method);
    $query=str_replace(' everything ',' * ',$query);
    $first=array_shift($arguments);
    if ($first) {
      if (is_array($first)) {
        $query .= ' (' . implode(',',
                            array_map(array(&amp;$this,'_escape'),
                            $first)) . ') ';
      } else {
        while($argument = array_shift($arguments)) {
          $first = preg_replace('/\?/',
                     $this-&gt;_quote($argument),$first,1);
        }
        $query .= $first;
      }
    }
    $ret=array();
    $res=mysql_query($query,$this-&gt;conn);
    if (!$res) { print mysql_error(); exit(); }
    while($row=mysql_fetch_assoc($res)) {
      $ret[]=$row;
    }
    return $ret;

  }
}

$x = new MyDB('test');
$x-&gt;selectEverythingFromMytableWhereIdIn(&quot; (?,?,?) &quot;,&quot;a'b&quot;,2,3);
$x-&gt;SelectBlahFromMytableWhereIdIn(array(1,2,3));
$x-&gt;InsertIntoBlahValues(array(1,2,3));
$x-&gt;truncateBlah();
</pre>
<p>Now I wonder where I should build community for this, Google Code or Sourceforge? Or should that be the darling <a href='http://forge.mysql.com'>MySQL Forge</a>.</p>
<p>As I started digging my stuff from history, I also managed to upload my past six month <a href='http://flickr.com/photos/midom/'>pictures</a> to flickr too &#8211; including Vienna, Taipei and few other trips. </p>
]]></content:encoded>
			<wfw:commentRss>http://mituzas.lt/2008/01/02/my-own-database-abstraction-class/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
