<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Not this... &#187; dbi</title>
	<atom:link href="http://blog.timbunce.org/tag/dbi/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.timbunce.org</link>
	<description>Listen. Reflect. Explore. Solve.</description>
	<lastBuildDate>Thu, 29 Jul 2010 00:00:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.timbunce.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/eb188a9f7199a98e44133dc454d3873b?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Not this... &#187; dbi</title>
		<link>http://blog.timbunce.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.timbunce.org/osd.xml" title="Not this..." />
	<atom:link rel='hub' href='http://blog.timbunce.org/?pushpress=hub'/>
		<item>
		<title>Reflections on Perl and DBI from an Early Contributor</title>
		<link>http://blog.timbunce.org/2010/07/08/reflections-on-perl-and-dbi-from-an-early-contributor/</link>
		<comments>http://blog.timbunce.org/2010/07/08/reflections-on-perl-and-dbi-from-an-early-contributor/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 12:48:27 +0000</pubDate>
		<dc:creator>TimBunce</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[cpan]]></category>
		<category><![CDATA[dbi]]></category>
		<category><![CDATA[perl4]]></category>

		<guid isPermaLink="false">http://timbunce.wordpress.com/?p=449</guid>
		<description><![CDATA[The name Buzz Moschetti probably isn&#8217;t familiar to you. Buzz was the author of the Perl 4 database for Interbase known as Interperl. Back in those days Perl 5 was barely a twinkle in Larry&#8217;s eye and database interfaces for Perl 4 required building a custom perl binary. Buzz was one of the four people [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&blog=2562816&post=449&subd=timbunce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>The name Buzz Moschetti probably isn&#8217;t familiar to you. Buzz was the author of the Perl 4 database for <a href="http://en.wikipedia.org/wiki/InterBase#History">Interbase</a> known as <a href="http://cpan.perl.org/modules/dbperl/perl4/interperl/README">Interperl</a>.</p>
<p>Back in those days Perl 5 was barely a twinkle in Larry&#8217;s eye and <a href="http://cpan.perl.org/modules/dbperl/perl4/">database interfaces for Perl 4</a> required building a custom perl binary.</p>
<p>Buzz was one of the four people to get the email on September 29th 1992 from Ted Lemon that started the <a href="http://cpan.perl.org/modules/dbperl/DBI/perldb-interest/">perldb-interest</a> project which defined a <a href="http://cpan.perl.org/modules/dbperl/DBI/dbispec.v04">specification</a> that ultimately lead to the DBI. (The other people were Kurt Andersen re informix, Kevin Stock re <a href="http://cpan.perl.org/modules/dbperl/perl4/oraperl/">oraperl</a>, and Michael Peppler re <a href="http://cpan.perl.org/modules/dbperl/perl4/sybperl/">sybperl</a>. I joined a few days later.)</p>
<p><strong>Update</strong>: It turns out that it was actually Buzz who sent that original email, Ted just forwarded it on to others, including me. So Buzz can be said to have started the process that led to the DBI!</p>
<p>I hadn&#8217;t heard from Buzz for <em>many</em> years until he sent me an email recently.</p>
<p>This is his story:<span id="more-449"></span><br />
<hr />
Thought I&#8217;d share a quick story with you.</p>
<p>Recently, I was frustrated with a development team&#8217;s efforts in putting<br />
together some DB-oriented reconciliations.  The candidate solution was a<br />
blend of precompiled SQL in COBOL code, file dumps and ftps, programs to<br />
read files, more programs to read other DBs, etc. etc.   Not only was<br />
the process orchestration a project in its own right, the end-to-end<br />
logic required to accurately perform the reconciliation was distributed<br />
across several programs and platforms, diluting the knowledge base.  I<br />
knew a perl program using multiple DBD drivers to different DB engines<br />
could do it in a much cleaner way, but over the years my job has changed<br />
and although I still use perl regularly, I don&#8217;t do much in the way of<br />
DBD/DBI.   To make matters worse, one of the targets was mainframe DB2<br />
and very little work had been done here with DBD::DB2.   Also, the<br />
Sybperl module continues to be heavily used in addition to DBD::Sybase,<br />
so local DBD/DBI expertise in general is thin.  I decided to get it<br />
working on my own.</p>
<p>The infrastructure team spun up for me a Linux virtual machine with a<br />
modern build environment on it.  This had the latest gcc compilers and a<br />
firm-approved build of perl 5.8.5 right out of the box.  It took a few<br />
days of low-priority requests to get the appropriate 32bit Linux<br />
client-side SDKs for the DB2 and Sybase products but soon enough I had<br />
an environment set up with headers and shared libs.  I was ready to<br />
build some perl modules, something I haven&#8217;t done in years.</p>
<p>I went to CPAN and downloaded DBD::DB2, untar&#8217;d it, and ran perl<br />
Makefile.PL and make.  Everything worked perfectly and the whole<br />
exercise took minutes.  &#8216;make test&#8217; sets PERL_DL_NONLAZY and warned of<br />
some unused symbols not being found, but that was OK.  The rest of the<br />
tests that I expected to work with my level of permissions worked fine.<br />
&#8216;make install&#8217; worked perfectly.  Buoyed by this success, I wrote a<br />
4-liner test program just to connect and fetch some data from a table I<br />
knew about.  Outside of the test environment, however, the shared libs<br />
for DB2 were not found so I cheated and relinked and reinstalled DB2.so<br />
with the -Wl,-rpath option to &#8220;cement in&#8221; the location of those libs so<br />
I wouldn&#8217;t have to fuss with LD_LIBRARY_PATH.   My test program now<br />
worked fine.  Newly comfortable with the process, I downloaded<br />
DBD::Sybase and built and installed the module in scarcely more time than<br />
it took for the compiler to run.  In my excitement I skipped over the<br />
DBD::Sybase 4-liner test program and went straight to a slightly bigger<br />
script that used both modules and grabbed data from both DBs.  It<br />
quietly and quickly executed.  </p>
<p>Total time from initial download with almost no clues to a running<br />
example: about 40 minutes.  Later, for grin&#8217;s sake, I threw in<br />
DBD::Oracle for good measure.  That went even faster &#8212; about 5 minutes<br />
&#8211; from CPAN download to printing &#8220;Oracle connected!&#8221; because I was more<br />
familiar with the connection string syntax that is bespoke for each<br />
engine.</p>
<p>As I watched the program run, it made me reflect on how far we&#8217;ve come<br />
and how easy yet sophisticated the perl module ecosystem has become.<br />
There is no question that this multi-DBD perl program is easier to<br />
understand and support than a solution involving a set of disconnected<br />
programs, platforms, and files.  But I think it is the organization and<br />
design of the resources as a whole &#8212; DBI, DBD, CPAN, MakeMaker, pod,<br />
binary and non-binary library locations, etc. &#8212; that makes the whole<br />
environment so clear, symmetric, and easy to use with confidence.  I<br />
think back to the build environment that I used to create <a href="http://cpan.perl.org/modules/dbperl/perl4/interperl/README">interperl</a>, and<br />
the progress that has been made in terms of both breadth of module<br />
functionality and depth of framework for module build portability is<br />
simply amazing.  Perl has grown far beyond just being another language.<br />
It has a value proposition as an able integrator of widely disparate<br />
functionality.  </p>
<p>I exited the Perl mainstream some time ago but I am watching from the<br />
side and I applaud the work you&#8217;ve done in this space.</p>
<p>Take care.</p>
<hr />
<p>Thanks Buzz!</p>
<br />Filed under: <a href='http://blog.timbunce.org/category/tech/software/perl/'>perl</a> Tagged: <a href='http://blog.timbunce.org/tag/cpan/'>cpan</a>, <a href='http://blog.timbunce.org/tag/dbi/'>dbi</a>, <a href='http://blog.timbunce.org/tag/perl/'>perl</a>, <a href='http://blog.timbunce.org/tag/perl4/'>perl4</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timbunce.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timbunce.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timbunce.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timbunce.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timbunce.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timbunce.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timbunce.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timbunce.wordpress.com/449/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timbunce.wordpress.com/449/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timbunce.wordpress.com/449/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&blog=2562816&post=449&subd=timbunce&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://blog.timbunce.org/2010/07/08/reflections-on-perl-and-dbi-from-an-early-contributor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cf82705f5ab43c73273ab5d690866b3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">TimBunce</media:title>
		</media:content>
	</item>
		<item>
		<title>Pay no attention to that callback behind the curtain!</title>
		<link>http://blog.timbunce.org/2008/05/01/pay-no-attention-to-that-callback-behind-the-curtain/</link>
		<comments>http://blog.timbunce.org/2008/05/01/pay-no-attention-to-that-callback-behind-the-curtain/#comments</comments>
		<pubDate>Thu, 01 May 2008 10:32:13 +0000</pubDate>
		<dc:creator>TimBunce</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[dbi]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://timbunce.wordpress.com/?p=38</guid>
		<description><![CDATA[So you&#8217;ve got some perl code that connects to a particular database via a particular DBI driver. You want it to connect to a different database or driver. But you can&#8217;t change that part of the code. What can you do? I ran into this problem recently. A large application is using an old version [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&blog=2562816&post=38&subd=timbunce&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve got some perl code that connects to a particular database via a particular DBI driver. You want it to connect to a different database or driver. <em>But you can&#8217;t change that part of the code.</em> What can you do?</p>
<p>I ran into this problem recently. A large application is using an old version of <a href="http://search.cpan.org/dist/DBIx-HA/">DBIx::HA</a> which doesn&#8217;t support <a href="http://search.cpan.org/perldoc?DBD::Gofer">DBD::Gofer</a>. DBIx::HA can&#8217;t be upgraded (long story, don&#8217;t ask) but I wanted to use DBD::Gofer to provide <a href="http://search.cpan.org/~timb/DBI/lib/DBD/Gofer.pm#cache">client-side caching</a> via <a href="http://search.cpan.org/perldoc?Cache::FastMmap">Cache::FastMmap</a>. (I&#8217;ll save more details of that, and the 40% reduction in database requests it gave, for another post.)</p>
<p>I needed a way for DBIx::HA to think that it was connecting to a particular driver and database, but for it to actually connect to another. Using <code>$ENV{DBI_AUTOPROXY}</code> wasn&#8217;t an option because that has global effect whereas I needed fine control over which connections were affected. It&#8217;s also fairly blunt instrument in other ways.</p>
<p>It seemed like I was stuck. Then I remembered the DBI callback mechanism &#8211; it would provide an elegant solution to this. I added it to DBI 1.49 back in November 2005 and enhanced it further in 1.55. I&#8217;d never documented it though. I think I was never quite sure it had sufficient functionality to be really useful. Now I&#8217;m sure it has.</p>
<p>The DBI callback mechanism lets you intercept, and optionally replace, <em>any</em> method call on a DBI handle. At the extreme, it lets you become a puppet master, deceiving the application in any way you want.</p>
<p><span style="text-align:center; display: block;"><a href="http://blog.timbunce.org/2008/05/01/pay-no-attention-to-that-callback-behind-the-curtain/"><img src="http://img.youtube.com/vi/YWyCCJ6B2WE/2.jpg" alt="" /></a></span></p>
<p>Here&#8217;s how the code looked (with a few irrelevant details changed):</p>
<pre>
    # The following section of code uses the DBI Callback mechanism to
    # intercept connect() calls to DBD::Sybase and, where appropriate,
    # reroute them to DBD::Gofer.
    our $in_callback;

    # get Gofer $drh and make it pretend to be named Sybase
    # to keep DBIx::HA 0.62 happy
    my $gofer_drh  = DBI-&gt;install_driver("Gofer");
    $gofer_drh-&gt;{Name} = "Sybase";

    # get the Sybase drh and install a callback to intercept connect()s
    my $sybase_drh = DBI-&gt;install_driver("Sybase");
    $sybase_drh-&gt;{Callbacks} = {
        connect =&gt; sub {
            # protect against recursion when gofer itself makes a connection
            return if $in_callback; local $in_callback = 1;

            my $drh = shift;
            my ($dsn, $u, $p, $attr) = @_;
            warn "connect via callback $drh $dsn\n" if $DEBUG;

            # we're only interested in connections to particular databases
            return unless $dsn =~ /some pattern/;

            # rewrite the DSN to connect to the same DSN via Gofer
            # using the null transport so we can use Gofer caching
            $dsn = "transport=null;dsn=dbi:Sybase(ReadOnly=1):$dsn";

            my $dbh = $gofer_drh-&gt;connect($dsn, $u, $p, $attr);

            if (not $dbh) { # gofer connection failed for some reason
                warn "connect via gofer failed: $DBI::errstr\n"
                    unless our $connect_via_gofer_err++; # warn once
                return; # DBI will now call original connect method
            }

            undef $_;    # tell DBI not to call original connect method
            return $dbh; # tell DBI to return this $dbh instead
        },
    };
</pre>
<p>So the application, via DBIx::HA, executed</p>
<p><code>&nbsp;&nbsp;$dbh = DBI-&gt;connect("dbi:Sybase:foo",...)</code></p>
<p>but what it got back was a DBD::Gofer dbh, as if the application has executed</p>
<p><code>&nbsp;&nbsp;$dbh = DBI-&gt;connect("dbi:Gofer:transport=null;dsn=dbi:Sybase(ReadOnly=1):foo",...)</code>.</p>
<p>I guess I should document the callback mechanism now. Meanwhile the closest thing  to documentation is <a href="http://search.cpan.org/src/TIMB/DBI-1.604/t/70callbacks.t">the test file</a>.</p>
<p>I&#8217;ve always enjoyed this kind of &#8220;plumbing&#8221;. If you come up with any interesting uses of DBI callbacks, do let me know.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/timbunce.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/timbunce.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timbunce.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timbunce.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timbunce.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timbunce.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timbunce.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timbunce.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timbunce.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timbunce.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timbunce.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timbunce.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.timbunce.org&blog=2562816&post=38&subd=timbunce&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://blog.timbunce.org/2008/05/01/pay-no-attention-to-that-callback-behind-the-curtain/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cf82705f5ab43c73273ab5d690866b3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">TimBunce</media:title>
		</media:content>

		<media:content url="http://img.youtube.com/vi/YWyCCJ6B2WE/2.jpg" medium="image" />
	</item>
	</channel>
</rss>