<?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; dbic</title>
	<atom:link href="http://blog.netcubed.de/tag/dbic/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>Phonetic search with DBIC</title>
		<link>http://blog.netcubed.de/2009/05/phonetic-search-with-dbic/</link>
		<comments>http://blog.netcubed.de/2009/05/phonetic-search-with-dbic/#comments</comments>
		<pubDate>Fri, 29 May 2009 14:49:42 +0000</pubDate>
		<dc:creator>Moritz Onken</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[dbic]]></category>
		<category><![CDATA[phonetic]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://blog.netcubed.de/?p=97</guid>
		<description><![CDATA[I recently ran across the probem that there are many people with almost identical last names and it is hard to guess every possible spelling until you finally find the person you were looking for.
In german, there are a lot of possible spellings for the name &#8220;Maier&#8221; which all sound the same: Meyer, Meier, Mayer, [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran across the probem that there are many people with almost identical last names and it is hard to guess every possible spelling until you finally find the person you were looking for.</p>
<p>In german, there are a lot of possible spellings for the name &#8220;Maier&#8221; which all sound the same: Meyer, Meier, Mayer, Mayr, Meyr etc.</p>
<p>A phonetic algorithm reduces a given word to a digest which is the same for all names, which sound similar. After asking for a proper name on #dbix-class I came up with the DBIx-Class-PhoneticSearch module.</p>
<p>For now it is only avaiable from <a href="http://github.com/monken/DBIx-Class-PhoneticSearch/tree/master">github</a>. But sometime soon it will be on the CPAN :-)</p>
<p>The usage is pretty easy:</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;">User</span><span style="color: #339933;">;</span>
&nbsp;
    <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>PhoneticSearch 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;">'user'</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;">'integer'</span><span style="color: #339933;">,</span> auto_increment <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      surname  <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 varying'</span><span style="color: #339933;">,</span> 
                    phonetic_search <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      forename <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 varying'</span><span style="color: #339933;">,</span> 
                    phonetic_search <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span> algorithm <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Koeln'</span><span style="color: #339933;">,</span> 
                                         no_indices <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    <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;
    __PACKAGE__<span style="color: #339933;">-&gt;</span><span style="color: #006600;">resultset_class</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'DBIx::Class::ResultSet::PhoneticSearch'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This defines a result class with a forename and surname column. Both are phonetic-enabled. forename uses the Koeln algorithm, which has been optimized for german names and words. Make sure you <code>deploy()</code> that schema again or add the two columns to your schema.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`user`</span> <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">COLUMN</span> <span style="color: #ff0000;">`surname_phonetic_phonix`</span> CHARACTER VARYING;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`user`</span> <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">COLUMN</span> <span style="color: #ff0000;">`forename_phonetic_koeln`</span> CHARACTER VARYING;</pre></div></div>

<p>Now you can search for any user by a similar sounding name:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">  <span style="color: #0000ff;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$schema</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">resultset</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'User'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #0000ff;">$rs</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> forename <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'John'</span><span style="color: #339933;">,</span> surname <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Night'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #0000ff;">$rs</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search_phonetic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> forename <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Jon'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">first</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">forename</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;"># John</span>
  <span style="color: #0000ff;">$rs</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search_phonetic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> surname <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Knight'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">first</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">surname</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Night</span></pre></div></div>

<p>The default algorithm is <a href="http://search.cpan.org/perldoc?Text::Phonetic::Phonix">Phonix</a> which is IMHO far superior to the popular Soundex algorithm. E. g. the last example (Knight -> Night) does not work with Soundex.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netcubed.de/2009/05/phonetic-search-with-dbic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reports with DBIC and Postgres</title>
		<link>http://blog.netcubed.de/2009/05/reports-with-dbic-and-postgres/</link>
		<comments>http://blog.netcubed.de/2009/05/reports-with-dbic-and-postgres/#comments</comments>
		<pubDate>Sat, 23 May 2009 10:52:47 +0000</pubDate>
		<dc:creator>Moritz Onken</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[dbic]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[reports]]></category>

		<guid isPermaLink="false">http://blog.netcubed.de/?p=91</guid>
		<description><![CDATA[Currently I use plain SQL statements to create reports which, for instance, show me the number of logins of all users on a 5 minutes scale.
One of those statements looks similar to this:

INSERT INTO report_item 
    &#40;
        SELECT '2009-05-23 02:30:00+0200'::timestamp WITH time zone + &#40;s.a [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I use plain SQL statements to create reports which, for instance, show me the number of logins of all users on a 5 minutes scale.</p>
<p>One of those statements looks similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> report_item 
    <span style="color: #66cc66;">&#40;</span>
        <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'2009-05-23 02:30:00+0200'</span>::timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>a <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' minute'</span><span style="color: #66cc66;">&#41;</span>::interval <span style="color: #993333; font-weight: bold;">AS</span> timestamp<span style="color: #66cc66;">,</span> 
               COUNT<span style="color: #66cc66;">&#40;</span>source<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;created_on&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> value
        <span style="color: #993333; font-weight: bold;">FROM</span> generate_series<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">595</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> s<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> 
          <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;created_on&quot;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">&quot;session&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> source
          <span style="color: #993333; font-weight: bold;">ON</span> source<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;created_on&quot;</span> <span style="color: #66cc66;">&gt;=</span> <span style="color: #ff0000;">'2009-05-23 02:30:00+0200'</span>::timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>a  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' minute'</span><span style="color: #66cc66;">&#41;</span>::interval 
             <span style="color: #993333; font-weight: bold;">AND</span> source<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;created_on&quot;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff0000;">'2009-05-23 02:30:00+0200'</span>::timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>a  <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' minute'</span><span style="color: #66cc66;">&#41;</span>::interval
&nbsp;
        <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> a
    <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This gives me the number of logins (or new sessions) per 5 minute time slot. Now it&#8217;s pretty easy to read the data from <code>report_item</code> and create a graph or do other nice things with it.</p>
<p>I was thinking about a more generic way to do these reports so that the user can choose, what kind of report he wants.</p>
<p>So I started to create a result class <code>Report</code> with the following columns:</p>
<ul>
<li>group_by<br/>The column which is used to group rows together. This is always a timestamp column, e.g. <code>created_on</code></li>
<li>interval_type<br/>Group by minutes, hours, days, weeks etc. (defaults to minutes)</li>
<li>interval_length<br/>Group by that amount of <code>interval_type</code> (defaults to 5)</li>
<li>aggregate_by<br/>Which SQL function should be used to aggregate data in a time slot (defaults to <code>COUNT</code>)</li>
<li>aggregate<br/>Which column should be aggregated</li>
<li>query<br/>Which data should be aggregated? This accepts a DBIC result set or a plain SQL statement
</ul>
<p>After you created a report in that table you call <code>$report->create_report</code> on that row. This will execute the query above with the correct values filled in and the result will be stored in a table <code>result_item</code>, which has a foreign key <code>report_id</code> to the report.</p>
<p>It does a decent job so far and I was wondering if anyone else did something like this before?</p>
<p>If anyone is interested in the code I can make a dist and upload it to github. Since the SQL is Postgres only I don&#8217;t want to push it to the CPAN.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netcubed.de/2009/05/reports-with-dbic-and-postgres/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
