📄 dbd.pm
字号:
package DBI::DBD;# vim:ts=8:sw=4use vars qw($VERSION); # set $VERSION early so we don't confuse PAUSE/CPAN etc# don't use Revision here because that's not in svn:keywords so that the# examples that use it below won't be messed up$VERSION = sprintf("12.%06d", q$Id: DBD.pm 10002 2007-09-26 21:03:25Z timbo $ =~ /(\d+)/o);# $Id: DBD.pm 10002 2007-09-26 21:03:25Z timbo $## Copyright (c) 1997-2006 Jonathan Leffler, Jochen Wiedmann, Steffen# Goeldner and Tim Bunce## You may distribute under the terms of either the GNU General Public# License or the Artistic License, as specified in the Perl README file.=head1 NAMEDBI::DBD - Perl DBI Database Driver Writer's Guide=head1 SYNOPSIS perldoc DBI::DBD=head2 Version and volatilityThis document is I<still> a minimal draft which is in need of further work.The changes will occur both because the B<DBI> specification is changingand hence the requirements on B<DBD> drivers change, and because feedbackfrom people reading this document will suggest improvements to it.Please read the B<DBI> documentation first and fully, including the B<DBI> FAQ.Then reread the B<DBI> specification again as you're reading this. It'll help.This document is a patchwork of contributions from various authors.More contributions (preferably as patches) are very welcome.=head1 DESCRIPTIONThis document is primarily intended to help people writing newdatabase drivers for the Perl Database Interface (Perl DBI).It may also help others interested in discovering why the internals ofa B<DBD> driver are written the way they are.This is a guide. Few (if any) of the statements in it are completelyauthoritative under all possible circumstances. This means you willneed to use judgement in applying the guidelines in this document.If in I<any> doubt at all, please do contact the I<dbi-dev> mailing list(details given below) where Tim Bunce and other driver authors can help.=head1 CREATING A NEW DRIVERThe first rule for creating a new database driver for the Perl DBI isvery simple: B<DON'T!>There is usually a driver already available for the database you wantto use, almost regardless of which database you choose. Very often, thedatabase will provide an ODBC driver interface, so you can often useB<DBD::ODBC> to access the database. This is typically less convenienton a Unix box than on a Microsoft Windows box, but there are numerousoptions for ODBC driver managers on Unix too, and very often the ODBCdriver is provided by the database supplier.Before deciding that you need to write a driver, do your homework toensure that you are not wasting your energies.[As of December 2002, the consensus is that if you need an ODBC drivermanager on Unix, then the unixODBC driver (available fromL<http://www.unixodbc.org/>) is the way to go.]The second rule for creating a new database driver for the Perl DBI isalso very simple: B<Don't -- get someone else to do it for you!>Nevertheless, there are occasions when it is necessary to write a newdriver, often to use a proprietary language or API to access thedatabase more swiftly, or more comprehensively, than an ODBC driver can.Then you should read this document very carefully, but with a suitablysceptical eye.If there is something in here that does not make any sense, question it.You might be right that the information is bogus, but don't come to thatconclusion too quickly.=head2 URLs and mailing listsThe primary web-site for locating B<DBI> software and information is http://dbi.perl.org/There are two main and one auxilliary mailing lists for people workingwith B<DBI>. The primary lists are I<dbi-users@perl.org> for general usersof B<DBI> and B<DBD> drivers, and I<dbi-dev@perl.org> mainly for B<DBD> driverwriters (don't join the I<dbi-dev> list unless you have a good reason).The auxilliary list is I<dbi-announce@perl.org> for announcing newreleases of B<DBI> or B<DBD> drivers.You can join these lists by accessing the web-site L<http://dbi.perl.org/>.The lists are closed so you cannot send email to any of the listsunless you join the list first.You should also consider monitoring the I<comp.lang.perl.*> newsgroups,especially I<comp.lang.perl.modules>.=head2 The Cheetah bookThe definitive book on Perl DBI is the Cheetah book, so called becauseof the picture on the cover. Its proper title is 'I<Programming thePerl DBI: Database programming with Perl>' by Alligator Descartesand Tim Bunce, published by O'Reilly Associates, February 2000, ISBN1-56592-699-4. Buy it now if you have not already done so, and read it.=head2 Locating driversBefore writing a new driver, it is in your interests to find outwhether there already is a driver for your database. If there is sucha driver, it would be much easier to make use of it than to write yourown!The primary web-site for locating Perl software isL<http://search.cpan.org/>. You should look under the variousmodules listings for the software you are after. For example: http://search.cpan.org/modlist/Database_InterfacesFollow the B<DBD::> and B<DBIx::> links at the top to see those subsets.See the B<DBI> docs for information on B<DBI> web sites and mailing lists.=head2 Registering a new driverBefore going through any official registration process, you will needto establish that there is no driver already in the works. You'll dothat by asking the B<DBI> mailing lists whether there is such a driveravailable, or whether anybody is working on one.When you get the go ahead, you will need to establish the name of thedriver and a prefix for the driver. Typically, the name is based on thename of the database software it uses, and the prefix is a contractionof that. Hence, B<DBD::Oracle> has the name I<Oracle> and the prefix'I<ora_>'.This information will be recorded in the B<DBI> module. Apart fromdocumentation purposes, registration is a prerequisite forL<installing private methods|DBI/install_method>.If you are writing a driver which will not be distributed on CPAN, thenyou should choose a prefix beginning with 'I<x_>', to avoid potentialprefix collisions with drivers registered in the future. Thus, if youwrote a non-CPAN distributed driver called B<DBD::CustomDB>, the prefixmight be 'I<x_cdb_>'.This document assumes you are writing a driver called B<DBD::Driver>, andthat the prefix 'I<drv_>' is assigned to the driver.=head2 Two styles of database driverThere are two distinct styles of database driver that can be written towork with the Perl DBI.Your driver can be written in pure Perl, requiring no C compiler.When feasible, this is the best solution, but most databases are notwritten in such a way that this can be done. Some examples of purePerl drivers are B<DBD::File> and B<DBD::CSV>.Alternatively, and most commonly, your driver will need to use some Ccode to gain access to the database. This will be classified as a C/XSdriver.=head2 What code will you write?There are a number of files that need to be written for either a purePerl driver or a C/XS driver. There are no extra files needed only bya pure Perl driver, but there are several extra files needed only by aC/XS driver.=head3 Files common to pure Perl and C/XS driversAssuming that your driver is called B<DBD::Driver>, these files are:=over 4=item * F<Makefile.PL>=item * F<README>=item * F<MANIFEST>=item * F<Driver.pm>=item * F<lib/Bundle/DBD/Driver.pm>=item * F<lib/DBD/Driver/Summary.pm>=item * F<t/*.t>=backThe first four files are mandatory. F<Makefile.PL> is used to controlhow the driver is built and installed. The F<README> file tells peoplewho download the file about how to build the module and any prerequisitesoftware that must be installed. The F<MANIFEST> file is used by thestandard Perl module distribution mechanism. It lists all the sourcefiles that need to be distributed with your module. F<Driver.pm> is whatis loaded by the B<DBI> code; it contains the methods peculiar to yourdriver.The F<lib/Bundle/DBD/Driver.pm> file allows you to specify other Perlmodules on which yours depends in a format that allows someone to type asimple command and ensure that all the pre-requisites are in place aswell as building your driver.The F<lib/DBD/Driver/Summary.pm> file contains (an updated version of) theinformation that was included - or that would have been included - inthe appendices of the Cheetah book as a summary of the abilities of yourdriver and the associated database.The files in the F<t> subdirectory are unit tests for your driver.You should write your tests as stringently as possible, while takinginto account the diversity of installations that you can encounter:=over 4=item *Your tests should not casually modify operational databases.=item *You should never damage existing tables in a database.=item *You should code your tests to use a constrained name space within thedatabase. For example, the tables (and all other named objects) that arecreated could all begin with 'I<dbd_drv_>'.=item *At the end of a test run, there should be no testing objects left behindin the database.=item *If you create any databases, you should remove them.=item *If your database supports temporary tables that are automaticallyremoved at the end of a session, then exploit them as often as possible.=item *Try to make your tests independent of each other. If you have atest F<t/t11dowhat.t> that depends upon the successful runningof F<t/t10thingamy.t>, people cannot run the single test caseF<t/t11dowhat.t>. Further, running F<t/t11dowhat.t> twice in a row islikely to fail (at least, if F<t/t11dowhat.t> modifies the database atall) because the database at the start of the second run is not what yousaw at the start of the first run.=item *Document in your F<README> file what you do, and what privileges peopleneed to do it.=item *You can, and probably should, sequence your tests by including a testnumber before an abbreviated version of the test name; the tests are runin the order in which the names are expanded by shell-style globbing.=item *It is in your interests to ensure that your tests work as widelyas possible.=backMany drivers also install sub-modules B<DBD::Driver::SubModule>for any of a variety of different reasons, such as to supportthe metadata methods (see the discussion of L</METADATA METHODS>below). Such sub-modules are conventionally stored in the directoryF<lib/DBD/Driver>. The module itself would usually be in a fileF<SubModule.pm>. All such sub-modules should themselves be versionstamped (see the discussions far below).=head3 Extra files needed by C/XS driversThe software for a C/XS driver will typically contain at least fourextra files that are not relevant to a pure Perl driver.=over 4=item * F<Driver.xs>=item * F<Driver.h>=item * F<dbdimp.h>=item * F<dbdimp.c>=backThe F<Driver.xs> file is used to generate C code that Perl can call to gainaccess to the C functions you write that will, in turn, call down ontoyour database software.The F<Driver.h> header is a stylized header that ensures you can access thenecessary Perl and B<DBI> macros, types, and function declarations.The F<dbdimp.h> is used to specify which functions have been implemented byyour driver.The F<dbdimp.c> file is where you write the C code that does the real workof translating between Perl-ish data types and what the database expectsto use and return.There are some (mainly small, but very important) differences betweenthe contents of F<Makefile.PL> and F<Driver.pm> for pure Perl and C/XSdrivers, so those files are described both in the section on creating apure Perl driver and in the section on creating a C/XS driver.Obviously, you can add extra source code files to the list.=head2 Requirements on a driver and driver writerTo be remotely useful, your driver must be implemented in a format thatallows it to be distributed via CPAN, the Comprehensive Perl ArchiveNetwork (L<http://www.cpan.org/> and L<http://search.cpan.org>).Of course, it is easier if you do not have to meet this criterion, butyou will not be able to ask for much help if you do not do so, andno-one is likely to want to install your module if they have to learn anew installation mechanism.=head1 CREATING A PURE PERL DRIVERWriting a pure Perl driver is surprisingly simple. However, there aresome problems you should be aware of. The best option is of coursepicking up an existing driver and carefully modifying one methodafter the other.Also look carefully at B<DBD::AnyData> and B<DBD::Template>.As an example we take a look at the B<DBD::File> driver, a driver foraccessing plain files as tables, which is part of the B<DBD::CSV> package.The minimal set of files we have to implement are F<Makefile.PL>,F<README>, F<MANIFEST> and F<Driver.pm>.=head2 Pure Perl version of Makefile.PLYou typically start with writing F<Makefile.PL>, a Makefilegenerator. The contents of this file are described in detail inthe L<ExtUtils::MakeMaker> man pages. It is definitely a good ideaif you start reading them. At least you should know about thevariables I<CONFIGURE>, I<DEFINED>, I<PM>, I<DIR>, I<EXE_FILES>,I<INC>, I<LIBS>, I<LINKTYPE>, I<NAME>, I<OPTIMIZE>, I<PL_FILES>,I<VERSION>, I<VERSION_FROM>, I<clean>, I<depend>, I<realclean> fromthe L<ExtUtils::MakeMaker> man page: these are used in almost anyF<Makefile.PL>.Additionally read the section on I<Overriding MakeMaker Methods> and thedescriptions of the I<distcheck>, I<disttest> and I<dist> targets: Theywill definitely be useful for you.Of special importance for B<DBI> drivers is the I<postamble> method fromthe L<ExtUtils::MM_Unix> man page.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -