<?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>netCUBED Blog &#187; dbix-class</title>
	<atom:link href="http://blog.netcubed.de/tag/dbix-class/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netcubed.de</link>
	<description>Just another web developer's weblog</description>
	<lastBuildDate>Mon, 29 Jun 2009 20:58:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How do your DBIC result base classes look like?</title>
		<link>http://blog.netcubed.de/2009/05/how-do-your-dbic-result-base-classes-look-like/</link>
		<comments>http://blog.netcubed.de/2009/05/how-do-your-dbic-result-base-classes-look-like/#comments</comments>
		<pubDate>Thu, 21 May 2009 08:57:51 +0000</pubDate>
		<dc:creator>Moritz Onken</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[dbic]]></category>
		<category><![CDATA[dbix-class]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[result]]></category>
		<category><![CDATA[resultset]]></category>

		<guid isPermaLink="false">http://blog.netcubed.de/?p=56</guid>
		<description><![CDATA[I was wondering how you design your base classes for DBIC result(set) classes. I think it&#8217;s a good idea both in terms of startup speed and ease of use to not use DBIx::Class as base of your result classes but use a custom one instead. The same applies for the resultset classes. Here is how [...]]]></description>
			<content:encoded><![CDATA[<p>I was wondering how you design your base classes for DBIC result(set) classes. I think it&#8217;s a good idea both in terms of <a href="http://lists.scsys.co.uk/pipermail/dbix-class/2007-August/004824.html" target="_blank">startup speed</a> and ease of use to not use <code>DBIx::Class</code> as base of your result classes but use a custom one instead. The same applies for the resultset classes. Here is how I designed my DBIC classes:</p>
<p>Make sure you use <code>load_namespaces</code>in <code>MySchema.pm</code>!</p>
<p>Directory structure:</p>
<pre>MySchema.pm
MySchema/
         Base/
              Result.pm
              ResultSet.pm
         Result/
                User.pm
         ResultSet/
                   User.pm</pre>
<p>This has the advantage that the base classes in <code>base</code> are not being loaded. All classes in <code>Result</code> use <code>MySchema::Base::Result</code> as their base class. Classes in <code>ResultSet</code> use <code>MySchema::Base::ResultSet</code> respectively.</p>
<p>My current result base class looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> MySchema<span style="color: #339933;">::</span><span style="color: #006600;">Base</span><span style="color: #339933;">::</span><span style="color: #006600;">Result</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> base <span style="color: #ff0000;">'DBIx::Class'</span><span style="color: #339933;">;</span>
&nbsp;
__PACKAGE__<span style="color: #339933;">-&gt;</span><span style="color: #006600;">load_components</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>RandomColumns InflateColumn<span style="color: #339933;">::</span><span style="color: #006600;">FS</span> TimeStamp Core<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
__PACKAGE__<span style="color: #339933;">-&gt;</span><span style="color: #006600;">table</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'dummy'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
__PACKAGE__<span style="color: #339933;">-&gt;</span><span style="color: #006600;">add_columns</span><span style="color: #009900;">&#40;</span>
	id <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
      data_type <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'character'</span><span style="color: #339933;">,</span>
      is_random <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>size <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> set <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'a'</span><span style="color: #339933;">..</span><span style="color: #ff0000;">'z'</span><span style="color: #339933;">,</span><span style="color: #ff0000;">'A'</span><span style="color: #339933;">..</span><span style="color: #ff0000;">'Z'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> check <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      size <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    created_on <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
        data_type <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'timestamp with time zone'</span><span style="color: #339933;">,</span>
        set_on_create <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        is_nullable <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    updated_on <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
        data_type <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'timestamp with time zone'</span><span style="color: #339933;">,</span>
        set_on_create <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        set_on_update <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        is_nullable <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
__PACKAGE__<span style="color: #339933;">-&gt;</span><span style="color: #006600;">set_primary_key</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> sqlt_deploy_hook <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sqlt_table</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
  <span style="color: #000066;">map</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">$sqlt_table</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">drop_constraint</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">type</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">&quot;FOREIGN KEY&quot;</span> <span style="color: #009900;">&#125;</span> 
    <span style="color: #0000ff;">$sqlt_table</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">get_constraints</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p>So what is going on here?</p>
<p>First I use <code>load_components</code> to load all the components which are valuable to all of my result classes. Since I always use a random combination of characters for the primary key column <code>id</code> I load <code>RandomColumns</code>. <code>InflateColumn::FS</code> makes sure that any BLOB is stored on the file system rather than in the database. <code>TimeStamp</code> can set columns to the current time on either update or create. It also loads <code>InflateColumn::DateTime</code> which is used to inflate any date or time columns to a DateTime object.</p>
<p>You need to define a table name, otherwise DBIC fails to compile. This table will never be created or seen in any of your result classes as long as you overwrite it in each class. So just name it <code>dummy</code> or something and you are good.</p>
<p>Next I create a couple of columns which should be avaiable on all result classes. My primary key is always called <code>id</code> so create that column for all my classes. To use the <code>RandomColumns</code> component you have to add that <code>is_random</code> line to the column definition. This will create a random 10-byte long string. Since I&#8217;m a little bit paranoid about collision I set the <code>check</code> parameter so the component checks before inserting a new row if a column with that id is already there. Although this is very unlikely because there are 52^10 combinations, <a href="http://www92.wolframalpha.com/input/?i=52%5E10" target="_blank">quite a lot&#8230;</a>  </p>
<p>The <code>created_on</code> and <code>updated_on</code> columns use the features of <code>TimeStamp</code> to set the time when the record was created or updated respectively. <code>timestamp with time zone</code> is a PostgreSQL specific column. You might need to the correct data type of your dbms.</p>
<p>I use <code>SQL::Translator</code>, <code>DBIx::Class::Schema::Versioned</code> and a few lines of code to deploy and update my database schema. Foreign key constraints on the database slow things down and DBIC handles them anyway so I decided to drop them from the SQL::Translator output. this is done in the <code>sqlt_deploy_hook</code> method.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netcubed.de/2009/05/how-do-your-dbic-result-base-classes-look-like/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
