<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package DBI::DBD;
# vim:ts=8:sw=4

use 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 15128 2012-02-04 20:51:39Z timbo $ =~ /(\d+)/o);


# $Id: DBD.pm 15128 2012-02-04 20:51:39Z 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 NAME

DBI::DBD - Perl DBI Database Driver Writer's Guide

=head1 SYNOPSIS

  perldoc DBI::DBD

=head2 Version and volatility

This document is I&lt;still&gt; a minimal draft which is in need of further work.

The changes will occur both because the B&lt;DBI&gt; specification is changing
and hence the requirements on B&lt;DBD&gt; drivers change, and because feedback
from people reading this document will suggest improvements to it.

Please read the B&lt;DBI&gt; documentation first and fully, including the B&lt;DBI&gt; FAQ.
Then reread the B&lt;DBI&gt; 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 DESCRIPTION

This document is primarily intended to help people writing new
database drivers for the Perl Database Interface (Perl DBI).
It may also help others interested in discovering why the internals of
a B&lt;DBD&gt; driver are written the way they are.

This is a guide.  Few (if any) of the statements in it are completely
authoritative under all possible circumstances.  This means you will
need to use judgement in applying the guidelines in this document.
If in I&lt;any&gt; doubt at all, please do contact the I&lt;dbi-dev&gt; mailing list
(details given below) where Tim Bunce and other driver authors can help.

=head1 CREATING A NEW DRIVER

The first rule for creating a new database driver for the Perl DBI is
very simple: B&lt;DON'T!&gt;

There is usually a driver already available for the database you want
to use, almost regardless of which database you choose. Very often, the
database will provide an ODBC driver interface, so you can often use
B&lt;DBD::ODBC&gt; to access the database. This is typically less convenient
on a Unix box than on a Microsoft Windows box, but there are numerous
options for ODBC driver managers on Unix too, and very often the ODBC
driver is provided by the database supplier.

Before deciding that you need to write a driver, do your homework to
ensure that you are not wasting your energies.

[As of December 2002, the consensus is that if you need an ODBC driver
manager on Unix, then the unixODBC driver (available from
L&lt;http://www.unixodbc.org/&gt;) is the way to go.]

The second rule for creating a new database driver for the Perl DBI is
also very simple: B&lt;Don't -- get someone else to do it for you!&gt;

Nevertheless, there are occasions when it is necessary to write a new
driver, often to use a proprietary language or API to access the
database more swiftly, or more comprehensively, than an ODBC driver can.
Then you should read this document very carefully, but with a suitably
sceptical 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 lists

The primary web-site for locating B&lt;DBI&gt; software and information is

  http://dbi.perl.org/

There are two main and one auxiliary mailing lists for people working
with B&lt;DBI&gt;.  The primary lists are I&lt;dbi-users@perl.org&gt; for general users
of B&lt;DBI&gt; and B&lt;DBD&gt; drivers, and I&lt;dbi-dev@perl.org&gt; mainly for B&lt;DBD&gt; driver
writers (don't join the I&lt;dbi-dev&gt; list unless you have a good reason).
The auxiliary list is I&lt;dbi-announce@perl.org&gt; for announcing new
releases of B&lt;DBI&gt; or B&lt;DBD&gt; drivers.

You can join these lists by accessing the web-site L&lt;http://dbi.perl.org/&gt;.
The lists are closed so you cannot send email to any of the lists
unless you join the list first.

You should also consider monitoring the I&lt;comp.lang.perl.*&gt; newsgroups,
especially I&lt;comp.lang.perl.modules&gt;.

=head2 The Cheetah book

The definitive book on Perl DBI is the Cheetah book, so called because
of the picture on the cover. Its proper title is 'I&lt;Programming the
Perl DBI: Database programming with Perl&gt;' by Alligator Descartes
and Tim Bunce, published by O'Reilly Associates, February 2000, ISBN
1-56592-699-4. Buy it now if you have not already done so, and read it.

=head2 Locating drivers

Before writing a new driver, it is in your interests to find out
whether there already is a driver for your database.  If there is such
a driver, it would be much easier to make use of it than to write your
own!

The primary web-site for locating Perl software is
L&lt;http://search.cpan.org/&gt;.  You should look under the various
modules listings for the software you are after. For example:

  http://search.cpan.org/modlist/Database_Interfaces

Follow the B&lt;DBD::&gt; and B&lt;DBIx::&gt; links at the top to see those subsets.

See the B&lt;DBI&gt; docs for information on B&lt;DBI&gt; web sites and mailing lists.

=head2 Registering a new driver

Before going through any official registration process, you will need
to establish that there is no driver already in the works. You'll do
that by asking the B&lt;DBI&gt; mailing lists whether there is such a driver
available, or whether anybody is working on one.

When you get the go ahead, you will need to establish the name of the
driver and a prefix for the driver. Typically, the name is based on the
name of the database software it uses, and the prefix is a contraction
of that. Hence, B&lt;DBD::Oracle&gt; has the name I&lt;Oracle&gt; and the prefix
'I&lt;ora_&gt;'. The prefix must be lowercase and contain no underscores other
than the one at the end.

This information will be recorded in the B&lt;DBI&gt; module. Apart from
documentation purposes, registration is a prerequisite for
L&lt;installing private methods|DBI/install_method&gt;.

If you are writing a driver which will not be distributed on CPAN, then
you should choose a prefix beginning with 'I&lt;x_&gt;', to avoid potential
prefix collisions with drivers registered in the future. Thus, if you
wrote a non-CPAN distributed driver called B&lt;DBD::CustomDB&gt;, the prefix
might be 'I&lt;x_cdb_&gt;'.

This document assumes you are writing a driver called B&lt;DBD::Driver&gt;, and
that the prefix 'I&lt;drv_&gt;' is assigned to the driver.

=head2 Two styles of database driver

There are two distinct styles of database driver that can be written to
work 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 not
written in such a way that this can be done. Some examples of pure
Perl drivers are B&lt;DBD::File&gt; and B&lt;DBD::CSV&gt;.

Alternatively, and most commonly, your driver will need to use some C
code 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 pure
Perl driver or a C/XS driver. There are no extra files needed only by
a pure Perl driver, but there are several extra files needed only by a
C/XS driver.

=head3 Files common to pure Perl and C/XS drivers

Assuming that your driver is called B&lt;DBD::Driver&gt;, these files are:

=over 4

=item * F&lt;Makefile.PL&gt;

=item * F&lt;META.yml&gt;

=item * F&lt;README&gt;

=item * F&lt;MANIFEST&gt;

=item * F&lt;Driver.pm&gt;

=item * F&lt;lib/Bundle/DBD/Driver.pm&gt;

=item * F&lt;lib/DBD/Driver/Summary.pm&gt;

=item * F&lt;t/*.t&gt;

=back

The first four files are mandatory. F&lt;Makefile.PL&gt; is used to control
how the driver is built and installed. The F&lt;README&gt; file tells people
who download the file about how to build the module and any prerequisite
software that must be installed. The F&lt;MANIFEST&gt; file is used by the
standard Perl module distribution mechanism. It lists all the source
files that need to be distributed with your module. F&lt;Driver.pm&gt; is what
is loaded by the B&lt;DBI&gt; code; it contains the methods peculiar to your
driver.

Although the F&lt;META.yml&gt; file is not B&lt;required&gt; you are advised to
create one. Of particular importance are the I&lt;build_requires&gt; and
I&lt;configure_requires&gt; attributes which newer CPAN modules understand.
You use these to tell the CPAN module (and CPANPLUS) that your build
and configure mechanisms require DBI. The best reference for META.yml
(at the time of writing) is
L&lt;http://module-build.sourceforge.net/META-spec-v1.4.html&gt;. You can find
a reasonable example of a F&lt;META.yml&gt; in DBD::ODBC.

The F&lt;lib/Bundle/DBD/Driver.pm&gt; file allows you to specify other Perl
modules on which yours depends in a format that allows someone to type a
simple command and ensure that all the pre-requisites are in place as
well as building your driver.

The F&lt;lib/DBD/Driver/Summary.pm&gt; file contains (an updated version of) the
information that was included - or that would have been included - in
the appendices of the Cheetah book as a summary of the abilities of your
driver and the associated database.

The files in the F&lt;t&gt; subdirectory are unit tests for your driver.
You should write your tests as stringently as possible, while taking
into 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 the
database. For example, the tables (and all other named objects) that are
created could all begin with 'I&lt;dbd_drv_&gt;'.

=item *

At the end of a test run, there should be no testing objects left behind
in the database.

=item *

If you create any databases, you should remove them.

=item *

If your database supports temporary tables that are automatically
removed 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 a
test F&lt;t/t11dowhat.t&gt; that depends upon the successful running
of F&lt;t/t10thingamy.t&gt;, people cannot run the single test case
F&lt;t/t11dowhat.t&gt;. Further, running F&lt;t/t11dowhat.t&gt; twice in a row is
likely to fail (at least, if F&lt;t/t11dowhat.t&gt; modifies the database at
all) because the database at the start of the second run is not what you
saw at the start of the first run.

=item *

Document in your F&lt;README&gt; file what you do, and what privileges people
need to do it.

=item *

You can, and probably should, sequence your tests by including a test
number before an abbreviated version of the test name; the tests are run
in 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 widely
as possible.

=back

Many drivers also install sub-modules B&lt;DBD::Driver::SubModule&gt;
for any of a variety of different reasons, such as to support
the metadata methods (see the discussion of L&lt;/METADATA METHODS&gt;
below). Such sub-modules are conventionally stored in the directory
F&lt;lib/DBD/Driver&gt;. The module itself would usually be in a file
F&lt;SubModule.pm&gt;. All such sub-modules should themselves be version
stamped (see the discussions far below).

=head3 Extra files needed by C/XS drivers

The software for a C/XS driver will typically contain at least four
extra files that are not relevant to a pure Perl driver.

=over 4

=item * F&lt;Driver.xs&gt;

=item * F&lt;Driver.h&gt;

=item * F&lt;dbdimp.h&gt;

=item * F&lt;dbdimp.c&gt;

=back

The F&lt;Driver.xs&gt; file is used to generate C code that Perl can call to gain
access to the C functions you write that will, in turn, call down onto
your database software.

The F&lt;Driver.h&gt; header is a stylized header that ensures you can access the
necessary Perl and B&lt;DBI&gt; macros, types, and function declarations.

The F&lt;dbdimp.h&gt; is used to specify which functions have been implemented by
your driver.

The F&lt;dbdimp.c&gt; file is where you write the C code that does the real work
of translating between Perl-ish data types and what the database expects
to use and return.

There are some (mainly small, but very important) differences between
the contents of F&lt;Makefile.PL&gt; and F&lt;Driver.pm&gt; for pure Perl and C/XS
drivers, so those files are described both in the section on creating a
pure 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 writer

To be remotely useful, your driver must be implemented in a format that
allows it to be distributed via CPAN, the Comprehensive Perl Archive
Network (L&lt;http://www.cpan.org/&gt; and L&lt;http://search.cpan.org&gt;).
Of course, it is easier if you do not have to meet this criterion, but
you will not be able to ask for much help if you do not do so, and
no-one is likely to want to install your module if they have to learn a
new installation mechanism.

=head1 CREATING A PURE PERL DRIVER

Writing a pure Perl driver is surprisingly simple. However, there are
some problems you should be aware of. The best option is of course
picking up an existing driver and carefully modifying one method
after the other.

Also look carefully at B&lt;DBD::AnyData&gt; and B&lt;DBD::Template&gt;.

As an example we take a look at the B&lt;DBD::File&gt; driver, a driver for
accessing plain files as tables, which is part of the B&lt;DBD::CSV&gt; package.

The minimal set of files we have to implement are F&lt;Makefile.PL&gt;,
F&lt;README&gt;, F&lt;MANIFEST&gt; and F&lt;Driver.pm&gt;.

=head2 Pure Perl version of Makefile.PL

You typically start with writing F&lt;Makefile.PL&gt;, a Makefile
generator. The contents of this file are described in detail in
the L&lt;ExtUtils::MakeMaker&gt; man pages. It is definitely a good idea
if you start reading them. At least you should know about the
variables I&lt;CONFIGURE&gt;, I&lt;DEFINED&gt;, I&lt;PM&gt;, I&lt;DIR&gt;, I&lt;EXE_FILES&gt;,
I&lt;INC&gt;, I&lt;LIBS&gt;, I&lt;LINKTYPE&gt;, I&lt;NAME&gt;, I&lt;OPTIMIZE&gt;, I&lt;PL_FILES&gt;,
I&lt;VERSION&gt;, I&lt;VERSION_FROM&gt;, I&lt;clean&gt;, I&lt;depend&gt;, I&lt;realclean&gt; from
the L&lt;ExtUtils::MakeMaker&gt; man page: these are used in almost any
F&lt;Makefile.PL&gt;.

Additionally read the section on I&lt;Overriding MakeMaker Methods&gt; and the
descriptions of the I&lt;distcheck&gt;, I&lt;disttest&gt; and I&lt;dist&gt; targets: They
will definitely be useful for you.

Of special importance for B&lt;DBI&gt; drivers is the I&lt;postamble&gt; method from
the L&lt;ExtUtils::MM_Unix&gt; man page.

For Emacs users, I recommend the I&lt;libscan&gt; method, which removes
Emacs backup files (file names which end with a tilde '~') from lists of
files.

Now an example, I use the word C&lt;Driver&gt; wherever you should insert
your driver's name:

  # -*- perl -*-

  use ExtUtils::MakeMaker;

  WriteMakefile(
      dbd_edit_mm_attribs( {
          'NAME'         =&gt; 'DBD::Driver',
          'VERSION_FROM' =&gt; 'Driver.pm',
          'INC'          =&gt; '',
          'dist'         =&gt; { 'SUFFIX'   =&gt; '.gz',
                              'COMPRESS' =&gt; 'gzip -9f' },
          'realclean'    =&gt; { FILES =&gt; '*.xsi' },
          'PREREQ_PM'    =&gt; '1.03',
          'CONFIGURE'    =&gt; sub {
              eval {require DBI::DBD;};
              if ($@) {
                  warn $@;
                  exit 0;
              }
              my $dbi_arch_dir = dbd_dbi_arch_dir();
              if (exists($opts{INC})) {
                  return {INC =&gt; "$opts{INC} -I$dbi_arch_dir"};
              } else {
                  return {INC =&gt; "-I$dbi_arch_dir"};
              }
          }
      },
      { create_pp_tests =&gt; 1})
  );

  package MY;
  sub postamble { return main::dbd_postamble(@_); }
  sub libscan {
      my ($self, $path) = @_;
      ($path =~ m/\~$/) ? undef : $path;
  }

Note the calls to C&lt;dbd_edit_mm_attribs()&gt; and C&lt;dbd_postamble()&gt;.

The second hash reference in the call to C&lt;dbd_edit_mm_attribs()&gt;
(containing C&lt;create_pp_tests()&gt;) is optional; you should not use it
unless your driver is a pure Perl driver (that is, it does not use C and
XS code). Therefore, the call to C&lt;dbd_edit_mm_attribs()&gt; is not
relevant for C/XS drivers and may be omitted; simply use the (single)
hash reference containing NAME etc as the only argument to C&lt;WriteMakefile()&gt;.

Note that the C&lt;dbd_edit_mm_attribs()&gt; code will fail if you do not have a
F&lt;t&gt; sub-directory containing at least one test case.

I&lt;PREREQ_PM&gt; tells MakeMaker that DBI (version 1.03 in this case) is
required for this module. This will issue a warning that DBI 1.03 is
missing if someone attempts to install your DBD without DBI 1.03. See
I&lt;CONFIGURE&gt; below for why this does not work reliably in stopping cpan
testers failing your module if DBI is not installed.

I&lt;CONFIGURE&gt; is a subroutine called by MakeMaker during
C&lt;WriteMakefile&gt;.  By putting the C&lt;require DBI::DBD&gt; in this section
we can attempt to load DBI::DBD but if it is missing we exit with
success. As we exit successfully without creating a Makefile when
DBI::DBD is missing cpan testers will not report a failure. This may
seem at odds with I&lt;PREREQ_PM&gt; but I&lt;PREREQ_PM&gt; does not cause
C&lt;WriteMakefile&gt; to fail (unless you also specify PREREQ_FATAL which
is strongly discouraged by MakeMaker) so C&lt;WriteMakefile&gt; would
continue to call C&lt;dbd_dbi_arch_dir&gt; and fail.

All drivers must use C&lt;dbd_postamble()&gt; or risk running into problems.

Note the specification of I&lt;VERSION_FROM&gt;; the named file
(F&lt;Driver.pm&gt;) will be scanned for the first line that looks like an
assignment to I&lt;$VERSION&gt;, and the subsequent text will be used to
determine the version number.  Note the commentary in
L&lt;ExtUtils::MakeMaker&gt; on the subject of correctly formatted version
numbers.

If your driver depends upon external software (it usually will), you
will need to add code to ensure that your environment is workable
before the call to C&lt;WriteMakefile()&gt;. If you need to check for the
existence of an external library and perhaps modify I&lt;INC&gt; to include
the paths to where the external library header files are located and
you cannot find the library or header files make sure you output a
message saying they cannot be found but C&lt;exit 0&gt; (success) B&lt;before&gt;
calling C&lt;WriteMakefile&gt; or CPAN testers will fail your module if the
external library is not found.

A full-fledged I&lt;Makefile.PL&gt; can be quite large (for example, the
files for B&lt;DBD::Oracle&gt; and B&lt;DBD::Informix&gt; are both over 1000 lines
long, and the Informix one uses - and creates - auxiliary modules
too).

See also L&lt;ExtUtils::MakeMaker&gt; and L&lt;ExtUtils::MM_Unix&gt;. Consider using
L&lt;CPAN::MakeMaker&gt; in place of I&lt;ExtUtils::MakeMaker&gt;.

=head2 README

The L&lt;README&gt; file should describe what the driver is for, the
pre-requisites for the build process, the actual build process, how to
report errors, and who to report them to.

Users will find ways of breaking the driver build and test process
which you would never even have dreamed to be possible in your worst
nightmares. Therefore, you need to write this document defensively,
precisely and concisely.

As always, use the F&lt;README&gt; from one of the established drivers as a basis
for your own; the version in B&lt;DBD::Informix&gt; is worth a look as it has
been quite successful in heading off problems.

=over 4

=item *

Note that users will have versions of Perl and B&lt;DBI&gt; that are both older
and newer than you expected, but this will seldom cause much trouble.
When it does, it will be because you are using features of B&lt;DBI&gt; that are
not supported in the version they are using.

=item *

Note that users will have versions of the database software that are
both older and newer than you expected. You will save yourself time in
the long run if you can identify the range of versions which have been
tested and warn about versions which are not known to be OK.

=item *

Note that many people trying to install your driver will not be experts
in the database software.

=item *

Note that many people trying to install your driver will not be experts
in C or Perl.

=back

=head2 MANIFEST

The F&lt;MANIFEST&gt; will be used by the Makefile's dist target to build the
distribution tar file that is uploaded to CPAN. It should list every
file that you want to include in your distribution, one per line.

=head2 lib/Bundle/DBD/Driver.pm

The CPAN module provides an extremely powerful bundle mechanism that
allows you to specify pre-requisites for your driver.

The primary pre-requisite is B&lt;Bundle::DBI&gt;; you may want or need to add
some more. With the bundle set up correctly, the user can type:

        perl -MCPAN -e 'install Bundle::DBD::Driver'

and Perl will download, compile, test and install all the Perl modules
needed to build your driver.

The prerequisite modules are listed in the C&lt;CONTENTS&gt; section, with the
official name of the module followed by a dash and an informal name or
description.

=over 4

=item *

Listing B&lt;Bundle::DBI&gt; as the main pre-requisite simplifies life.

=item *

Don't forget to list your driver.

=item *

Note that unless the DBMS is itself a Perl module, you cannot list it as
a pre-requisite in this file.

=item *

You should keep the version of the bundle the same as the version of
your driver.

=item *

You should add configuration management, copyright, and licencing
information at the top.

=back

A suitable skeleton for this file is shown below.

  package Bundle::DBD::Driver;

  $VERSION = '0.01';

  1;

  __END__

  =head1 NAME

  Bundle::DBD::Driver - A bundle to install all DBD::Driver related modules

  =head1 SYNOPSIS

  C&lt;perl -MCPAN -e 'install Bundle::DBD::Driver'&gt;

  =head1 CONTENTS

  Bundle::DBI  - Bundle for DBI by TIMB (Tim Bunce)

  DBD::Driver  - DBD::Driver by YOU (Your Name)

  =head1 DESCRIPTION

  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.

  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:

    C&lt;perl -MCPAN -e 'install Bundle::CPAN'&gt;

  =head1 SEE ALSO

  Bundle::DBI

  =head1 AUTHOR

  Your Name E&lt;lt&gt;F&lt;you@yourdomain.com&gt;E&lt;gt&gt;

  =head1 THANKS

  This bundle was created by ripping off Bundle::libnet created by
  Graham Barr E&lt;lt&gt;F&lt;gbarr@ti.com&gt;E&lt;gt&gt;, and radically simplified
  with some information from Jochen Wiedmann E&lt;lt&gt;F&lt;joe@ispsoft.de&gt;E&lt;gt&gt;.
  The template was then included in the DBI::DBD documentation by
  Jonathan Leffler E&lt;lt&gt;F&lt;jleffler@informix.com&gt;E&lt;gt&gt;.

  =cut

=head2 lib/DBD/Driver/Summary.pm

There is no substitute for taking the summary file from a driver that
was documented in the Perl book (such as B&lt;DBD::Oracle&gt; or B&lt;DBD::Informix&gt; or
B&lt;DBD::ODBC&gt;, to name but three), and adapting it to describe the
facilities available via B&lt;DBD::Driver&gt; when accessing the Driver database.

=head2 Pure Perl version of Driver.pm

The F&lt;Driver.pm&gt; file defines the Perl module B&lt;DBD::Driver&gt; for your driver.
It will define a package B&lt;DBD::Driver&gt; along with some version information,
some variable definitions, and a function C&lt;driver()&gt; which will have a more
or less standard structure.

It will also define three sub-packages of B&lt;DBD::Driver&gt;:

=over 4

=item DBD::Driver::dr

with methods C&lt;connect()&gt;, C&lt;data_sources()&gt; and C&lt;disconnect_all()&gt;;

=item DBD::Driver::db

with methods such as C&lt;prepare()&gt;;

=item DBD::Driver::st

with methods such as C&lt;execute()&gt; and C&lt;fetch()&gt;.

=back

The F&lt;Driver.pm&gt; file will also contain the documentation specific to
B&lt;DBD::Driver&gt; in the format used by perldoc.

In a pure Perl driver, the F&lt;Driver.pm&gt; file is the core of the
implementation. You will need to provide all the key methods needed by B&lt;DBI&gt;.

Now let's take a closer look at an excerpt of F&lt;File.pm&gt; as an example.
We ignore things that are common to any module (even non-DBI modules)
or really specific to the B&lt;DBD::File&gt; package.

=head3 The DBD::Driver package

=head4 The header

  package DBD::File;

  use strict;
  use vars qw($VERSION $drh);

  $VERSION = "1.23.00"  # Version number of DBD::File

This is where the version number of your driver is specified, and is
where F&lt;Makefile.PL&gt; looks for this information. Please ensure that any
other modules added with your driver are also version stamped so that
CPAN does not get confused.

It is recommended that you use a two-part (1.23) or three-part (1.23.45)
version number. 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 Subversion you could use:

  $VERSION = sprintf("12.%06d", q$Revision: 12345 $ =~ /(\d+)/o);

(use lots of leading zeros on the second portion so if you move the code to a
shared repository like svn.perl.org the much larger revision numbers won't
cause a problem, at least not for a few years).  For RCS or CVS you can use:

  $VERSION = sprintf "%d.%02d", '$Revision: 11.21 $ ' =~ /(\d+)\.(\d+)/;

which pads out the fractional part with leading zeros so all is well
(so long as you don't go past x.99)

  $drh = undef;         # holds driver handle once initialized

This is where the driver handle will be stored, once created.
Note that you may assume there is only one handle for your driver.

=head4 The driver constructor

The C&lt;driver()&gt; method is the driver handle constructor. Note that
the C&lt;driver()&gt; method is in the B&lt;DBD::Driver&gt; package, not in
one of the sub-packages B&lt;DBD::Driver::dr&gt;, B&lt;DBD::Driver::db&gt;, or
B&lt;DBD::Driver::db&gt;.

  sub driver
  {
      return $drh if $drh;      # already created - return same one
      my ($class, $attr) = @_;

      $class .= "::dr";

      DBD::Driver::db-&gt;install_method('drv_example_dbh_method');
      DBD::Driver::st-&gt;install_method('drv_example_sth_method');

      # not a 'my' since we use it above to prevent multiple drivers
      $drh = DBI::_new_drh($class, {
              'Name'        =&gt; 'File',
              'Version'     =&gt; $VERSION,
              'Attribution' =&gt; 'DBD::File by Jochen Wiedmann',
          })
          or return undef;

      return $drh;
  }

This is a reasonable example of how B&lt;DBI&gt; implements its handles. There
are three kinds: B&lt;driver handles&gt; (typically stored in I&lt;$drh&gt;; from
now on called I&lt;drh&gt; or I&lt;$drh&gt;), B&lt;database handles&gt; (from now on
called I&lt;dbh&gt; or I&lt;$dbh&gt;) and B&lt;statement handles&gt; (from now on called
I&lt;sth&gt; or I&lt;$sth&gt;).

The prototype of C&lt;DBI::_new_drh()&gt; is

  $drh = DBI::_new_drh($class, $public_attrs, $private_attrs);

with the following arguments:

=over 4

=item I&lt;$class&gt;

is typically the class for your driver, (for example, "DBD::File::dr"),
passed as the first argument to the C&lt;driver()&gt; method.

=item I&lt;$public_attrs&gt;

is a hash ref to attributes like I&lt;Name&gt;, I&lt;Version&gt;, and I&lt;Attribution&gt;.
These are processed and used by B&lt;DBI&gt;. You had better not make any
assumptions about them nor should you add private attributes here.

=item I&lt;$private_attrs&gt;

This is another (optional) hash ref with your private attributes.
B&lt;DBI&gt; will store them and otherwise leave them alone.

=back

The C&lt;DBI::_new_drh()&gt; method and the C&lt;driver()&gt; method both return C&lt;undef&gt;
for failure (in which case you must look at I&lt;$DBI::err&gt; and I&lt;$DBI::errstr&gt;
for the failure information, because you have no driver handle to use).


=head4 Using install_method() to expose driver-private methods

    DBD::Foo::db-&gt;install_method($method_name, \%attr);

Installs the driver-private method named by $method_name into the
DBI method dispatcher so it can be called directly, avoiding the
need to use the func() method.

It is called as a static method on the driver class to which the
method belongs. The method name must begin with the corresponding
registered driver-private prefix. For example, for DBD::Oracle
$method_name must being with 'C&lt;ora_&gt;', and for DBD::AnyData it
must begin with 'C&lt;ad_&gt;'.

The C&lt;\%attr&gt; attributes can be used to provide fine control over how the DBI
dispatcher handles the dispatching of the method. However it's undocumented
at the moment. See the IMA_* #define's in DBI.xs and the O=&gt;0x000x values in
the initialization of %DBI::DBI_methods in DBI.pm.  (Volunteers to polish up
and document the interface are very welcome to get in touch via dbi-dev@perl.org).

Methods installed using install_method default to the standard error
handling behaviour for DBI methods: clearing err and errstr before
calling the method, and checking for errors to trigger RaiseError 
etc. on return. This differs from the default behaviour of func(). 

Note for driver authors: The DBD::Foo::xx-&gt;install_method call won't
work until the class-hierarchy has been setup. Normally the DBI
looks after that just after the driver is loaded. This means
install_method() can't be called at the time the driver is loaded
unless the class-hierarchy is set up first. The way to do that is
to call the setup_driver() method:

    DBI-&gt;setup_driver('DBD::Foo');

before using install_method().


=head4 The CLONE special subroutine

Also needed here, in the B&lt;DBD::Driver&gt; package, is a C&lt;CLONE()&gt; method
that will be called by perl when an interpreter is cloned. All your
C&lt;CLONE()&gt; method needs to do, currently, is clear the cached I&lt;$drh&gt; so
the new interpreter won't start using the cached I&lt;$drh&gt; from the old
interpreter:

  sub CLONE {
    undef $drh;
  }

See L&lt;http://search.cpan.org/dist/perl/pod/perlmod.pod#Making_your_module_threadsafe&gt;
for details.

=head3 The DBD::Driver::dr package

The next lines of code look as follows:

  package DBD::Driver::dr; # ====== DRIVER ======

  $DBD::Driver::dr::imp_data_size = 0;

Note that no I&lt;@ISA&gt; is needed here, or for the other B&lt;DBD::Driver::*&gt;
classes, because the B&lt;DBI&gt; takes care of that for you when the driver is
loaded.

 *FIX ME* Explain what the imp_data_size is, so that implementors aren't
 practicing cargo-cult programming.

=head4 The database handle constructor

The database handle constructor is the driver's (hence the changed
namespace) C&lt;connect()&gt; method:

  sub connect
  {
      my ($drh, $dr_dsn, $user, $auth, $attr) = @_;

      # Some database specific verifications, default settings
      # and the like can go here. This should only include
      # syntax checks or similar stuff where it's legal to
      # 'die' in case of errors.
      # For example, many database packages requires specific
      # environment variables to be set; this could be where you
      # validate that they are set, or default them if they are not set.

      my $driver_prefix = "drv_"; # the assigned prefix for this driver

      # Process attributes from the DSN; we assume ODBC syntax
      # here, that is, the DSN looks like var1=val1;...;varN=valN
      foreach my $var ( split /;/, $dr_dsn ) {
          my ($attr_name, $attr_value) = split '=', $var, 2;
	  return $drh-&gt;set_err($DBI::stderr, "Can't parse DSN part '$var'")
              unless defined $attr_value;

          # add driver prefix to attribute name if it doesn't have it already
          $attr_name = $driver_prefix.$attr_name
              unless $attr_name =~ /^$driver_prefix/o;

	  # Store attribute into %$attr, replacing any existing value.
          # The DBI will STORE() these into $dbh after we've connected
	  $attr-&gt;{$attr_name} = $attr_value;
      }

      # Get the attributes we'll use to connect.
      # We use delete here because these no need to STORE them
      my $db = delete $attr-&gt;{drv_database} || delete $attr-&gt;{drv_db}
          or return $drh-&gt;set_err($DBI::stderr, "No database name given in DSN '$dr_dsn'");
      my $host = delete $attr-&gt;{drv_host} || 'localhost';
      my $port = delete $attr-&gt;{drv_port} || 123456;

      # Assume you can attach to your database via drv_connect:
      my $connection = drv_connect($db, $host, $port, $user, $auth)
          or return $drh-&gt;set_err($DBI::stderr, "Can't connect to $dr_dsn: ...");

      # create a 'blank' dbh (call superclass constructor)
      my ($outer, $dbh) = DBI::_new_dbh($drh, { Name =&gt; $dr_dsn });

      $dbh-&gt;STORE('Active', 1 );
      $dbh-&gt;{drv_connection} = $connection;

      return $outer;
  }

This is mostly the same as in the I&lt;driver handle constructor&gt; above.
The arguments are described in L&lt;DBI&gt;.

The constructor C&lt;DBI::_new_dbh()&gt; is called, returning a database handle.
The constructor's prototype is:

  ($outer, $inner) = DBI::_new_dbh($drh, $public_attr, $private_attr);

with similar arguments to those in the I&lt;driver handle constructor&gt;,
except that the I&lt;$class&gt; is replaced by I&lt;$drh&gt;. The I&lt;Name&gt; attribute
is a standard B&lt;DBI&gt; attribute (see L&lt;DBI/Database Handle Attributes&gt;).

In scalar context, only the outer handle is returned.

Note the use of the C&lt;STORE()&gt; method for setting the I&lt;dbh&gt; attributes.
That's because within the driver code, the handle object you have is
the 'inner' handle of a tied hash, not the outer handle that the
users of your driver have.

Because you have the inner handle, tie magic doesn't get invoked
when you get or set values in the hash. This is often very handy for
speed when you want to get or set simple non-special driver-specific
attributes.

However, some attribute values, such as those handled by the B&lt;DBI&gt; like
I&lt;PrintError&gt;, don't actually exist in the hash and must be read via
C&lt;$h-E&lt;gt&gt;FETCH($attrib)&gt; and set via C&lt;$h-E&lt;gt&gt;STORE($attrib, $value)&gt;.
If in any doubt, use these methods.

=head4 The data_sources() method

The C&lt;data_sources()&gt; method must populate and return a list of valid data
sources, prefixed with the "I&lt;dbi:Driver&gt;" incantation that allows them to
be used in the first argument of the C&lt;DBI-E&lt;gt&gt;connect()&gt; method.
An example of this might be scanning the F&lt;$HOME/.odbcini&gt; file on Unix
for ODBC data sources (DSNs).

As a trivial example, consider a fixed list of data sources:

  sub data_sources
  {
      my($drh, $attr) = @_;
      my(@list) = ();
      # You need more sophisticated code than this to set @list...
      push @list, "dbi:Driver:abc";
      push @list, "dbi:Driver:def";
      push @list, "dbi:Driver:ghi";
      # End of code to set @list
      return @list;
  }

=head4 The disconnect_all() method

If you need to release any resources when the driver is unloaded, you
can provide a disconnect_all method.

=head4 Other driver handle methods

If you need any other driver handle methods, they can follow here.

=head4 Error handling

It is quite likely that something fails in the connect method.
With B&lt;DBD::File&gt; for example, you might catch an error when setting the
current directory to something not existent by using the
(driver-specific) I&lt;f_dir&gt; attribute.

To report an error, you use the C&lt;set_err()&gt; method:

  $h-&gt;set_err($err, $errmsg, $state);

This will ensure that the error is recorded correctly and that
I&lt;RaiseError&gt; and I&lt;PrintError&gt; etc are handled correctly.

Typically you'll always use the method instance, aka your method's first
argument.

As C&lt;set_err()&gt; always returns C&lt;undef&gt; your error handling code can
usually be simplified to something like this:

  return $h-&gt;set_err($err, $errmsg, $state) if ...;

=head3 The DBD::Driver::db package

  package DBD::Driver::db; # ====== DATABASE ======

  $DBD::Driver::db::imp_data_size = 0;

=head4 The statement handle constructor

There's nothing much new in the statement handle constructor, which
is the C&lt;prepare()&gt; method:

  sub prepare
  {
      my ($dbh, $statement, @attribs) = @_;

      # create a 'blank' sth
      my ($outer, $sth) = DBI::_new_sth($dbh, { Statement =&gt; $statement });

      $sth-&gt;STORE('NUM_OF_PARAMS', ($statement =~ tr/?//));

      $sth-&gt;{drv_params} = [];

      return $outer;
  }

This is still the same -- check the arguments and call the super class
constructor C&lt;DBI::_new_sth()&gt;. Again, in scalar context, only the outer
handle is returned. The I&lt;Statement&gt; attribute should be cached as
shown.

Note the prefix I&lt;drv_&gt; in the attribute names: it is required that
all your private attributes use a lowercase prefix unique to your driver.
As mentioned earlier in this document, the B&lt;DBI&gt; contains a registry of
known driver prefixes and may one day warn about unknown attributes
that don't have a registered prefix.

Note that we parse the statement here in order to set the attribute
I&lt;NUM_OF_PARAMS&gt;. The technique illustrated is not very reliable; it can
be confused by question marks appearing in quoted strings, delimited
identifiers or in SQL comments that are part of the SQL statement. We
could set I&lt;NUM_OF_PARAMS&gt; in the C&lt;execute()&gt; method instead because
the B&lt;DBI&gt; specification explicitly allows a driver to defer this, but then
the user could not call C&lt;bind_param()&gt;.

=head4 Transaction handling

Pure Perl drivers will rarely support transactions. Thus your C&lt;commit()&gt;
and C&lt;rollback()&gt; methods will typically be quite simple:

  sub commit
  {
      my ($dbh) = @_;
      if ($dbh-&gt;FETCH('Warn')) {
          warn("Commit ineffective while AutoCommit is on");
      }
      0;
  }

  sub rollback {
      my ($dbh) = @_;
      if ($dbh-&gt;FETCH('Warn')) {
          warn("Rollback ineffective while AutoCommit is on");
      }
      0;
  }

Or even simpler, just use the default methods provided by the B&lt;DBI&gt; that
do nothing except return C&lt;undef&gt;.

The B&lt;DBI&gt;'s default C&lt;begin_work()&gt; method can be used by inheritance.

=head4 The STORE() and FETCH() methods

These methods (that we have already used, see above) are called for
you, whenever the user does a:

  $dbh-&gt;{$attr} = $val;

or, respectively,

  $val = $dbh-&gt;{$attr};

See L&lt;perltie&gt; for details on tied hash refs to understand why these
methods are required.

The B&lt;DBI&gt; will handle most attributes for you, in particular attributes
like I&lt;RaiseError&gt; or I&lt;PrintError&gt;. All you have to do is handle your
driver's private attributes and any attributes, like I&lt;AutoCommit&gt; and
I&lt;ChopBlanks&gt;, that the B&lt;DBI&gt; can't handle for you.

A good example might look like this:

  sub STORE
  {
      my ($dbh, $attr, $val) = @_;
      if ($attr eq 'AutoCommit') {
          # AutoCommit is currently the only standard attribute we have
          # to consider.
          if (!$val) { die "Can't disable AutoCommit"; }
          return 1;
      }
      if ($attr =~ m/^drv_/) {
          # Handle only our private attributes here
          # Note that we could trigger arbitrary actions.
          # Ideally we should warn about unknown attributes.
          $dbh-&gt;{$attr} = $val; # Yes, we are allowed to do this,
          return 1;             # but only for our private attributes
      }
      # Else pass up to DBI to handle for us
      $dbh-&gt;SUPER::STORE($attr, $val);
  }

  sub FETCH
  {
      my ($dbh, $attr) = @_;
      if ($attr eq 'AutoCommit') { return 1; }
      if ($attr =~ m/^drv_/) {
          # Handle only our private attributes here
          # Note that we could trigger arbitrary actions.
          return $dbh-&gt;{$attr}; # Yes, we are allowed to do this,
                                # but only for our private attributes
      }
      # Else pass up to DBI to handle
      $dbh-&gt;SUPER::FETCH($attr);
  }

The B&lt;DBI&gt; will actually store and fetch driver-specific attributes (with all
lowercase names) without warning or error, so there's actually no need to
implement driver-specific any code in your C&lt;FETCH()&gt; and C&lt;STORE()&gt;
methods unless you need extra logic/checks, beyond getting or setting
the value.

Unless your driver documentation indicates otherwise, the return value of
the C&lt;STORE()&gt; method is unspecified and the caller shouldn't use that value.

=head4 Other database handle methods

As with the driver package, other database handle methods may follow here.
In particular you should consider a (possibly empty) C&lt;disconnect()&gt;
method and possibly a C&lt;quote()&gt; method if B&lt;DBI&gt;'s default isn't correct for
you. You may also need the C&lt;type_info_all()&gt; and C&lt;get_info()&gt; methods,
as described elsewhere in this document.

Where reasonable use C&lt;$h-E&lt;gt&gt;SUPER::foo()&gt; to call the B&lt;DBI&gt;'s method in
some or all cases and just wrap your custom behavior around that.

If you want to use private trace flags you'll probably want to be
able to set them by name. To do that you'll need to define a
C&lt;parse_trace_flag()&gt; method (note that's "parse_trace_flag", singular,
not "parse_trace_flags", plural).

  sub parse_trace_flag {
      my ($h, $name) = @_;
      return 0x01000000 if $name eq 'foo';
      return 0x02000000 if $name eq 'bar';
      return 0x04000000 if $name eq 'baz';
      return 0x08000000 if $name eq 'boo';
      return 0x10000000 if $name eq 'bop';
      return $h-&gt;SUPER::parse_trace_flag($name);
  }

All private flag names must be lowercase, and all private flags
must be in the top 8 of the 32 bits.

=head3 The DBD::Driver::st package

This package follows the same pattern the others do:

  package DBD::Driver::st;

  $DBD::Driver::st::imp_data_size = 0;

=head4 The execute() and bind_param() methods

This is perhaps the most difficult method because we have to consider
parameter bindings here. In addition to that, there are a number of
statement attributes which must be set for inherited B&lt;DBI&gt; methods to
function correctly (see L&lt;/Statement attributes&gt; below).

We present a simplified implementation by using the I&lt;drv_params&gt;
attribute from above:

  sub bind_param
  {
      my ($sth, $pNum, $val, $attr) = @_;
      my $type = (ref $attr) ? $attr-&gt;{TYPE} : $attr;
      if ($type) {
          my $dbh = $sth-&gt;{Database};
          $val = $dbh-&gt;quote($sth, $type);
      }
      my $params = $sth-&gt;{drv_params};
      $params-&gt;[$pNum-1] = $val;
      1;
  }

  sub execute
  {
      my ($sth, @bind_values) = @_;

      # start of by finishing any previous execution if still active
      $sth-&gt;finish if $sth-&gt;FETCH('Active');

      my $params = (@bind_values) ?
          \@bind_values : $sth-&gt;{drv_params};
      my $numParam = $sth-&gt;FETCH('NUM_OF_PARAMS');
      return $sth-&gt;set_err($DBI::stderr, "Wrong number of parameters")
          if @$params != $numParam;
      my $statement = $sth-&gt;{'Statement'};
      for (my $i = 0;  $i &lt; $numParam;  $i++) {
          $statement =~ s/?/$params-&gt;[$i]/; # XXX doesn't deal with quoting etc!
      }
      # Do anything ... we assume that an array ref of rows is
      # created and store it:
      $sth-&gt;{'drv_data'} = $data;
      $sth-&gt;{'drv_rows'} = @$data; # number of rows
      $sth-&gt;STORE('NUM_OF_FIELDS') = $numFields;
      $sth-&gt;{Active} = 1;
      @$data || '0E0';
  }

There are a number of things you should note here.

We initialize the I&lt;NUM_OF_FIELDS&gt; and I&lt;Active&gt; attributes here,
because they are essential for C&lt;bind_columns()&gt; to work.

We use attribute C&lt;$sth-E&lt;gt&gt;{Statement}&gt; which we created
within C&lt;prepare()&gt;. The attribute C&lt;$sth-E&lt;gt&gt;{Database}&gt;, which is
nothing else than the I&lt;dbh&gt;, was automatically created by B&lt;DBI&gt;.

Finally, note that (as specified in the B&lt;DBI&gt; specification) we return the
string C&lt;'0E0'&gt; instead of the number 0, so that the result tests true but
equal to zero.

  $sth-&gt;execute() or die $sth-&gt;errstr;

=head4 The execute_array(), execute_for_fetch() and bind_param_array() methods

In general, DBD's only need to implement C&lt;execute_for_fetch()&gt; and
C&lt;bind_param_array&gt;. DBI's default C&lt;execute_array()&gt; will invoke the
DBD's C&lt;execute_for_fetch()&gt; as needed.

The following sequence describes the interaction between
DBI C&lt;execute_array&gt; and a DBD's C&lt;execute_for_fetch&gt;:

=over

=item 1

App calls C&lt;$sth-E&lt;gt&gt;execute_array(\%attrs, @array_of_arrays)&gt;

=item 2

If C&lt;@array_of_arrays&gt; was specified, DBI processes C&lt;@array_of_arrays&gt; by calling
DBD's C&lt;bind_param_array()&gt;. Alternately, App may have directly called
C&lt;bind_param_array()&gt;

=item 3

DBD validates and binds each array

=item 4

DBI retrieves the validated param arrays from DBD's ParamArray attribute

=item 5

DBI calls DBD's C&lt;execute_for_fetch($fetch_tuple_sub, \@tuple_status)&gt;,
where C&lt;&amp;$fetch_tuple_sub&gt; is a closure to iterate over the
returned ParamArray values, and C&lt;\@tuple_status&gt; is an array to receive
the disposition status of each tuple.

=item 6

DBD iteratively calls C&lt;&amp;$fetch_tuple_sub&gt; to retrieve parameter tuples
to be added to its bulk database operation/request.

=item 7

when DBD reaches the limit of tuples it can handle in a single database
operation/request, or the C&lt;&amp;$fetch_tuple_sub&gt; indicates no more
tuples by returning undef, the DBD executes the bulk operation, and
reports the disposition of each tuple in \@tuple_status.

=item 8

DBD repeats steps 6 and 7 until all tuples are processed.

=back

E.g., here's the essence of L&lt;DBD::Oracle&gt;'s execute_for_fetch:

       while (1) {
           my @tuple_batch;
           for (my $i = 0; $i &lt; $batch_size; $i++) {
                push @tuple_batch, [ @{$fetch_tuple_sub-&gt;() || last} ];
           }
           last unless @tuple_batch;
           my $res = ora_execute_array($sth, \@tuple_batch,
              scalar(@tuple_batch), $tuple_batch_status);
           push @$tuple_status, @$tuple_batch_status;
       }

Note that DBI's default execute_array()/execute_for_fetch() implementation
requires the use of positional (i.e., '?') placeholders. Drivers
which B&lt;require&gt; named placeholders must either emulate positional
placeholders (e.g., see L&lt;DBD::Oracle&gt;), or must implement their own
execute_array()/execute_for_fetch() methods to properly sequence bound
parameter arrays.

=head4 Fetching data

Only one method needs to be written for fetching data, C&lt;fetchrow_arrayref()&gt;.
The other methods, C&lt;fetchrow_array()&gt;, C&lt;fetchall_arrayref()&gt;, etc, as well
as the database handle's C&lt;select*&gt; methods are part of B&lt;DBI&gt;, and call
C&lt;fetchrow_arrayref()&gt; as necessary.

  sub fetchrow_arrayref
  {
      my ($sth) = @_;
      my $data = $sth-&gt;{drv_data};
      my $row = shift @$data;
      if (!$row) {
          $sth-&gt;STORE(Active =&gt; 0); # mark as no longer active
          return undef;
      }
      if ($sth-&gt;FETCH('ChopBlanks')) {
          map { $_ =~ s/\s+$//; } @$row;
      }
      return $sth-&gt;_set_fbav($row);
  }
  *fetch = \&amp;fetchrow_arrayref; # required alias for fetchrow_arrayref

Note the use of the method C&lt;_set_fbav()&gt; -- this is required so that
C&lt;bind_col()&gt; and C&lt;bind_columns()&gt; work.

If an error occurs which leaves the I&lt;$sth&gt; in a state where remaining rows
can't be fetched then I&lt;Active&gt; should be turned off before the method returns.

The C&lt;rows()&gt; method for this driver can be implemented like this:

  sub rows { shift-&gt;{drv_rows} }

because it knows in advance how many rows it has fetched.
Alternatively you could delete that method and so fallback
to the B&lt;DBI&gt;'s own method which does the right thing based
on the number of calls to C&lt;_set_fbav()&gt;.

=head4 The more_results method

If your driver doesn't support multiple result sets, then don't even implement this method.

Otherwise, this method needs to get the statement handle ready to fetch results
from the next result set, if there is one. Typically you'd start with:

    $sth-&gt;finish;

then you should delete all the attributes from the attribute cache that may no
longer be relevant for the new result set:

    delete $sth-&gt;{$_}
        for qw(NAME TYPE PRECISION SCALE ...);

for drivers written in C use:

    hv_delete((HV*)SvRV(sth), "NAME", 4, G_DISCARD);
    hv_delete((HV*)SvRV(sth), "NULLABLE", 8, G_DISCARD);
    hv_delete((HV*)SvRV(sth), "NUM_OF_FIELDS", 13, G_DISCARD);
    hv_delete((HV*)SvRV(sth), "PRECISION", 9, G_DISCARD);
    hv_delete((HV*)SvRV(sth), "SCALE", 5, G_DISCARD);
    hv_delete((HV*)SvRV(sth), "TYPE", 4, G_DISCARD);

Don't forget to also delete, or update, any driver-private attributes that may
not be correct for the next resultset.

The NUM_OF_FIELDS attribute is a special case. It should be set using STORE:

    $sth-&gt;STORE(NUM_OF_FIELDS =&gt; 0); /* for DBI &lt;= 1.53 */
    $sth-&gt;STORE(NUM_OF_FIELDS =&gt; $new_value);

for drivers written in C use this incantation:

    /* Adjust NUM_OF_FIELDS - which also adjusts the row buffer size */
    DBIc_NUM_FIELDS(imp_sth) = 0; /* for DBI &lt;= 1.53 */
    DBIc_STATE(imp_xxh)-&gt;set_attr_k(sth, sv_2mortal(newSVpvn("NUM_OF_FIELDS",13)), 0,
        sv_2mortal(newSViv(mysql_num_fields(imp_sth-&gt;result)))
    );

For DBI versions prior to 1.54 you'll also need to explicitly adjust the
number of elements in the row buffer array (C&lt;DBIc_FIELDS_AV(imp_sth)&gt;)
to match the new result set. Fill any new values with newSV(0) not &amp;sv_undef.
Alternatively you could free DBIc_FIELDS_AV(imp_sth) and set it to null,
but that would mean bind_columns() wouldn't work across result sets.


=head4 Statement attributes

The main difference between I&lt;dbh&gt; and I&lt;sth&gt; attributes is, that you
should implement a lot of attributes here that are required by
the B&lt;DBI&gt;, such as I&lt;NAME&gt;, I&lt;NULLABLE&gt;, I&lt;TYPE&gt;, etc. See
L&lt;DBI/Statement Handle Attributes&gt; for a complete list.

Pay attention to attributes which are marked as read only, such as
I&lt;NUM_OF_PARAMS&gt;. These attributes can only be set the first time
a statement is executed. If a statement is prepared, then executed
multiple times, warnings may be generated.

You can protect against these warnings, and prevent the recalculation
of attributes which might be expensive to calculate (such as the
I&lt;NAME&gt; and I&lt;NAME_*&gt; attributes):

    my $storedNumParams = $sth-&gt;FETCH('NUM_OF_PARAMS');
    if (!defined $storedNumParams or $storedNumFields &lt; 0) {
        $sth-&gt;STORE('NUM_OF_PARAMS') = $numParams;

        # Set other useful attributes that only need to be set once
        # for a statement, like $sth-&gt;{NAME} and $sth-&gt;{TYPE}
    }

One particularly important attribute to set correctly (mentioned in
L&lt;DBI/ATTRIBUTES COMMON TO ALL HANDLES&gt; is I&lt;Active&gt;. Many B&lt;DBI&gt; methods,
including C&lt;bind_columns()&gt;, depend on this attribute.

Besides that the C&lt;STORE()&gt; and C&lt;FETCH()&gt; methods are mainly the same
as above for I&lt;dbh&gt;'s.

=head4 Other statement methods

A trivial C&lt;finish()&gt; method to discard stored data, reset any attributes
(such as I&lt;Active&gt;) and do C&lt;$sth-E&lt;gt&gt;SUPER::finish()&gt;.

If you've defined a C&lt;parse_trace_flag()&gt; method in B&lt;::db&gt; you'll also want
it in B&lt;::st&gt;, so just alias it in:

  *parse_trace_flag = \&amp;DBD::foo:db::parse_trace_flag;

And perhaps some other methods that are not part of the B&lt;DBI&gt;
specification, in particular to make metadata available.
Remember that they must have names that begin with your drivers
registered prefix so they can be installed using C&lt;install_method()&gt;.

If C&lt;DESTROY()&gt; is called on a statement handle that's still active
(C&lt;$sth-E&lt;gt&gt;{Active}&gt; is true) then it should effectively call C&lt;finish()&gt;.

    sub DESTROY {
        my $sth = shift;
        $sth-&gt;finish if $sth-&gt;FETCH('Active');
    }

=head2 Tests

The test process should conform as closely as possibly to the Perl
standard test harness.

In particular, most (all) of the tests should be run in the F&lt;t&gt; sub-directory,
and should simply produce an C&lt;ok&gt; when run under C&lt;make test&gt;.
For details on how this is done, see the Camel book and the section in
Chapter 7, "The Standard Perl Library" on L&lt;Test::Harness&gt;.

The tests may need to adapt to the type of database which is being used
for testing, and to the privileges of the user testing the driver. For
example, the B&lt;DBD::Informix&gt; test code has to adapt in a number of
places to the type of database to which it is connected as different
Informix databases have different capabilities: some of the tests are
for databases without transaction logs; others are for databases with a
transaction log; some versions of the server have support for blobs, or
stored procedures, or user-defined data types, and others do not.

When a complete file of tests must be skipped, you can provide a reason
in a pseudo-comment:

    if ($no_transactions_available)
    {
        print "1..0 # Skip: No transactions available\n";
        exit 0;
    }

Consider downloading the B&lt;DBD::Informix&gt; code and look at the code in
F&lt;DBD/Informix/TestHarness.pm&gt; which is used throughout the
B&lt;DBD::Informix&gt; tests in the F&lt;t&gt; sub-directory.

=head1 CREATING A C/XS DRIVER

Please also see the section under L&lt;CREATING A PURE PERL DRIVER&gt;
regarding the creation of the F&lt;Makefile.PL&gt;.

Creating a new C/XS driver from scratch will always be a daunting task.
You can and should greatly simplify your task by taking a good
reference driver implementation and modifying that to match the
database product for which you are writing a driver.

The de facto reference driver has been the one for B&lt;DBD::Oracle&gt; written
by Tim Bunce, who is also the author of the B&lt;DBI&gt; package. The B&lt;DBD::Oracle&gt;
module is a good example of a driver implemented around a C-level API.

Nowadays it it seems better to base on B&lt;DBD::ODBC&gt;, another driver
maintained by Tim and Jeff Urlwin, because it offers a lot of metadata
and seems to become the guideline for the future development. (Also as
B&lt;DBD::Oracle&gt; digs deeper into the Oracle 8 OCI interface it'll get even
more hairy than it is now.)

The B&lt;DBD::Informix&gt; driver is one driver implemented using embedded SQL
instead of a function-based API.
B&lt;DBD::Ingres&gt; may also be worth a look.

=head2 C/XS version of Driver.pm

A lot of the code in the F&lt;Driver.pm&gt; file is very similar to the code for pure Perl modules
- see above.  However,
there are also some subtle (and not so subtle) differences, including:

=over 8

=item *

The variables I&lt;$DBD::Driver::{dr|db|st}::imp_data_size&gt; are not defined
here, but in the XS code, because they declare the size of certain
C structures.

=item *

Some methods are typically moved to the XS code, in particular
C&lt;prepare()&gt;, C&lt;execute()&gt;, C&lt;disconnect()&gt;, C&lt;disconnect_all()&gt; and the
C&lt;STORE()&gt; and C&lt;FETCH()&gt; methods.

=item *

Other methods are still part of F&lt;Driver.pm&gt;, but have callbacks to
the XS code.

=item *

If the driver-specific parts of the I&lt;imp_drh_t&gt; structure need to be
formally initialized (which does not seem to be a common requirement),
then you need to add a call to an appropriate XS function in the driver
method of C&lt;DBD::Driver::driver()&gt;, and you define the corresponding function
in F&lt;Driver.xs&gt;, and you define the C code in F&lt;dbdimp.c&gt; and the prototype in
F&lt;dbdimp.h&gt;.

For example, B&lt;DBD::Informix&gt; has such a requirement, and adds the
following call after the call to C&lt;_new_drh()&gt; in F&lt;Informix.pm&gt;:

  DBD::Informix::dr::driver_init($drh);

and the following code in F&lt;Informix.xs&gt;:

  # Initialize the DBD::Informix driver data structure
  void
  driver_init(drh)
      SV *drh
      CODE:
      ST(0) = dbd_ix_dr_driver_init(drh) ? &amp;sv_yes : &amp;sv_no;

and the code in F&lt;dbdimp.h&gt; declares:

  extern int dbd_ix_dr_driver_init(SV *drh);

and the code in F&lt;dbdimp.ec&gt; (equivalent to F&lt;dbdimp.c&gt;) defines:

  /* Formally initialize the DBD::Informix driver structure */
  int
  dbd_ix_dr_driver(SV *drh)
  {
      D_imp_drh(drh);
      imp_drh-&gt;n_connections = 0;       /* No active connections */
      imp_drh-&gt;current_connection = 0;  /* No current connection */
      imp_drh-&gt;multipleconnections = (ESQLC_VERSION &gt;= 600) ? True : False;
      dbd_ix_link_newhead(&amp;imp_drh-&gt;head);  /* Empty linked list of connections */
      return 1;
  }

B&lt;DBD::Oracle&gt; has a similar requirement but gets around it by checking
whether the private data part of the driver handle is all zeroed out,
rather than add extra functions.

=back

Now let's take a closer look at an excerpt from F&lt;Oracle.pm&gt; (revised
heavily to remove idiosyncrasies) as an example, ignoring things that
were already discussed for pure Perl drivers.

=head3 The connect method

The connect method is the database handle constructor.
You could write either of two versions of this method: either one which
takes connection attributes (new code) and one which ignores them (old
code only).

If you ignore the connection attributes, then you omit all mention of
the I&lt;$auth&gt; variable (which is a reference to a hash of attributes), and
the XS system manages the differences for you.

  sub connect
  {
      my ($drh, $dbname, $user, $auth, $attr) = @_;

      # Some database specific verifications, default settings
      # and the like following here. This should only include
      # syntax checks or similar stuff where it's legal to
      # 'die' in case of errors.

      my $dbh = DBI::_new_dbh($drh, {
              'Name'   =&gt; $dbname,
          })
          or return undef;

      # Call the driver-specific function _login in Driver.xs file which
      # calls the DBMS-specific function(s) to connect to the database,
      # and populate internal handle data.
      DBD::Driver::db::_login($dbh, $dbname, $user, $auth, $attr)
          or return undef;

      $dbh;
  }

This is mostly the same as in the pure Perl case, the exception being
the use of the private C&lt;_login()&gt; callback, which is the function
that will really connect to the database. It is implemented in
F&lt;Driver.xst&gt; (you should not implement it) and calls
C&lt;dbd_db_login6()&gt; or C&lt;dbd_db_login6_sv&gt; from F&lt;dbdimp.c&gt;. See below
for details.

If your driver has driver-specific attributes which may be passed in the
connect method and hence end up in C&lt;$attr&gt; in C&lt;dbd_db_login6&gt; then it
is best to delete any you process so DBI does not send them again
via STORE after connect. You can do this in C like this:

  DBD_ATTRIB_DELETE(attr, "my_attribute_name",
                    strlen("my_attribute_name"));

However, prior to DBI subversion version 11605 (and fixed post 1.607)
DBD_ATTRIB_DELETE segfaulted so if you cannot guarantee the DBI version
will be post 1.607 you need to use:

  hv_delete((HV*)SvRV(attr), "my_attribute_name",
                     strlen("my_attribute_name"), G_DISCARD);

 *FIX ME* Discuss removing attributes in Perl code.

=head3 The disconnect_all method

 *FIX ME* T.B.S

=head3 The data_sources method

If your C&lt;data_sources()&gt; method can be implemented in pure Perl, then do
so because it is easier than doing it in XS code (see the section above
for pure Perl drivers).

If your C&lt;data_sources()&gt; method must call onto compiled functions, then
you will need to define I&lt;dbd_dr_data_sources&gt; in your F&lt;dbdimp.h&gt; file, which
will trigger F&lt;Driver.xst&gt; (in B&lt;DBI&gt; v1.33 or greater) to generate the XS
code that calls your actual C function (see the discussion below for
details) and you do not code anything in F&lt;Driver.pm&gt; to handle it.

=head3 The prepare method

The prepare method is the statement handle constructor, and most of it
is not new. Like the C&lt;connect()&gt; method, it now has a C callback:

  package DBD::Driver::db; # ====== DATABASE ======
  use strict;

  sub prepare
  {
      my ($dbh, $statement, $attribs) = @_;

      # create a 'blank' sth
      my $sth = DBI::_new_sth($dbh, {
          'Statement' =&gt; $statement,
          })
          or return undef;

      # Call the driver-specific function _prepare in Driver.xs file
      # which calls the DBMS-specific function(s) to prepare a statement
      # and populate internal handle data.
      DBD::Driver::st::_prepare($sth, $statement, $attribs)
          or return undef;
      $sth;
  }

=head3 The execute method

 *FIX ME* T.B.S

=head3 The fetchrow_arrayref method

 *FIX ME* T.B.S

=head3 Other methods?

 *FIX ME* T.B.S

=head2 Driver.xs

F&lt;Driver.xs&gt; should look something like this:

  #include "Driver.h"

  DBISTATE_DECLARE;

  INCLUDE: Driver.xsi

  MODULE = DBD::Driver    PACKAGE = DBD::Driver::dr

  /* Non-standard drh XS methods following here, if any.       */
  /* If none (the usual case), omit the MODULE line above too. */

  MODULE = DBD::Driver    PACKAGE = DBD::Driver::db

  /* Non-standard dbh XS methods following here, if any.       */
  /* Currently this includes things like _list_tables from     */
  /* DBD::mSQL and DBD::mysql.                                 */

  MODULE = DBD::Driver    PACKAGE = DBD::Driver::st

  /* Non-standard sth XS methods following here, if any.       */
  /* In particular this includes things like _list_fields from */
  /* DBD::mSQL and DBD::mysql for accessing metadata.          */

Note especially the include of F&lt;Driver.xsi&gt; here: B&lt;DBI&gt; inserts stub
functions for almost all private methods here which will typically do
much work for you.

Wherever you really have to implement something, it will call a private
function in F&lt;dbdimp.c&gt;, and this is what you have to implement.

You need to set up an extra routine if your driver needs to export
constants of its own, analogous to the SQL types available when you say:

  use DBI qw(:sql_types);

 *FIX ME* T.B.S

=head2 Driver.h

F&lt;Driver.h&gt; is very simple and the operational contents should look like this:

  #ifndef DRIVER_H_INCLUDED
  #define DRIVER_H_INCLUDED

  #define NEED_DBIXS_VERSION 93    /* 93 for DBI versions 1.00 to 1.51+ */
  #define PERL_NO_GET_CONTEXT      /* if used require DBI 1.51+ */

  #include &lt;DBIXS.h&gt;      /* installed by the DBI module  */

  #include "dbdimp.h"

  #include "dbivport.h"   /* see below                    */

  #include &lt;dbd_xsh.h&gt;    /* installed by the DBI module  */

  #endif /* DRIVER_H_INCLUDED */

The F&lt;DBIXS.h&gt; header defines most of the interesting information that
the writer of a driver needs.

The file F&lt;dbd_xsh.h&gt; header provides prototype declarations for the C
functions that you might decide to implement. Note that you should
normally only define one of C&lt;dbd_db_login()&gt;, C&lt;dbd_db_login6()&gt; or
C&lt;dbd_db_login6_sv&gt; unless you are intent on supporting really old
versions of B&lt;DBI&gt; (prior to B&lt;DBI&gt; 1.06) as well as modern
versions. The only standard, B&lt;DBI&gt;-mandated functions that you need
write are those specified in the F&lt;dbd_xsh.h&gt; header. You might also
add extra driver-specific functions in F&lt;Driver.xs&gt;.

The F&lt;dbivport.h&gt; file should be I&lt;copied&gt; from the latest B&lt;DBI&gt; release
into your distribution each time you modify your driver. Its job is to
allow you to enhance your code to work with the latest B&lt;DBI&gt; API while
still allowing your driver to be compiled and used with older versions
of the B&lt;DBI&gt; (for example, when the C&lt;DBIh_SET_ERR_CHAR()&gt; macro was added
to B&lt;DBI&gt; 1.41, an emulation of it was added to F&lt;dbivport.h&gt;). This makes
users happy and your life easier. Always read the notes in F&lt;dbivport.h&gt;
to check for any limitations in the emulation that you should be aware
of.

With B&lt;DBI&gt; v1.51 or better I recommend that the driver defines
I&lt;PERL_NO_GET_CONTEXT&gt; before F&lt;DBIXS.h&gt; is included. This can significantly
improve efficiency when running under a thread enabled perl. (Remember that
the standard perl in most Linux distributions is built with threads enabled.
So is ActiveState perl for Windows, and perl built for Apache mod_perl2.)
If you do this there are some things to keep in mind:

=over 4

=item *

If I&lt;PERL_NO_GET_CONTEXT&gt; is defined, then every function that calls the Perl
API will need to start out with a C&lt;dTHX;&gt; declaration.

=item *

You'll know which functions need this, because the C compiler will
complain that the undeclared identifier C&lt;my_perl&gt; is used if I&lt;and only if&gt;
the perl you are using to develop and test your driver has threads enabled.

=item *

If you don't remember to test with a thread-enabled perl before making
a release it's likely that you'll get failure reports from users who are.

=item *

For driver private functions it is possible to gain even more
efficiency by replacing C&lt;dTHX;&gt; with C&lt;pTHX_&gt; prepended to the
parameter list and then C&lt;aTHX_&gt; prepended to the argument list where
the function is called.

=back

See L&lt;perlguts/How multiple interpreters and concurrency are supported&gt; for
additional information about I&lt;PERL_NO_GET_CONTEXT&gt;.

=head2 Implementation header dbdimp.h

This header file has two jobs:

First it defines data structures for your private part of the handles.

Second it defines macros that rename the generic names like
C&lt;dbd_db_login()&gt; to database specific names like C&lt;ora_db_login()&gt;. This
avoids name clashes and enables use of different drivers when you work
with a statically linked perl.

It also will have the important task of disabling XS methods that you
don't want to implement.

Finally, the macros will also be used to select alternate
implementations of some functions. For example, the C&lt;dbd_db_login()&gt;
function is not passed the attribute hash.

Since B&lt;DBI&gt; v1.06, if a C&lt;dbd_db_login6()&gt; macro is defined (for a function
with 6 arguments), it will be used instead with the attribute hash
passed as the sixth argument.

Since B&lt;DBI&gt; post v1.607, if a C&lt;dbd_db_login6_sv()&gt; macro is defined (for
a function like dbd_db_login6 but with scalar pointers for the dbname,
username and password), it will be used instead. This will allow your
login6 function to see if there are any Unicode characters in the
dbname.

People used to just pick Oracle's F&lt;dbdimp.c&gt; and use the same names,
structures and types. I strongly recommend against that. At first glance
this saves time, but your implementation will be less readable. It was
just hell when I had to separate B&lt;DBI&gt; specific parts, Oracle specific
parts, mSQL specific parts and mysql specific parts in B&lt;DBD::mysql&gt;'s
I&lt;dbdimp.h&gt; and I&lt;dbdimp.c&gt;. (B&lt;DBD::mysql&gt; was a port of B&lt;DBD::mSQL&gt;
which was based on B&lt;DBD::Oracle&gt;.) [Seconded, based on the experience
taking B&lt;DBD::Informix&gt; apart, even though the version inherited in 1996
was only based on B&lt;DBD::Oracle&gt;.]

This part of the driver is I&lt;your exclusive part&gt;. Rewrite it from
scratch, so it will be clean and short: in other words, a better piece
of code. (Of course keep an eye on other people's work.)

  struct imp_drh_st {
      dbih_drc_t com;           /* MUST be first element in structure   */
      /* Insert your driver handle attributes here */
  };

  struct imp_dbh_st {
      dbih_dbc_t com;           /* MUST be first element in structure   */
      /* Insert your database handle attributes here */
  };

  struct imp_sth_st {
      dbih_stc_t com;           /* MUST be first element in structure   */
      /* Insert your statement handle attributes here */
  };

  /*  Rename functions for avoiding name clashes; prototypes are  */
  /*  in dbd_xsh.h                                                */
  #define dbd_init            drv_dr_init
  #define dbd_db_login6_sv    drv_db_login_sv
  #define dbd_db_do           drv_db_do
  ... many more here ...

These structures implement your private part of the handles.

You I&lt;have&gt; to use the name C&lt;imp_dbh_{dr|db|st}&gt; and the first field
I&lt;must&gt; be of type I&lt;dbih_drc_t|_dbc_t|_stc_t&gt; and I&lt;must&gt; be called
C&lt;com&gt;.

You should never access these fields directly, except by using the
I&lt;DBIc_xxx()&gt; macros below.

=head2 Implementation source dbdimp.c

Conventionally, F&lt;dbdimp.c&gt; is the main implementation file (but
B&lt;DBD::Informix&gt; calls the file F&lt;dbdimp.ec&gt;). This section includes a
short note on each function that is used in the F&lt;Driver.xsi&gt; template
and thus I&lt;has&gt; to be implemented.

Of course, you will probably also need to implement other support
functions, which should usually be file static if they are placed in
F&lt;dbdimp.c&gt;. If they are placed in other files, you need to list those
files in F&lt;Makefile.PL&gt; (and F&lt;MANIFEST&gt;) to handle them correctly.

It is wise to adhere to a namespace convention for your functions to
avoid conflicts. For example, for a driver with prefix I&lt;drv_&gt;, you
might call externally visible functions I&lt;dbd_drv_xxxx&gt;. You should also
avoid non-constant global variables as much as possible to improve the
support for threading.

Since Perl requires support for function prototypes (ANSI or ISO or
Standard C), you should write your code using function prototypes too.

It is possible to use either the unmapped names such as C&lt;dbd_init()&gt; or
the mapped names such as C&lt;dbd_ix_dr_init()&gt; in the F&lt;dbdimp.c&gt; file.
B&lt;DBD::Informix&gt; uses the mapped names which makes it easier to identify
where to look for linkage problems at runtime (which will report errors
using the mapped names).

Most other drivers, and in particular B&lt;DBD::Oracle&gt;, use the unmapped
names in the source code which makes it a little easier to compare code
between drivers and eases discussions on the I&lt;dbi-dev&gt; mailing list.
The majority of the code fragments here will use the unmapped names.

Ultimately, you should provide implementations for most of the
functions listed in the F&lt;dbd_xsh.h&gt; header. The exceptions are
optional functions (such as C&lt;dbd_st_rows()&gt;) and those functions with
alternative signatures, such as C&lt;dbd_db_login6_sv&gt;,
C&lt;dbd_db_login6()&gt; and I&lt;dbd_db_login()&gt;. Then you should only
implement one of the alternatives, and generally the newer one of the
alternatives.

=head3 The dbd_init method

  #include "Driver.h"

  DBISTATE_DECLARE;

  void dbd_init(dbistate_t* dbistate)
  {
      DBISTATE_INIT;  /*  Initialize the DBI macros  */
  }

The C&lt;dbd_init()&gt; function will be called when your driver is first
loaded; the bootstrap command in C&lt;DBD::Driver::dr::driver()&gt; triggers this,
and the call is generated in the I&lt;BOOT&gt; section of F&lt;Driver.xst&gt;.
These statements are needed to allow your driver to use the B&lt;DBI&gt; macros.
They will include your private header file F&lt;dbdimp.h&gt; in turn.
Note that I&lt;DBISTATE_INIT&gt; requires the name of the argument to C&lt;dbd_init()&gt;
to be called C&lt;dbistate()&gt;.

=head3 The dbd_drv_error method

You need a function to record errors so B&lt;DBI&gt; can access them properly.
You can call it whatever you like, but we'll call it C&lt;dbd_drv_error()&gt;
here.

The argument list depends on your database software; different systems
provide different ways to get at error information.

  static void dbd_drv_error(SV *h, int rc, const char *what)
  {

Note that I&lt;h&gt; is a generic handle, may it be a driver handle, a
database or a statement handle.

      D_imp_xxh(h);

This macro will declare and initialize a variable I&lt;imp_xxh&gt; with
a pointer to your private handle pointer. You may cast this to
to I&lt;imp_drh_t&gt;, I&lt;imp_dbh_t&gt; or I&lt;imp_sth_t&gt;.

To record the error correctly, equivalent to the C&lt;set_err()&gt; method,
use one of the C&lt;DBIh_SET_ERR_CHAR(...)&gt; or C&lt;DBIh_SET_ERR_SV(...)&gt; macros,
which were added in B&lt;DBI&gt; 1.41:

  DBIh_SET_ERR_SV(h, imp_xxh, err, errstr, state, method);
  DBIh_SET_ERR_CHAR(h, imp_xxh, err_c, err_i, errstr, state, method);

For C&lt;DBIh_SET_ERR_SV&gt; the I&lt;err&gt;, I&lt;errstr&gt;, I&lt;state&gt;, and I&lt;method&gt;
parameters are C&lt;SV*&gt; (use &amp;sv_undef instead of NULL).

For C&lt;DBIh_SET_ERR_CHAR&gt; the I&lt;err_c&gt;, I&lt;errstr&gt;, I&lt;state&gt;, I&lt;method&gt;
parameters are C&lt;char*&gt;.

The I&lt;err_i&gt; parameter is an C&lt;IV&gt; that's used instead of I&lt;err_c&gt; if
I&lt;err_c&gt; is C&lt;Null&gt;.

The I&lt;method&gt; parameter can be ignored.

The C&lt;DBIh_SET_ERR_CHAR&gt; macro is usually the simplest to use when you
just have an integer error code and an error message string:

  DBIh_SET_ERR_CHAR(h, imp_xxh, Nullch, rc, what, Nullch, Nullch);

As you can see, any parameters that aren't relevant to you can be C&lt;Null&gt;.

To make drivers compatible with B&lt;DBI&gt; &lt; 1.41 you should be using F&lt;dbivport.h&gt;
as described in L&lt;/Driver.h&gt; above.

The (obsolete) macros such as C&lt;DBIh_EVENT2&gt; should be removed from drivers.

The names C&lt;dbis&gt; and C&lt;DBIS&gt;, which were used in previous versions of
this document, should be replaced with the C&lt;DBIc_DBISTATE(imp_xxh)&gt; macro.

The name C&lt;DBILOGFP&gt;, which was also used in previous versions of this
document, should be replaced by C&lt;DBIc_LOGPIO(imp_xxh)&gt;.

Your code should not call the C C&lt;E&lt;lt&gt;stdio.hE&lt;gt&gt;&gt; I/O functions; you
should use C&lt;PerlIO_printf()&gt; as shown:

      if (DBIc_TRACE_LEVEL(imp_xxh) &gt;= 2)
          PerlIO_printf(DBIc_LOGPIO(imp_xxh), "foobar %s: %s\n",
              foo, neatsvpv(errstr,0));

That's the first time we see how tracing works within a B&lt;DBI&gt; driver. Make
use of this as often as you can, but don't output anything at a trace
level less than 3. Levels 1 and 2 are reserved for the B&lt;DBI&gt;.

You can define up to 8 private trace flags using the top 8 bits
of C&lt;DBIc_TRACE_FLAGS(imp)&gt;, that is: C&lt;0xFF000000&gt;. See the
C&lt;parse_trace_flag()&gt; method elsewhere in this document.

=head3 The dbd_dr_data_sources method

This method is optional; the support for it was added in B&lt;DBI&gt; v1.33.

As noted in the discussion of F&lt;Driver.pm&gt;, if the data sources
can be determined by pure Perl code, do it that way. If, as in
B&lt;DBD::Informix&gt;, the information is obtained by a C function call, then
you need to define a function that matches the prototype:

  extern AV *dbd_dr_data_sources(SV *drh, imp_drh_t *imp_drh, SV *attrs);

An outline implementation for B&lt;DBD::Informix&gt; follows, assuming that the
C&lt;sqgetdbs()&gt; function call shown will return up to 100 databases names,
with the pointers to each name in the array dbsname and the name strings
themselves being stores in dbsarea.

  AV *dbd_dr_data_sources(SV *drh, imp_drh_t *imp_drh, SV *attr)
  {
      int ndbs;
      int i;
      char *dbsname[100];
      char  dbsarea[10000];
      AV *av = Nullav;

      if (sqgetdbs(&amp;ndbs, dbsname, 100, dbsarea, sizeof(dbsarea)) == 0)
      {
          av = NewAV();
          av_extend(av, (I32)ndbs);
          sv_2mortal((SV *)av);
          for (i = 0; i &lt; ndbs; i++)
            av_store(av, i, newSVpvf("dbi:Informix:%s", dbsname[i]));
      }
      return(av);
  }

The actual B&lt;DBD::Informix&gt; implementation has a number of extra lines of
code, logs function entry and exit, reports the error from C&lt;sqgetdbs()&gt;,
and uses C&lt;#define&gt;'d constants for the array sizes.

=head3 The dbd_db_login6 method

  int dbd_db_login6_sv(SV* dbh, imp_dbh_t* imp_dbh, SV* dbname,
                       SV* user, SV* auth, SV *attr);

  or

  int dbd_db_login6(SV* dbh, imp_dbh_t* imp_dbh, char* dbname,
                   char* user, char* auth, SV *attr);

This function will really connect to the database. The argument I&lt;dbh&gt;
is the database handle. I&lt;imp_dbh&gt; is the pointer to the handles private
data, as is I&lt;imp_xxx&gt; in C&lt;dbd_drv_error()&gt; above. The arguments
I&lt;dbname&gt;, I&lt;user&gt;, I&lt;auth&gt; and I&lt;attr&gt; correspond to the arguments of
the driver handle's C&lt;connect()&gt; method.

You will quite often use database specific attributes here, that are
specified in the DSN. I recommend you parse the DSN (using Perl) within
the C&lt;connect()&gt; method and pass the segments of the DSN via the
attributes parameter through C&lt;_login()&gt; to C&lt;dbd_db_login6()&gt;.

Here's how you fetch them; as an example we use I&lt;hostname&gt; attribute,
which can be up to 12 characters long excluding null terminator:

  SV** svp;
  STRLEN len;
  char* hostname;

  if ( (svp = DBD_ATTRIB_GET_SVP(attr, "drv_hostname", 12)) &amp;&amp; SvTRUE(*svp)) {
      hostname = SvPV(*svp, len);
      DBD_ATTRIB_DELETE(attr, "drv_hostname", 12); /* avoid later STORE */
  } else {
      hostname = "localhost";
  }

If you handle any driver specific attributes in the dbd_db_login6
method you probably want to delete them from C&lt;attr&gt; (as above with
DBD_ATTRIB_DELETE). If you don't delete your handled attributes DBI
will call C&lt;STORE&gt; for each attribute after the connect/login and this
is at best redundant for attributes you have already processed.

B&lt;Note: Until revision 11605 (post DBI 1.607), there was a problem with
DBD_ATTRIBUTE_DELETE so unless you require a DBI version after 1.607
you need to replace each DBD_ATTRIBUTE_DELETE call with:&gt;

  hv_delete((HV*)SvRV(attr), key, key_len, G_DISCARD)

Note that you can also obtain standard attributes such as I&lt;AutoCommit&gt; and
I&lt;ChopBlanks&gt; from the attributes parameter, using C&lt;DBD_ATTRIB_GET_IV&gt; for
integer attributes.

If, for example, your database does not support transactions but
I&lt;AutoCommit&gt; is set off (requesting transaction support), then you can
emulate a 'failure to connect'.

Now you should really connect to the database. In general, if the
connection fails, it is best to ensure that all allocated resources are
released so that the handle does not need to be destroyed separately. If
you are successful (and possibly even if you fail but you have allocated
some resources), you should use the following macros:

  DBIc_IMPSET_on(imp_dbh);

This indicates that the driver (implementor) has allocated resources in
the I&lt;imp_dbh&gt; structure and that the implementors private C&lt;dbd_db_destroy()&gt;
function should be called when the handle is destroyed.

  DBIc_ACTIVE_on(imp_dbh);

This indicates that the handle has an active connection to the server
and that the C&lt;dbd_db_disconnect()&gt; function should be called before the
handle is destroyed.

Note that if you do need to fail, you should report errors via the I&lt;drh&gt;
or I&lt;imp_drh&gt; rather than via I&lt;dbh&gt; or I&lt;imp_dbh&gt; because I&lt;imp_dbh&gt; will be
destroyed by the failure, so errors recorded in that handle will not be
visible to B&lt;DBI&gt;, and hence not the user either.

Note too, that the function is passed I&lt;dbh&gt; and I&lt;imp_dbh&gt;, and there
is a macro C&lt;D_imp_drh_from_dbh&gt; which can recover the I&lt;imp_drh&gt; from
the I&lt;imp_dbh&gt;. However, there is no B&lt;DBI&gt; macro to provide you with the
I&lt;drh&gt; given either the I&lt;imp_dbh&gt; or the I&lt;dbh&gt; or the I&lt;imp_drh&gt; (and
there's no way to recover the I&lt;dbh&gt; given just the I&lt;imp_dbh&gt;).

This suggests that, despite the above notes about C&lt;dbd_drv_error()&gt;
taking an C&lt;SV *&gt;, it may be better to have two error routines, one
taking I&lt;imp_dbh&gt; and one taking I&lt;imp_drh&gt; instead. With care, you can
factor most of the formatting code out so that these are small routines
calling a common error formatter. See the code in B&lt;DBD::Informix&gt;
1.05.00 for more information.

The C&lt;dbd_db_login6()&gt; function should return I&lt;TRUE&gt; for success,
I&lt;FALSE&gt; otherwise.

Drivers implemented long ago may define the five-argument function
C&lt;dbd_db_login()&gt; instead of C&lt;dbd_db_login6()&gt;. The missing argument is
the attributes. There are ways to work around the missing attributes,
but they are ungainly; it is much better to use the 6-argument form.
Even later drivers will use C&lt;dbd_db_login6_sv()&gt; which provides the
dbname, username and password as SVs.

=head3 The dbd_db_commit and dbd_db_rollback methods

  int dbd_db_commit(SV *dbh, imp_dbh_t *imp_dbh);
  int dbd_db_rollback(SV* dbh, imp_dbh_t* imp_dbh);

These are used for commit and rollback. They should return I&lt;TRUE&gt; for
success, I&lt;FALSE&gt; for error.

The arguments I&lt;dbh&gt; and I&lt;imp_dbh&gt; are the same as for C&lt;dbd_db_login6()&gt;
above; I will omit describing them in what follows, as they appear
always.

These functions should return I&lt;TRUE&gt; for success, I&lt;FALSE&gt; otherwise.

=head3 The dbd_db_disconnect method

This is your private part of the C&lt;disconnect()&gt; method. Any I&lt;dbh&gt; with
the I&lt;ACTIVE&gt; flag on must be disconnected. (Note that you have to set
it in C&lt;dbd_db_connect()&gt; above.)

  int dbd_db_disconnect(SV* dbh, imp_dbh_t* imp_dbh);

The database handle will return I&lt;TRUE&gt; for success, I&lt;FALSE&gt; otherwise.
In any case it should do a:

  DBIc_ACTIVE_off(imp_dbh);

before returning so B&lt;DBI&gt; knows that C&lt;dbd_db_disconnect()&gt; was executed.

Note that there's nothing to stop a I&lt;dbh&gt; being I&lt;disconnected&gt; while
it still have active children. If your database API reacts badly to
trying to use an I&lt;sth&gt; in this situation then you'll need to add code
like this to all I&lt;sth&gt; methods:

  if (!DBIc_ACTIVE(DBIc_PARENT_COM(imp_sth)))
    return 0;

Alternatively, you can add code to your driver to keep explicit track of
the statement handles that exist for each database handle and arrange
to destroy those handles before disconnecting from the database. There
is code to do this in B&lt;DBD::Informix&gt;. Similar comments apply to the
driver handle keeping track of all the database handles.

Note that the code which destroys the subordinate handles should only
release the associated database resources and mark the handles inactive;
it does not attempt to free the actual handle structures.

This function should return I&lt;TRUE&gt; for success, I&lt;FALSE&gt; otherwise, but
it is not clear what anything can do about a failure.

=head3 The dbd_db_discon_all method

  int dbd_discon_all (SV *drh, imp_drh_t *imp_drh);

This function may be called at shutdown time. It should make
best-efforts to disconnect all database handles - if possible. Some
databases don't support that, in which case you can do nothing
but return 'success'.

This function should return I&lt;TRUE&gt; for success, I&lt;FALSE&gt; otherwise, but
it is not clear what anything can do about a failure.

=head3 The dbd_db_destroy method

This is your private part of the database handle destructor. Any I&lt;dbh&gt; with
the I&lt;IMPSET&gt; flag on must be destroyed, so that you can safely free
resources. (Note that you have to set it in C&lt;dbd_db_connect()&gt; above.)

  void dbd_db_destroy(SV* dbh, imp_dbh_t* imp_dbh)
  {
      DBIc_IMPSET_off(imp_dbh);
  }

The B&lt;DBI&gt; F&lt;Driver.xst&gt; code will have called C&lt;dbd_db_disconnect()&gt; for you,
if the handle is still 'active', before calling C&lt;dbd_db_destroy()&gt;.

Before returning the function must switch I&lt;IMPSET&gt; to off, so B&lt;DBI&gt; knows
that the destructor was called.

A B&lt;DBI&gt; handle doesn't keep references to its children. But children
do keep references to their parents. So a database handle won't be
C&lt;DESTROY&gt;'d until all its children have been C&lt;DESTROY&gt;'d.

=head3 The dbd_db_STORE_attrib method

This function handles

  $dbh-&gt;{$key} = $value;

Its prototype is:

  int dbd_db_STORE_attrib(SV* dbh, imp_dbh_t* imp_dbh, SV* keysv,
                          SV* valuesv);

You do not handle all attributes; on the contrary, you should not handle
B&lt;DBI&gt; attributes here: leave this to B&lt;DBI&gt;. (There are two exceptions,
I&lt;AutoCommit&gt; and I&lt;ChopBlanks&gt;, which you should care about.)

The return value is I&lt;TRUE&gt; if you have handled the attribute or I&lt;FALSE&gt;
otherwise. If you are handling an attribute and something fails, you
should call C&lt;dbd_drv_error()&gt;, so B&lt;DBI&gt; can raise exceptions, if desired.
If C&lt;dbd_drv_error()&gt; returns, however, you have a problem: the user will
never know about the error, because he typically will not check
C&lt;$dbh-E&lt;gt&gt;errstr()&gt;.

I cannot recommend a general way of going on, if C&lt;dbd_drv_error()&gt; returns,
but there are examples where even the B&lt;DBI&gt; specification expects that
you C&lt;croak()&gt;. (See the I&lt;AutoCommit&gt; method in L&lt;DBI&gt;.)

If you have to store attributes, you should either use your private
data structure I&lt;imp_xxx&gt;, the handle hash (via C&lt;(HV*)SvRV(dbh)&gt;), or use
the private I&lt;imp_data&gt;.

The first is best for internal C values like integers or pointers and
where speed is important within the driver. The handle hash is best for
values the user may want to get/set via driver-specific attributes.
The private I&lt;imp_data&gt; is an additional C&lt;SV&gt; attached to the handle. You
could think of it as an unnamed handle attribute. It's not normally used.

=head3 The dbd_db_FETCH_attrib method

This is the counterpart of C&lt;dbd_db_STORE_attrib()&gt;, needed for:

  $value = $dbh-&gt;{$key};

Its prototype is:

  SV* dbd_db_FETCH_attrib(SV* dbh, imp_dbh_t* imp_dbh, SV* keysv);

Unlike all previous methods this returns an C&lt;SV&gt; with the value. Note
that you should normally execute C&lt;sv_2mortal()&gt;, if you return a nonconstant
value. (Constant values are C&lt;&amp;sv_undef&gt;, C&lt;&amp;sv_no&gt; and C&lt;&amp;sv_yes&gt;.)

Note, that B&lt;DBI&gt; implements a caching algorithm for attribute values.
If you think, that an attribute may be fetched, you store it in the
I&lt;dbh&gt; itself:

  if (cacheit) /* cache value for later DBI 'quick' fetch? */
      hv_store((HV*)SvRV(dbh), key, kl, cachesv, 0);

=head3 The dbd_st_prepare method

This is the private part of the C&lt;prepare()&gt; method. Note that you
B&lt;must not&gt; really execute the statement here. You may, however,
preparse and validate the statement, or do similar things.

  int dbd_st_prepare(SV* sth, imp_sth_t* imp_sth, char* statement,
                     SV* attribs);

A typical, simple, possibility is to do nothing and rely on the perl
C&lt;prepare()&gt; code that set the I&lt;Statement&gt; attribute on the handle. This
attribute can then be used by C&lt;dbd_st_execute()&gt;.

If the driver supports placeholders then the I&lt;NUM_OF_PARAMS&gt; attribute
must be set correctly by C&lt;dbd_st_prepare()&gt;:

  DBIc_NUM_PARAMS(imp_sth) = ...

If you can, you should also setup attributes like I&lt;NUM_OF_FIELDS&gt;, I&lt;NAME&gt;,
etc. here, but B&lt;DBI&gt; doesn't require that - they can be deferred until
execute() is called. However, if you do, document it.

In any case you should set the I&lt;IMPSET&gt; flag, as you did in
C&lt;dbd_db_connect()&gt; above:

  DBIc_IMPSET_on(imp_sth);

=head3 The dbd_st_execute method

This is where a statement will really be executed.

  int dbd_st_execute(SV* sth, imp_sth_t* imp_sth);

C&lt;dbd_st_execute&gt; should return -2 for any error, -1 if the number of
rows affected is unknown else it should be the number of affected
(updated, inserted) rows.

Note that you must be aware a statement may be executed repeatedly.
Also, you should not expect that C&lt;finish()&gt; will be called between two
executions, so you might need code, like the following, near the start
of the function:

  if (DBIc_ACTIVE(imp_sth))
      dbd_st_finish(h, imp_sth);

If your driver supports the binding of parameters (it should!), but the
database doesn't, you must do it here. This can be done as follows:

  SV *svp;
  char* statement = DBD_ATTRIB_GET_PV(h, "Statement", 9, svp, "");
  int numParam = DBIc_NUM_PARAMS(imp_sth);
  int i;

  for (i = 0; i &lt; numParam; i++)
  {
      char* value = dbd_db_get_param(sth, imp_sth, i);
      /* It is your drivers task to implement dbd_db_get_param,    */
      /* it must be setup as a counterpart of dbd_bind_ph.         */
      /* Look for '?' and replace it with 'value'.  Difficult      */
      /* task, note that you may have question marks inside        */
      /* quotes and comments the like ...  :-(                     */
      /* See DBD::mysql for an example. (Don't look too deep into  */
      /* the example, you will notice where I was lazy ...)        */
  }

The next thing is to really execute the statement.

Note that you must set the attributes I&lt;NUM_OF_FIELDS&gt;, I&lt;NAME&gt;, etc
when the statement is successfully executed if the driver has not
already done so: they may be used even before a potential C&lt;fetchrow()&gt;.
In particular you have to tell B&lt;DBI&gt; the number of fields that the
statement has, because it will be used by B&lt;DBI&gt; internally. Thus the
function will typically ends with:

  if (isSelectStatement) {
      DBIc_NUM_FIELDS(imp_sth) = numFields;
      DBIc_ACTIVE_on(imp_sth);
  }

It is important that the I&lt;ACTIVE&gt; flag only be set for C&lt;SELECT&gt;
statements (or any other statements that can return many
values from the database using a cursor-like mechanism). See
C&lt;dbd_db_connect()&gt; above for more explanations.

There plans for a preparse function to be provided by B&lt;DBI&gt;, but this has
not reached fruition yet.
Meantime, if you want to know how ugly it can get, try looking at the
C&lt;dbd_ix_preparse()&gt; in B&lt;DBD::Informix&gt; F&lt;dbdimp.ec&gt; and the related
functions in F&lt;iustoken.c&gt; and F&lt;sqltoken.c&gt;.

=head3 The dbd_st_fetch method

This function fetches a row of data. The row is stored in in an array,
of C&lt;SV&gt;'s that B&lt;DBI&gt; prepares for you. This has two advantages: it is fast
(you even reuse the C&lt;SV&gt;'s, so they don't have to be created after the
first C&lt;fetchrow()&gt;), and it guarantees that B&lt;DBI&gt; handles C&lt;bind_cols()&gt; for
you.

What you do is the following:

  AV* av;
  int numFields = DBIc_NUM_FIELDS(imp_sth); /* Correct, if NUM_FIELDS
      is constant for this statement. There are drivers where this is
      not the case! */
  int chopBlanks = DBIc_is(imp_sth, DBIcf_ChopBlanks);
  int i;

  if (!fetch_new_row_of_data(...)) {
      ... /* check for error or end-of-data */
      DBIc_ACTIVE_off(imp_sth); /* turn off Active flag automatically */
      return Nullav;
  }
  /* get the fbav (field buffer array value) for this row       */
  /* it is very important to only call this after you know      */
  /* that you have a row of data to return.                     */
  av = DBIc_DBISTATE(imp_sth)-&gt;get_fbav(imp_sth);
  for (i = 0; i &lt; numFields; i++) {
      SV* sv = fetch_a_field(..., i);
      if (chopBlanks &amp;&amp; SvOK(sv) &amp;&amp; type_is_blank_padded(field_type[i])) {
          /*  Remove white space from end (only) of sv  */
      }
      sv_setsv(AvARRAY(av)[i], sv); /* Note: (re)use! */
  }
  return av;

There's no need to use a C&lt;fetch_a_field()&gt; function returning an C&lt;SV*&gt;.
It's more common to use your database API functions to fetch the
data as character strings and use code like this:

  sv_setpvn(AvARRAY(av)[i], char_ptr, char_count);

C&lt;NULL&gt; values must be returned as C&lt;undef&gt;. You can use code like this:

  SvOK_off(AvARRAY(av)[i]);

The function returns the C&lt;AV&gt; prepared by B&lt;DBI&gt; for success or C&lt;Nullav&gt;
otherwise.

 *FIX ME* Discuss what happens when there's no more data to fetch.
 Are errors permitted if another fetch occurs after the first fetch
 that reports no more data. (Permitted, not required.)

If an error occurs which leaves the I&lt;$sth&gt; in a state where remaining
rows can't be fetched then I&lt;Active&gt; should be turned off before the
method returns.

=head3 The dbd_st_finish3 method

The C&lt;$sth-E&lt;gt&gt;finish()&gt; method can be called if the user wishes to
indicate that no more rows will be fetched even if the database has more
rows to offer, and the B&lt;DBI&gt; code can call the function when handles are
being destroyed. See the B&lt;DBI&gt; specification for more background details.

In both circumstances, the B&lt;DBI&gt; code ends up calling the
C&lt;dbd_st_finish3()&gt; method (if you provide a mapping for
C&lt;dbd_st_finish3()&gt; in F&lt;dbdimp.h&gt;), or C&lt;dbd_st_finish()&gt; otherwise.
The difference is that C&lt;dbd_st_finish3()&gt; takes a third argument which
is an C&lt;int&gt; with the value 1 if it is being called from a C&lt;destroy()&gt;
method and 0 otherwise.

Note that B&lt;DBI&gt; v1.32 and earlier test on C&lt;dbd_db_finish3()&gt; to call
C&lt;dbd_st_finish3()&gt;; if you provide C&lt;dbd_st_finish3()&gt;, either define
C&lt;dbd_db_finish3()&gt; too, or insist on B&lt;DBI&gt; v1.33 or later.

All it I&lt;needs&gt; to do is turn off the I&lt;Active&gt; flag for the I&lt;sth&gt;.
It will only be called by F&lt;Driver.xst&gt; code, if the driver has set I&lt;ACTIVE&gt;
to on for the I&lt;sth&gt;.

Outline example:

  int dbd_st_finish3(SV* sth, imp_sth_t* imp_sth, int from_destroy) {
      if (DBIc_ACTIVE(imp_sth))
      {
          /* close cursor or equivalent action */
          DBIc_ACTIVE_off(imp_sth);
      }
      return 1;
  }

The from_destroy parameter is true if C&lt;dbd_st_finish3()&gt; is being called
from C&lt;DESTROY()&gt; - and so the statement is about to be destroyed.
For many drivers there is no point in doing anything more than turning off
the I&lt;Active&gt; flag in this case.

The function returns I&lt;TRUE&gt; for success, I&lt;FALSE&gt; otherwise, but there isn't
a lot anyone can do to recover if there is an error.

=head3 The dbd_st_destroy method

This function is the private part of the statement handle destructor.

  void dbd_st_destroy(SV* sth, imp_sth_t* imp_sth) {
      ... /* any clean-up that's needed */
      DBIc_IMPSET_off(imp_sth); /* let DBI know we've done it   */
  }

The B&lt;DBI&gt; F&lt;Driver.xst&gt; code will call C&lt;dbd_st_finish()&gt; for you, if the
I&lt;sth&gt; has the I&lt;ACTIVE&gt; flag set, before calling C&lt;dbd_st_destroy()&gt;.

=head3 The dbd_st_STORE_attrib and dbd_st_FETCH_attrib methods

These functions correspond to C&lt;dbd_db_STORE()&gt; and C&lt;dbd_db_FETCH()&gt; attrib
above, except that they are for statement handles.
See above.

  int dbd_st_STORE_attrib(SV* sth, imp_sth_t* imp_sth, SV* keysv,
                          SV* valuesv);
  SV* dbd_st_FETCH_attrib(SV* sth, imp_sth_t* imp_sth, SV* keysv);

=head3 The dbd_bind_ph method

This function is internally used by the C&lt;bind_param()&gt; method, the
C&lt;bind_param_inout()&gt; method and by the B&lt;DBI&gt; F&lt;Driver.xst&gt; code if
C&lt;execute()&gt; is called with any bind parameters.

  int dbd_bind_ph (SV *sth, imp_sth_t *imp_sth, SV *param,
                   SV *value, IV sql_type, SV *attribs,
                   int is_inout, IV maxlen);

The I&lt;param&gt; argument holds an C&lt;IV&gt; with the parameter number (1, 2, ...).
The I&lt;value&gt; argument is the parameter value and I&lt;sql_type&gt; is its type.

If your driver does not support C&lt;bind_param_inout()&gt; then you should
ignore I&lt;maxlen&gt; and croak if I&lt;is_inout&gt; is I&lt;TRUE&gt;.

If your driver I&lt;does&gt; support C&lt;bind_param_inout()&gt; then you should
note that I&lt;value&gt; is the C&lt;SV&gt; I&lt;after&gt; dereferencing the reference
passed to C&lt;bind_param_inout()&gt;.

In drivers of simple databases the function will, for example, store
the value in a parameter array and use it later in C&lt;dbd_st_execute()&gt;.
See the B&lt;DBD::mysql&gt; driver for an example.

=head3 Implementing bind_param_inout support

To provide support for parameters bound by reference rather than by
value, the driver must do a number of things.  First, and most
importantly, it must note the references and stash them in its own
driver structure.  Secondly, when a value is bound to a column, the
driver must discard any previous reference bound to the column.  On
each execute, the driver must evaluate the references and internally
bind the values resulting from the references.  This is only applicable
if the user writes:

  $sth-&gt;execute;

If the user writes:

  $sth-&gt;execute(@values);

then B&lt;DBI&gt; automatically calls the binding code for each element of
I&lt;@values&gt;.  These calls are indistinguishable from explicit user calls to
C&lt;bind_param()&gt;.

=head2 C/XS version of Makefile.PL

The F&lt;Makefile.PL&gt; file for a C/XS driver is similar to the code needed
for a pure Perl driver, but there are a number of extra bits of
information needed by the build system.

For example, the attributes list passed to C&lt;WriteMakefile()&gt; needs
to specify the object files that need to be compiled and built into
the shared object (DLL). This is often, but not necessarily, just
F&lt;dbdimp.o&gt; (unless that should be F&lt;dbdimp.obj&gt; because you're building
on MS Windows).

Note that you can reliably determine the extension of the object files
from the I&lt;$Config{obj_ext}&gt; values, and there are many other useful pieces
of configuration information lurking in that hash.
You get access to it with:

    use Config;

=head2 Methods which do not need to be written

The B&lt;DBI&gt; code implements the majority of the methods which are accessed
using the notation C&lt;DBI-E&lt;gt&gt;function()&gt;, the only exceptions being
C&lt;DBI-E&lt;gt&gt;connect()&gt; and C&lt;DBI-E&lt;gt&gt;data_sources()&gt; which require
support from the driver.

The B&lt;DBI&gt; code implements the following documented driver, database and
statement functions which do not need to be written by the B&lt;DBD&gt; driver
writer.

=over 4

=item $dbh-&gt;do()

The default implementation of this function prepares, executes and
destroys the statement.  This can be replaced if there is a better
way to implement this, such as C&lt;EXECUTE IMMEDIATE&gt; which can
sometimes be used if there are no parameters.

=item $h-&gt;errstr()

=item $h-&gt;err()

=item $h-&gt;state()

=item $h-&gt;trace()

The B&lt;DBD&gt; driver does not need to worry about these routines at all.

=item $h-&gt;{ChopBlanks}

This attribute needs to be honored during C&lt;fetch()&gt; operations, but does
not need to be handled by the attribute handling code.

=item $h-&gt;{RaiseError}

The B&lt;DBD&gt; driver does not need to worry about this attribute at all.

=item $h-&gt;{PrintError}

The B&lt;DBD&gt; driver does not need to worry about this attribute at all.

=item $sth-&gt;bind_col()

Assuming the driver uses the C&lt;DBIc_DBISTATE(imp_xxh)-E&lt;gt&gt;get_fbav()&gt;
function (C drivers, see below), or the C&lt;$sth-E&lt;gt&gt;_set_fbav($data)&gt;
method (Perl drivers) the driver does not need to do anything about this
routine.

=item $sth-&gt;bind_columns()

Regardless of whether the driver uses
C&lt;DBIc_DBISTATE(imp_xxh)-E&lt;gt&gt;get_fbav()&gt;, the driver does not need
to do anything about this routine as it simply iteratively calls
C&lt;$sth-E&lt;gt&gt;bind_col()&gt;.

=back

The B&lt;DBI&gt; code implements a default implementation of the following
functions which do not need to be written by the B&lt;DBD&gt; driver writer
unless the default implementation is incorrect for the Driver.

=over 4

=item $dbh-&gt;quote()

This should only be written if the database does not accept the ANSI
SQL standard for quoting strings, with the string enclosed in single
quotes and any embedded single quotes replaced by two consecutive
single quotes.

For the two argument form of quote, you need to implement the
C&lt;type_info()&gt; method to provide the information that quote needs.

=item $dbh-&gt;ping()

This should be implemented as a simple efficient way to determine
whether the connection to the database is still alive. Typically
code like this:

  sub ping {
      my $dbh = shift;
      $sth = $dbh-&gt;prepare_cached(q{
          select * from A_TABLE_NAME where 1=0
      }) or return 0;
      $sth-&gt;execute or return 0;
      $sth-&gt;finish;
      return 1;
  }

where I&lt;A_TABLE_NAME&gt; is the name of a table that always exists (such as a
database system catalogue).

=item $drh-&gt;default_user

The default implementation of default_user will get the database
username and password fields from C&lt;$ENV{DBI_USER}&gt; and
C&lt;$ENV{DBI_PASS}&gt;. You can override this method. It is called as
follows:

  ($user, $pass) = $drh-&gt;default_user($user, $pass, $attr)

=back

=head1 METADATA METHODS

The exposition above ignores the B&lt;DBI&gt; MetaData methods.
The metadata methods are all associated with a database handle.

=head2 Using DBI::DBD::Metadata

The B&lt;DBI::DBD::Metadata&gt; module is a good semi-automatic way for the
developer of a B&lt;DBD&gt; module to write the C&lt;get_info()&gt; and C&lt;type_info()&gt;
functions quickly and accurately.

=head3 Generating the get_info method

Prior to B&lt;DBI&gt; v1.33, this existed as the method C&lt;write_getinfo_pm()&gt;
in the B&lt;DBI::DBD&gt; module. From B&lt;DBI&gt; v1.33, it exists as the method
C&lt;write_getinfo_pm()&gt; in the B&lt;DBI::DBD::Metadata&gt; module. This
discussion assumes you have B&lt;DBI&gt; v1.33 or later.

You examine the documentation for C&lt;write_getinfo_pm()&gt; using:

    perldoc DBI::DBD::Metadata

To use it, you need a Perl B&lt;DBI&gt; driver for your database which implements
the C&lt;get_info()&gt; method. In practice, this means you need to install
B&lt;DBD::ODBC&gt;, an ODBC driver manager, and an ODBC driver for your
database.

With the pre-requisites in place, you might type:

    perl -MDBI::DBD::Metadata -we \
       "write_getinfo_pm (qw{ dbi:ODBC:foo_db username password Driver })"

The procedure writes to standard output the code that should be added to
your F&lt;Driver.pm&gt; file and the code that should be written to
F&lt;lib/DBD/Driver/GetInfo.pm&gt;.

You should review the output to ensure that it is sensible.

=head3 Generating the type_info method

Given the idea of the C&lt;write_getinfo_pm()&gt; method, it was not hard
to devise a parallel method, C&lt;write_typeinfo_pm()&gt;, which does the
analogous job for the B&lt;DBI&gt; C&lt;type_info_all()&gt; metadata method. The
C&lt;write_typeinfo_pm()&gt; method was added to B&lt;DBI&gt; v1.33.

You examine the documentation for C&lt;write_typeinfo_pm()&gt; using:

    perldoc DBI::DBD::Metadata

The setup is exactly analogous to the mechanism described in
L&lt;/Generating the get_info method&gt;.

With the pre-requisites in place, you might type:

    perl -MDBI::DBD::Metadata -we \
       "write_typeinfo (qw{ dbi:ODBC:foo_db username password Driver })"

The procedure writes to standard output the code that should be added to
your F&lt;Driver.pm&gt; file and the code that should be written to
F&lt;lib/DBD/Driver/TypeInfo.pm&gt;.

You should review the output to ensure that it is sensible.

=head2 Writing DBD::Driver::db::get_info

If you use the B&lt;DBI::DBD::Metadata&gt; module, then the code you need is
generated for you.

If you decide not to use the B&lt;DBI::DBD::Metadata&gt; module, you
should probably borrow the code from a driver that has done so (eg
B&lt;DBD::Informix&gt; from version 1.05 onwards) and crib the code from
there, or look at the code that generates that module and follow
that. The method in F&lt;Driver.pm&gt; will be very simple; the method in
F&lt;lib/DBD/Driver/GetInfo.pm&gt; is not very much more complex unless your
DBMS itself is much more complex.

Note that some of the B&lt;DBI&gt; utility methods rely on information from the
C&lt;get_info()&gt; method to perform their operations correctly. See, for
example, the C&lt;quote_identifier()&gt; and quote methods, discussed below.

=head2 Writing DBD::Driver::db::type_info_all

If you use the C&lt;DBI::DBD::Metadata&gt; module, then the code you need is
generated for you.

If you decide not to use the C&lt;DBI::DBD::Metadata&gt; module, you
should probably borrow the code from a driver that has done so (eg
C&lt;DBD::Informix&gt; from version 1.05 onwards) and crib the code from
there, or look at the code that generates that module and follow
that. The method in F&lt;Driver.pm&gt; will be very simple; the method in
F&lt;lib/DBD/Driver/TypeInfo.pm&gt; is not very much more complex unless your
DBMS itself is much more complex.

=head2 Writing DBD::Driver::db::type_info

The guidelines on writing this method are still not really clear.
No sample implementation is available.

=head2 Writing DBD::Driver::db::table_info

 *FIX ME* The guidelines on writing this method have not been written yet.
 No sample implementation is available.

=head2 Writing DBD::Driver::db::column_info

 *FIX ME* The guidelines on writing this method have not been written yet.
 No sample implementation is available.

=head2 Writing DBD::Driver::db::primary_key_info

 *FIX ME* The guidelines on writing this method have not been written yet.
 No sample implementation is available.

=head2 Writing DBD::Driver::db::primary_key

 *FIX ME* The guidelines on writing this method have not been written yet.
 No sample implementation is available.

=head2 Writing DBD::Driver::db::foreign_key_info

 *FIX ME* The guidelines on writing this method have not been written yet.
 No sample implementation is available.

=head2 Writing DBD::Driver::db::tables

This method generates an array of names in a format suitable for being
embedded in SQL statements in places where a table name is expected.

If your database hews close enough to the SQL standard or if you have
implemented an appropriate C&lt;table_info()&gt; function and and the appropriate
C&lt;quote_identifier()&gt; function, then the B&lt;DBI&gt; default version of this method
will work for your driver too.

Otherwise, you have to write a function yourself, such as:

    sub tables
    {
        my($dbh, $cat, $sch, $tab, $typ) = @_;
        my(@res);
        my($sth) = $dbh-&gt;table_info($cat, $sch, $tab, $typ);
        my(@arr);
        while (@arr = $sth-&gt;fetchrow_array)
        {
            push @res, $dbh-&gt;quote_identifier($arr[0], $arr[1], $arr[2]);
        }
        return @res;
    }

See also the default implementation in F&lt;DBI.pm&gt;.

=head2 Writing DBD::Driver::db::quote

This method takes a value and converts it into a string suitable for
embedding in an SQL statement as a string literal.

If your DBMS accepts the SQL standard notation for strings (single
quotes around the string as a whole with any embedded single quotes
doubled up), then you do not need to write this method as B&lt;DBI&gt; provides a
default method that does it for you.

If your DBMS uses an alternative notation or escape mechanism, then you
need to provide an equivalent function. For example, suppose your DBMS
used C notation with double quotes around the string and backslashes
escaping both double quotes and backslashes themselves. Then you might
write the function as:

    sub quote
    {
        my($dbh, $str) = @_;
        $str =~ s/["\\]/\\$&amp;/gmo;
        return qq{"$str"};
    }

Handling newlines and other control characters is left as an exercise
for the reader.

This sample method ignores the I&lt;$data_type&gt; indicator which is the
optional second argument to the method.

=head2 Writing DBD::Driver::db::quote_identifier

This method is called to ensure that the name of the given table (or
other database object) can be embedded into an SQL statement without
danger of misinterpretation. The result string should be usable in the
text of an SQL statement as the identifier for a table.

If your DBMS accepts the SQL standard notation for quoted identifiers
(which uses double quotes around the identifier as a whole, with any
embedded double quotes doubled up) and accepts I&lt;"schema"."identifier"&gt;
(and I&lt;"catalog"."schema"."identifier"&gt; when a catalog is specified), then
you do not need to write this method as B&lt;DBI&gt; provides a default method
that does it for you.

In fact, even if your DBMS does not handle exactly that notation but
you have implemented the C&lt;get_info()&gt; method and it gives the correct
responses, then it will work for you. If your database is fussier, then
you need to implement your own version of the function.

For example, B&lt;DBD::Informix&gt; has to deal with an environment variable
I&lt;DELIMIDENT&gt;. If it is not set, then the DBMS treats names enclosed in
double quotes as strings rather than names, which is usually a syntax
error. Additionally, the catalog portion of the name is separated from
the schema and table by a different delimiter (colon instead of dot),
and the catalog portion is never enclosed in quotes. (Fortunately,
valid strings for the catalog will never contain weird characters that
might need to be escaped, unless you count dots, dashes, slashes and
at-signs as weird.) Finally, an Informix database can contain objects
that cannot be accessed because they were created by a user with the
I&lt;DELIMIDENT&gt; environment variable set, but the current user does not
have it set. By design choice, the C&lt;quote_identifier()&gt; method encloses
those identifiers in double quotes anyway, which generally triggers a
syntax error, and the metadata methods which generate lists of tables
etc omit those identifiers from the result sets.

    sub quote_identifier
    {
        my($dbh, $cat, $sch, $obj) = @_;
        my($rv) = "";
        my($qq) = (defined $ENV{DELIMIDENT}) ? '"' : '';
        $rv .= qq{$cat:} if (defined $cat);
        if (defined $sch)
        {
            if ($sch !~ m/^\w+$/o)
            {
                $qq = '"';
                $sch =~ s/$qq/$qq$qq/gm;
            }
            $rv .= qq{$qq$sch$qq.};
        }
        if (defined $obj)
        {
            if ($obj !~ m/^\w+$/o)
            {
                $qq = '"';
                $obj =~ s/$qq/$qq$qq/gm;
            }
            $rv .= qq{$qq$obj$qq};
        }
        return $rv;
    }

Handling newlines and other control characters is left as an exercise
for the reader.

Note that there is an optional fourth parameter to this function which
is a reference to a hash of attributes; this sample implementation
ignores that.

This sample implementation also ignores the single-argument variant of
the method.

=head1 TRACING

Tracing in DBI is controlled with a combination of a trace level and a
set of flags which together are known as the trace settings. The trace
settings are stored in a single integer and divided into levels and
flags by a set of masks (C&lt;DBIc_TRACE_LEVEL_MASK&gt; and
C&lt;DBIc_TRACE_FLAGS_MASK&gt;).

Each handle has it's own trace settings and so does the DBI. When you
call a method the DBI merges the handles settings into its own for the
duration of the call: the trace flags of the handle are OR'd into the
trace flags of the DBI, and if the handle has a higher trace level
then the DBI trace level is raised to match it. The previous DBI trace
settings are restored when the called method returns.

=head2 Trace Level

The trace level is the first 4 bits of the trace settings (masked by
C&lt;DBIc_TRACE_FLAGS_MASK&gt;) and represents trace levels of 1 to 15. Do
not output anything at trace levels less than 3 as they are reserved
for DBI.

For advice on what to output at each level see "Trace Levels" in
L&lt;DBI&gt;.

To test for a trace level you can use the C&lt;DBIc_TRACE_LEVEL&gt; macro
like this:

  if (DBIc_TRACE_LEVEL(imp_xxh) &gt;= 2) {
      PerlIO_printf(DBIc_LOGPIO(imp_xxh), "foobar");
  }

Also B&lt;note&gt; the use of PerlIO_printf which you should always use for
tracing and never the C C&lt;stdio.h&gt; I/O functions.

=head2 Trace Flags

Trace flags are used to enable tracing of specific activities within
the DBI and drivers. The DBI defines some trace flags and drivers can
define others. DBI trace flag names begin with a capital letter and
driver specific names begin with a lowercase letter. For a list of DBI
defined trace flags see "Trace Flags" in L&lt;DBI&gt;.

If you want to use private trace flags you'll probably want to be able
to set them by name. Drivers are expected to override the
parse_trace_flag (note the singular) and check if $trace_flag_name is
a driver specific trace flags and, if not, then call the DBIs default
parse_trace_flag(). To do that you'll need to define a
parse_trace_flag() method like this:

  sub parse_trace_flag {
      my ($h, $name) = @_;
      return 0x01000000 if $name eq 'foo';
      return 0x02000000 if $name eq 'bar';
      return 0x04000000 if $name eq 'baz';
      return 0x08000000 if $name eq 'boo';
      return 0x10000000 if $name eq 'bop';
      return $h-&gt;SUPER::parse_trace_flag($name);
  }

All private flag names must be lowercase, and all private flags must
be in the top 8 of the 32 bits of C&lt;DBIc_TRACE_FLAGS(imp)&gt; i.e.,
0xFF000000.

If you've defined a parse_trace_flag() method in ::db you'll also want
it in ::st, so just alias it in:

  *parse_trace_flag = \&amp;DBD::foo:db::parse_trace_flag;

You may want to act on the current 'SQL' trace flag that DBI defines
to output SQL prepared/executed as DBI currently does not do SQL
tracing.

=head2 Trace Macros

Access to the trace level and trace flags is via a set of macros.

  DBIc_TRACE_SETTINGS(imp) returns the trace settings
  DBIc_TRACE_LEVEL(imp) returns the trace level
  DBIc_TRACE_FLAGS(imp) returns the trace flags
  DBIc_TRACE(imp, flags, flaglevel, level)

  e.g.,

  DBIc_TRACE(imp, 0, 0, 4)
    if level &gt;= 4

  DBIc_TRACE(imp, DBDtf_FOO, 2, 4)
    if tracing DBDtf_FOO &amp; level&gt;=2 or level&gt;=4

  DBIc_TRACE(imp, DBDtf_FOO, 2, 0)
    as above but never trace just due to level

=head1 WRITING AN EMULATION LAYER FOR AN OLD PERL INTERFACE

Study F&lt;Oraperl.pm&gt; (supplied with B&lt;DBD::Oracle&gt;) and F&lt;Ingperl.pm&gt; (supplied
with B&lt;DBD::Ingres&gt;) and the corresponding I&lt;dbdimp.c&gt; files for ideas.

Note that the emulation code sets C&lt;$dbh-E&lt;gt&gt;{CompatMode} = 1;&gt; for each
connection so that the internals of the driver can implement behaviour
compatible with the old interface when dealing with those handles.

=head2 Setting emulation perl variables

For example, ingperl has a I&lt;$sql_rowcount&gt; variable. Rather than try
to manually update this in F&lt;Ingperl.pm&gt; it can be done faster in C code.
In C&lt;dbd_init()&gt;:

  sql_rowcount = perl_get_sv("Ingperl::sql_rowcount", GV_ADDMULTI);

In the relevant places do:

  if (DBIc_COMPAT(imp_sth))     /* only do this for compatibility mode handles */
      sv_setiv(sql_rowcount, the_row_count);

=head1 OTHER MISCELLANEOUS INFORMATION

=head2 The imp_xyz_t types

Any handle has a corresponding C structure filled with private data.
Some of this data is reserved for use by B&lt;DBI&gt; (except for using the
DBIc macros below), some is for you. See the description of the
F&lt;dbdimp.h&gt; file above for examples. Most functions in F&lt;dbdimp.c&gt;
are passed both the handle C&lt;xyz&gt; and a pointer to C&lt;imp_xyz&gt;. In
rare cases, however, you may use the following macros:

=over 4

=item D_imp_dbh(dbh)

Given a function argument I&lt;dbh&gt;, declare a variable I&lt;imp_dbh&gt; and
initialize it with a pointer to the handles private data. Note: This
must be a part of the function header, because it declares a variable.

=item D_imp_sth(sth)

Likewise for statement handles.

=item D_imp_xxx(h)

Given any handle, declare a variable I&lt;imp_xxx&gt; and initialize it
with a pointer to the handles private data. It is safe, for example,
to cast I&lt;imp_xxx&gt; to C&lt;imp_dbh_t*&gt;, if C&lt;DBIc_TYPE(imp_xxx) == DBIt_DB&gt;.
(You can also call C&lt;sv_derived_from(h, "DBI::db")&gt;, but that's much
slower.)

=item D_imp_dbh_from_sth

Given a I&lt;imp_sth&gt;, declare a variable I&lt;imp_dbh&gt; and initialize it with a
pointer to the parent database handle's implementors structure.

=back

=head2 Using DBIc_IMPSET_on

The driver code which initializes a handle should use C&lt;DBIc_IMPSET_on()&gt;
as soon as its state is such that the cleanup code must be called.
When this happens is determined by your driver code.

B&lt;Failure to call this can lead to corruption of data structures.&gt;

For example, B&lt;DBD::Informix&gt; maintains a linked list of database
handles in the driver, and within each handle, a linked list of
statements. Once a statement is added to the linked list, it is crucial
that it is cleaned up (removed from the list). When I&lt;DBIc_IMPSET_on()&gt;
was being called too late, it was able to cause all sorts of problems.

=head2 Using DBIc_is(), DBIc_has(), DBIc_on() and DBIc_off()

Once upon a long time ago, the only way of handling the internal B&lt;DBI&gt;
boolean flags/attributes was through macros such as:

  DBIc_WARN       DBIc_WARN_on        DBIc_WARN_off
  DBIc_COMPAT     DBIc_COMPAT_on      DBIc_COMPAT_off

Each of these took an I&lt;imp_xxh&gt; pointer as an argument.

Since then, new attributes have been added such as I&lt;ChopBlanks&gt;,
I&lt;RaiseError&gt; and I&lt;PrintError&gt;, and these do not have the full set of
macros. The approved method for handling these is now the four macros:

  DBIc_is(imp, flag)
  DBIc_has(imp, flag)       an alias for DBIc_is
  DBIc_on(imp, flag)
  DBIc_off(imp, flag)
  DBIc_set(imp, flag, on)   set if on is true, else clear

Consequently, the C&lt;DBIc_XXXXX&gt; family of macros is now mostly deprecated
and new drivers should avoid using them, even though the older drivers
will probably continue to do so for quite a while yet. However...

There is an I&lt;important exception&gt; to that. The I&lt;ACTIVE&gt; and I&lt;IMPSET&gt;
flags should be set via the C&lt;DBIc_ACTIVE_on()&gt; and C&lt;DBIc_IMPSET_on()&gt; macros,
and unset via the C&lt;DBIc_ACTIVE_off()&gt; and C&lt;DBIc_IMPSET_off()&gt; macros.

=head2 Using the get_fbav() method

B&lt;THIS IS CRITICAL for C/XS drivers&gt;.

The C&lt;$sth-E&lt;gt&gt;bind_col()&gt; and C&lt;$sth-E&lt;gt&gt;bind_columns()&gt; documented
in the B&lt;DBI&gt; specification do not have to be implemented by the driver
writer because B&lt;DBI&gt; takes care of the details for you.

However, the key to ensuring that bound columns work is to call the
function C&lt;DBIc_DBISTATE(imp_xxh)-E&lt;gt&gt;get_fbav()&gt; in the code which
fetches a row of data.

This returns an C&lt;AV&gt;, and each element of the C&lt;AV&gt; contains the C&lt;SV&gt; which
should be set to contain the returned data.

The pure Perl equivalent is the C&lt;$sth-E&lt;gt&gt;_set_fbav($data)&gt; method, as
described in the part on pure Perl drivers.

=head2 Casting strings to Perl types based on a SQL type

DBI from 1.611 (and DBIXS_REVISION 13606) defines the
sql_type_cast_svpv method which may be used to cast a string
representation of a value to a more specific Perl type based on a SQL
type. You should consider using this method when processing bound
column data as it provides some support for the TYPE bind_col
attribute which is rarely used in drivers.

  int sql_type_cast_svpv(pTHX_ SV *sv, int sql_type, U32 flags, void *v)

C&lt;sv&gt; is what you would like cast, C&lt;sql_type&gt; is one of the DBI defined
SQL types (e.g., C&lt;SQL_INTEGER&gt;) and C&lt;flags&gt; is a bitmask as follows:

=over

=item DBIstcf_STRICT

If set this indicates you want an error state returned if the cast
cannot be performed.

=item DBIstcf_DISCARD_STRING

If set and the pv portion of the C&lt;sv&gt; is cast then this will cause
sv's pv to be freed up.

=back

sql_type_cast_svpv returns the following states:

 -2 sql_type is not handled - sv not changed
 -1 sv is undef, sv not changed
  0 sv could not be cast cleanly and DBIstcf_STRICT was specified
  1 sv could not be case cleanly and DBIstcf_STRICT was not specified
  2 sv was cast ok

The current implementation of sql_type_cast_svpv supports
C&lt;SQL_INTEGER&gt;, C&lt;SQL_DOUBLE&gt; and C&lt;SQL_NUMERIC&gt;. C&lt;SQL_INTEGER&gt; uses
sv_2iv and hence may set IV, UV or NV depending on the
number. C&lt;SQL_DOUBLE&gt; uses sv_2nv so may set NV and C&lt;SQL_NUMERIC&gt;
will set IV or UV or NV.

DBIstcf_STRICT should be implemented as the StrictlyTyped attribute
and DBIstcf_DISCARD_STRING implemented as the DiscardString attribute
to the bind_col method and both default to off.

See DBD::Oracle for an example of how this is used.

=head1 SUBCLASSING DBI DRIVERS

This is definitely an open subject. It can be done, as demonstrated by
the B&lt;DBD::File&gt; driver, but it is not as simple as one might think.

(Note that this topic is different from subclassing the B&lt;DBI&gt;. For an
example of that, see the F&lt;t/subclass.t&gt; file supplied with the B&lt;DBI&gt;.)

The main problem is that the I&lt;dbh&gt;'s and I&lt;sth&gt;'s that your C&lt;connect()&gt; and
C&lt;prepare()&gt; methods return are not instances of your B&lt;DBD::Driver::db&gt;
or B&lt;DBD::Driver::st&gt; packages, they are not even derived from it.
Instead they are instances of the B&lt;DBI::db&gt; or B&lt;DBI::st&gt; classes or
a derived subclass. Thus, if you write a method C&lt;mymethod()&gt; and do a

  $dbh-&gt;mymethod()

then the autoloader will search for that method in the package B&lt;DBI::db&gt;.
Of course you can instead to a

  $dbh-&gt;func('mymethod')

and that will indeed work, even if C&lt;mymethod()&gt; is inherited, but not
without additional work. Setting I&lt;@ISA&gt; is not sufficient.

=head2 Overwriting methods

The first problem is, that the C&lt;connect()&gt; method has no idea of
subclasses. For example, you cannot implement base class and subclass
in the same file: The C&lt;install_driver()&gt; method wants to do a

  require DBD::Driver;

In particular, your subclass B&lt;has&gt; to be a separate driver, from
the view of B&lt;DBI&gt;, and you cannot share driver handles.

Of course that's not much of a problem. You should even be able
to inherit the base classes C&lt;connect()&gt; method. But you cannot
simply overwrite the method, unless you do something like this,
quoted from B&lt;DBD::CSV&gt;:

  sub connect ($$;$$$) {
      my ($drh, $dbname, $user, $auth, $attr) = @_;

      my $this = $drh-&gt;DBD::File::dr::connect($dbname, $user, $auth, $attr);
      if (!exists($this-&gt;{csv_tables})) {
          $this-&gt;{csv_tables} = {};
      }

      $this;
  }

Note that we cannot do a

  $drh-&gt;SUPER::connect($dbname, $user, $auth, $attr);

as we would usually do in a an OO environment, because I&lt;$drh&gt; is an instance
of B&lt;DBI::dr&gt;. And note, that the C&lt;connect()&gt; method of B&lt;DBD::File&gt; is
able to handle subclass attributes. See the description of Pure Perl
drivers above.

It is essential that you always call superclass method in the above
manner. However, that should do.

=head2 Attribute handling

Fortunately the B&lt;DBI&gt; specifications allow a simple, but still
performant way of handling attributes. The idea is based on the
convention that any driver uses a prefix I&lt;driver_&gt; for its private
methods. Thus it's always clear whether to pass attributes to the super
class or not. For example, consider this C&lt;STORE()&gt; method from the
B&lt;DBD::CSV&gt; class:

  sub STORE {
      my ($dbh, $attr, $val) = @_;
      if ($attr !~ /^driver_/) {
          return $dbh-&gt;DBD::File::db::STORE($attr, $val);
      }
      if ($attr eq 'driver_foo') {
      ...
  }

=cut

use Exporter ();
use Config qw(%Config);
use Carp;
use Cwd;
use File::Spec;
use strict;
use vars qw(
    @ISA @EXPORT
    $is_dbi
);

BEGIN {
    if ($^O eq 'VMS') {
	require vmsish;
	import  vmsish;
	require VMS::Filespec;
	import  VMS::Filespec;
    }
    else {
	*vmsify  = sub { return $_[0] };
	*unixify = sub { return $_[0] };
    }
}

@ISA = qw(Exporter);

@EXPORT = qw(
    dbd_dbi_dir
    dbd_dbi_arch_dir
    dbd_edit_mm_attribs
    dbd_postamble
);

BEGIN {
    $is_dbi = (-r 'DBI.pm' &amp;&amp; -r 'DBI.xs' &amp;&amp; -r 'DBIXS.h');
    require DBI unless $is_dbi;
}

my $done_inst_checks;

sub _inst_checks {
    return if $done_inst_checks++;
    my $cwd = cwd();
    if ($cwd =~ /\Q$Config{path_sep}/) {
	warn "*** Warning: Path separator characters (`$Config{path_sep}') ",
	    "in the current directory path ($cwd) may cause problems\a\n\n";
        sleep 2;
    }
    if ($cwd =~ /\s/) {
	warn "*** Warning: whitespace characters ",
	    "in the current directory path ($cwd) may cause problems\a\n\n";
        sleep 2;
    }
    if (   $^O eq 'MSWin32'
	&amp;&amp; $Config{cc} eq 'cl'
	&amp;&amp; !(exists $ENV{'LIB'} &amp;&amp; exists $ENV{'INCLUDE'}))
    {
	die &lt;&lt;EOT;
*** You're using Microsoft Visual C++ compiler or similar but
    the LIB and INCLUDE environment variables are not both set.

    You need to run the VCVARS32.BAT batch file that was supplied
    with the compiler before you can use it.

    A copy of vcvars32.bat can typically be found in the following
    directories under your Visual Studio install directory:
        Visual C++ 6.0:     vc98\\bin
        Visual Studio .NET: vc7\\bin

    Find it, run it, then retry this.

    If you think this error is not correct then just set the LIB and
    INCLUDE environment variables to some value to disable the check.
EOT
    }
}

sub dbd_edit_mm_attribs {
    # this both edits the attribs in-place and returns the flattened attribs
    my $mm_attr = shift;
    my $dbd_attr = shift || {};
    croak "dbd_edit_mm_attribs( \%makemaker [, \%other ]): too many parameters"
	if @_;
    _inst_checks();

    # what can be done
    my %test_variants = (
	p =&gt; {	name =&gt; "DBI::PurePerl",
		match =&gt; qr/^\d/,
		add =&gt; [ '$ENV{DBI_PUREPERL} = 2',
			 'END { delete $ENV{DBI_PUREPERL}; }' ],
	},
	g =&gt; {	name =&gt; "DBD::Gofer",
		match =&gt; qr/^\d/,
		add =&gt; [ q{$ENV{DBI_AUTOPROXY} = 'dbi:Gofer:transport=null;policy=pedantic'},
			 q|END { delete $ENV{DBI_AUTOPROXY}; }| ],
	},
	n =&gt; {	name =&gt; "DBI::SQL::Nano",
		match =&gt; qr/^(?:48dbi_dbd_sqlengine|49dbd_file|5\ddbm_\w+|85gofer)\.t$/,
		add =&gt; [ q{$ENV{DBI_SQL_NANO} = 1},
			 q|END { delete $ENV{DBI_SQL_NANO}; }| ],
	},
    #   mx =&gt; {	name =&gt; "DBD::Multiplex",
    #           add =&gt; [ q{local $ENV{DBI_AUTOPROXY} = 'dbi:Multiplex:';} ],
    #   }
    #   px =&gt; {	name =&gt; "DBD::Proxy",
    #		need mechanism for starting/stopping the proxy server
    #		add =&gt; [ q{local $ENV{DBI_AUTOPROXY} = 'dbi:Proxy:XXX';} ],
    #   }
    );

    # decide what needs doing
    $dbd_attr-&gt;{create_pp_tests} or delete $test_variants{p};
    $dbd_attr-&gt;{create_nano_tests} or delete $test_variants{n};
    $dbd_attr-&gt;{create_gap_tests} or delete $test_variants{g};

    # expand for all combinations
    my @all_keys = my @tv_keys = sort keys %test_variants;
    while( @tv_keys ) {
	my $cur_key = shift @tv_keys;
	last if( 1 &lt; length $cur_key );
	my @new_keys;
	foreach my $remain (@tv_keys) {
	    push @new_keys, $cur_key . $remain unless $remain =~ /$cur_key/;
	}
	push @tv_keys, @new_keys;
	push @all_keys, @new_keys;
    }

    my %uniq_keys;
    foreach my $key (@all_keys) {
	@tv_keys = sort split //, $key;
	my $ordered = join( '', @tv_keys );
	$uniq_keys{$ordered} = 1;
    }
    @all_keys = sort { length $a &lt;=&gt; length $b or $a cmp $b } keys %uniq_keys;

    # do whatever needs doing
    if( keys %test_variants ) {
	# XXX need to convert this to work within the generated Makefile
	# so 'make' creates them and 'make clean' deletes them
	opendir DIR, 't' or die "Can't read 't' directory: $!";
	my @tests = grep { /\.t$/ } readdir DIR;
	closedir DIR;

        foreach my $test_combo (@all_keys) {
	    @tv_keys = split //, $test_combo;
	    my @test_names = map { $test_variants{$_}-&gt;{name} } @tv_keys;
            printf "Creating test wrappers for " . join( " + ", @test_names ) . ":\n";
	    my @test_matches = map { $test_variants{$_}-&gt;{match} } @tv_keys;
	    my @test_adds;
	    foreach my $test_add ( map { $test_variants{$_}-&gt;{add} } @tv_keys) {
		push @test_adds, @$test_add;
	    }
	    my $v_type = $test_combo;
	    $v_type = 'x' . $v_type if length( $v_type ) &gt; 1;

	TEST:
            foreach my $test (sort @tests) {
		foreach my $match (@test_matches) {
		    next TEST if $test !~ $match;
		}
                my $usethr = ($test =~ /(\d+|\b)thr/ &amp;&amp; $] &gt;= 5.008 &amp;&amp; $Config{useithreads});
                my $v_test = "t/zv${v_type}_$test";
                my $v_perl = ($test =~ /taint/) ? "perl -wT" : "perl -w";
		printf "%s %s\n", $v_test, ($usethr) ? "(use threads)" : "";
		open PPT, "&gt;$v_test" or warn "Can't create $v_test: $!";
		print PPT "#!$v_perl\n";
		print PPT "use threads;\n" if $usethr;
		print PPT "$_;\n" foreach @test_adds;
		print PPT "require './t/$test'; # or warn \$!;\n";
		close PPT or warn "Error writing $v_test: $!";
	    }
	}
    }
    return %$mm_attr;
}

sub dbd_dbi_dir {
    _inst_checks();
    return '.' if $is_dbi;
    my $dbidir = $INC{'DBI.pm'} || die "DBI.pm not in %INC!";
    $dbidir =~ s:/DBI\.pm$::;
    return $dbidir;
}

sub dbd_dbi_arch_dir {
    _inst_checks();
    return '$(INST_ARCHAUTODIR)' if $is_dbi;
    my $dbidir = dbd_dbi_dir();
    my %seen;
    my @try = grep { not $seen{$_}++ } map { vmsify( unixify($_) . "/auto/DBI/" ) } @INC;
    my @xst = grep { -f vmsify( unixify($_) . "/Driver.xst" ) } @try;
    Carp::croak("Unable to locate Driver.xst in @try") unless @xst;
    Carp::carp( "Multiple copies of Driver.xst found in: @xst") if @xst &gt; 1;
    print "Using DBI $DBI::VERSION (for perl $] on $Config{archname}) installed in $xst[0]\n";
    return File::Spec-&gt;canonpath($xst[0]);
}

sub dbd_postamble {
    my $self = shift;
    _inst_checks();
    my $dbi_instarch_dir = ($is_dbi) ? "." : dbd_dbi_arch_dir();
    my $dbi_driver_xst= File::Spec-&gt;catfile($dbi_instarch_dir, 'Driver.xst');
    my $xstf_h = File::Spec-&gt;catfile($dbi_instarch_dir, 'Driver_xst.h');

    # we must be careful of quotes, expecially for Win32 here.
    return '
# --- This section was generated by DBI::DBD::dbd_postamble()
DBI_INSTARCH_DIR='.$dbi_instarch_dir.'
DBI_DRIVER_XST='.$dbi_driver_xst.'

# The main dependancy (technically correct but probably not used)
$(BASEEXT).c: $(BASEEXT).xsi

# This dependancy is needed since MakeMaker uses the .xs.o rule
$(BASEEXT)$(OBJ_EXT): $(BASEEXT).xsi

$(BASEEXT).xsi: $(DBI_DRIVER_XST) '.$xstf_h.'
	$(PERL) -p -e "s/~DRIVER~/$(BASEEXT)/g" $(DBI_DRIVER_XST) &gt; $(BASEEXT).xsi

# ---
';
}

package DBDI; # just to reserve it via PAUSE for the future

1;

__END__

=head1 AUTHORS

Jonathan Leffler &lt;jleffler@us.ibm.com&gt; (previously &lt;jleffler@informix.com&gt;),
Jochen Wiedmann &lt;joe@ispsoft.de&gt;,
Steffen Goeldner &lt;sgoeldner@cpan.org&gt;,
and Tim Bunce &lt;dbi-users@perl.org&gt;.

=cut
</pre></body></html>