📄 dbd.html
字号:
This bundle includes all the modules used by the Perl Database
Interface (DBI) driver for Driver (DBD::Driver), assuming the
use of DBI version 1.13 or later, created by Tim Bunce.</pre>
<pre>
If you've not previously used the CPAN module to install any
bundles, you will be interrogated during its setup phase.
But when you've done it once, it remembers what you told it.
You could start by running:</pre>
<pre>
C<perl -MCPAN -e 'install Bundle::CPAN'></pre>
<pre>
=head1 SEE ALSO</pre>
<pre>
Bundle::DBI</pre>
<pre>
=head1 AUTHOR</pre>
<pre>
Your Name E<lt>F<you@yourdomain.com>E<gt></pre>
<pre>
=head1 THANKS</pre>
<pre>
This bundle was created by ripping off Bundle::libnet created by
Graham Barr E<lt>F<gbarr@ti.com>E<gt>, and radically simplified
with some information from Jochen Wiedmann E<lt>F<joe@ispsoft.de>E<gt>.
The template was then included in the DBI::DBD documentation by
Jonathan Leffler E<lt>F<jleffler@informix.com>E<gt>.</pre>
<pre>
=cut</pre>
<p>
</p>
<h2><a name="lib_dbd_driver_summary_pm">lib/DBD/Driver/Summary.pm</a></h2>
<p>There is no substitute for taking the summary file from a driver that
was documented in the Perl book (such as DBD::Oracle or DBD::Informix or
DBD::ODBC, to name but three), and adapting it to describe the
facilities available via DBD::Driver when accessing the Driver database.</p>
<p>
</p>
<h2><a name="pure_perl_version_of_driver_pm">Pure Perl version of Driver.pm</a></h2>
<p>The <a href="#item_driver_2epm"><code>Driver.pm</code></a> file defines the Perl module DBD::Driver for your driver.
It will define a package DBD::Driver along with some version information,
some variable definitions, and a function <code>driver()</code> which will have a more
or less standard structure.</p>
<p>It will also define three sub-packages of DBD::Driver:</p>
<dl>
<dt><strong><a name="item_dbd_3a_3adriver_3a_3adr">DBD::Driver::dr</a></strong>
<dd>
<p>with methods connect(), <code>data_sources()</code> and disconnect_all();</p>
</dd>
</li>
<dt><strong><a name="item_dbd_3a_3adriver_3a_3adb">DBD::Driver::db</a></strong>
<dd>
<p>with methods such as prepare();</p>
</dd>
</li>
<dt><strong><a name="item_dbd_3a_3adriver_3a_3ast">DBD::Driver::st</a></strong>
<dd>
<p>with methods such as <code>execute()</code> and fetch().</p>
</dd>
</li>
</dl>
<p>The Driver.pm file will also contain the documentation specific to
DBD::Driver in the format used by perldoc.</p>
<p>In a pure Perl driver, the Driver.pm file is the core of the
implementation.
You will need to provide all the key methods needed by DBI.</p>
<p>Now let's take a closer look at an excerpt of File.pm as an example.
We ignore things that are common to any module (even non-DBI modules)
or really specific to the DBD::File package.</p>
<p>
</p>
<h3><a name="the_dbd__driver_package">The DBD::Driver package</a></h3>
<p>
</p>
<h4><a name="the_header">The header</a></h4>
<pre>
<span class="keyword">package</span> <span class="variable">DBD::File</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">strict</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">vars</span> <span class="string">qw($VERSION $drh)</span><span class="operator">;</span>
</pre>
<pre>
$VERSION = "1.23.00" # Version number of DBD::File</pre>
<p>This is where the version number of your driver is specified.
The code in Makefile.PL is told to look in this file for the
information.
It is recommended that you use a two-part (1.23) or three-part (1.23.45)
version number.
Please ensure that any other modules added with your driver are also
version stamped so that CPAN does not get confused.
Also consider the CPAN system, which gets confused and considers
version 1.10 to precede version 1.9, so that using a raw CVS, RCS or
SCCS version number is probably not appropriate (despite being very
common). For RCS or CVS you can use this code:</p>
<pre>
<span class="variable">$VERSION</span> <span class="operator">=</span> <span class="keyword">sprintf</span> <span class="string">"%d.%02d"</span><span class="operator">,</span> <span class="string">'$Revision: 11.21 $ '</span> <span class="operator">=~</span> <span class="regex">/(\d+)\.(\d+)/</span><span class="operator">;</span>
</pre>
<p>which pads out the fractional part with leading zeros so all is well
(so long as you don't go past x.99)</p>
<pre>
<span class="variable">$drh</span> <span class="operator">=</span> <span class="keyword">undef</span><span class="operator">;</span> <span class="comment"># holds driver handle once initialized</span>
</pre>
<p>This is where the driver handle will be stored, once created.
Note that you may assume there is only one handle for your driver.</p>
<p>
</p>
<h4><a name="the_driver_constructor">The driver constructor</a></h4>
<p>Note that the <em>driver</em> method is in the DBD::Driver package, not in one
of the sub-packages DBD::Driver::dr, DBD::Driver::db, or
DBD::Driver::db.</p>
<pre>
<span class="keyword">sub</span><span class="variable"> driver
</span><span class="operator">{</span>
<span class="keyword">return</span> <span class="variable">$drh</span> <span class="keyword">if</span> <span class="variable">$drh</span><span class="operator">;</span> <span class="comment"># already created - return same one</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$class</span><span class="operator">,</span> <span class="variable">$attr</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$class</span> <span class="operator">.=</span> <span class="string">"::dr"</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># not a 'my' since we use it above to prevent multiple drivers</span>
<span class="variable">$drh</span> <span class="operator">=</span> <span class="variable">DBI::_new_drh</span><span class="operator">(</span><span class="variable">$class</span><span class="operator">,</span> <span class="operator">{</span>
<span class="string">'Name'</span> <span class="operator">=></span> <span class="string">'File'</span><span class="operator">,</span>
<span class="string">'Version'</span> <span class="operator">=></span> <span class="variable">$VERSION</span><span class="operator">,</span>
<span class="string">'Attribution'</span> <span class="operator">=></span> <span class="string">'DBD::File by Jochen Wiedmann'</span><span class="operator">,</span>
<span class="operator">}</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">return</span> <span class="keyword">undef</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">return</span> <span class="variable">$drh</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>The <em>driver</em> method is the driver handle constructor. It's a
reasonable example of how DBI implements its handles. There are three
kinds: <strong>driver handles</strong> (typically stored in <code>$drh</code>; from now on
called <code>drh</code> or <code>$drh</code>), <strong>database handles</strong> (from now on called <code>dbh</code> or
<code>$dbh</code>) and <strong>statement handles</strong> (from now on called <code>sth</code> or
<code>$sth</code>).</p>
<p>The prototype of DBI::_new_drh is</p>
<pre>
<span class="variable">$drh</span> <span class="operator">=</span> <span class="variable">DBI::_new_drh</span><span class="operator">(</span><span class="variable">$class</span><span class="operator">,</span> <span class="variable">$public_attrs</span><span class="operator">,</span> <span class="variable">$private_attrs</span><span class="operator">);</span>
</pre>
<p>with the following arguments:</p>
<dl>
<dt><strong><a name="item__class"><em>$class</em></a></strong>
<dd>
<p>is typically the class for your driver, (for example, "DBD::File::dr"),
passed as the first argument to the <em>driver</em> method.</p>
</dd>
</li>
<dt><strong><a name="item__public_attrs"><em>$public_attrs</em></a></strong>
<dd>
<p>is a hash ref to attributes like <em>Name</em>, <em>Version</em>, and <em>Attribution</em>.
These are processed and used by DBI.
You had better not make any assumptions about them nor should you add
private attributes here.</p>
</dd>
</li>
<dt><strong><a name="item__private_attrs"><em>$private_attrs</em></a></strong>
<dd>
<p>This is another (optional) hash ref with your private attributes.
DBI will store them and otherwise leave them alone.</p>
</dd>
</li>
</dl>
<p>The <em>DBI::new_drh</em> method and the <em>driver</em> method both return <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>
for failure (in which case you must look at $DBI::err and $DBI::errstr
for the failure information, because you have no driver handle to use).</p>
<p>
</p>
<h4><a name="the_clone_special_subroutine">The CLONE special subroutine</a></h4>
<p>Also needed here, in the DBD::Driver package, is a <code>CLONE()</code> method
that will be called by perl when an intrepreter is cloned. All your
CLONE method needs to do, currently, is clear the cached $drh so
the new interpreter won't start using the cached $drh from the old
interpreter:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> CLONE </span><span class="operator">{</span>
<span class="keyword">undef</span> <span class="variable">$drh</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>See <a href="http://search.cpan.org/dist/perl/pod/perlmod.pod#Making_your_module_threadsafe">http://search.cpan.org/dist/perl/pod/perlmod.pod#Making_your_module_threadsafe</a>
for details.</p>
<p>
</p>
<h3><a name="the_dbd__driver__dr_package">The DBD::Driver::dr package</a></h3>
<p>
</p>
<h4><a name="the_database_handle_constructor">The database handle constructor</a></h4>
<p>The next lines of code look as follows:</p>
<pre>
<span class="keyword">package</span> <span class="variable">DBD::Driver::dr</span><span class="operator">;</span> <span class="comment"># ====== DRIVER ======</span>
</pre>
<pre>
<span class="variable">$DBD::Driver::dr::imp_data_size</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>Note that no @ISA is needed here, or for the other DBD::Driver::*
classes, because the DBI takes care of that for you when the driver is
loaded.</p>
<p>The database handle constructor is a driver method, thus we have
to change the namespace.</p>
<pre>
<span class="keyword">sub</span><span class="variable"> connect
</span><span class="operator">{</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$drh</span><span class="operator">,</span> <span class="variable">$dr_dsn</span><span class="operator">,</span> <span class="variable">$user</span><span class="operator">,</span> <span class="variable">$auth</span><span class="operator">,</span> <span class="variable">$attr</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
</pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -