📄 dbd.pm
字号:
package DBI::DBD;# vim:ts=8:sw=4use vars qw($VERSION); # set $VERSION early so we don't confuse PAUSE/CPAN etc$VERSION = sprintf("%d.%02d", q$Revision: 11.21 $ =~ /(\d+)\.(\d+)/o);# $Id: DBD.pm,v 11.21 2004/02/01 11:16:16 timbo Exp $## Copyright (c) 1997-2003 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 volatility $Revision: 11.21 $ $Date: 2004/02/01 11:16:16 $This document is I<still> a minimal draft which is in need of further work.The changes will occur both because the DBI specification is changingand hence the requirements on DBD drivers change, and because feedbackfrom people reading this document will suggest improvements to it.Please read the DBI documentation first and fully, including the DBI FAQ.Then reread the 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 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 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: "DON'T!"There is usually a driver already available for the database you want touse, almost regardless of which database you choose.And very often, the database will provide an ODBC driver interface, soyou can often use DBD::ODBC to access the database.This is typically less convenient on a Unix box than on a MicrosoftWindows box, but there are numerous options for ODBC driver managers onUnix too, and very often the ODBC driver is provided by the databasesupplier.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: "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 that conclusion too quickly.=head2 URLs and mailing listsThe primary web-site for locating DBI software and information is http://dbi.perl.org/There are two main and one auxilliary mailing lists for people workingwith DBI. The primary lists are dbi-users@perl.org for general usersof DBI and DBD drivers, and dbi-dev@perl.org mainly for DBD driverwriters (don't join the dbi-dev list unless you have a good reason).The auxilliary list is dbi-announce@perl.org for announcing newreleases of DBI or DBD drivers.You can join these lists by accessing the web-siteL<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 comp.lang.perl.* newsgroups,especially 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 'Programming the Perl DBI: Database programming withPerl' by Alligator Descartes and Tim Bunce, published by O'ReillyAssociates, February 2000, ISBN 1-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 DBD:: and DBIx:: links at the top to see those subsets.See the DBI docs for information on 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 do that by asking the DBI mailing lists whether there is such adriver available, 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 the name of the database software ituses, and the prefix is a contraction of that.Hence, DBD::Oracle has the name Oracle and the prefix 'ora_'.This information will be recorded in the DBI module.Apart from documentation purposes, registration is a prerequisite forL<installing private methods|DBI/install_method>.This document assumes you are writing a driver called DBD::Driver, andthat the prefix '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 example pure Perl drivers are DBD::File and 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/XS driver.=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 by a pure Perl driver, but there areseveral extra files needed only by a C/XS driver.=head3 Files common to pure Perl and C/XS driversAssuming that your driver is called DBD::Driver, these files are:=over 4=item * Makefile.PL=item * README=item * MANIFEST=item * Driver.pm=item * lib/Bundle/DBD/Driver.pm=item * lib/DBD/Driver/Summary.pm=item * t/*.t=backNeedless to say, all these files are either text files or pure Perl.The first four files are mandatory.Makefile.PL is used to control how the driver is built and installed.The README file tells people who download the file about how to buildthe module and any prerequisite software that must be installed.The MANIFEST file is used by the standard Perl module distribution mechanism.It lists all the source files that need to be distributed with your module.Driver.pm is what is loaded by the DBI code; it contains the methodspeculiar to your driver.The 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 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 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.Your tests should not casually modify operational databases.You should never damage existing tables in a database.You should code your tests to use a constrained name space within thedatabase.For example, the tables (and all other named objects) that are createdcould all begin with 'dbd_drv_'.At the end of a test run, there should be no testing objects left behindin the database.If you create any databases, you should remove them.If your database supports temporary tables that are automaticallyremoved at the end of a session, then exploit them as often as possible.Try to make your tests independent of each other.If you have a test t/t11dowhat.t that depends upon the successfulrunning of t/t10thingamy.t, people cannot run the single test caset/t11dowhat.t.Further, running t/t11dowhat.t twice in a row is likely to fail (atleast, if t/t11dowhat.t modifies the database at all) because thedatabase at the start of the second run is not what you saw at the startof the first run.Document in your README file what you do, and what privileges peopleneed to do it.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.Many drivers also install sub-modules DBD::Driver::SubModule for any ofa variety of different reasons, such as to support the metadata methods(see the discussion of L</METADATA METHODS> below).Such sub-modules are conventionally stored in the directory lib/DBD/Driver.The module itself would usually be in a file SubModule.pm.All such sub-modules should themselves be version stamped (see thediscussions 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 * Driver.xs=item * Driver.h=item * dbdimp.h=item * dbdimp.c=backThe 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 Driver.h header is a stylized header that ensures you can access thenecessary Perl and DBI macros, types, and function declarations.The dbdimp.h is used to specify which functions have been implemented byyour driver.The 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 Makefile.PL and 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 (http://www.cpan.org/ and 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 DBD::AnyData and DBD::Template.As an example we take a look at the I<DBD::File> driver, a driver foraccessing plain files as tables, which is part of the I<DBD::CSV>package.In what follows I assume the name C<Driver> for your new package and theprefix 'drv_'.The minimal set of files we have to implement are I<Makefile.PL>,I<README>, I<MANIFEST> and I<Driver.pm>.Files in the 'nice to have' category include '=head2 Pure Perl version of Makefile.PLYou typically start with writing C<Makefile.PL>, a Makefile generator.The contents of this file are described in detail in theC<ExtUtils::MakeMaker> man pages.It is definitely a good idea if you start reading them.At least you should know about the variables 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> from the C<ExtUtils::MakeMaker> man page: Theseare used in almost any 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 DBI drivers is the I<postamble> method fromthe C<ExtUtils::MM_Unix> man page.And, for Emacs users, I recommend the I<libscan> method, which removesEmacs backup files (file names which end with a tilde '~') from lists offiles.Now an example, I use the word C<Driver> wherever you should insertyour driver's name: # -*- perl -*- use DBI 1.03; use DBI::DBD; use ExtUtils::MakeMaker; WriteMakefile( dbd_edit_mm_attribs( { 'NAME' => 'DBD::Driver', 'VERSION_FROM' => 'Driver.pm', 'INC' => $DBI_INC_DIR, 'dist' => { 'SUFFIX' => '.gz', 'COMPRESS' => 'gzip -9f' }, 'realclean' => { FILES => '*.xsi' }, }, { create_pp_tests => 1}) ); package MY; sub postamble { return main::dbd_postamble(@_); } sub libscan { my ($self, $path) = @_; ($path =~ m/\~$/) ? undef : $path; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -