# $Id: DBI.pm 11570 2008-07-22 21:45:16Z timbo $ # vim: ts=8:sw=4:noet # # Copyright (c) 1994-2008 Tim Bunce Ireland # # See COPYRIGHT section in pod text below for usage and distribution rights. # require 5.006_00; BEGIN { $DBI::VERSION = "1.607"; # ==> ALSO update the version in the pod text below! } =head1 NAME DBI - Database independent interface for Perl =head1 SYNOPSIS use DBI; @driver_names = DBI->available_drivers; %drivers = DBI->installed_drivers; @data_sources = DBI->data_sources($driver_name, \%attr); $dbh = DBI->connect($data_source, $username, $auth, \%attr); $rv = $dbh->do($statement); $rv = $dbh->do($statement, \%attr); $rv = $dbh->do($statement, \%attr, @bind_values); $ary_ref = $dbh->selectall_arrayref($statement); $hash_ref = $dbh->selectall_hashref($statement, $key_field); $ary_ref = $dbh->selectcol_arrayref($statement); $ary_ref = $dbh->selectcol_arrayref($statement, \%attr); @row_ary = $dbh->selectrow_array($statement); $ary_ref = $dbh->selectrow_arrayref($statement); $hash_ref = $dbh->selectrow_hashref($statement); $sth = $dbh->prepare($statement); $sth = $dbh->prepare_cached($statement); $rc = $sth->bind_param($p_num, $bind_value); $rc = $sth->bind_param($p_num, $bind_value, $bind_type); $rc = $sth->bind_param($p_num, $bind_value, \%attr); $rv = $sth->execute; $rv = $sth->execute(@bind_values); $rv = $sth->execute_array(\%attr, ...); $rc = $sth->bind_col($col_num, \$col_variable); $rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind); @row_ary = $sth->fetchrow_array; $ary_ref = $sth->fetchrow_arrayref; $hash_ref = $sth->fetchrow_hashref; $ary_ref = $sth->fetchall_arrayref; $ary_ref = $sth->fetchall_arrayref( $slice, $max_rows ); $hash_ref = $sth->fetchall_hashref( $key_field ); $rv = $sth->rows; $rc = $dbh->begin_work; $rc = $dbh->commit; $rc = $dbh->rollback; $quoted_string = $dbh->quote($string); $rc = $h->err; $str = $h->errstr; $rv = $h->state; $rc = $dbh->disconnect; I =head2 GETTING HELP If you have questions about DBI, or DBD driver modules, you can get help from the I mailing list. You don't have to subscribe to the list in order to post, though I'd recommend it. You can get help on subscribing and using the list by emailing I. I don't recommend the DBI cpanform (at http://www.cpanforum.com/dist/DBI) because relatively few people read it compared with dbi-users@perl.org. To help you make the best use of the dbi-users mailing list, and any other lists or forums you may use, I I recommend that you read "How To Ask Questions The Smart Way" by Eric Raymond: L. If you think you've found a bug then please also read "How to Report Bugs Effectively" by Simon Tatham: L. The DBI home page at L and the DBI FAQ at L are always worth a visit. They include links to other resources. Before asking any questions, reread this document, consult the archives and read the DBI FAQ. The archives are listed at the end of this document and on the DBI home page. This document often uses terms like I, I, I. If you're not familar with those terms then it would be a good idea to read at least the following perl manuals first: L, L, L, and L. Please note that Tim Bunce does not maintain the mailing lists or the web page (generous volunteers do that). So please don't send mail directly to him; he just doesn't have the time to answer questions personally. The I mailing list has lots of experienced people who should be able to help you if you need it. If you do email Tim he's very likely to just forward it to the mailing list. =head2 NOTES This is the DBI specification that corresponds to the DBI version 1.607 ($Revision: 11570 $). The DBI is evolving at a steady pace, so it's good to check that you have the latest copy. The significant user-visible changes in each release are documented in the L module so you can read them by executing C. Some DBI changes require changes in the drivers, but the drivers can take some time to catch up. Newer versions of the DBI have added features that may not yet be supported by the drivers you use. Talk to the authors of your drivers if you need a new feature that's not yet supported. Features added after DBI 1.21 (February 2002) are marked in the text with the version number of the DBI release they first appeared in. Extensions to the DBI API often use the C namespace. See L. DBI extension modules can be found at L. And all modules related to the DBI can be found at L. =cut # The POD text continues at the end of the file. package DBI; use Carp(); use DynaLoader (); use Exporter (); BEGIN { @ISA = qw(Exporter DynaLoader); # Make some utility functions available if asked for @EXPORT = (); # we export nothing by default @EXPORT_OK = qw(%DBI %DBI_methods hash); # also populated by export_ok_tags: %EXPORT_TAGS = ( sql_types => [ qw( SQL_GUID SQL_WLONGVARCHAR SQL_WVARCHAR SQL_WCHAR SQL_BIGINT SQL_BIT SQL_TINYINT SQL_LONGVARBINARY SQL_VARBINARY SQL_BINARY SQL_LONGVARCHAR SQL_UNKNOWN_TYPE SQL_ALL_TYPES SQL_CHAR SQL_NUMERIC SQL_DECIMAL SQL_INTEGER SQL_SMALLINT SQL_FLOAT SQL_REAL SQL_DOUBLE SQL_DATETIME SQL_DATE SQL_INTERVAL SQL_TIME SQL_TIMESTAMP SQL_VARCHAR SQL_BOOLEAN SQL_UDT SQL_UDT_LOCATOR SQL_ROW SQL_REF SQL_BLOB SQL_BLOB_LOCATOR SQL_CLOB SQL_CLOB_LOCATOR SQL_ARRAY SQL_ARRAY_LOCATOR SQL_MULTISET SQL_MULTISET_LOCATOR SQL_TYPE_DATE SQL_TYPE_TIME SQL_TYPE_TIMESTAMP SQL_TYPE_TIME_WITH_TIMEZONE SQL_TYPE_TIMESTAMP_WITH_TIMEZONE SQL_INTERVAL_YEAR SQL_INTERVAL_MONTH SQL_INTERVAL_DAY SQL_INTERVAL_HOUR SQL_INTERVAL_MINUTE SQL_INTERVAL_SECOND SQL_INTERVAL_YEAR_TO_MONTH SQL_INTERVAL_DAY_TO_HOUR SQL_INTERVAL_DAY_TO_MINUTE SQL_INTERVAL_DAY_TO_SECOND SQL_INTERVAL_HOUR_TO_MINUTE SQL_INTERVAL_HOUR_TO_SECOND SQL_INTERVAL_MINUTE_TO_SECOND ) ], sql_cursor_types => [ qw( SQL_CURSOR_FORWARD_ONLY SQL_CURSOR_KEYSET_DRIVEN SQL_CURSOR_DYNAMIC SQL_CURSOR_STATIC SQL_CURSOR_TYPE_DEFAULT ) ], # for ODBC cursor types utils => [ qw( neat neat_list $neat_maxlen dump_results looks_like_number data_string_diff data_string_desc data_diff ) ], profile => [ qw( dbi_profile dbi_profile_merge dbi_profile_merge_nodes dbi_time ) ], # notionally "in" DBI::Profile and normally imported from there ); $DBI::dbi_debug = 0; $DBI::neat_maxlen = 1000; $DBI::stderr = 2_000_000_000; # a very round number below 2**31 # If you get an error here like "Can't find loadable object ..." # then you haven't installed the DBI correctly. Read the README # then install it again. if ( $ENV{DBI_PUREPERL} ) { eval { bootstrap DBI } if $ENV{DBI_PUREPERL} == 1; require DBI::PurePerl if $@ or $ENV{DBI_PUREPERL} >= 2; $DBI::PurePerl ||= 0; # just to silence "only used once" warnings } else { bootstrap DBI; } $EXPORT_TAGS{preparse_flags} = [ grep { /^DBIpp_\w\w_/ } keys %{__PACKAGE__."::"} ]; Exporter::export_ok_tags(keys %EXPORT_TAGS); } # Alias some handle methods to also be DBI class methods for (qw(trace_msg set_err parse_trace_flag parse_trace_flags)) { no strict; *$_ = \&{"DBD::_::common::$_"}; } use strict; DBI->trace(split /=/, $ENV{DBI_TRACE}, 2) if $ENV{DBI_TRACE}; $DBI::connect_via ||= "connect"; # check if user wants a persistent database connection ( Apache + mod_perl ) if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) { $DBI::connect_via = "Apache::DBI::connect"; DBI->trace_msg("DBI connect via $DBI::connect_via in $INC{'Apache/DBI.pm'}\n"); } # check for weaken support, used by ChildHandles my $HAS_WEAKEN = eval { require Scalar::Util; # this will croak() if this Scalar::Util doesn't have a working weaken(). Scalar::Util::weaken( \my $test ); # same test as in t/72childhandles.t 1; }; %DBI::installed_drh = (); # maps driver names to installed driver handles sub installed_drivers { %DBI::installed_drh } %DBI::installed_methods = (); # XXX undocumented, may change sub installed_methods { %DBI::installed_methods } # Setup special DBI dynamic variables. See DBI::var::FETCH for details. # These are dynamically associated with the last handle used. tie $DBI::err, 'DBI::var', '*err'; # special case: referenced via IHA list tie $DBI::state, 'DBI::var', '"state'; # special case: referenced via IHA list tie $DBI::lasth, 'DBI::var', '!lasth'; # special case: return boolean tie $DBI::errstr, 'DBI::var', '&errstr'; # call &errstr in last used pkg tie $DBI::rows, 'DBI::var', '&rows'; # call &rows in last used pkg sub DBI::var::TIESCALAR{ my $var = $_[1]; bless \$var, 'DBI::var'; } sub DBI::var::STORE { Carp::croak("Can't modify \$DBI::${$_[0]} special variable") } { # used to catch DBI->{Attrib} mistake sub DBI::DBI_tie::TIEHASH { bless {} } sub DBI::DBI_tie::STORE { Carp::carp("DBI->{$_[1]} is invalid syntax (you probably want \$h->{$_[1]})");} *DBI::DBI_tie::FETCH = \&DBI::DBI_tie::STORE; } tie %DBI::DBI => 'DBI::DBI_tie'; # --- Driver Specific Prefix Registry --- my $dbd_prefix_registry = { ad_ => { class => 'DBD::AnyData', }, ado_ => { class => 'DBD::ADO', }, amzn_ => { class => 'DBD::Amazon', }, best_ => { class => 'DBD::BestWins', }, csv_ => { class => 'DBD::CSV', }, db2_ => { class => 'DBD::DB2', }, dbi_ => { class => 'DBI', }, dbm_ => { class => 'DBD::DBM', }, df_ => { class => 'DBD::DF', }, f_ => { class => 'DBD::File', }, file_ => { class => 'DBD::TextFile', }, go_ => { class => 'DBD::Gofer', }, ib_ => { class => 'DBD::InterBase', }, ing_ => { class => 'DBD::Ingres', }, ix_ => { class => 'DBD::Informix', }, jdbc_ => { class => 'DBD::JDBC', }, monetdb_ => { class => 'DBD::monetdb', }, msql_ => { class => 'DBD::mSQL', }, mvsftp_ => { class => 'DBD::MVS_FTPSQL', }, mysql_ => { class => 'DBD::mysql', }, mx_ => { class => 'DBD::Multiplex', }, nullp_ => { class => 'DBD::NullP', }, odbc_ => { class => 'DBD::ODBC', }, ora_ => { class => 'DBD::Oracle', }, pg_ => { class => 'DBD::Pg', }, plb_ => { class => 'DBD::Plibdata', }, proxy_ => { class => 'DBD::Proxy', }, rdb_ => { class => 'DBD::RDB', }, sapdb_ => { class => 'DBD::SAP_DB', }, solid_ => { class => 'DBD::Solid', }, sponge_ => { class => 'DBD::Sponge', }, sql_ => { class => 'SQL::Statement', }, syb_ => { class => 'DBD::Sybase', }, tdat_ => { class => 'DBD::Teradata', }, tmpl_ => { class => 'DBD::Template', }, tmplss_ => { class => 'DBD::TemplateSS', }, tuber_ => { class => 'DBD::Tuber', }, uni_ => { class => 'DBD::Unify', }, vt_ => { class => 'DBD::Vt', }, wmi_ => { class => 'DBD::WMI', }, x_ => { }, # for private use xbase_ => { class => 'DBD::XBase', }, xl_ => { class => 'DBD::Excel', }, yaswi_ => { class => 'DBD::Yaswi', }, }; sub dump_dbd_registry { require Data::Dumper; local $Data::Dumper::Sortkeys=1; local $Data::Dumper::Indent=1; print Data::Dumper->Dump([$dbd_prefix_registry], [qw($dbd_prefix_registry)]); } # --- Dynamically create the DBI Standard Interface my $keeperr = { O=>0x0004 }; %DBI::DBI_methods = ( # Define the DBI interface methods per class: common => { # Interface methods common to all DBI handle classes 'DESTROY' => { O=>0x004|0x10000 }, 'CLEAR' => $keeperr, 'EXISTS' => $keeperr, 'FETCH' => { O=>0x0404 }, 'FETCH_many' => { O=>0x0404 }, 'FIRSTKEY' => $keeperr, 'NEXTKEY' => $keeperr, 'STORE' => { O=>0x0418 | 0x4 }, _not_impl => undef, can => { O=>0x0100 }, # special case, see dispatch debug => { U =>[1,2,'[$debug_level]'], O=>0x0004 }, # old name for trace dump_handle => { U =>[1,3,'[$message [, $level]]'], O=>0x0004 }, err => $keeperr, errstr => $keeperr, state => $keeperr, func => { O=>0x0006 }, parse_trace_flag => { U =>[2,2,'$name'], O=>0x0404, T=>8 }, parse_trace_flags => { U =>[2,2,'$flags'], O=>0x0404, T=>8 }, private_data => { U =>[1,1], O=>0x0004 }, set_err => { U =>[3,6,'$err, $errmsg [, $state, $method, $rv]'], O=>0x0010 }, trace => { U =>[1,3,'[$trace_level, [$filename]]'], O=>0x0004 }, trace_msg => { U =>[2,3,'$message_text [, $min_level ]' ], O=>0x0004, T=>8 }, swap_inner_handle => { U =>[2,3,'$h [, $allow_reparent ]'] }, private_attribute_info => { }, }, dr => { # Database Driver Interface 'connect' => { U =>[1,5,'[$db [,$user [,$passwd [,\%attr]]]]'], H=>3, O=>0x8000 }, 'connect_cached'=>{U=>[1,5,'[$db [,$user [,$passwd [,\%attr]]]]'], H=>3, O=>0x8000 }, 'disconnect_all'=>{ U =>[1,1], O=>0x0800 }, data_sources => { U =>[1,2,'[\%attr]' ], O=>0x0800 }, default_user => { U =>[3,4,'$user, $pass [, \%attr]' ] }, dbixs_revision => $keeperr, }, db => { # Database Session Class Interface data_sources => { U =>[1,2,'[\%attr]' ], O=>0x0200 }, take_imp_data => { U =>[1,1], O=>0x10000 }, clone => { U =>[1,2,'[\%attr]'] }, connected => { U =>[1,0], O => 0x0004 }, begin_work => { U =>[1,2,'[ \%attr ]'], O=>0x0400 }, commit => { U =>[1,1], O=>0x0480|0x0800 }, rollback => { U =>[1,1], O=>0x0480|0x0800 }, 'do' => { U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x3200 }, last_insert_id => { U =>[5,6,'$catalog, $schema, $table_name, $field_name [, \%attr ]'], O=>0x2800 }, preparse => { }, # XXX prepare => { U =>[2,3,'$statement [, \%attr]'], O=>0xA200 }, prepare_cached => { U =>[2,4,'$statement [, \%attr [, $if_active ] ]'], O=>0xA200 }, selectrow_array => { U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 }, selectrow_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 }, selectrow_hashref=>{ U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 }, selectall_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 }, selectall_hashref=>{ U =>[3,0,'$statement, $keyfield [, \%attr [, @bind_params ] ]'], O=>0x2000 }, selectcol_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'], O=>0x2000 }, ping => { U =>[1,1], O=>0x0404 }, disconnect => { U =>[1,1], O=>0x0400|0x0800|0x10000 }, quote => { U =>[2,3, '$string [, $data_type ]' ], O=>0x0430 }, quote_identifier=> { U =>[2,6, '$name [, ...] [, \%attr ]' ], O=>0x0430 }, rows => $keeperr, tables => { U =>[1,6,'$catalog, $schema, $table, $type [, \%attr ]' ], O=>0x2200 }, table_info => { U =>[1,6,'$catalog, $schema, $table, $type [, \%attr ]' ], O=>0x2200|0x8800 }, column_info => { U =>[5,6,'$catalog, $schema, $table, $column [, \%attr ]'],O=>0x2200|0x8800 }, primary_key_info=> { U =>[4,5,'$catalog, $schema, $table [, \%attr ]' ], O=>0x2200|0x8800 }, primary_key => { U =>[4,5,'$catalog, $schema, $table [, \%attr ]' ], O=>0x2200 }, foreign_key_info=> { U =>[7,8,'$pk_catalog, $pk_schema, $pk_table, $fk_catalog, $fk_schema, $fk_table [, \%attr ]' ], O=>0x2200|0x8800 }, statistics_info => { U =>[6,7,'$catalog, $schema, $table, $unique_only, $quick, [, \%attr ]' ], O=>0x2200|0x8800 }, type_info_all => { U =>[1,1], O=>0x2200|0x0800 }, type_info => { U =>[1,2,'$data_type'], O=>0x2200 }, get_info => { U =>[2,2,'$info_type'], O=>0x2200|0x0800 }, }, st => { # Statement Class Interface bind_col => { U =>[3,4,'$column, \\$var [, \%attr]'] }, bind_columns => { U =>[2,0,'\\$var1 [, \\$var2, ...]'] }, bind_param => { U =>[3,4,'$parameter, $var [, \%attr]'] }, bind_param_inout=> { U =>[4,5,'$parameter, \\$var, $maxlen, [, \%attr]'] }, execute => { U =>[1,0,'[@args]'], O=>0x1040 }, bind_param_array => { U =>[3,4,'$parameter, $var [, \%attr]'] }, bind_param_inout_array => { U =>[4,5,'$parameter, \\@var, $maxlen, [, \%attr]'] }, execute_array => { U =>[2,0,'\\%attribs [, @args]'], O=>0x1040|0x4000 }, execute_for_fetch => { U =>[2,3,'$fetch_sub [, $tuple_status]'], O=>0x1040|0x4000 }, fetch => undef, # alias for fetchrow_arrayref fetchrow_arrayref => undef, fetchrow_hashref => undef, fetchrow_array => undef, fetchrow => undef, # old alias for fetchrow_array fetchall_arrayref => { U =>[1,3, '[ $slice [, $max_rows]]'] }, fetchall_hashref => { U =>[2,2,'$key_field'] }, blob_read => { U =>[4,5,'$field, $offset, $len [, \\$buf [, $bufoffset]]'] }, blob_copy_to_file => { U =>[3,3,'$field, $filename_or_handleref'] }, dump_results => { U =>[1,5,'$maxfieldlen, $linesep, $fieldsep, $filehandle'] }, more_results => { U =>[1,1] }, finish => { U =>[1,1] }, cancel => { U =>[1,1], O=>0x0800 }, rows => $keeperr, _get_fbav => undef, _set_fbav => { T=>6 }, }, ); while ( my ($class, $meths) = each %DBI::DBI_methods ) { my $ima_trace = 0+($ENV{DBI_IMA_TRACE}||0); while ( my ($method, $info) = each %$meths ) { my $fullmeth = "DBI::${class}::$method"; if ($DBI::dbi_debug >= 15) { # quick hack to list DBI methods # and optionally filter by IMA flags my $O = $info->{O}||0; printf "0x%04x %-20s\n", $O, $fullmeth unless $ima_trace && !($O & $ima_trace); } DBI->_install_method($fullmeth, 'DBI.pm', $info); } } { package DBI::common; @DBI::dr::ISA = ('DBI::common'); @DBI::db::ISA = ('DBI::common'); @DBI::st::ISA = ('DBI::common'); } # End of init code END { return unless defined &DBI::trace_msg; # return unless bootstrap'd ok local ($!,$?); DBI->trace_msg(sprintf(" -- DBI::END (\$\@: %s, \$!: %s)\n", $@||'', $!||''), 2); # Let drivers know why we are calling disconnect_all: $DBI::PERL_ENDING = $DBI::PERL_ENDING = 1; # avoid typo warning DBI->disconnect_all() if %DBI::installed_drh; } sub CLONE { my $olddbis = $DBI::_dbistate; _clone_dbis() unless $DBI::PurePerl; # clone the DBIS structure DBI->trace_msg(sprintf "CLONE DBI for new thread %s\n", $DBI::PurePerl ? "" : sprintf("(dbis %x -> %x)",$olddbis, $DBI::_dbistate)); while ( my ($driver, $drh) = each %DBI::installed_drh) { no strict 'refs'; next if defined &{"DBD::${driver}::CLONE"}; warn("$driver has no driver CLONE() function so is unsafe threaded\n"); } %DBI::installed_drh = (); # clear loaded drivers so they have a chance to reinitialize } sub parse_dsn { my ($class, $dsn) = @_; $dsn =~ s/^(dbi):(\w*?)(?:\((.*?)\))?://i or return; my ($scheme, $driver, $attr, $attr_hash) = (lc($1), $2, $3); $driver ||= $ENV{DBI_DRIVER} || ''; $attr_hash = { split /\s*=>?\s*|\s*,\s*/, $attr, -1 } if $attr; return ($scheme, $driver, $attr, $attr_hash, $dsn); } # --- The DBI->connect Front Door methods sub connect_cached { # For library code using connect_cached() with mod_perl # we redirect those calls to Apache::DBI::connect() as well my ($class, $dsn, $user, $pass, $attr) = @_; my $dbi_connect_method = ($DBI::connect_via eq "Apache::DBI::connect") ? 'Apache::DBI::connect' : 'connect_cached'; $attr = { $attr ? %$attr : (), # clone, don't modify callers data dbi_connect_method => $dbi_connect_method, }; return $class->connect($dsn, $user, $pass, $attr); } sub connect { my $class = shift; my ($dsn, $user, $pass, $attr, $old_driver) = my @orig_args = @_; my $driver; if ($attr and !ref($attr)) { # switch $old_driver<->$attr if called in old style Carp::carp("DBI->connect using 'old-style' syntax is deprecated and will be an error in future versions"); ($old_driver, $attr) = ($attr, $old_driver); } my $connect_meth = $attr->{dbi_connect_method}; $connect_meth ||= $DBI::connect_via; # fallback to default $dsn ||= $ENV{DBI_DSN} || $ENV{DBI_DBNAME} || '' unless $old_driver; if ($DBI::dbi_debug) { local $^W = 0; pop @_ if $connect_meth ne 'connect'; my @args = @_; $args[2] = '****'; # hide password DBI->trace_msg(" -> $class->$connect_meth(".join(", ",@args).")\n"); } Carp::croak('Usage: $class->connect([$dsn [,$user [,$passwd [,\%attr]]]])') if (ref $old_driver or ($attr and not ref $attr) or ref $pass); # extract dbi:driver prefix from $dsn into $1 $dsn =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i or '' =~ /()/; # ensure $1 etc are empty if match fails my $driver_attrib_spec = $2 || ''; # Set $driver. Old style driver, if specified, overrides new dsn style. $driver = $old_driver || $1 || $ENV{DBI_DRIVER} or Carp::croak("Can't connect to data source '$dsn' " ."because I can't work out what driver to use " ."(it doesn't seem to contain a 'dbi:driver:' prefix " ."and the DBI_DRIVER env var is not set)"); my $proxy; if ($ENV{DBI_AUTOPROXY} && $driver ne 'Proxy' && $driver ne 'Sponge' && $driver ne 'Switch') { my $dbi_autoproxy = $ENV{DBI_AUTOPROXY}; $proxy = 'Proxy'; if ($dbi_autoproxy =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i) { $proxy = $1; $driver_attrib_spec = join ",", ($driver_attrib_spec) ? $driver_attrib_spec : (), ($2 ) ? $2 : (); } $dsn = "$dbi_autoproxy;dsn=dbi:$driver:$dsn"; $driver = $proxy; DBI->trace_msg(" DBI_AUTOPROXY: dbi:$driver($driver_attrib_spec):$dsn\n"); } # avoid recursion if proxy calls DBI->connect itself local $ENV{DBI_AUTOPROXY}; my %attributes; # take a copy we can delete from if ($old_driver) { %attributes = %$attr if $attr; } else { # new-style connect so new default semantics %attributes = ( PrintError => 1, AutoCommit => 1, ref $attr ? %$attr : (), # attributes in DSN take precedence over \%attr connect parameter $driver_attrib_spec ? (split /\s*=>?\s*|\s*,\s*/, $driver_attrib_spec, -1) : (), ); } $attr = \%attributes; # now set $attr to refer to our local copy my $drh = $DBI::installed_drh{$driver} || $class->install_driver($driver) or die "panic: $class->install_driver($driver) failed"; # attributes in DSN take precedence over \%attr connect parameter $user = $attr->{Username} if defined $attr->{Username}; $pass = $attr->{Password} if defined $attr->{Password}; delete $attr->{Password}; # always delete Password as closure stores it securely if ( !(defined $user && defined $pass) ) { ($user, $pass) = $drh->default_user($user, $pass, $attr); } $attr->{Username} = $user; # force the Username to be the actual one used my $connect_closure = sub { my ($old_dbh, $override_attr) = @_; #use Data::Dumper; #warn "connect_closure: ".Data::Dumper::Dumper([$attr,\%attributes, $override_attr]); my $dbh; unless ($dbh = $drh->$connect_meth($dsn, $user, $pass, $attr)) { $user = '' if !defined $user; $dsn = '' if !defined $dsn; # $drh->errstr isn't safe here because $dbh->DESTROY may not have # been called yet and so the dbh errstr would not have been copied # up to the drh errstr. Certainly true for connect_cached! my $errstr = $DBI::errstr; # Getting '(no error string)' here is a symptom of a ref loop $errstr = '(no error string)' if !defined $errstr; my $msg = "$class connect('$dsn','$user',...) failed: $errstr"; DBI->trace_msg(" $msg\n"); # XXX HandleWarn unless ($attr->{HandleError} && $attr->{HandleError}->($msg, $drh, $dbh)) { Carp::croak($msg) if $attr->{RaiseError}; Carp::carp ($msg) if $attr->{PrintError}; } $! = 0; # for the daft people who do DBI->connect(...) || die "$!"; return $dbh; # normally undef, but HandleError could change it } # merge any attribute overrides but don't change $attr itself (for closure) my $apply = { ($override_attr) ? (%$attr, %$override_attr ) : %$attr }; # handle basic RootClass subclassing: my $rebless_class = $apply->{RootClass} || ($class ne 'DBI' ? $class : ''); if ($rebless_class) { no strict 'refs'; if ($apply->{RootClass}) { # explicit attribute (ie not static methd call class) delete $apply->{RootClass}; DBI::_load_class($rebless_class, 0); } unless (@{"$rebless_class\::db::ISA"} && @{"$rebless_class\::st::ISA"}) { Carp::carp("DBI subclasses '$rebless_class\::db' and ::st are not setup, RootClass ignored"); $rebless_class = undef; $class = 'DBI'; } else { $dbh->{RootClass} = $rebless_class; # $dbh->STORE called via plain DBI::db DBI::_set_isa([$rebless_class], 'DBI'); # sets up both '::db' and '::st' DBI::_rebless($dbh, $rebless_class); # appends '::db' } } if (%$apply) { if ($apply->{DbTypeSubclass}) { my $DbTypeSubclass = delete $apply->{DbTypeSubclass}; DBI::_rebless_dbtype_subclass($dbh, $rebless_class||$class, $DbTypeSubclass); } my $a; foreach $a (qw(Profile RaiseError PrintError AutoCommit)) { # do these first next unless exists $apply->{$a}; $dbh->{$a} = delete $apply->{$a}; } while ( my ($a, $v) = each %$apply) { eval { $dbh->{$a} = $v } or $@ && warn $@; } } # confirm to driver (ie if subclassed) that we've connected sucessfully # and finished the attribute setup. pass in the original arguments $dbh->connected(@orig_args); #if ref $dbh ne 'DBI::db' or $proxy; DBI->trace_msg(" <- connect= $dbh\n") if $DBI::dbi_debug; return $dbh; }; my $dbh = &$connect_closure(undef, undef); $dbh->{dbi_connect_closure} = $connect_closure if $dbh; return $dbh; } sub disconnect_all { keys %DBI::installed_drh; # reset iterator while ( my ($name, $drh) = each %DBI::installed_drh ) { $drh->disconnect_all() if ref $drh; } } sub disconnect { # a regular beginners bug Carp::croak("DBI->disconnect is not a DBI method (read the DBI manual)"); } sub install_driver { # croaks on failure my $class = shift; my($driver, $attr) = @_; my $drh; $driver ||= $ENV{DBI_DRIVER} || ''; # allow driver to be specified as a 'dbi:driver:' string $driver = $1 if $driver =~ s/^DBI:(.*?)://i; Carp::croak("usage: $class->install_driver(\$driver [, \%attr])") unless ($driver and @_<=3); # already installed return $drh if $drh = $DBI::installed_drh{$driver}; $class->trace_msg(" -> $class->install_driver($driver" .") for $^O perl=$] pid=$$ ruid=$< euid=$>\n") if $DBI::dbi_debug; # --- load the code my $driver_class = "DBD::$driver"; eval qq{package # hide from PAUSE DBI::_firesafe; # just in case require $driver_class; # load the driver }; if ($@) { my $err = $@; my $advice = ""; if ($err =~ /Can't find loadable object/) { $advice = "Perhaps DBD::$driver was statically linked into a new perl binary." ."\nIn which case you need to use that new perl binary." ."\nOr perhaps only the .pm file was installed but not the shared object file." } elsif ($err =~ /Can't locate.*?DBD\/$driver\.pm in \@INC/) { my @drv = $class->available_drivers(1); $advice = "Perhaps the DBD::$driver perl module hasn't been fully installed,\n" ."or perhaps the capitalisation of '$driver' isn't right.\n" ."Available drivers: ".join(", ", @drv)."."; } elsif ($err =~ /Can't load .*? for module DBD::/) { $advice = "Perhaps a required shared library or dll isn't installed where expected"; } elsif ($err =~ /Can't locate .*? in \@INC/) { $advice = "Perhaps a module that DBD::$driver requires hasn't been fully installed"; } Carp::croak("install_driver($driver) failed: $err$advice\n"); } if ($DBI::dbi_debug) { no strict 'refs'; (my $driver_file = $driver_class) =~ s/::/\//g; my $dbd_ver = ${"$driver_class\::VERSION"} || "undef"; $class->trace_msg(" install_driver: $driver_class version $dbd_ver" ." loaded from $INC{qq($driver_file.pm)}\n"); } # --- do some behind-the-scenes checks and setups on the driver $class->setup_driver($driver_class); # --- run the driver function $drh = eval { $driver_class->driver($attr || {}) }; unless ($drh && ref $drh && !$@) { my $advice = ""; $@ ||= "$driver_class->driver didn't return a handle"; # catch people on case in-sensitive systems using the wrong case $advice = "\nPerhaps the capitalisation of DBD '$driver' isn't right." if $@ =~ /locate object method/; Carp::croak("$driver_class initialisation failed: $@$advice"); } $DBI::installed_drh{$driver} = $drh; $class->trace_msg(" <- install_driver= $drh\n") if $DBI::dbi_debug; $drh; } *driver = \&install_driver; # currently an alias, may change sub setup_driver { my ($class, $driver_class) = @_; my $type; foreach $type (qw(dr db st)){ my $class = $driver_class."::$type"; no strict 'refs'; push @{"${class}::ISA"}, "DBD::_::$type" unless UNIVERSAL::isa($class, "DBD::_::$type"); my $mem_class = "DBD::_mem::$type"; push @{"${class}_mem::ISA"}, $mem_class unless UNIVERSAL::isa("${class}_mem", $mem_class) or $DBI::PurePerl; } } sub _rebless { my $dbh = shift; my ($outer, $inner) = DBI::_handles($dbh); my $class = shift(@_).'::db'; bless $inner => $class; bless $outer => $class; # outer last for return } sub _set_isa { my ($classes, $topclass) = @_; my $trace = DBI->trace_msg(" _set_isa([@$classes])\n"); foreach my $suffix ('::db','::st') { my $previous = $topclass || 'DBI'; # trees are rooted here foreach my $class (@$classes) { my $base_class = $previous.$suffix; my $sub_class = $class.$suffix; my $sub_class_isa = "${sub_class}::ISA"; no strict 'refs'; if (@$sub_class_isa) { DBI->trace_msg(" $sub_class_isa skipped (already set to @$sub_class_isa)\n") if $trace; } else { @$sub_class_isa = ($base_class) unless @$sub_class_isa; DBI->trace_msg(" $sub_class_isa = $base_class\n") if $trace; } $previous = $class; } } } sub _rebless_dbtype_subclass { my ($dbh, $rootclass, $DbTypeSubclass) = @_; # determine the db type names for class hierarchy my @hierarchy = DBI::_dbtype_names($dbh, $DbTypeSubclass); # add the rootclass prefix to each ('DBI::' or 'MyDBI::' etc) $_ = $rootclass.'::'.$_ foreach (@hierarchy); # load the modules from the 'top down' DBI::_load_class($_, 1) foreach (reverse @hierarchy); # setup class hierarchy if needed, does both '::db' and '::st' DBI::_set_isa(\@hierarchy, $rootclass); # finally bless the handle into the subclass DBI::_rebless($dbh, $hierarchy[0]); } sub _dbtype_names { # list dbtypes for hierarchy, ie Informix=>ADO=>ODBC my ($dbh, $DbTypeSubclass) = @_; if ($DbTypeSubclass && $DbTypeSubclass ne '1' && ref $DbTypeSubclass ne 'CODE') { # treat $DbTypeSubclass as a comma separated list of names my @dbtypes = split /\s*,\s*/, $DbTypeSubclass; $dbh->trace_msg(" DbTypeSubclass($DbTypeSubclass)=@dbtypes (explicit)\n"); return @dbtypes; } # XXX will call $dbh->get_info(17) (=SQL_DBMS_NAME) in future? my $driver = $dbh->{Driver}->{Name}; if ( $driver eq 'Proxy' ) { # XXX Looking into the internals of DBD::Proxy is questionable! ($driver) = $dbh->{proxy_client}->{application} =~ /^DBI:(.+?):/i or die "Can't determine driver name from proxy"; } my @dbtypes = (ucfirst($driver)); if ($driver eq 'ODBC' || $driver eq 'ADO') { # XXX will move these out and make extensible later: my $_dbtype_name_regexp = 'Oracle'; # eg 'Oracle|Foo|Bar' my %_dbtype_name_map = ( 'Microsoft SQL Server' => 'MSSQL', 'SQL Server' => 'Sybase', 'Adaptive Server Anywhere' => 'ASAny', 'ADABAS D' => 'AdabasD', ); my $name; $name = $dbh->func(17, 'GetInfo') # SQL_DBMS_NAME if $driver eq 'ODBC'; $name = $dbh->{ado_conn}->Properties->Item('DBMS Name')->Value if $driver eq 'ADO'; die "Can't determine driver name! ($DBI::errstr)\n" unless $name; my $dbtype; if ($_dbtype_name_map{$name}) { $dbtype = $_dbtype_name_map{$name}; } else { if ($name =~ /($_dbtype_name_regexp)/) { $dbtype = lc($1); } else { # generic mangling for other names: $dbtype = lc($name); } $dbtype =~ s/\b(\w)/\U$1/g; $dbtype =~ s/\W+/_/g; } # add ODBC 'behind' ADO push @dbtypes, 'ODBC' if $driver eq 'ADO'; # add discovered dbtype in front of ADO/ODBC unshift @dbtypes, $dbtype; } @dbtypes = &$DbTypeSubclass($dbh, \@dbtypes) if (ref $DbTypeSubclass eq 'CODE'); $dbh->trace_msg(" DbTypeSubclass($DbTypeSubclass)=@dbtypes\n"); return @dbtypes; } sub _load_class { my ($load_class, $missing_ok) = @_; DBI->trace_msg(" _load_class($load_class, $missing_ok)\n", 2); no strict 'refs'; return 1 if @{"$load_class\::ISA"}; # already loaded/exists (my $module = $load_class) =~ s!::!/!g; DBI->trace_msg(" _load_class require $module\n", 2); eval { require "$module.pm"; }; return 1 unless $@; return 0 if $missing_ok && $@ =~ /^Can't locate \Q$module.pm\E/; die $@; } sub init_rootclass { # deprecated return 1; } *internal = \&DBD::Switch::dr::driver; sub available_drivers { my($quiet) = @_; my(@drivers, $d, $f); local(*DBI::DIR, $@); my(%seen_dir, %seen_dbd); my $haveFileSpec = eval { require File::Spec }; foreach $d (@INC){ chomp($d); # Perl 5 beta 3 bug in #!./perl -Ilib from Test::Harness my $dbd_dir = ($haveFileSpec ? File::Spec->catdir($d, 'DBD') : "$d/DBD"); next unless -d $dbd_dir; next if $seen_dir{$d}; $seen_dir{$d} = 1; # XXX we have a problem here with case insensitive file systems # XXX since we can't tell what case must be used when loading. opendir(DBI::DIR, $dbd_dir) || Carp::carp "opendir $dbd_dir: $!\n"; foreach $f (readdir(DBI::DIR)){ next unless $f =~ s/\.pm$//; next if $f eq 'NullP'; if ($seen_dbd{$f}){ Carp::carp "DBD::$f in $d is hidden by DBD::$f in $seen_dbd{$f}\n" unless $quiet; } else { push(@drivers, $f); } $seen_dbd{$f} = $d; } closedir(DBI::DIR); } # "return sort @drivers" will not DWIM in scalar context. return wantarray ? sort @drivers : @drivers; } sub installed_versions { my ($class, $quiet) = @_; my %error; my %version = ( DBI => $DBI::VERSION ); $version{"DBI::PurePerl"} = $DBI::PurePerl::VERSION if $DBI::PurePerl; for my $driver ($class->available_drivers($quiet)) { next if $DBI::PurePerl && grep { -d "$_/auto/DBD/$driver" } @INC; my $drh = eval { local $SIG{__WARN__} = sub {}; $class->install_driver($driver); }; ($error{"DBD::$driver"}=$@),next if $@; no strict 'refs'; my $vers = ${"DBD::$driver" . '::VERSION'}; $version{"DBD::$driver"} = $vers || '?'; } if (wantarray) { return map { m/^DBD::(\w+)/ ? ($1) : () } sort keys %version; } if (!defined wantarray) { # void context require Config; # add more detail $version{OS} = "$^O\t($Config::Config{osvers})"; $version{Perl} = "$]\t($Config::Config{archname})"; $version{$_} = (($error{$_} =~ s/ \(\@INC.*//s),$error{$_}) for keys %error; printf " %-16s: %s\n",$_,$version{$_} for reverse sort keys %version; } return \%version; } sub data_sources { my ($class, $driver, @other) = @_; my $drh = $class->install_driver($driver); my @ds = $drh->data_sources(@other); return @ds; } sub neat_list { my ($listref, $maxlen, $sep) = @_; $maxlen = 0 unless defined $maxlen; # 0 == use internal default $sep = ", " unless defined $sep; join($sep, map { neat($_,$maxlen) } @$listref); } sub dump_results { # also aliased as a method in DBD::_::st my ($sth, $maxlen, $lsep, $fsep, $fh) = @_; return 0 unless $sth; $maxlen ||= 35; $lsep ||= "\n"; $fh ||= \*STDOUT; my $rows = 0; my $ref; while($ref = $sth->fetch) { print $fh $lsep if $rows++ and $lsep; my $str = neat_list($ref,$maxlen,$fsep); print $fh $str; # done on two lines to avoid 5.003 errors } print $fh "\n$rows rows".($DBI::err ? " ($DBI::err: $DBI::errstr)" : "")."\n"; $rows; } sub data_diff { my ($a, $b, $logical) = @_; my $diff = data_string_diff($a, $b); return "" if $logical and !$diff; my $a_desc = data_string_desc($a); my $b_desc = data_string_desc($b); return "" if !$diff and $a_desc eq $b_desc; $diff ||= "Strings contain the same sequence of characters" if length($a); $diff .= "\n" if $diff; return "a: $a_desc\nb: $b_desc\n$diff"; } sub data_string_diff { # Compares 'logical' characters, not bytes, so a latin1 string and an # an equivalent unicode string will compare as equal even though their # byte encodings are different. my ($a, $b) = @_; unless (defined $a and defined $b) { # one undef return "" if !defined $a and !defined $b; return "String a is undef, string b has ".length($b)." characters" if !defined $a; return "String b is undef, string a has ".length($a)." characters" if !defined $b; } require utf8; # hack to cater for perl 5.6 *utf8::is_utf8 = sub { (DBI::neat(shift)=~/^"/) } unless defined &utf8::is_utf8; my @a_chars = (utf8::is_utf8($a)) ? unpack("U*", $a) : unpack("C*", $a); my @b_chars = (utf8::is_utf8($b)) ? unpack("U*", $b) : unpack("C*", $b); my $i = 0; while (@a_chars && @b_chars) { ++$i, shift(@a_chars), shift(@b_chars), next if $a_chars[0] == $b_chars[0];# compare ordinal values my @desc = map { $_ > 255 ? # if wide character... sprintf("\\x{%04X}", $_) : # \x{...} chr($_) =~ /[[:cntrl:]]/ ? # else if control character ... sprintf("\\x%02X", $_) : # \x.. chr($_) # else as themselves } ($a_chars[0], $b_chars[0]); # highlight probable double-encoding? foreach my $c ( @desc ) { next unless $c =~ m/\\x\{08(..)}/; $c .= "='" .chr(hex($1)) ."'" } return sprintf "Strings differ at index $i: a[$i]=$desc[0], b[$i]=$desc[1]"; } return "String a truncated after $i characters" if @b_chars; return "String b truncated after $i characters" if @a_chars; return ""; } sub data_string_desc { # describe a data string my ($a) = @_; require bytes; require utf8; # hacks to cater for perl 5.6 *utf8::is_utf8 = sub { (DBI::neat(shift)=~/^"/) } unless defined &utf8::is_utf8; *utf8::valid = sub { 1 } unless defined &utf8::valid; # Give sufficient info to help diagnose at least these kinds of situations: # - valid UTF8 byte sequence but UTF8 flag not set # (might be ascii so also need to check for hibit to make it worthwhile) # - UTF8 flag set but invalid UTF8 byte sequence # could do better here, but this'll do for now my $utf8 = sprintf "UTF8 %s%s", utf8::is_utf8($a) ? "on" : "off", utf8::valid($a||'') ? "" : " but INVALID encoding"; return "$utf8, undef" unless defined $a; my $is_ascii = $a =~ m/^[\000-\177]*$/; return sprintf "%s, %s, %d characters %d bytes", $utf8, $is_ascii ? "ASCII" : "non-ASCII", length($a), bytes::length($a); } sub connect_test_perf { my($class, $dsn,$dbuser,$dbpass, $attr) = @_; Carp::croak("connect_test_perf needs hash ref as fourth arg") unless ref $attr; # these are non standard attributes just for this special method my $loops ||= $attr->{dbi_loops} || 5; my $par ||= $attr->{dbi_par} || 1; # parallelism my $verb ||= $attr->{dbi_verb} || 1; my $meth ||= $attr->{dbi_meth} || 'connect'; print "$dsn: testing $loops sets of $par connections:\n"; require "FileHandle.pm"; # don't let toke.c create empty FileHandle package local $| = 1; my $drh = $class->install_driver($dsn) or Carp::croak("Can't install $dsn driver\n"); # test the connection and warm up caches etc $drh->connect($dsn,$dbuser,$dbpass) or Carp::croak("connect failed: $DBI::errstr"); my $t1 = dbi_time(); my $loop; for $loop (1..$loops) { my @cons; print "Connecting... " if $verb; for (1..$par) { print "$_ "; push @cons, ($drh->connect($dsn,$dbuser,$dbpass) or Carp::croak("connect failed: $DBI::errstr\n")); } print "\nDisconnecting...\n" if $verb; for (@cons) { $_->disconnect or warn "disconnect failed: $DBI::errstr" } } my $t2 = dbi_time(); my $td = $t2 - $t1; printf "$meth %d and disconnect them, %d times: %.4fs / %d = %.4fs\n", $par, $loops, $td, $loops*$par, $td/($loops*$par); return $td; } # Help people doing DBI->errstr, might even document it one day # XXX probably best moved to cheaper XS code if this gets documented sub err { $DBI::err } sub errstr { $DBI::errstr } # --- Private Internal Function for Creating New DBI Handles # XXX move to PurePerl? *DBI::dr::TIEHASH = \&DBI::st::TIEHASH; *DBI::db::TIEHASH = \&DBI::st::TIEHASH; # These three special constructors are called by the drivers # The way they are called is likely to change. our $shared_profile; sub _new_drh { # called by DBD::::driver() my ($class, $initial_attr, $imp_data) = @_; # Provide default storage for State,Err and Errstr. # Note that these are shared by all child handles by default! XXX # State must be undef to get automatic faking in DBI::var::FETCH my ($h_state_store, $h_err_store, $h_errstr_store) = (undef, 0, ''); my $attr = { # these attributes get copied down to child handles by default 'State' => \$h_state_store, # Holder for DBI::state 'Err' => \$h_err_store, # Holder for DBI::err 'Errstr' => \$h_errstr_store, # Holder for DBI::errstr 'TraceLevel' => 0, FetchHashKeyName=> 'NAME', %$initial_attr, }; my ($h, $i) = _new_handle('DBI::dr', '', $attr, $imp_data, $class); # XXX DBI_PROFILE unless DBI::PurePerl because for some reason # it kills the t/zz_*_pp.t tests (they silently exit early) if ($ENV{DBI_PROFILE} && !$DBI::PurePerl) { # The profile object created here when the first driver is loaded # is shared by all drivers so we end up with just one set of profile # data and thus the 'total time in DBI' is really the true total. if (!$shared_profile) { # first time $h->{Profile} = $ENV{DBI_PROFILE}; $shared_profile = $h->{Profile}; } else { $h->{Profile} = $shared_profile; } } return $h unless wantarray; ($h, $i); } sub _new_dbh { # called by DBD::::dr::connect() my ($drh, $attr, $imp_data) = @_; my $imp_class = $drh->{ImplementorClass} or Carp::croak("DBI _new_dbh: $drh has no ImplementorClass"); substr($imp_class,-4,4) = '::db'; my $app_class = ref $drh; substr($app_class,-4,4) = '::db'; $attr->{Err} ||= \my $err; $attr->{Errstr} ||= \my $errstr; $attr->{State} ||= \my $state; _new_handle($app_class, $drh, $attr, $imp_data, $imp_class); } sub _new_sth { # called by DBD::::db::prepare) my ($dbh, $attr, $imp_data) = @_; my $imp_class = $dbh->{ImplementorClass} or Carp::croak("DBI _new_sth: $dbh has no ImplementorClass"); substr($imp_class,-4,4) = '::st'; my $app_class = ref $dbh; substr($app_class,-4,4) = '::st'; _new_handle($app_class, $dbh, $attr, $imp_data, $imp_class); } # end of DBI package # -------------------------------------------------------------------- # === The internal DBI Switch pseudo 'driver' class === { package # hide from PAUSE DBD::Switch::dr; DBI->setup_driver('DBD::Switch'); # sets up @ISA $DBD::Switch::dr::imp_data_size = 0; $DBD::Switch::dr::imp_data_size = 0; # avoid typo warning my $drh; sub driver { return $drh if $drh; # a package global my $inner; ($drh, $inner) = DBI::_new_drh('DBD::Switch::dr', { 'Name' => 'Switch', 'Version' => $DBI::VERSION, 'Attribution' => "DBI $DBI::VERSION by Tim Bunce", }); Carp::croak("DBD::Switch init failed!") unless ($drh && $inner); return $drh; } sub CLONE { undef $drh; } sub FETCH { my($drh, $key) = @_; return DBI->trace if $key eq 'DebugDispatch'; return undef if $key eq 'DebugLog'; # not worth fetching, sorry return $drh->DBD::_::dr::FETCH($key); undef; } sub STORE { my($drh, $key, $value) = @_; if ($key eq 'DebugDispatch') { DBI->trace($value); } elsif ($key eq 'DebugLog') { DBI->trace(-1, $value); } else { $drh->DBD::_::dr::STORE($key, $value); } } } # -------------------------------------------------------------------- # === OPTIONAL MINIMAL BASE CLASSES FOR DBI SUBCLASSES === # We only define default methods for harmless functions. # We don't, for example, define a DBD::_::st::prepare() { package # hide from PAUSE DBD::_::common; # ====== Common base class methods ====== use strict; # methods common to all handle types: sub _not_impl { my ($h, $method) = @_; $h->trace_msg("Driver does not implement the $method method.\n"); return; # empty list / undef } # generic TIEHASH default methods: sub FIRSTKEY { } sub NEXTKEY { } sub EXISTS { defined($_[0]->FETCH($_[1])) } # XXX undef? sub CLEAR { Carp::carp "Can't CLEAR $_[0] (DBI)" } sub FETCH_many { # XXX should move to C one day my $h = shift; return map { $h->FETCH($_) } @_; } *dump_handle = \&DBI::dump_handle; sub install_method { # special class method called directly by apps and/or drivers # to install new methods into the DBI dispatcher # DBD::Foo::db->install_method("foo_mumble", { usage => [...], options => '...' }); my ($class, $method, $attr) = @_; Carp::croak("Class '$class' must begin with DBD:: and end with ::db or ::st") unless $class =~ /^DBD::(\w+)::(dr|db|st)$/; my ($driver, $subtype) = ($1, $2); Carp::croak("invalid method name '$method'") unless $method =~ m/^([a-z]+_)\w+$/; my $prefix = $1; my $reg_info = $dbd_prefix_registry->{$prefix}; Carp::carp("method name prefix '$prefix' is not associated with a registered driver") unless $reg_info; my $full_method = "DBI::${subtype}::$method"; $DBI::installed_methods{$full_method} = $attr; my (undef, $filename, $line) = caller; # XXX reformat $attr as needed for _install_method my %attr = %{$attr||{}}; # copy so we can edit DBI->_install_method("DBI::${subtype}::$method", "$filename at line $line", \%attr); } sub parse_trace_flags { my ($h, $spec) = @_; my $level = 0; my $flags = 0; my @unknown; for my $word (split /\s*[|&,]\s*/, $spec) { if (DBI::looks_like_number($word) && $word <= 0xF && $word >= 0) { $level = $word; } elsif ($word eq 'ALL') { $flags = 0x7FFFFFFF; # XXX last bit causes negative headaches last; } elsif (my $flag = $h->parse_trace_flag($word)) { $flags |= $flag; } else { push @unknown, $word; } } if (@unknown && (ref $h ? $h->FETCH('Warn') : 1)) { Carp::carp("$h->parse_trace_flags($spec) ignored unknown trace flags: ". join(" ", map { DBI::neat($_) } @unknown)); } $flags |= $level; return $flags; } sub parse_trace_flag { my ($h, $name) = @_; # 0xddDDDDrL (driver, DBI, reserved, Level) return 0x00000100 if $name eq 'SQL'; return; } sub private_attribute_info { return undef; } } { package # hide from PAUSE DBD::_::dr; # ====== DRIVER ====== @DBD::_::dr::ISA = qw(DBD::_::common); use strict; sub default_user { my ($drh, $user, $pass, $attr) = @_; $user = $ENV{DBI_USER} unless defined $user; $pass = $ENV{DBI_PASS} unless defined $pass; return ($user, $pass); } sub connect { # normally overridden, but a handy default my ($drh, $dsn, $user, $auth) = @_; my ($this) = DBI::_new_dbh($drh, { 'Name' => $dsn, }); # XXX debatable as there's no "server side" here # (and now many uses would trigger warnings on DESTROY) # $this->STORE(Active => 1); # so drivers should set it in their own connect $this; } sub connect_cached { my $drh = shift; my ($dsn, $user, $auth, $attr) = @_; my $cache = $drh->{CachedKids} ||= {}; my $key = do { local $^W; join "!\001", $dsn, $user, $auth, DBI::_concat_hash_sorted($attr, "=\001", ",\001", 0, 0) }; my $dbh = $cache->{$key}; $drh->trace_msg(sprintf(" connect_cached: key '$key', cached dbh $dbh\n", DBI::neat($key), DBI::neat($dbh))) if $DBI::dbi_debug >= 4; my $cb = $attr->{Callbacks}; # take care not to autovivify if ($dbh && $dbh->FETCH('Active') && eval { $dbh->ping }) { # If the caller has provided a callback then call it if ($cb and $cb = $cb->{"connect_cached.reused"}) { local $_ = "connect_cached.reused"; $cb->($dbh, $dsn, $user, $auth, $attr); } return $dbh; } # If the caller has provided a callback then call it if ($cb and $cb = $cb->{"connect_cached.new"}) { local $_ = "connect_cached.new"; $cb->($dbh, $dsn, $user, $auth, $attr); } $dbh = $drh->connect(@_); $cache->{$key} = $dbh; # replace prev entry, even if connect failed return $dbh; } } { package # hide from PAUSE DBD::_::db; # ====== DATABASE ====== @DBD::_::db::ISA = qw(DBD::_::common); use strict; sub clone { my ($old_dbh, $attr) = @_; my $closure = $old_dbh->{dbi_connect_closure} or return; unless ($attr) { # copy attributes visible in the attribute cache keys %$old_dbh; # reset iterator while ( my ($k, $v) = each %$old_dbh ) { # ignore non-code refs, i.e., caches, handles, Err etc next if ref $v && ref $v ne 'CODE'; # HandleError etc $attr->{$k} = $v; } # explicitly set attributes which are unlikely to be in the # attribute cache, i.e., boolean's and some others $attr->{$_} = $old_dbh->FETCH($_) for (qw( AutoCommit ChopBlanks InactiveDestroy LongTruncOk PrintError PrintWarn Profile RaiseError ShowErrorStatement TaintIn TaintOut )); } # use Data::Dumper; warn Dumper([$old_dbh, $attr]); my $new_dbh = &$closure($old_dbh, $attr); unless ($new_dbh) { # need to copy err/errstr from driver back into $old_dbh my $drh = $old_dbh->{Driver}; return $old_dbh->set_err($drh->err, $drh->errstr, $drh->state); } return $new_dbh; } sub quote_identifier { my ($dbh, @id) = @_; my $attr = (@id > 3 && ref($id[-1])) ? pop @id : undef; my $info = $dbh->{dbi_quote_identifier_cache} ||= [ $dbh->get_info(29) || '"', # SQL_IDENTIFIER_QUOTE_CHAR $dbh->get_info(41) || '.', # SQL_CATALOG_NAME_SEPARATOR $dbh->get_info(114) || 1, # SQL_CATALOG_LOCATION ]; my $quote = $info->[0]; foreach (@id) { # quote the elements next unless defined; s/$quote/$quote$quote/g; # escape embedded quotes $_ = qq{$quote$_$quote}; } # strip out catalog if present for special handling my $catalog = (@id >= 3) ? shift @id : undef; # join the dots, ignoring any null/undef elements (ie schema) my $quoted_id = join '.', grep { defined } @id; if ($catalog) { # add catalog correctly $quoted_id = ($info->[2] == 2) # SQL_CL_END ? $quoted_id . $info->[1] . $catalog : $catalog . $info->[1] . $quoted_id; } return $quoted_id; } sub quote { my ($dbh, $str, $data_type) = @_; return "NULL" unless defined $str; unless ($data_type) { $str =~ s/'/''/g; # ISO SQL2 return "'$str'"; } my $dbi_literal_quote_cache = $dbh->{'dbi_literal_quote_cache'} ||= [ {} , {} ]; my ($prefixes, $suffixes) = @$dbi_literal_quote_cache; my $lp = $prefixes->{$data_type}; my $ls = $suffixes->{$data_type}; if ( ! defined $lp || ! defined $ls ) { my $ti = $dbh->type_info($data_type); $lp = $prefixes->{$data_type} = $ti ? $ti->{LITERAL_PREFIX} || "" : "'"; $ls = $suffixes->{$data_type} = $ti ? $ti->{LITERAL_SUFFIX} || "" : "'"; } return $str unless $lp || $ls; # no quoting required # XXX don't know what the standard says about escaping # in the 'general case' (where $lp != "'"). # So we just do this and hope: $str =~ s/$lp/$lp$lp/g if $lp && $lp eq $ls && ($lp eq "'" || $lp eq '"'); return "$lp$str$ls"; } sub rows { -1 } # here so $DBI::rows 'works' after using $dbh sub do { my($dbh, $statement, $attr, @params) = @_; my $sth = $dbh->prepare($statement, $attr) or return undef; $sth->execute(@params) or return undef; my $rows = $sth->rows; ($rows == 0) ? "0E0" : $rows; } sub _do_selectrow { my ($method, $dbh, $stmt, $attr, @bind) = @_; my $sth = ((ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr)) or return; $sth->execute(@bind) or return; my $row = $sth->$method() and $sth->finish; return $row; } sub selectrow_hashref { return _do_selectrow('fetchrow_hashref', @_); } # XXX selectrow_array/ref also have C implementations in Driver.xst sub selectrow_arrayref { return _do_selectrow('fetchrow_arrayref', @_); } sub selectrow_array { my $row = _do_selectrow('fetchrow_arrayref', @_) or return; return $row->[0] unless wantarray; return @$row; } # XXX selectall_arrayref also has C implementation in Driver.xst # which fallsback to this if a slice is given sub selectall_arrayref { my ($dbh, $stmt, $attr, @bind) = @_; my $sth = (ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr) or return; $sth->execute(@bind) || return; my $slice = $attr->{Slice}; # typically undef, else hash or array ref if (!$slice and $slice=$attr->{Columns}) { if (ref $slice eq 'ARRAY') { # map col idx to perl array idx $slice = [ @{$attr->{Columns}} ]; # take a copy for (@$slice) { $_-- } } } my $rows = $sth->fetchall_arrayref($slice, my $MaxRows = $attr->{MaxRows}); $sth->finish if defined $MaxRows; return $rows; } sub selectall_hashref { my ($dbh, $stmt, $key_field, $attr, @bind) = @_; my $sth = (ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr); return unless $sth; $sth->execute(@bind) || return; return $sth->fetchall_hashref($key_field); } sub selectcol_arrayref { my ($dbh, $stmt, $attr, @bind) = @_; my $sth = (ref $stmt) ? $stmt : $dbh->prepare($stmt, $attr); return unless $sth; $sth->execute(@bind) || return; my @columns = ($attr->{Columns}) ? @{$attr->{Columns}} : (1); my @values = (undef) x @columns; my $idx = 0; for (@columns) { $sth->bind_col($_, \$values[$idx++]) || return; } my @col; if (my $max = $attr->{MaxRows}) { push @col, @values while @col<$max && $sth->fetch; } else { push @col, @values while $sth->fetch; } return \@col; } sub prepare_cached { my ($dbh, $statement, $attr, $if_active) = @_; # Needs support at dbh level to clear cache before complaining about # active children. The XS template code does this. Drivers not using # the template must handle clearing the cache themselves. my $cache = $dbh->{CachedKids} ||= {}; my $key = do { local $^W; join "!\001", $statement, DBI::_concat_hash_sorted($attr, "=\001", ",\001", 0, 0) }; my $sth = $cache->{$key}; if ($sth) { return $sth unless $sth->FETCH('Active'); Carp::carp("prepare_cached($statement) statement handle $sth still Active") unless ($if_active ||= 0); $sth->finish if $if_active <= 1; return $sth if $if_active <= 2; } $sth = $dbh->prepare($statement, $attr); $cache->{$key} = $sth if $sth; return $sth; } sub ping { my $dbh = shift; $dbh->_not_impl('ping'); # "0 but true" is a special kind of true 0 that is used here so # applications can check if the ping was a real ping or not ($dbh->FETCH('Active')) ? "0 but true" : 0; } sub begin_work { my $dbh = shift; return $dbh->set_err($DBI::stderr, "Already in a transaction") unless $dbh->FETCH('AutoCommit'); $dbh->STORE('AutoCommit', 0); # will croak if driver doesn't support it $dbh->STORE('BegunWork', 1); # trigger post commit/rollback action return 1; } sub primary_key { my ($dbh, @args) = @_; my $sth = $dbh->primary_key_info(@args) or return; my ($row, @col); push @col, $row->[3] while ($row = $sth->fetch); Carp::croak("primary_key method not called in list context") unless wantarray; # leave us some elbow room return @col; } sub tables { my ($dbh, @args) = @_; my $sth = $dbh->table_info(@args[0,1,2,3,4]) or return; my $tables = $sth->fetchall_arrayref or return; my @tables; if ($dbh->get_info(29)) { # SQL_IDENTIFIER_QUOTE_CHAR @tables = map { $dbh->quote_identifier( @{$_}[0,1,2] ) } @$tables; } else { # temporary old style hack (yeach) @tables = map { my $name = $_->[2]; if ($_->[1]) { my $schema = $_->[1]; # a sad hack (mostly for Informix I recall) my $quote = ($schema eq uc($schema)) ? '' : '"'; $name = "$quote$schema$quote.$name" } $name; } @$tables; } return @tables; } sub type_info { # this should be sufficient for all drivers my ($dbh, $data_type) = @_; my $idx_hash; my $tia = $dbh->{dbi_type_info_row_cache}; if ($tia) { $idx_hash = $dbh->{dbi_type_info_idx_cache}; } else { my $temp = $dbh->type_info_all; return unless $temp && @$temp; # we cache here because type_info_all may be expensive to call # (and we take a copy so the following shift can't corrupt # the data that may be returned by future calls to type_info_all) $tia = $dbh->{dbi_type_info_row_cache} = [ @$temp ]; $idx_hash = $dbh->{dbi_type_info_idx_cache} = shift @$tia; } my $dt_idx = $idx_hash->{DATA_TYPE} || $idx_hash->{data_type}; Carp::croak("type_info_all returned non-standard DATA_TYPE index value ($dt_idx != 1)") if $dt_idx && $dt_idx != 1; # --- simple DATA_TYPE match filter my @ti; my @data_type_list = (ref $data_type) ? @$data_type : ($data_type); foreach $data_type (@data_type_list) { if (defined($data_type) && $data_type != DBI::SQL_ALL_TYPES()) { push @ti, grep { $_->[$dt_idx] == $data_type } @$tia; } else { # SQL_ALL_TYPES push @ti, @$tia; } last if @ti; # found at least one match } # --- format results into list of hash refs my $idx_fields = keys %$idx_hash; my @idx_names = map { uc($_) } keys %$idx_hash; my @idx_values = values %$idx_hash; Carp::croak "type_info_all result has $idx_fields keys but ".(@{$ti[0]})." fields" if @ti && @{$ti[0]} != $idx_fields; my @out = map { my %h; @h{@idx_names} = @{$_}[ @idx_values ]; \%h; } @ti; return $out[0] unless wantarray; return @out; } sub data_sources { my ($dbh, @other) = @_; my $drh = $dbh->{Driver}; # XXX proxy issues? return $drh->data_sources(@other); } } { package # hide from PAUSE DBD::_::st; # ====== STATEMENT ====== @DBD::_::st::ISA = qw(DBD::_::common); use strict; sub bind_param { Carp::croak("Can't bind_param, not implement by driver") } # # ******************************************************** # # BEGIN ARRAY BINDING # # Array binding support for drivers which don't support # array binding, but have sufficient interfaces to fake it. # NOTE: mixing scalars and arrayrefs requires using bind_param_array # for *all* params...unless we modify bind_param for the default # case... # # 2002-Apr-10 D. Arnold sub bind_param_array { my $sth = shift; my ($p_id, $value_array, $attr) = @_; return $sth->set_err($DBI::stderr, "Value for parameter $p_id must be a scalar or an arrayref, not a ".ref($value_array)) if defined $value_array and ref $value_array and ref $value_array ne 'ARRAY'; return $sth->set_err($DBI::stderr, "Can't use named placeholder '$p_id' for non-driver supported bind_param_array") unless DBI::looks_like_number($p_id); # because we rely on execute(@ary) here return $sth->set_err($DBI::stderr, "Placeholder '$p_id' is out of range") if $p_id <= 0; # can't easily/reliably test for too big # get/create arrayref to hold params my $hash_of_arrays = $sth->{ParamArrays} ||= { }; # If the bind has attribs then we rely on the driver conforming to # the DBI spec in that a single bind_param() call with those attribs # makes them 'sticky' and apply to all later execute(@values) calls. # Since we only call bind_param() if we're given attribs then # applications using drivers that don't support bind_param can still # use bind_param_array() so long as they don't pass any attribs. $$hash_of_arrays{$p_id} = $value_array; return $sth->bind_param($p_id, undef, $attr) if $attr; 1; } sub bind_param_inout_array { my $sth = shift; # XXX not supported so we just call bind_param_array instead # and then return an error my ($p_num, $value_array, $attr) = @_; $sth->bind_param_array($p_num, $value_array, $attr); return $sth->set_err($DBI::stderr, "bind_param_inout_array not supported"); } sub bind_columns { my $sth = shift; my $fields = $sth->FETCH('NUM_OF_FIELDS') || 0; if ($fields <= 0 && !$sth->{Active}) { return $sth->set_err($DBI::stderr, "Statement has no result columns to bind" ." (perhaps you need to successfully call execute first)"); } # Backwards compatibility for old-style call with attribute hash # ref as first arg. Skip arg if undef or a hash ref. my $attr; $attr = shift if !defined $_[0] or ref($_[0]) eq 'HASH'; my $idx = 0; $sth->bind_col(++$idx, shift, $attr) or return while (@_ and $idx < $fields); return $sth->set_err($DBI::stderr, "bind_columns called with ".($idx+@_)." values but $fields are needed") if @_ or $idx != $fields; return 1; } sub execute_array { my $sth = shift; my ($attr, @array_of_arrays) = @_; my $NUM_OF_PARAMS = $sth->FETCH('NUM_OF_PARAMS'); # may be undef at this point # get tuple status array or hash attribute my $tuple_sts = $attr->{ArrayTupleStatus}; return $sth->set_err($DBI::stderr, "ArrayTupleStatus attribute must be an arrayref") if $tuple_sts and ref $tuple_sts ne 'ARRAY'; # bind all supplied arrays if (@array_of_arrays) { $sth->{ParamArrays} = { }; # clear out old params return $sth->set_err($DBI::stderr, @array_of_arrays." bind values supplied but $NUM_OF_PARAMS expected") if defined ($NUM_OF_PARAMS) && @array_of_arrays != $NUM_OF_PARAMS; $sth->bind_param_array($_, $array_of_arrays[$_-1]) or return foreach (1..@array_of_arrays); } my $fetch_tuple_sub; if ($fetch_tuple_sub = $attr->{ArrayTupleFetch}) { # fetch on demand return $sth->set_err($DBI::stderr, "Can't use both ArrayTupleFetch and explicit bind values") if @array_of_arrays; # previous bind_param_array calls will simply be ignored if (UNIVERSAL::isa($fetch_tuple_sub,'DBI::st')) { my $fetch_sth = $fetch_tuple_sub; return $sth->set_err($DBI::stderr, "ArrayTupleFetch sth is not Active, need to execute() it first") unless $fetch_sth->{Active}; # check column count match to give more friendly message my $NUM_OF_FIELDS = $fetch_sth->{NUM_OF_FIELDS}; return $sth->set_err($DBI::stderr, "$NUM_OF_FIELDS columns from ArrayTupleFetch sth but $NUM_OF_PARAMS expected") if defined($NUM_OF_FIELDS) && defined($NUM_OF_PARAMS) && $NUM_OF_FIELDS != $NUM_OF_PARAMS; $fetch_tuple_sub = sub { $fetch_sth->fetchrow_arrayref }; } elsif (!UNIVERSAL::isa($fetch_tuple_sub,'CODE')) { return $sth->set_err($DBI::stderr, "ArrayTupleFetch '$fetch_tuple_sub' is not a code ref or statement handle"); } } else { my $NUM_OF_PARAMS_given = keys %{ $sth->{ParamArrays} || {} }; return $sth->set_err($DBI::stderr, "$NUM_OF_PARAMS_given bind values supplied but $NUM_OF_PARAMS expected") if defined($NUM_OF_PARAMS) && $NUM_OF_PARAMS != $NUM_OF_PARAMS_given; # get the length of a bound array my $maxlen; my %hash_of_arrays = %{$sth->{ParamArrays}}; foreach (keys(%hash_of_arrays)) { my $ary = $hash_of_arrays{$_}; next unless ref $ary eq 'ARRAY'; $maxlen = @$ary if !$maxlen || @$ary > $maxlen; } # if there are no arrays then execute scalars once $maxlen = 1 unless defined $maxlen; my @bind_ids = 1..keys(%hash_of_arrays); my $tuple_idx = 0; $fetch_tuple_sub = sub { return if $tuple_idx >= $maxlen; my @tuple = map { my $a = $hash_of_arrays{$_}; ref($a) ? $a->[$tuple_idx] : $a } @bind_ids; ++$tuple_idx; return \@tuple; }; } # pass thru the callers scalar or list context return $sth->execute_for_fetch($fetch_tuple_sub, $tuple_sts); } sub execute_for_fetch { my ($sth, $fetch_tuple_sub, $tuple_status) = @_; # start with empty status array ($tuple_status) ? @$tuple_status = () : $tuple_status = []; my $rc_total = 0; my $err_count; while ( my $tuple = &$fetch_tuple_sub() ) { if ( my $rc = $sth->execute(@$tuple) ) { push @$tuple_status, $rc; $rc_total = ($rc >= 0 && $rc_total >= 0) ? $rc_total + $rc : -1; } else { $err_count++; push @$tuple_status, [ $sth->err, $sth->errstr, $sth->state ]; # XXX drivers implementing execute_for_fetch could opt to "last;" here # if they know the error code means no further executes will work. } } my $tuples = @$tuple_status; return $sth->set_err($DBI::stderr, "executing $tuples generated $err_count errors") if $err_count; $tuples ||= "0E0"; return $tuples unless wantarray; return ($tuples, $rc_total); } sub fetchall_arrayref { # ALSO IN Driver.xst my ($sth, $slice, $max_rows) = @_; # when batch fetching with $max_rows were very likely to try to # fetch the 'next batch' after the previous batch returned # <=$max_rows. So don't treat that as an error. return undef if $max_rows and not $sth->FETCH('Active'); my $mode = ref($slice) || 'ARRAY'; my @rows; my $row; if ($mode eq 'ARRAY') { # we copy the array here because fetch (currently) always # returns the same array ref. XXX if ($slice && @$slice) { $max_rows = -1 unless defined $max_rows; push @rows, [ @{$row}[ @$slice] ] while($max_rows-- and $row = $sth->fetch); } elsif (defined $max_rows) { push @rows, [ @$row ] while($max_rows-- and $row = $sth->fetch); } else { push @rows, [ @$row ] while($row = $sth->fetch); } } elsif ($mode eq 'HASH') { $max_rows = -1 unless defined $max_rows; if (keys %$slice) { my @o_keys = keys %$slice; my @i_keys = map { lc } keys %$slice; # XXX this could be made faster by pre-binding a local hash # using bind_columns and then copying it per row while ($max_rows-- and $row = $sth->fetchrow_hashref('NAME_lc')) { my %hash; @hash{@o_keys} = @{$row}{@i_keys}; push @rows, \%hash; } } else { # XXX assumes new ref each fetchhash push @rows, $row while ($max_rows-- and $row = $sth->fetchrow_hashref()); } } else { Carp::croak("fetchall_arrayref($mode) invalid") } return \@rows; } sub fetchall_hashref { my ($sth, $key_field) = @_; my $hash_key_name = $sth->{FetchHashKeyName} || 'NAME'; my $names_hash = $sth->FETCH("${hash_key_name}_hash"); my @key_fields = (ref $key_field) ? @$key_field : ($key_field); my @key_indexes; my $num_of_fields = $sth->FETCH('NUM_OF_FIELDS'); foreach (@key_fields) { my $index = $names_hash->{$_}; # perl index not column $index = $_ - 1 if !defined $index && DBI::looks_like_number($_) && $_>=1 && $_ <= $num_of_fields; return $sth->set_err($DBI::stderr, "Field '$_' does not exist (not one of @{[keys %$names_hash]})") unless defined $index; push @key_indexes, $index; } my $rows = {}; my $NAME = $sth->FETCH($hash_key_name); my @row = (undef) x $num_of_fields; $sth->bind_columns(\(@row)); while ($sth->fetch) { my $ref = $rows; $ref = $ref->{$row[$_]} ||= {} for @key_indexes; @{$ref}{@$NAME} = @row; } return $rows; } *dump_results = \&DBI::dump_results; sub blob_copy_to_file { # returns length or undef on error my($self, $field, $filename_or_handleref, $blocksize) = @_; my $fh = $filename_or_handleref; my($len, $buf) = (0, ""); $blocksize ||= 512; # not too ambitious local(*FH); unless(ref $fh) { open(FH, ">$fh") || return undef; $fh = \*FH; } while(defined($self->blob_read($field, $len, $blocksize, \$buf))) { print $fh $buf; $len += length $buf; } close(FH); $len; } sub more_results { shift->{syb_more_results}; # handy grandfathering } } unless ($DBI::PurePerl) { # See install_driver { @DBD::_mem::dr::ISA = qw(DBD::_mem::common); } { @DBD::_mem::db::ISA = qw(DBD::_mem::common); } { @DBD::_mem::st::ISA = qw(DBD::_mem::common); } # DBD::_mem::common::DESTROY is implemented in DBI.xs } 1; __END__ =head1 DESCRIPTION The DBI is a database access module for the Perl programming language. It defines a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used. It is important to remember that the DBI is just an interface. The DBI is a layer of "glue" between an application and one or more database I modules. It is the driver modules which do most of the real work. The DBI provides a standard interface and framework for the drivers to operate within. =head2 Architecture of a DBI Application |<- Scope of DBI ->| .-. .--------------. .-------------. .-------. | |---| XYZ Driver |---| XYZ Engine | | Perl | | | `--------------' `-------------' | script| |A| |D| .--------------. .-------------. | using |--|P|--|B|---|Oracle Driver |---|Oracle Engine| | DBI | |I| |I| `--------------' `-------------' | API | | |... |methods| | |... Other drivers `-------' | |... `-' The API, or Application Programming Interface, defines the call interface and variables for Perl scripts to use. The API is implemented by the Perl DBI extension. The DBI "dispatches" the method calls to the appropriate driver for actual execution. The DBI is also responsible for the dynamic loading of drivers, error checking and handling, providing default implementations for methods, and many other non-database specific duties. Each driver contains implementations of the DBI methods using the private interface functions of the corresponding database engine. Only authors of sophisticated/multi-database applications or generic library functions need be concerned with drivers. =head2 Notation and Conventions The following conventions are used in this document: $dbh Database handle object $sth Statement handle object $drh Driver handle object (rarely seen or used in applications) $h Any of the handle types above ($dbh, $sth, or $drh) $rc General Return Code (boolean: true=ok, false=error) $rv General Return Value (typically an integer) @ary List of values returned from the database, typically a row of data $rows Number of rows processed (if available, else -1) $fh A filehandle undef NULL values are represented by undefined values in Perl \%attr Reference to a hash of attribute values passed to methods Note that Perl will automatically destroy database and statement handle objects if all references to them are deleted. =head2 Outline Usage To use DBI, first you need to load the DBI module: use DBI; use strict; (The C isn't required but is strongly recommended.) Then you need to L to your data source and get a I for that connection: $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 }); Since connecting can be expensive, you generally just connect at the start of your program and disconnect at the end. Explicitly defining the required C behaviour is strongly recommended and may become mandatory in a later version. This determines whether changes are automatically committed to the database when executed, or need to be explicitly committed later. The DBI allows an application to "prepare" statements for later execution. A prepared statement is identified by a statement handle held in a Perl variable. We'll call the Perl variable C<$sth> in our examples. The typical method call sequence for a C statement is: prepare, execute, execute, execute. for example: $sth = $dbh->prepare("INSERT INTO table(foo,bar,baz) VALUES (?,?,?)"); while() { chomp; my ($foo,$bar,$baz) = split /,/; $sth->execute( $foo, $bar, $baz ); } The C method can be used for non repeated I-C statement. =head1 THE DBI PACKAGE AND CLASS In this section, we cover the DBI class methods, utility functions, and the dynamic attributes associated with generic DBI handles. =head2 DBI Constants Constants representing the values of the SQL standard types can be imported individually by name, or all together by importing the special C<:sql_types> tag. The names and values of all the defined SQL standard types can be produced like this: foreach (@{ $DBI::EXPORT_TAGS{sql_types} }) { printf "%s=%d\n", $_, &{"DBI::$_"}; } These constants are defined by SQL/CLI, ODBC or both. C is (currently) omitted, because SQL/CLI and ODBC provide conflicting codes. See the L, L, and L methods for possible uses. Note that just because the DBI defines a named constant for a given data type doesn't mean that drivers will support that data type. =head2 DBI Class Methods The following methods are provided by the DBI class: =head3 C ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($dsn) or die "Can't parse DBI DSN '$dsn'"; Breaks apart a DBI Data Source Name (DSN) and returns the individual parts. If $dsn doesn't contain a valid DSN then parse_dsn() returns an empty list. $scheme is the first part of the DSN and is currently always 'dbi'. $driver is the driver name, possibly defaulted to $ENV{DBI_DRIVER}, and may be undefined. $attr_string is the contents of the optional attribute string, which may be undefined. If $attr_string is not empty then $attr_hash is a reference to a hash containing the parsed attribute names and values. $driver_dsn is the last part of the DBI DSN string. For example: ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn("DBI:MyDriver(RaiseError=>1):db=test;port=42"); $scheme = 'dbi'; $driver = 'MyDriver'; $attr_string = 'RaiseError=>1'; $attr_hash = { 'RaiseError' => '1' }; $driver_dsn = 'db=test;port=42'; The parse_dsn() method was added in DBI 1.43. =head3 C $dbh = DBI->connect($data_source, $username, $password) or die $DBI::errstr; $dbh = DBI->connect($data_source, $username, $password, \%attr) or die $DBI::errstr; Establishes a database connection, or session, to the requested C<$data_source>. Returns a database handle object if the connection succeeds. Use C<$dbh-Edisconnect> to terminate the connection. If the connect fails (see below), it returns C and sets both C<$DBI::err> and C<$DBI::errstr>. (It does I explicitly set C<$!>.) You should generally test the return status of C and C if it has failed. Multiple simultaneous connections to multiple databases through multiple drivers can be made via the DBI. Simply make one C call for each database and keep a copy of each returned database handle. The C<$data_source> value must begin with "CIC<:>". The I specifies the driver that will be used to make the connection. (Letter case is significant.) As a convenience, if the C<$data_source> parameter is undefined or empty, the DBI will substitute the value of the environment variable C. If just the I part is empty (i.e., the C<$data_source> prefix is "C"), the environment variable C is used. If neither variable is set, then C dies. Examples of C<$data_source> values are: dbi:DriverName:database_name dbi:DriverName:database_name@hostname:port dbi:DriverName:database=database_name;host=hostname;port=port There is I for the text following the driver name. Each driver is free to use whatever syntax it wants. The only requirement the DBI makes is that all the information is supplied in a single string. You must consult the documentation for the drivers you are using for a description of the syntax they require. It is recommended that drivers support the ODBC style, shown in the last example above. It is also recommended that that they support the three common names 'C', 'C', and 'C' (plus 'C' as an alias for C). This simplifies automatic construction of basic DSNs: C<"dbi:$driver:database=$db;host=$host;port=$port">. Drivers should aim to 'do something reasonable' when given a DSN in this form, but if any part is meaningless for that driver (such as 'port' for Informix) it should generate an error if that part is not empty. If the environment variable C is defined (and the driver in C<$data_source> is not "C") then the connect request will automatically be changed to: $ENV{DBI_AUTOPROXY};dsn=$data_source C is typically set as "C". If $ENV{DBI_AUTOPROXY} doesn't begin with 'C' then "dbi:Proxy:" will be prepended to it first. See the DBD::Proxy documentation for more details. If C<$username> or C<$password> are undefined (rather than just empty), then the DBI will substitute the values of the C and C environment variables, respectively. The DBI will warn if the environment variables are not defined. However, the everyday use of these environment variables is not recommended for security reasons. The mechanism is primarily intended to simplify testing. See below for alternative way to specify the username and password. Cconnect> automatically installs the driver if it has not been installed yet. Driver installation either returns a valid driver handle, or it I with an error message that includes the string "C" and the underlying problem. So Cconnect> will die on a driver installation failure and will only return C on a connect failure, in which case C<$DBI::errstr> will hold the error message. Use C if you need to catch the "C" error. The C<$data_source> argument (with the "C" prefix removed) and the C<$username> and C<$password> arguments are then passed to the driver for processing. The DBI does not define any interpretation for the contents of these fields. The driver is free to interpret the C<$data_source>, C<$username>, and C<$password> fields in any way, and supply whatever defaults are appropriate for the engine being accessed. (Oracle, for example, uses the ORACLE_SID and TWO_TASK environment variables if no C<$data_source> is specified.) The C and C attributes for each connection default to "on". (See L and L for more information.) However, it is strongly recommended that you explicitly define C rather than rely on the default. The C attribute defaults to on if $^W is true, i.e., perl is running with warnings enabled. The C<\%attr> parameter can be used to alter the default settings of C, C, C, and other attributes. For example: $dbh = DBI->connect($data_source, $user, $pass, { PrintError => 0, AutoCommit => 0 }); The username and password can also be specified using the attributes C and C, in which case they take precedence over the C<$username> and C<$password> parameters. You can also define connection attribute values within the C<$data_source> parameter. For example: dbi:DriverName(PrintWarn=>1,PrintError=>0,Taint=>1):... Individual attributes values specified in this way take precedence over any conflicting values specified via the C<\%attr> parameter to C. The C attribute can be used to specify which driver method should be called to establish the connection. The only useful values are 'connect', 'connect_cached', or some specialized case like 'Apache::DBI::connect' (which is automatically the default when running within Apache). Where possible, each session (C<$dbh>) is independent from the transactions in other sessions. This is useful when you need to hold cursors open across transactions--for example, if you use one session for your long lifespan cursors (typically read-only) and another for your short update transactions. For compatibility with old DBI scripts, the driver can be specified by passing its name as the fourth argument to C (instead of C<\%attr>): $dbh = DBI->connect($data_source, $user, $pass, $driver); In this "old-style" form of C, the C<$data_source> should not start with "C". (If it does, the embedded driver_name will be ignored). Also note that in this older form of C, the C<$dbh-E{AutoCommit}> attribute is I, the C<$dbh-E{PrintError}> attribute is off, and the old C environment variable is checked if C is not defined. Beware that this "old-style" C will soon be withdrawn in a future version of DBI. =head3 C $dbh = DBI->connect_cached($data_source, $username, $password) or die $DBI::errstr; $dbh = DBI->connect_cached($data_source, $username, $password, \%attr) or die $DBI::errstr; C is like L, except that the database handle returned is also stored in a hash associated with the given parameters. If another call is made to C with the same parameter values, then the corresponding cached C<$dbh> will be returned if it is still valid. The cached database handle is replaced with a new connection if it has been disconnected or if the C method fails. That the behaviour of this method differs in several respects from the behaviour of persistent connections implemented by Apache::DBI. However, if Apache::DBI is loaded then C will use it. Caching connections can be useful in some applications, but it can also cause problems, such as too many connections, and so should be used with care. In particular, avoid changing the attributes of a database handle created via connect_cached() because it will affect other code that may be using the same handle. Where multiple separate parts of a program are using connect_cached() to connect to the same database with the same (initial) attributes it is a good idea to add a private attribute to the connect_cached() call to effectively limit the scope of the caching. For example: DBI->connect_cached(..., { private_foo_cachekey => "Bar", ... }); Handles returned from that connect_cached() call will only be returned by other connect_cached() call elsewhere in the code if those other calls also pass in the same attribute values, including the private one. (I've used C here as an example, you can use any attribute name with a C prefix.) Taking that one step further, you can limit a particular connect_cached() call to return handles unique to that one place in the code by setting the private attribute to a unique value for that place: DBI->connect_cached(..., { private_foo_cachekey => __FILE__.__LINE__, ... }); By using a private attribute you still get connection caching for the individual calls to connect_cached() but, by making separate database conections for separate parts of the code, the database handles are isolated from any attribute changes made to other handles. The cache can be accessed (and cleared) via the L attribute: my $CachedKids_hashref = $dbh->{Driver}->{CachedKids}; %$CachedKids_hashref = () if $CachedKids_hashref; =head3 C @ary = DBI->available_drivers; @ary = DBI->available_drivers($quiet); Returns a list of all available drivers by searching for C modules through the directories in C<@INC>. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for C<$quiet> will inhibit the warning. =head3 C %drivers = DBI->installed_drivers(); Returns a list of driver name and driver handle pairs for all drivers 'installed' (loaded) into the current process. The driver name does not include the 'DBD::' prefix. To get a list of all drivers available in your perl instalation you can use L. Added in DBI 1.49. =head3 C DBI->installed_versions; @ary = DBI->installed_versions; %hash = DBI->installed_versions; Calls available_drivers() and attempts to load each of them in turn using install_driver(). For each load that succeeds the driver name and version number are added to a hash. When running under L drivers which appear not be pure-perl are ignored. When called in array context the list of successfully loaded drivers is returned (without the 'DBD::' prefix). When called in scalar context a reference to the hash is returned and the hash will also contain other entries for the C version, C name, etc. When called in a void context the installed_versions() method will print out a formatted list of the hash contents, one per line. Due to the potentially high memory cost and unknown risks of loading in an unknown number of drivers that just happen to be installed on the system, this method is not recommended for general use. Use available_drivers() instead. The installed_versions() method is primarily intended as a quick way to see from the command line what's installed. For example: perl -MDBI -e 'DBI->installed_versions' The installed_versions() method was added in DBI 1.38. =head3 C @ary = DBI->data_sources($driver); @ary = DBI->data_sources($driver, \%attr); Returns a list of data sources (databases) available via the named driver. If C<$driver> is empty or C, then the value of the C environment variable is used. The driver will be loaded if it hasn't been already. Note that if the driver loading fails then data_sources() I with an error message that includes the string "C" and the underlying problem. Data sources are returned in a form suitable for passing to the L method (that is, they will include the "C" prefix). Note that many drivers have no way of knowing what data sources might be available for it. These drivers return an empty or incomplete list or may require driver-specific attributes. There is also a data_sources() method defined for database handles. =head3 C DBI->trace($trace_setting) DBI->trace($trace_setting, $trace_filename) DBI->trace($trace_setting, $trace_filehandle) $trace_setting = DBI->trace; The Ctrace> method sets the I trace settings and returns the I trace settings. It can also be used to change where the trace output is sent. There's a similar method, C<$h-Etrace>, which sets the trace settings for the specific handle it's called on. See the L section for full details about the DBI's powerful tracing facilities. =head2 DBI Utility Functions In addition to the DBI methods listed in the previous section, the DBI package also provides several utility functions. These can be imported into your code by listing them in the C statement. For example: use DBI qw(neat data_diff); Alternatively, all these utility functions (except hash) can be imported using the C<:utils> import tag. For example: use DBI qw(:utils); =head3 C $description = data_string_desc($string); Returns an informal description of the string. For example: UTF8 off, ASCII, 42 characters 42 bytes UTF8 off, non-ASCII, 42 characters 42 bytes UTF8 on, non-ASCII, 4 characters 6 bytes UTF8 on but INVALID encoding, non-ASCII, 4 characters 6 bytes UTF8 off, undef The initial C on/off refers to Perl's internal SvUTF8 flag. If $string has the SvUTF8 flag set but the sequence of bytes it contains are not a valid UTF-8 encoding then data_string_desc() will report C. The C vs C portion shows C if I the characters in the string are ASCII (have code points <= 127). The data_string_desc() function was added in DBI 1.46. =head3 C $diff = data_string_diff($a, $b); Returns an informal description of the first character difference between the strings. If both $a and $b contain the same sequence of characters then data_string_diff() returns an empty string. For example: Params a & b Result ------------ ------ 'aaa', 'aaa' '' 'aaa', 'abc' 'Strings differ at index 2: a[2]=a, b[2]=b' 'aaa', undef 'String b is undef, string a has 3 characters' 'aaa', 'aa' 'String b truncated after 2 characters' Unicode characters are reported in C<\x{XXXX}> format. Unicode code points in the range U+0800 to U+08FF are unassigned and most likely to occur due to double-encoding. Characters in this range are reported as C<\x{08XX}='C'> where C is the corresponding latin-1 character. The data_string_diff() function only considers logical I and not the underlying encoding. See L for an alternative. The data_string_diff() function was added in DBI 1.46. =head3 C $diff = data_diff($a, $b); $diff = data_diff($a, $b, $logical); Returns an informal description of the difference between two strings. It calls L and L and returns the combined results as a multi-line string. For example, C will return: a: UTF8 off, ASCII, 3 characters 3 bytes b: UTF8 on, non-ASCII, 3 characters 5 bytes Strings differ at index 2: a[2]=c, b[2]=\x{263A} If $a and $b are identical in both the characters they contain I their physical encoding then data_diff() returns an empty string. If $logical is true then physical encoding differences are ignored (but are still reported if there is a difference in the characters). The data_diff() function was added in DBI 1.46. =head3 C $str = neat($value); $str = neat($value, $maxlen); Return a string containing a neat (and tidy) representation of the supplied value. Strings will be quoted, although internal quotes will I be escaped. Values known to be numeric will be unquoted. Undefined (NULL) values will be shown as C (without quotes). If the string is flagged internally as utf8 then double quotes will be used, otherwise single quotes are used and unprintable characters will be replaced by dot (.). For result strings longer than C<$maxlen> the result string will be truncated to C<$maxlen-4> and "C<...'>" will be appended. If C<$maxlen> is 0 or C, it defaults to C<$DBI::neat_maxlen> which, in turn, defaults to 400. This function is designed to format values for human consumption. It is used internally by the DBI for L output. It should typically I be used for formatting values for database use. (See also L.) =head3 C $str = neat_list(\@listref, $maxlen, $field_sep); Calls C on each element of the list and returns a string containing the results joined with C<$field_sep>. C<$field_sep> defaults to C<", ">. =head3 C @bool = looks_like_number(@array); Returns true for each element that looks like a number. Returns false for each element that does not look like a number. Returns C for each element that is undefined or empty. =head3 C $hash_value = DBI::hash($buffer, $type); Return a 32-bit integer 'hash' value corresponding to the contents of $buffer. The $type parameter selects which kind of hash algorithm should be used. For the technically curious, type 0 (which is the default if $type isn't specified) is based on the Perl 5.1 hash except that the value is forced to be negative (for obscure historical reasons). Type 1 is the better "Fowler / Noll / Vo" (FNV) hash. See L for more information. Both types are implemented in C and are very fast. This function doesn't have much to do with databases, except that it can be handy to store hash values in a database. =head2 DBI Dynamic Attributes Dynamic attributes are always associated with the I (that handle is represented by C<$h> in the descriptions below). Where an attribute is equivalent to a method call, then refer to the method call for all related documentation. Warning: these attributes are provided as a convenience but they do have limitations. Specifically, they have a short lifespan: because they are associated with the last handle used, they should only be used I after calling the method that "sets" them. If in any doubt, use the corresponding method call. =head3 C<$DBI::err> Equivalent to C<$h-Eerr>. =head3 C<$DBI::errstr> Equivalent to C<$h-Eerrstr>. =head3 C<$DBI::state> Equivalent to C<$h-Estate>. =head3 C<$DBI::rows> Equivalent to C<$h-Erows>. Please refer to the documentation for the L method. =head3 C<$DBI::lasth> Returns the DBI object handle used for the most recent DBI method call. If the last DBI method call was a DESTROY then $DBI::lasth will return the handle of the parent of the destroyed handle, if there is one. =head1 METHODS COMMON TO ALL HANDLES The following methods can be used by all types of DBI handles. =head3 C $rv = $h->err; Returns the I database engine error code from the last driver method called. The code is typically an integer but you should not assume that. The DBI resets $h->err to undef before almost all DBI method calls, so the value only has a short lifespan. Also, for most drivers, the statement handles share the same error variable as the parent database handle, so calling a method on one handle may reset the error on the related handles. (Methods which don't reset err before being called include err() and errstr(), obviously, state(), rows(), func(), trace(), trace_msg(), ping(), and the tied hash attribute FETCH() and STORE() methods.) If you need to test for specific error conditions I have your program be portable to different database engines, then you'll need to determine what the corresponding error codes are for all those engines and test for all of them. The DBI uses the value of $DBI::stderr as the C value for internal errors. Drivers should also do likewise. The default value for $DBI::stderr is 2000000000. A driver may return C<0> from err() to indicate a warning condition after a method call. Similarly, a driver may return an empty string to indicate a 'success with information' condition. In both these cases the value is false but not undef. The errstr() and state() methods may be used to retrieve extra information in these cases. See L for more information. =head3 C $str = $h->errstr; Returns the native database engine error message from the last DBI method called. This has the same lifespan issues as the L method described above. The returned string may contain multiple messages separated by newline characters. The errstr() method should not be used to test for errors, use err() for that, because drivers may return 'success with information' or warning messages via errstr() for methods that have not 'failed'. See L for more information. =head3 C $str = $h->state; Returns a state code in the standard SQLSTATE five character format. Note that the specific success code C<00000> is translated to any empty string (false). If the driver does not support SQLSTATE (and most don't), then state() will return C (General Error) for all errors. The driver is free to return any value via C, e.g., warning codes, even if it has not declared an error by returning a true value via the L method described above. The state() method should not be used to test for errors, use err() for that, because drivers may return a 'success with information' or warning state code via state() for methods that have not 'failed'. =head3 C $rv = $h->set_err($err, $errstr); $rv = $h->set_err($err, $errstr, $state); $rv = $h->set_err($err, $errstr, $state, $method); $rv = $h->set_err($err, $errstr, $state, $method, $rv); Set the C, C, and C values for the handle. This method is typically only used by DBI drivers and DBI subclasses. If the L attribute holds a reference to a subroutine it is called first. The subroutine can alter the $err, $errstr, $state, and $method values. See L for full details. If the subroutine returns a true value then the handle C, C, and C values are not altered and set_err() returns an empty list (it normally returns $rv which defaults to undef, see below). Setting C to a I value indicates an error and will trigger the normal DBI error handling mechanisms, such as C and C, if they are enabled, when execution returns from the DBI back to the application. Setting C to C<""> indicates an 'information' state, and setting it to C<"0"> indicates a 'warning' state. Setting C to C also sets C to undef, and C to C<"">, irrespective of the values of the $errstr and $state parameters. The $method parameter provides an alternate method name for the C/C/C error string instead of the fairly unhelpful 'C'. The C method normally returns undef. The $rv parameter provides an alternate return value. Some special rules apply if the C or C values for the handle are I set... If C is true then: "C< [err was %s now %s]>" is appended if $err is true and C is already true and the new err value differs from the original one. Similarly "C< [state was %s now %s]>" is appended if $state is true and C is already true and the new state value differs from the original one. Finally "C<\n>" and the new $errstr are appended if $errstr differs from the existing errstr value. Obviously the C<%s>'s above are replaced by the corresponding values. The handle C value is set to $err if: $err is true; or handle C value is undef; or $err is defined and the length is greater than the handle C length. The effect is that an 'information' state only overrides undef; a 'warning' overrides undef or 'information', and an 'error' state overrides anything. The handle C value is set to $state if $state is true and the handle C value was set (by the rules above). Support for warning and information states was added in DBI 1.41. =head3 C $h->trace($trace_settings); $h->trace($trace_settings, $trace_filename); $trace_settings = $h->trace; The trace() method is used to alter the trace settings for a handle (and any future children of that handle). It can also be used to change where the trace output is sent. There's a similar method, Ctrace>, which sets the global default trace settings. See the L section for full details about the DBI's powerful tracing facilities. =head3 C $h->trace_msg($message_text); $h->trace_msg($message_text, $min_level); Writes C<$message_text> to the trace file if the trace level is greater than or equal to $min_level (which defaults to 1). Can also be called as Ctrace_msg($msg)>. See L for more details. =head3 C $h->func(@func_arguments, $func_name) or die ...; The C method can be used to call private non-standard and non-portable methods implemented by the driver. Note that the function name is given as the I argument. It's also important to note that the func() method does not clear a previous error ($DBI::err etc.) and it does not trigger automatic error detection (RaiseError etc.) so you must check the return status and/or $h->err to detect errors. (This method is not directly related to calling stored procedures. Calling stored procedures is currently not defined by the DBI. Some drivers, such as DBD::Oracle, support it in non-portable ways. See driver documentation for more details.) See also install_method() in L for how you can avoid needing to use func() and gain direct access to driver-private methods. =head3 C $is_implemented = $h->can($method_name); Returns true if $method_name is implemented by the driver or a default method is provided by the DBI. It returns false where a driver hasn't implemented a method and the default method is provided by the DBI is just an empty stub. =head3 C $trace_settings_integer = $h->parse_trace_flags($trace_settings); Parses a string containing trace settings and returns the corresponding integer value used internally by the DBI and drivers. The $trace_settings argument is a string containing a trace level between 0 and 15 and/or trace flag names separated by vertical bar ("C<|>") or comma ("C<,>") characters. For example: C<"SQL|3|foo">. It uses the parse_trace_flag() method, described below, to process the individual trage flag names. The parse_trace_flags() method was added in DBI 1.42. =head3 C $bit_flag = $h->parse_trace_flag($trace_flag_name); Returns the bit flag corresponding to the trace flag name in $trace_flag_name. Drivers are expected to override this method and check if $trace_flag_name is a driver specific trace flags and, if not, then call the DBIs default parse_trace_flag(). The parse_trace_flag() method was added in DBI 1.42. =head3 C $hash_ref = $h->private_attribute_info(); Returns a reference to a hash whose keys are the names of driver-private attributes available for the kind of handle (driver, database, statement) that the method was called on. For example, the return value when called with a DBD::Sybase $dbh could look like this: { syb_dynamic_supported => undef, syb_oc_version => undef, syb_server_version => undef, syb_server_version_string => undef, } and when called with a DBD::Sybase $sth they could look like this: { syb_types => undef, syb_proc_status => undef, syb_result_type => undef, } The values should be undef. Meanings may be assigned to particular values in future. =head3 C $rc = $h1->swap_inner_handle( $h2 ); $rc = $h1->swap_inner_handle( $h2, $allow_reparent ); Brain transplants for handles. You don't need to know about this unless you want to become a handle surgeon. A DBI handle is a reference to a tied hash. A tied hash has an I hash that actually holds the contents. The swap_inner_handle() method swaps the inner hashes between two handles. The $h1 and $h2 handles still point to the same tied hashes, but what those hashes are tied to has been swapped. In effect $h1 I $h2 and vice-versa. This is powerful stuff, expect problems. Use with care. As a small safety measure, the two handles, $h1 and $h2, have to share the same parent unless $allow_reparent is true. The swap_inner_handle() method was added in DBI 1.44. Here's a quick kind of 'diagram' as a worked example to help think about what's happening: Original state: dbh1o -> dbh1i sthAo -> sthAi(dbh1i) dbh2o -> dbh2i swap_inner_handle dbh1o with dbh2o: dbh2o -> dbh1i sthAo -> sthAi(dbh1i) dbh1o -> dbh2i create new sth from dbh1o: dbh2o -> dbh1i sthAo -> sthAi(dbh1i) dbh1o -> dbh2i sthBo -> sthBi(dbh2i) swap_inner_handle sthAo with sthBo: dbh2o -> dbh1i sthBo -> sthAi(dbh1i) dbh1o -> dbh2i sthAo -> sthBi(dbh2i) =head1 ATTRIBUTES COMMON TO ALL HANDLES These attributes are common to all types of DBI handles. Some attributes are inherited by child handles. That is, the value of an inherited attribute in a newly created statement handle is the same as the value in the parent database handle. Changes to attributes in the new statement handle do not affect the parent database handle and changes to the database handle do not affect existing statement handles, only future ones. Attempting to set or get the value of an unknown attribute generates a warning, except for private driver specific attributes (which all have names starting with a lowercase letter). Example: $h->{AttributeName} = ...; # set/write ... = $h->{AttributeName}; # get/read =head3 C (boolean, inherited) The C attribute enables useful warnings for certain bad practices. It is enabled by default and should only be disabled in rare circumstances. Since warnings are generated using the Perl C function, they can be intercepted using the Perl C<$SIG{__WARN__}> hook. The C attribute is not related to the C attribute. =head3 C (boolean, read-only) The C attribute is true if the handle object is "active". This is rarely used in applications. The exact meaning of active is somewhat vague at the moment. For a database handle it typically means that the handle is connected to a database (C<$dbh-Edisconnect> sets C off). For a statement handle it typically means that the handle is a C statements that either cannot be prepared in advance (due to a limitation of the driver) or do not need to be executed repeatedly. It should not be used for C". Drivers using any approach like this should issue a warning if C is true because it is generally unsafe - another process may have modified the table between your insert and the select. For situations where you know it is safe, such as when you have locked the table, you can silence the warning by passing C => 0 in \%attr. B<*> If no insert has been performed yet, or the last insert failed, then the value is implementation defined. Given all the caveats above, it's clear that this method must be used with care. The C method was added in DBI 1.38. =head3 C @row_ary = $dbh->selectrow_array($statement); @row_ary = $dbh->selectrow_array($statement, \%attr); @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values); This utility method combines L, L and L into a single call. If called in a list context, it returns the first row of data from the statement. The C<$statement> parameter can be a previously prepared statement handle, in which case the C is skipped. If any method fails, and L is not set, C will return an empty list. If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that. Also, in a scalar context, an C is returned if there are no more rows or if an error occurred. That C can't be distinguished from an C returned because the first field value was NULL. For these reasons you should exercise some caution if you use C in a scalar context, or just don't do that. =head3 C $ary_ref = $dbh->selectrow_arrayref($statement); $ary_ref = $dbh->selectrow_arrayref($statement, \%attr); $ary_ref = $dbh->selectrow_arrayref($statement, \%attr, @bind_values); This utility method combines L, L and L into a single call. It returns the first row of data from the statement. The C<$statement> parameter can be a previously prepared statement handle, in which case the C is skipped. If any method fails, and L is not set, C will return undef. =head3 C $hash_ref = $dbh->selectrow_hashref($statement); $hash_ref = $dbh->selectrow_hashref($statement, \%attr); $hash_ref = $dbh->selectrow_hashref($statement, \%attr, @bind_values); This utility method combines L, L and L into a single call. It returns the first row of data from the statement. The C<$statement> parameter can be a previously prepared statement handle, in which case the C is skipped. If any method fails, and L is not set, C will return undef. =head3 C $ary_ref = $dbh->selectall_arrayref($statement); $ary_ref = $dbh->selectall_arrayref($statement, \%attr); $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values); This utility method combines L, L and L into a single call. It returns a reference to an array containing a reference to an array (or hash, see below) for each row of data fetched. The C<$statement> parameter can be a previously prepared statement handle, in which case the C is skipped. This is recommended if the statement is going to be executed many times. If L is not set and any method except C fails then C will return C; if C fails then it will return with whatever data has been fetched thus far. You should check C<$sth-Eerr> afterwards (or use the C attribute) to discover if the data is complete or was truncated due to an error. The L method called by C supports a $max_rows parameter. You can specify a value for $max_rows by including a 'C' attribute in \%attr. In which case finish() is called for you after fetchall_arrayref() returns. The L method called by C also supports a $slice parameter. You can specify a value for $slice by including a 'C' or 'C' attribute in \%attr. The only difference between the two is that if C is not defined and C is an array ref, then the array is assumed to contain column index values (which count from 1), rather than perl array index values. In which case the array is copied and each value decremented before passing to C. You may often want to fetch an array of rows where each row is stored as a hash. That can be done simple using: my $emps = $dbh->selectall_arrayref( "SELECT ename FROM emp ORDER BY ename", { Slice => {} } ); foreach my $emp ( @$emps ) { print "Employee: $emp->{ename}\n"; } Or, to fetch into an array instead of an array ref: @result = @{ $dbh->selectall_arrayref($sql, { Slice => {} }) }; See L method for more details. =head3 C $hash_ref = $dbh->selectall_hashref($statement, $key_field); $hash_ref = $dbh->selectall_hashref($statement, $key_field, \%attr); $hash_ref = $dbh->selectall_hashref($statement, $key_field, \%attr, @bind_values); This utility method combines L, L and L into a single call. It returns a reference to a hash containing one entry, at most, for each row, as returned by fetchall_hashref(). The C<$statement> parameter can be a previously prepared statement handle, in which case the C is skipped. This is recommended if the statement is going to be executed many times. The C<$key_field> parameter defines which column, or columns, are used as keys in the returned hash. It can either be the name of a single field, or a reference to an array containing multiple field names. Using multiple names yields a tree of nested hashes. If a row has the same key as an earlier row then it replaces the earlier row. If any method except C fails, and L is not set, C will return C. If C fails and L is not set, then it will return with whatever data it has fetched thus far. $DBI::err should be checked to catch that. See fetchall_hashref() for more details. =head3 C $ary_ref = $dbh->selectcol_arrayref($statement); $ary_ref = $dbh->selectcol_arrayref($statement, \%attr); $ary_ref = $dbh->selectcol_arrayref($statement, \%attr, @bind_values); This utility method combines L, L, and fetching one column from all the rows, into a single call. It returns a reference to an array containing the values of the first column from each row. The C<$statement> parameter can be a previously prepared statement handle, in which case the C is skipped. This is recommended if the statement is going to be executed many times. If any method except C fails, and L is not set, C will return C. If C fails and L is not set, then it will return with whatever data it has fetched thus far. $DBI::err should be checked to catch that. The C method defaults to pushing a single column value (the first) from each row into the result array. However, it can also push another column, or even multiple columns per row, into the result array. This behaviour can be specified via a 'C' attribute which must be a ref to an array containing the column number or numbers to use. For example: # get array of id and name pairs: my $ary_ref = $dbh->selectcol_arrayref("select id, name from table", { Columns=>[1,2] }); my %hash = @$ary_ref; # build hash from key-value pairs so $hash{$id} => name You can specify a maximum number of rows to fetch by including a 'C' attribute in \%attr. =head3 C $sth = $dbh->prepare($statement) or die $dbh->errstr; $sth = $dbh->prepare($statement, \%attr) or die $dbh->errstr; Prepares a statement for later execution by the database engine and returns a reference to a statement handle object. The returned statement handle can be used to get attributes of the statement and invoke the L method. See L. Drivers for engines without the concept of preparing a statement will typically just store the statement in the returned handle and process it when C<$sth-Eexecute> is called. Such drivers are unlikely to give much useful information about the statement, such as C<$sth-E{NUM_OF_FIELDS}>, until after C<$sth-Eexecute> has been called. Portable applications should take this into account. In general, DBI drivers do not parse the contents of the statement (other than simply counting any L). The statement is passed directly to the database engine, sometimes known as pass-thru mode. This has advantages and disadvantages. On the plus side, you can access all the functionality of the engine being used. On the downside, you're limited if you're using a simple engine, and you need to take extra care if writing applications intended to be portable between engines. Portable applications should not assume that a new statement can be prepared and/or executed while still fetching results from a previous statement. Some command-line SQL tools use statement terminators, like a semicolon, to indicate the end of a statement. Such terminators should not normally be used with the DBI. =head3 C $sth = $dbh->prepare_cached($statement) $sth = $dbh->prepare_cached($statement, \%attr) $sth = $dbh->prepare_cached($statement, \%attr, $if_active) Like L except that the statement handle returned will be stored in a hash associated with the C<$dbh>. If another call is made to C with the same C<$statement> and C<%attr> parameter values, then the corresponding cached C<$sth> will be returned without contacting the database server. The C<$if_active> parameter lets you adjust the behaviour if an already cached statement handle is still Active. There are several alternatives: =over 4 =item B<0>: A warning will be generated, and finish() will be called on the statement handle before it is returned. This is the default behaviour if $if_active is not passed. =item B<1>: finish() will be called on the statement handle, but the warning is suppressed. =item B<2>: Disables any checking. =item B<3>: The existing active statement handle will be removed from the cache and a new statement handle prepared and cached in its place. This is the safest option because it doesn't affect the state of the old handle, it just removes it from the cache. [Added in DBI 1.40] =back Here are some examples of C: sub insert_hash { my ($table, $field_values) = @_; # sort to keep field order, and thus sql, stable for prepare_cached my @fields = sort keys %$field_values; my @values = @{$field_values}{@fields}; my $sql = sprintf "insert into %s (%s) values (%s)", $table, join(",", @fields), join(",", ("?")x@fields); my $sth = $dbh->prepare_cached($sql); return $sth->execute(@values); } sub search_hash { my ($table, $field_values) = @_; # sort to keep field order, and thus sql, stable for prepare_cached my @fields = sort keys %$field_values; my @values = @{$field_values}{@fields}; my $qualifier = ""; $qualifier = "where ".join(" and ", map { "$_=?" } @fields) if @fields; $sth = $dbh->prepare_cached("SELECT * FROM $table $qualifier"); return $dbh->selectall_arrayref($sth, {}, @values); } I This caching can be useful in some applications, but it can also cause problems and should be used with care. Here is a contrived case where caching would cause a significant problem: my $sth = $dbh->prepare_cached('SELECT * FROM foo WHERE bar=?'); $sth->execute(...); while (my $data = $sth->fetchrow_hashref) { # later, in some other code called within the loop... my $sth2 = $dbh->prepare_cached('SELECT * FROM foo WHERE bar=?'); $sth2->execute(...); while (my $data2 = $sth2->fetchrow_arrayref) { do_stuff(...); } } In this example, since both handles are preparing the exact same statement, C<$sth2> will not be its own statement handle, but a duplicate of C<$sth> returned from the cache. The results will certainly not be what you expect. Typically the the inner fetch loop will work normally, fetching all the records and terminating when there are no more, but now $sth is the same as $sth2 the outer fetch loop will also terminate. You'll know if you run into this problem because prepare_cached() will generate a warning by default (when $if_active is false). The cache used by prepare_cached() is keyed by both the statement and any attributes so you can also avoid this issue by doing something like: $sth = $dbh->prepare_cached("...", { dbi_dummy => __FILE__.__LINE__ }); which will ensure that prepare_cached only returns statements cached by that line of code in that source file. If you'd like the cache to managed intelligently, you can tie the hashref returned by C to an appropriate caching module, such as L: my $cache; tie %$cache, 'Tie::Cache::LRU', 500; $dbh->{CachedKids} = $cache; =head3 C $rc = $dbh->commit or die $dbh->errstr; Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off. If C is on, then calling C will issue a "commit ineffective with AutoCommit" warning. See also L in the L section below. =head3 C $rc = $dbh->rollback or die $dbh->errstr; Rollback (undo) the most recent series of uncommitted database changes if the database supports transactions and AutoCommit is off. If C is on, then calling C will issue a "rollback ineffective with AutoCommit" warning. See also L in the L section below. =head3 C $rc = $dbh->begin_work or die $dbh->errstr; Enable transactions (by turning C off) until the next call to C or C. After the next C or C, C will automatically be turned on again. If C is already off when C is called then it does nothing except return an error. If the driver does not support transactions then when C attempts to set C off the driver will trigger a fatal error. See also L in the L section below. =head3 C $rc = $dbh->disconnect or warn $dbh->errstr; Disconnects the database from the database handle. C is typically only used before exiting the program. The handle is of little use after disconnecting. The transaction behaviour of the C method is, sadly, undefined. Some database systems (such as Oracle and Ingres) will automatically commit any outstanding changes, but others (such as Informix) will rollback any outstanding changes. Applications not using C should explicitly call C or C before calling C. The database is automatically disconnected by the C method if still connected when there are no longer any references to the handle. The C method for each driver should implicitly call C to undo any uncommitted changes. This is vital behaviour to ensure that incomplete transactions don't get committed simply because Perl calls C on every object before exiting. Also, do not rely on the order of object destruction during "global destruction", as it is undefined. Generally, if you want your changes to be commited or rolled back when you disconnect, then you should explicitly call L or L before disconnecting. If you disconnect from a database while you still have active statement handles (e.g., SELECT statement handles that may have more data to fetch), you will get a warning. The warning may indicate that a fetch loop terminated early, perhaps due to an uncaught error. To avoid the warning call the C method on the active handles. =head3 C $rc = $dbh->ping; Attempts to determine, in a reasonably efficient way, if the database server is still running and the connection to it is still working. Individual drivers should implement this function in the most suitable manner for their database engine. The current I implementation always returns true without actually doing anything. Actually, it returns "C<0 but true>" which is true but zero. That way you can tell if the return value is genuine or just the default. Drivers should override this method with one that does the right thing for their type of database. Few applications would have direct use for this method. See the specialized Apache::DBI module for one example usage. =head3 C $value = $dbh->get_info( $info_type ); Returns information about the implementation, i.e. driver and data source capabilities, restrictions etc. It returns C for unknown or unimplemented information types. For example: $database_version = $dbh->get_info( 18 ); # SQL_DBMS_VER $max_select_tables = $dbh->get_info( 106 ); # SQL_MAXIMUM_TABLES_IN_SELECT See L for more detailed information about the information types and their meanings and possible return values. The DBI::Const::GetInfoType module exports a %GetInfoType hash that can be used to map info type names to numbers. For example: $database_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ); The names are a merging of the ANSI and ODBC standards (which differ in some cases). See L for more details. Because some DBI methods make use of get_info(), drivers are strongly encouraged to support I the following very minimal set of information types to ensure the DBI itself works properly: Type Name Example A Example B ---- -------------------------- ------------ ---------------- 17 SQL_DBMS_NAME 'ACCESS' 'Oracle' 18 SQL_DBMS_VER '03.50.0000' '08.01.0721 ...' 29 SQL_IDENTIFIER_QUOTE_CHAR '`' '"' 41 SQL_CATALOG_NAME_SEPARATOR '.' '@' 114 SQL_CATALOG_LOCATION 1 2 =head3 C $sth = $dbh->table_info( $catalog, $schema, $table, $type ); $sth = $dbh->table_info( $catalog, $schema, $table, $type, \%attr ); # then $sth->fetchall_arrayref or $sth->fetchall_hashref etc Returns an active statement handle that can be used to fetch information about tables and views that exist in the database. The arguments $catalog, $schema and $table may accept search patterns according to the database/driver, for example: $table = '%FOO%'; Remember that the underscore character ('C<_>') is a search pattern that means match any character, so 'FOO_%' is the same as 'FOO%' and 'FOO_BAR%' will match names like 'FOO1BAR'. The value of $type is a comma-separated list of one or more types of tables to be returned in the result set. Each value may optionally be quoted, e.g.: $type = "TABLE"; $type = "'TABLE','VIEW'"; In addition the following special cases may also be supported by some drivers: =over 4 =item * If the value of $catalog is '%' and $schema and $table name are empty strings, the result set contains a list of catalog names. For example: $sth = $dbh->table_info('%', '', ''); =item * If the value of $schema is '%' and $catalog and $table are empty strings, the result set contains a list of schema names. =item * If the value of $type is '%' and $catalog, $schema, and $table are all empty strings, the result set contains a list of table types. =back If your driver doesn't support one or more of the selection filter parameters then you may get back more than you asked for and can do the filtering yourself. This method can be expensive, and can return a large amount of data. (For example, small Oracle installation returns over 2000 rows.) So it's a good idea to use the filters to limit the data as much as possible. The statement handle returned has at least the following fields in the order show below. Other fields, after these, may also be present. B: Table catalog identifier. This field is NULL (C) if not applicable to the data source, which is usually the case. This field is empty if not applicable to the table. B: The name of the schema containing the TABLE_NAME value. This field is NULL (C) if not applicable to data source, and empty if not applicable to the table. B: Name of the table (or view, synonym, etc). B: One of the following: "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" or a type identifier that is specific to the data source. B: A description of the table. May be NULL (C). Note that C might not return records for all tables. Applications can use any valid table regardless of whether it's returned by C. See also L, L and L. =head3 C $sth = $dbh->column_info( $catalog, $schema, $table, $column ); # then $sth->fetchall_arrayref or $sth->fetchall_hashref etc Returns an active statement handle that can be used to fetch information about columns in specified tables. The arguments $schema, $table and $column may accept search patterns according to the database/driver, for example: $table = '%FOO%'; Note: The support for the selection criteria is driver specific. If the driver doesn't support one or more of them then you may get back more than you asked for and can do the filtering yourself. If the arguments don't match any tables then you'll still get a statement handle, it'll just return no rows. The statement handle returned has at least the following fields in the order shown below. Other fields, after these, may also be present. B: The catalog identifier. This field is NULL (C) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. B: The schema identifier. This field is NULL (C) if not applicable to the data source, and empty if not applicable to the table. B: The table identifier. Note: A driver may provide column metadata not only for base tables, but also for derived objects like SYNONYMS etc. B: The column identifier. B: The concise data type code. B: A data source dependent data type name. B: The column size. This is the maximum length in characters for character data types, the number of digits or bits for numeric data types or the length in the representation of temporal types. See the relevant specifications for detailed information. B: The length in bytes of transferred data. B: The total number of significant digits to the right of the decimal point. B: The radix for numeric precision. The value is 10 or 2 for numeric data types and NULL (C) if not applicable. B: Indicates if a column can accept NULLs. The following values are defined: SQL_NO_NULLS 0 SQL_NULLABLE 1 SQL_NULLABLE_UNKNOWN 2 B: A description of the column. B: The default value of the column. B: The SQL data type. B: The subtype code for datetime and interval data types. B: The maximum length in bytes of a character or binary data type column. B: The column sequence number (starting with 1). B: Indicates if the column can accept NULLs. Possible values are: 'NO', 'YES' and ''. SQL/CLI defines the following additional columns: CHAR_SET_CAT CHAR_SET_SCHEM CHAR_SET_NAME COLLATION_CAT COLLATION_SCHEM COLLATION_NAME UDT_CAT UDT_SCHEM UDT_NAME DOMAIN_CAT DOMAIN_SCHEM DOMAIN_NAME SCOPE_CAT SCOPE_SCHEM SCOPE_NAME MAX_CARDINALITY DTD_IDENTIFIER IS_SELF_REF Drivers capable of supplying any of those values should do so in the corresponding column and supply undef values for the others. Drivers wishing to provide extra database/driver specific information should do so in extra columns beyond all those listed above, and use lowercase field names with the driver-specific prefix (i.e., 'ora_...'). Applications accessing such fields should do so by name and not by column number. The result set is ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION. Note: There is some overlap with statement attributes (in perl) and SQLDescribeCol (in ODBC). However, SQLColumns provides more metadata. See also L and L. =head3 C $sth = $dbh->primary_key_info( $catalog, $schema, $table ); # then $sth->fetchall_arrayref or $sth->fetchall_hashref etc Returns an active statement handle that can be used to fetch information about columns that make up the primary key for a table. The arguments don't accept search patterns (unlike table_info()). The statement handle will return one row per column, ordered by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, and KEY_SEQ. If there is no primary key then the statement handle will fetch no rows. Note: The support for the selection criteria, such as $catalog, is driver specific. If the driver doesn't support catalogs and/or schemas, it may ignore these criteria. The statement handle returned has at least the following fields in the order shown below. Other fields, after these, may also be present. B: The catalog identifier. This field is NULL (C) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. B: The schema identifier. This field is NULL (C) if not applicable to the data source, and empty if not applicable to the table. B: The table identifier. B: The column identifier. B: The column sequence number (starting with 1). Note: This field is named B in SQL/CLI. B: The primary key constraint identifier. This field is NULL (C) if not applicable to the data source. See also L and L. =head3 C @key_column_names = $dbh->primary_key( $catalog, $schema, $table ); Simple interface to the primary_key_info() method. Returns a list of the column names that comprise the primary key of the specified table. The list is in primary key column sequence order. If there is no primary key then an empty list is returned. =head3 C $sth = $dbh->foreign_key_info( $pk_catalog, $pk_schema, $pk_table , $fk_catalog, $fk_schema, $fk_table ); $sth = $dbh->foreign_key_info( $pk_catalog, $pk_schema, $pk_table , $fk_catalog, $fk_schema, $fk_table , \%attr ); # then $sth->fetchall_arrayref or $sth->fetchall_hashref etc Returns an active statement handle that can be used to fetch information about foreign keys in and/or referencing the specified table(s). The arguments don't accept search patterns (unlike table_info()). C<$pk_catalog>, C<$pk_schema>, C<$pk_table> identify the primary (unique) key table (B). C<$fk_catalog>, C<$fk_schema>, C<$fk_table> identify the foreign key table (B). If both B and B are given, the function returns the foreign key, if any, in table B that refers to the primary (unique) key of table B. (Note: In SQL/CLI, the result is implementation-defined.) If only B is given, then the result set contains the primary key of that table and all foreign keys that refer to it. If only B is given, then the result set contains all foreign keys in that table and the primary keys to which they refer. (Note: In SQL/CLI, the result includes unique keys too.) For example: $sth = $dbh->foreign_key_info( undef, $user, 'master'); $sth = $dbh->foreign_key_info( undef, undef, undef , undef, $user, 'detail'); $sth = $dbh->foreign_key_info( undef, $user, 'master', undef, $user, 'detail'); # then $sth->fetchall_arrayref or $sth->fetchall_hashref etc Note: The support for the selection criteria, such as C<$catalog>, is driver specific. If the driver doesn't support catalogs and/or schemas, it may ignore these criteria. The statement handle returned has the following fields in the order shown below. Because ODBC never includes unique keys, they define different columns in the result set than SQL/CLI. SQL/CLI column names are shown in parentheses. B: The primary (unique) key table catalog identifier. This field is NULL (C) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. B: The primary (unique) key table schema identifier. This field is NULL (C) if not applicable to the data source, and empty if not applicable to the table. B: The primary (unique) key table identifier. B: The primary (unique) key column identifier. B: The foreign key table catalog identifier. This field is NULL (C) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. B: The foreign key table schema identifier. This field is NULL (C) if not applicable to the data source, and empty if not applicable to the table. B: The foreign key table identifier. B: The foreign key column identifier. B: The column sequence number (starting with 1). B: The referential action for the UPDATE rule. The following codes are defined: CASCADE 0 RESTRICT 1 SET NULL 2 NO ACTION 3 SET DEFAULT 4 B: The referential action for the DELETE rule. The codes are the same as for UPDATE_RULE. B: The foreign key name. B: The primary (unique) key name. B: The deferrability of the foreign key constraint. The following codes are defined: INITIALLY DEFERRED 5 INITIALLY IMMEDIATE 6 NOT DEFERRABLE 7 B< ( UNIQUE_OR_PRIMARY )>: This column is necessary if a driver includes all candidate (i.e. primary and alternate) keys in the result set (as specified by SQL/CLI). The value of this column is UNIQUE if the foreign key references an alternate key and PRIMARY if the foreign key references a primary key, or it may be undefined if the driver doesn't have access to the information. See also L and L. =head3 C B This method is experimental and may change. $sth = $dbh->statistics_info( $catalog, $schema, $table, $unique_only, $quick ); # then $sth->fetchall_arrayref or $sth->fetchall_hashref etc Returns an active statement handle that can be used to fetch statistical information about a table and its indexes. The arguments don't accept search patterns (unlike L). If the boolean argument $unique_only is true, only UNIQUE indexes will be returned in the result set, otherwise all indexes will be returned. If the boolean argument $quick is set, the actual statistical information columns (CARDINALITY and PAGES) will only be returned if they are readily available from the server, and might not be current. Some databases may return stale statistics or no statistics at all with this flag set. The statement handle will return at most one row per column name per index, plus at most one row for the entire table itself, ordered by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, and ORDINAL_POSITION. Note: The support for the selection criteria, such as $catalog, is driver specific. If the driver doesn't support catalogs and/or schemas, it may ignore these criteria. The statement handle returned has at least the following fields in the order shown below. Other fields, after these, may also be present. B: The catalog identifier. This field is NULL (C) if not applicable to the data source, which is often the case. This field is empty if not applicable to the table. B: The schema identifier. This field is NULL (C) if not applicable to the data source, and empty if not applicable to the table. B: The table identifier. B: Unique index indicator. Returns 0 for unique indexes, 1 for non-unique indexes B: Index qualifier identifier. The identifier that is used to qualify the index name when doing a C; NULL (C) is returned if an index qualifier is not supported by the data source. If a non-NULL (defined) value is returned in this column, it must be used to qualify the index name on a C statement; otherwise, the TABLE_SCHEM should be used to qualify the index name. B: The index identifier. B: The type of information being returned. Can be any of the following values: 'table', 'btree', 'clustered', 'content', 'hashed', or 'other'. In the case that this field is 'table', all fields other than TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TYPE, CARDINALITY, and PAGES will be NULL (C). B: Column sequence number (starting with 1). B: The column identifier. B: Column sort sequence. C for Ascending, C for Descending, or NULL (C) if not supported for this index. B: Cardinality of the table or index. For indexes, this is the number of unique values in the index. For tables, this is the number of rows in the table. If not supported, the value will be NULL (C). B: Number of storage pages used by this table or index. If not supported, the value will be NULL (C). B: The index filter condition as a string. If the index is not a filtered index, or it cannot be determined whether the index is a filtered index, this value is NULL (C). If the index is a filtered index, but the filter condition cannot be determined, this value is the empty string C<''>. Otherwise it will be the literal filter condition as a string, such as C. See also L and L. =head3 C @names = $dbh->tables( $catalog, $schema, $table, $type ); @names = $dbh->tables; # deprecated Simple interface to table_info(). Returns a list of matching table names, possibly including a catalog/schema prefix. See L for a description of the parameters. If C<$dbh-Eget_info(29)> returns true (29 is SQL_IDENTIFIER_QUOTE_CHAR) then the table names are constructed and quoted by L to ensure they are usable even if they contain whitespace or reserved words etc. This means that the table names returned will include quote characters. =head3 C $type_info_all = $dbh->type_info_all; Returns a reference to an array which holds information about each data type variant supported by the database and driver. The array and its contents should be treated as read-only. The first item is a reference to an 'index' hash of CE C pairs. The items following that are references to arrays, one per supported data type variant. The leading index hash defines the names and order of the fields within the arrays that follow it. For example: $type_info_all = [ { TYPE_NAME => 0, DATA_TYPE => 1, COLUMN_SIZE => 2, # was PRECISION originally LITERAL_PREFIX => 3, LITERAL_SUFFIX => 4, CREATE_PARAMS => 5, NULLABLE => 6, CASE_SENSITIVE => 7, SEARCHABLE => 8, UNSIGNED_ATTRIBUTE=> 9, FIXED_PREC_SCALE => 10, # was MONEY originally AUTO_UNIQUE_VALUE => 11, # was AUTO_INCREMENT originally LOCAL_TYPE_NAME => 12, MINIMUM_SCALE => 13, MAXIMUM_SCALE => 14, SQL_DATA_TYPE => 15, SQL_DATETIME_SUB => 16, NUM_PREC_RADIX => 17, INTERVAL_PRECISION=> 18, }, [ 'VARCHAR', SQL_VARCHAR, undef, "'","'", undef,0, 1,1,0,0,0,undef,1,255, undef ], [ 'INTEGER', SQL_INTEGER, undef, "", "", undef,0, 0,1,0,0,0,undef,0, 0, 10 ], ]; More than one row may have the same value in the C field if there are different ways to spell the type name and/or there are variants of the type with different attributes (e.g., with and without C set, with and without C, etc). The rows are ordered by C first and then by how closely each type maps to the corresponding ODBC SQL data type, closest first. The meaning of the fields is described in the documentation for the L method. An 'index' hash is provided so you don't need to rely on index values defined above. However, using DBD::ODBC with some old ODBC drivers may return older names, shown as comments in the example above. Another issue with the index hash is that the lettercase of the keys is not defined. It is usually uppercase, as show here, but drivers may return names with any lettercase. Drivers are also free to return extra driver-specific columns of information - though it's recommended that they start at column index 50 to leave room for expansion of the DBI/ODBC specification. The type_info_all() method is not normally used directly. The L method provides a more usable and useful interface to the data. =head3 C @type_info = $dbh->type_info($data_type); Returns a list of hash references holding information about one or more variants of $data_type. The list is ordered by C first and then by how closely each type maps to the corresponding ODBC SQL data type, closest first. If called in a scalar context then only the first (best) element is returned. If $data_type is undefined or C, then the list will contain hashes for all data type variants supported by the database and driver. If $data_type is an array reference then C returns the information for the I type in the array that has any matches. The keys of the hash follow the same letter case conventions as the rest of the DBI (see L). The following uppercase items should always exist, though may be undef: =over 4 =item TYPE_NAME (string) Data type name for use in CREATE TABLE statements etc. =item DATA_TYPE (integer) SQL data type number. =item COLUMN_SIZE (integer) For numeric types, this is either the total number of digits (if the NUM_PREC_RADIX value is 10) or the total number of bits allowed in the column (if NUM_PREC_RADIX is 2). For string types, this is the maximum size of the string in characters. For date and interval types, this is the maximum number of characters needed to display the value. =item LITERAL_PREFIX (string) Characters used to prefix a literal. A typical prefix is "C<'>" for characters, or possibly "C<0x>" for binary values passed as hexadecimal. NULL (C) is returned for data types for which this is not applicable. =item LITERAL_SUFFIX (string) Characters used to suffix a literal. Typically "C<'>" for characters. NULL (C) is returned for data types where this is not applicable. =item CREATE_PARAMS (string) Parameter names for data type definition. For example, C for a C would be "C" if the DECIMAL type should be declared as CIC<)> where I and I are integer values. For a C it would be "C". NULL (C) is returned for data types for which this is not applicable. =item NULLABLE (integer) Indicates whether the data type accepts a NULL value: C<0> or an empty string = no, C<1> = yes, C<2> = unknown. =item CASE_SENSITIVE (boolean) Indicates whether the data type is case sensitive in collations and comparisons. =item SEARCHABLE (integer) Indicates how the data type can be used in a WHERE clause, as follows: 0 - Cannot be used in a WHERE clause 1 - Only with a LIKE predicate 2 - All comparison operators except LIKE 3 - Can be used in a WHERE clause with any comparison operator =item UNSIGNED_ATTRIBUTE (boolean) Indicates whether the data type is unsigned. NULL (C) is returned for data types for which this is not applicable. =item FIXED_PREC_SCALE (boolean) Indicates whether the data type always has the same precision and scale (such as a money type). NULL (C) is returned for data types for which this is not applicable. =item AUTO_UNIQUE_VALUE (boolean) Indicates whether a column of this data type is automatically set to a unique value whenever a new row is inserted. NULL (C) is returned for data types for which this is not applicable. =item LOCAL_TYPE_NAME (string) Localized version of the C for use in dialog with users. NULL (C) is returned if a localized name is not available (in which case C should be used). =item MINIMUM_SCALE (integer) The minimum scale of the data type. If a data type has a fixed scale, then C holds the same value. NULL (C) is returned for data types for which this is not applicable. =item MAXIMUM_SCALE (integer) The maximum scale of the data type. If a data type has a fixed scale, then C holds the same value. NULL (C) is returned for data types for which this is not applicable. =item SQL_DATA_TYPE (integer) This column is the same as the C column, except for interval and datetime data types. For interval and datetime data types, the C field will return C or C, and the C field below will return the subcode for the specific interval or datetime data type. If this field is NULL, then the driver does not support or report on interval or datetime subtypes. =item SQL_DATETIME_SUB (integer) For interval or datetime data types, where the C field above is C or C, this field will hold the I for the specific interval or datetime data type. Otherwise it will be NULL (C). Although not mentioned explicitly in the standards, it seems there is a simple relationship between these values: DATA_TYPE == (10 * SQL_DATA_TYPE) + SQL_DATETIME_SUB =item NUM_PREC_RADIX (integer) The radix value of the data type. For approximate numeric types, C contains the value 2 and C holds the number of bits. For exact numeric types, C contains the value 10 and C holds the number of decimal digits. NULL (C) is returned either for data types for which this is not applicable or if the driver cannot report this information. =item INTERVAL_PRECISION (integer) The interval leading precision for interval types. NULL is returned either for data types for which this is not applicable or if the driver cannot report this information. =back For example, to find the type name for the fields in a select statement you can do: @names = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} } Since DBI and ODBC drivers vary in how they map their types into the ISO standard types you may need to search for more than one type. Here's an example looking for a usable type to store a date: $my_date_type = $dbh->type_info( [ SQL_DATE, SQL_TIMESTAMP ] ); Similarly, to more reliably find a type to store small integers, you could use a list starting with C, C, C, etc. See also L. =head3 C $sql = $dbh->quote($value); $sql = $dbh->quote($value, $data_type); Quote a string literal for use as a literal value in an SQL statement, by escaping any special characters (such as quotation marks) contained within the string and adding the required type of outer quotation marks. $sql = sprintf "SELECT foo FROM bar WHERE baz = %s", $dbh->quote("Don't"); For most database types, at least those that conform to SQL standards, quote would return C<'Don''t'> (including the outer quotation marks). For others it may return something like C<'Don\'t'> An undefined C<$value> value will be returned as the string C (without single quotation marks) to match how NULLs are represented in SQL. If C<$data_type> is supplied, it is used to try to determine the required quoting behaviour by using the information returned by L. As a special case, the standard numeric types are optimized to return C<$value> without calling C. Quote will probably I be able to deal with all possible input (such as binary data or data containing newlines), and is not related in any way with escaping or quoting shell meta-characters. It is valid for the quote() method to return an SQL expression that evaluates to the desired string. For example: $quoted = $dbh->quote("one\ntwo\0three") may return something like: CONCAT('one', CHAR(12), 'two', CHAR(0), 'three') The quote() method should I be used with L. =head3 C $sql = $dbh->quote_identifier( $name ); $sql = $dbh->quote_identifier( $catalog, $schema, $table, \%attr ); Quote an identifier (table name etc.) for use in an SQL statement, by escaping any special characters (such as double quotation marks) it contains and adding the required type of outer quotation marks. Undefined names are ignored and the remainder are quoted and then joined together, typically with a dot (C<.>) character. For example: $id = $dbh->quote_identifier( undef, 'Her schema', 'My table' ); would, for most database types, return C<"Her schema"."My table"> (including all the double quotation marks). If three names are supplied then the first is assumed to be a catalog name and special rules may be applied based on what L returns for SQL_CATALOG_NAME_SEPARATOR (41) and SQL_CATALOG_LOCATION (114). For example, for Oracle: $id = $dbh->quote_identifier( 'link', 'schema', 'table' ); would return C<"schema"."table"@"link">. =head3 C $imp_data = $dbh->take_imp_data; Leaves the $dbh in an almost dead, zombie-like, state and returns a binary string of raw implementation data from the driver which describes the current database connection. Effectively it detaches the underlying database API connection data from the DBI handle. After calling take_imp_data(), all other methods except C will generate a warning and return undef. Why would you want to do this? You don't, forget I even mentioned it. Unless, that is, you're implementing something advanced like a multi-threaded connection pool. See L. The returned $imp_data can be passed as a C attribute to a later connect() call, even in a separate thread in the same process, where the driver can use it to 'adopt' the existing connection that the implementation data was taken from. Some things to keep in mind... B<*> the $imp_data holds the only reference to the underlying database API connection data. That connection is still 'live' and won't be cleaned up properly unless the $imp_data is used to create a new $dbh which is then allowed to disconnect() normally. B<*> using the same $imp_data to create more than one other new $dbh at a time may well lead to unpleasant problems. Don't do that. Any child statement handles are effectively destroyed when take_imp_data() is called. The C method was added in DBI 1.36 but wasn't useful till 1.49. =head2 Database Handle Attributes This section describes attributes specific to database handles. Changes to these database handle attributes do not affect any other existing or future database handles. Attempting to set or get the value of an unknown attribute generates a warning, except for private driver-specific attributes (which all have names starting with a lowercase letter). Example: $h->{AutoCommit} = ...; # set/write ... = $h->{AutoCommit}; # get/read =head3 C (boolean) If true, then database changes cannot be rolled-back (undone). If false, then database changes automatically occur within a "transaction", which must either be committed or rolled back using the C or C methods. Drivers should always default to C mode (an unfortunate choice largely forced on the DBI by ODBC and JDBC conventions.) Attempting to set C to an unsupported value is a fatal error. This is an important feature of the DBI. Applications that need full transaction behaviour can set C<$dbh-E{AutoCommit} = 0> (or set C to 0 via L) without having to check that the value was assigned successfully. For the purposes of this description, we can divide databases into three categories: Databases which don't support transactions at all. Databases in which a transaction is always active. Databases in which a transaction must be explicitly started (C<'BEGIN WORK'>). B<* Databases which don't support transactions at all> For these databases, attempting to turn C off is a fatal error. C and C both issue warnings about being ineffective while C is in effect. B<* Databases in which a transaction is always active> These are typically mainstream commercial relational databases with "ANSI standard" transaction behaviour. If C is off, then changes to the database won't have any lasting effect unless L is called (but see also L). If L is called then any changes since the last commit are undone. If C is on, then the effect is the same as if the DBI called C automatically after every successful database operation. So calling C or C explicitly while C is on would be ineffective because the changes would have already been commited. Changing C from off to on will trigger a L. For databases which don't support a specific auto-commit mode, the driver has to commit each statement automatically using an explicit C after it completes successfully (and roll it back using an explicit C if it fails). The error information reported to the application will correspond to the statement which was executed, unless it succeeded and the commit or rollback failed. B<* Databases in which a transaction must be explicitly started> For these databases, the intention is to have them act like databases in which a transaction is always active (as described above). To do this, the driver will automatically begin an explicit transaction when C is turned off, or after a L or L (or when the application issues the next database operation after one of those events). In this way, the application does not have to treat these databases as a special case. See L, L and L for other important notes about transactions. =head3 C (handle) Holds the handle of the parent driver. The only recommended use for this is to find the name of the driver using: $dbh->{Driver}->{Name} =head3 C (string) Holds the "name" of the database. Usually (and recommended to be) the same as the "C" string used to connect to the database, but with the leading "C" removed. =head3 C (string, read-only) Returns the statement string passed to the most recent L method called in this database handle, even if that method failed. This is especially useful where C is enabled and the exception handler checks $@ and sees that a 'prepare' method call failed. =head3 C (integer) A hint to the driver indicating the size of the local row cache that the application would like the driver to use for future C 1 - Disable the local row cache >1 - Cache this many rows <0 - Cache as many rows that will fit into this much memory for each C statement, C returns the number of rows affected, if known. If no rows were affected, then C returns "C<0E0>", which Perl will treat as 0 but will regard as true. Note that it is I an error for no rows to be affected by a statement. If the number of rows affected is not known, then C returns -1. For C statement by checking if C<$sth-E{NUM_OF_FIELDS}> is greater than zero after calling C. If any arguments are given, then C will effectively call L for each value before executing the statement. Values bound in this way are usually treated as C types unless the driver can determine the correct type (which is rare), or unless C (or C) has already been used to specify the type. Note that passing C an empty array is the same as passing no arguments at all, which will execute the statement with previously bound values. That's probably not what you want. If execute() is called on a statement handle that's still active ($sth->{Active} is true) then it should effectively call finish() to tidy up the previous execution results before starting this new execution. =head3 C $tuples = $sth->execute_array(\%attr) or die $sth->errstr; $tuples = $sth->execute_array(\%attr, @bind_values) or die $sth->errstr; ($tuples, $rows) = $sth->execute_array(\%attr) or die $sth->errstr; ($tuples, $rows) = $sth->execute_array(\%attr, @bind_values) or die $sth->errstr; Execute the prepared statement once for each parameter tuple (group of values) provided either in the @bind_values, or by prior calls to L, or via a reference passed in \%attr. When called in scalar context the execute_array() method returns the number of tuples executed, or C if an error occured. Like execute(), a successful execute_array() always returns true regardless of the number of tuples executed, even if it's zero. If there were any errors the ArrayTupleStatus array can be used to discover which tuples failed and with what errors. When called in list context the execute_array() method returns two scalars; $tuples is the same as calling execute_array() in scalar context and $rows is the sum of the number of rows affected for each tuple, if available or -1 if the driver cannot determine this. If you are doing an update operation the returned rows affected may not be what you expect if, for instance, one or more of the tuples affected the same row multiple times. Some drivers may not yet support list context, in which case $rows will be undef, or may not be able to provide the number of rows affected when performing this batch operation, in which case $rows will be -1. Bind values for the tuples to be executed may be supplied row-wise by an C attribute, or else column-wise in the C<@bind_values> argument, or else column-wise by prior calls to L. Where column-wise binding is used (via the C<@bind_values> argument or calls to bind_param_array()) the maximum number of elements in any one of the bound value arrays determines the number of tuples executed. Placeholders with fewer values in their parameter arrays are treated as if padded with undef (NULL) values. If a scalar value is bound, instead of an array reference, it is treated as a I length array with all elements having the same value. It's does not influence the number of tuples executed, so if all bound arrays have zero elements then zero tuples will be executed. If I bound values are scalars then one tuple will be executed, making execute_array() act just like execute(). The C attribute can be used to specify a reference to a subroutine that will be called to provide the bind values for each tuple execution. The subroutine should return an reference to an array which contains the appropriate number of bind values, or return an undef if there is no more data to execute. As a convienience, the C attribute can also be used to specify a statement handle. In which case the fetchrow_arrayref() method will be called on the given statement handle in order to provide the bind values for each tuple execution. The values specified via bind_param_array() or the @bind_values parameter may be either scalars, or arrayrefs. If any C<@bind_values> are given, then C will effectively call L for each value before executing the statement. Values bound in this way are usually treated as C types unless the driver can determine the correct type (which is rare), or unless C, C, C, or C has already been used to specify the type. See L for details. The C attribute can be used to specify a reference to an array which will receive the execute status of each executed parameter tuple. Note the C attribute was mandatory until DBI 1.38. For tuples which are successfully executed, the element at the same ordinal position in the status array is the resulting rowcount. If the execution of a tuple causes an error, then the corresponding status array element will be set to a reference to an array containing the error code and error string set by the failed execution. If B tuple execution returns an error, C will return C. In that case, the application should inspect the status array to determine which parameter tuples failed. Some databases may not continue executing tuples beyond the first failure. In this case the status array will either hold fewer elements, or the elements beyond the failure will be undef. If all parameter tuples are successfully executed, C returns the number tuples executed. If no tuples were executed, then execute_array() returns "C<0E0>", just like execute() does, which Perl will treat as 0 but will regard as true. For example: $sth = $dbh->prepare("INSERT INTO staff (first_name, last_name) VALUES (?, ?)"); my $tuples = $sth->execute_array( { ArrayTupleStatus => \my @tuple_status }, \@first_names, \@last_names, ); if ($tuples) { print "Successfully inserted $tuples records\n"; } else { for my $tuple (0..@last_names-1) { my $status = $tuple_status[$tuple]; $status = [0, "Skipped"] unless defined $status; next unless ref $status; printf "Failed to insert (%s, %s): %s\n", $first_names[$tuple], $last_names[$tuple], $status->[1]; } } Support for data returning statements such as SELECT is driver-specific and subject to change. At present, the default implementation provided by DBI only supports non-data returning statements. Transaction semantics when using array binding are driver and database specific. If C is on, the default DBI implementation will cause each parameter tuple to be inidividually committed (or rolled back in the event of an error). If C is off, the application is responsible for explicitly committing the entire set of bound parameter tuples. Note that different drivers and databases may have different behaviours when some parameter tuples cause failures. In some cases, the driver or database may automatically rollback the effect of all prior parameter tuples that succeeded in the transaction; other drivers or databases may retain the effect of prior successfully executed parameter tuples. Be sure to check your driver and database for its specific behaviour. Note that, in general, performance will usually be better with C turned off, and using explicit C after each C call. The C method was added in DBI 1.22, and ArrayTupleFetch was added in 1.36. =head3 C $tuples = $sth->execute_for_fetch($fetch_tuple_sub); $tuples = $sth->execute_for_fetch($fetch_tuple_sub, \@tuple_status); ($tuples, $rows) = $sth->execute_for_fetch($fetch_tuple_sub); ($tuples, $rows) = $sth->execute_for_fetch($fetch_tuple_sub, \@tuple_status); The execute_for_fetch() method is used to perform bulk operations and is most often used via the execute_array() method, not directly. The fetch subroutine, referenced by $fetch_tuple_sub, is expected to return a reference to an array (known as a 'tuple') or undef. The execute_for_fetch() method calls $fetch_tuple_sub, without any parameters, until it returns a false value. Each tuple returned is used to provide bind values for an $sth->execute(@$tuple) call. In scalar context execute_for_fetch() returns C if there were any errors and the number of tuples executed otherwise. Like execute() and execute_array() a zero is returned as "0E0" so execute_for_fetch() is only false on error. If there were any errors the @tuple_status array can be used to discover which tuples failed and with what errors. When called in list context execute_for_fetch() returns two scalars; $tuples is the same as calling execute_for_fetch() in scalar context and $rows is the sum of the number of rows affected for each tuple, if available or -1 if the driver cannot determine this. If you are doing an update operation the returned rows affected may not be what you expect if, for instance, one or more of the tuples affected the same row multiple times. Some drivers may not yet support list context, in which case $rows will be undef, or may not be able to provide the number of rows affected when performing this batch operation, in which case $rows will be -1. If \@tuple_status is passed then the execute_for_fetch method uses it to return status information. The tuple_status array holds one element per tuple. If the corresponding execute() did not fail then the element holds the return value from execute(), which is typically a row count. If the execute() did fail then the element holds a reference to an array containing ($sth->err, $sth->errstr, $sth->state). If the driver detects an error that it knows means no further tuples can be executed then it may return, with an error status, even though $fetch_tuple_sub may still have more tuples to be executed. Although each tuple returned by $fetch_tuple_sub is effectively used to call $sth->execute(@$tuple_array_ref) the exact timing may vary. Drivers are free to accumulate sets of tuples to pass to the database server in bulk group operations for more efficient execution. However, the $fetch_tuple_sub is specifically allowed to return the same array reference each time (which is what fetchrow_arrayref() usually does). For example: my $sel = $dbh1->prepare("select foo, bar from table1"); $sel->execute; my $ins = $dbh2->prepare("insert into table2 (foo, bar) values (?,?)"); my $fetch_tuple_sub = sub { $sel->fetchrow_arrayref }; my @tuple_status; $rc = $ins->execute_for_fetch($fetch_tuple_sub, \@tuple_status); my @errors = grep { ref $_ } @tuple_status; Similarly, if you already have an array containing the data rows to be processed you'd use a subroutine to shift off and return each array ref in turn: $ins->execute_for_fetch( sub { shift @array_of_arrays }, \@tuple_status); The C method was added in DBI 1.38. =head3 C $ary_ref = $sth->fetchrow_arrayref; $ary_ref = $sth->fetch; # alias Fetches the next row of data and returns a reference to an array holding the field values. Null fields are returned as C values in the array. This is the fastest way to fetch data, particularly if used with C<$sth-Ebind_columns>. If there are no more rows or if an error occurs, then C returns an C. You should check C<$sth-Eerr> afterwards (or use the C attribute) to discover if the C returned was due to an error. Note that the same array reference is returned for each fetch, so don't store the reference and then use it after a later fetch. Also, the elements of the array are also reused for each row, so take care if you want to take a reference to an element. See also L. =head3 C @ary = $sth->fetchrow_array; An alternative to C. Fetches the next row of data and returns it as a list containing the field values. Null fields are returned as C values in the list. If there are no more rows or if an error occurs, then C returns an empty list. You should check C<$sth-Eerr> afterwards (or use the C attribute) to discover if the empty list returned was due to an error. If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that. Also, in a scalar context, an C is returned if there are no more rows or if an error occurred. That C can't be distinguished from an C returned because the first field value was NULL. For these reasons you should exercise some caution if you use C in a scalar context. =head3 C $hash_ref = $sth->fetchrow_hashref; $hash_ref = $sth->fetchrow_hashref($name); An alternative to C. Fetches the next row of data and returns it as a reference to a hash containing field name and field value pairs. Null fields are returned as C values in the hash. If there are no more rows or if an error occurs, then C returns an C. You should check C<$sth-Eerr> afterwards (or use the C attribute) to discover if the C returned was due to an error. The optional C<$name> parameter specifies the name of the statement handle attribute. For historical reasons it defaults to "C", however using either "C" or "C" is recomended for portability. The keys of the hash are the same names returned by C<$sth-E{$name}>. If more than one field has the same name, there will only be one entry in the returned hash for those fields. Because of the extra work C and Perl have to perform, it is not as efficient as C or C. By default a reference to a new hash is returned for each row. It is likely that a future version of the DBI will support an attribute which will enable the same hash to be reused for each row. This will give a significant performance boost, but it won't be enabled by default because of the risk of breaking old code. =head3 C $tbl_ary_ref = $sth->fetchall_arrayref; $tbl_ary_ref = $sth->fetchall_arrayref( $slice ); $tbl_ary_ref = $sth->fetchall_arrayref( $slice, $max_rows ); The C method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array that contains one reference per row. If there are no rows to return, C returns a reference to an empty array. If an error occurs, C returns the data fetched thus far, which may be none. You should check C<$sth-Eerr> afterwards (or use the C attribute) to discover if the data is complete or was truncated due to an error. If $slice is an array reference, C uses L to fetch each row as an array ref. If the $slice array is not empty then it is used as a slice to select individual columns by perl array index number (starting at 0, unlike column and parameter numbers which start at 1). With no parameters, or if $slice is undefined, C acts as if passed an empty array ref. If $slice is a hash reference, C uses L to fetch each row as a hash reference. If the $slice hash is empty then fetchrow_hashref() is simply called in a tight loop and the keys in the hashes have whatever name lettercase is returned by default from fetchrow_hashref. (See L attribute.) If the $slice hash is not empty, then it is used as a slice to select individual columns by name. The values of the hash should be set to 1. The key names of the returned hashes match the letter case of the names in the parameter hash, regardless of the L attribute. For example, to fetch just the first column of every row: $tbl_ary_ref = $sth->fetchall_arrayref([0]); To fetch the second to last and last column of every row: $tbl_ary_ref = $sth->fetchall_arrayref([-2,-1]); To fetch all fields of every row as a hash ref: $tbl_ary_ref = $sth->fetchall_arrayref({}); To fetch only the fields called "foo" and "bar" of every row as a hash ref (with keys named "foo" and "BAR"): $tbl_ary_ref = $sth->fetchall_arrayref({ foo=>1, BAR=>1 }); The first two examples return a reference to an array of array refs. The third and forth return a reference to an array of hash refs. If $max_rows is defined and greater than or equal to zero then it is used to limit the number of rows fetched before returning. fetchall_arrayref() can then be called again to fetch more rows. This is especially useful when you need the better performance of fetchall_arrayref() but don't have enough memory to fetch and return all the rows in one go. Here's an example (assumes RaiseError is enabled): my $rows = []; # cache for batches of rows while( my $row = ( shift(@$rows) || # get row from cache, or reload cache: shift(@{$rows=$sth->fetchall_arrayref(undef,10_000)||[]}) ) ) { ... } That I be the fastest way to fetch and process lots of rows using the DBI, but it depends on the relative cost of method calls vs memory allocation. A standard C loop with column binding is often faster because the cost of allocating memory for the batch of rows is greater than the saving by reducing method calls. It's possible that the DBI may provide a way to reuse the memory of a previous batch in future, which would then shift the balance back towards fetchall_arrayref(). =head3 C $hash_ref = $sth->fetchall_hashref($key_field); The C method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to a hash containing a key for each distinct value of the $key_field column that was fetched. For each key the corresponding value is a reference to a hash containing all the selected columns and their values, as returned by fetchrow_hashref(). If there are no rows to return, C returns a reference to an empty hash. If an error occurs, C returns the data fetched thus far, which may be none. You should check C<$sth-Eerr> afterwards (or use the C attribute) to discover if the data is complete or was truncated due to an error. The $key_field parameter provides the name of the field that holds the value to be used for the key for the returned hash. For example: $dbh->{FetchHashKeyName} = 'NAME_lc'; $sth = $dbh->prepare("SELECT FOO, BAR, ID, NAME, BAZ FROM TABLE"); $sth->execute; $hash_ref = $sth->fetchall_hashref('id'); print "Name for id 42 is $hash_ref->{42}->{name}\n"; The $key_field parameter can also be specified as an integer column number (counting from 1). If $key_field doesn't match any column in the statement, as a name first then as a number, then an error is returned. For queries returing more than one 'key' column, you can specify multiple column names by passing $key_field as a reference to an array containing one or more key column names (or index numbers). For example: $sth = $dbh->prepare("SELECT foo, bar, baz FROM table"); $sth->execute; $hash_ref = $sth->fetchall_hashref( [ qw(foo bar) ] ); print "For foo 42 and bar 38, baz is $hash_ref->{42}->{38}->{baz}\n"; The fetchall_hashref() method is normally used only where the key fields values for each row are unique. If multiple rows are returned with the same values for the key fields then later rows overwrite earlier ones. =head3 C $rc = $sth->finish; Indicate that no more data will be fetched from this statement handle before it is either executed again or destroyed. The C method is rarely needed, and frequently overused, but can sometimes be helpful in a few very specific situations to allow the server to free up resources (such as sort buffers). When all the data has been fetched from a C C (for some specific operations like C and C), or after fetching all the rows of a C statements, it is generally not possible to know how many rows will be returned except by fetching them all. Some drivers will return the number of rows the application has fetched so far, but others may return -1 until all rows have been fetched. So use of the C method or C<$DBI::rows> with C is to execute a "SELECT COUNT(*) FROM ..." SQL statement with the same "..." as your query and then fetch the row count from that. =head3 C $rc = $sth->bind_col($column_number, \$var_to_bind); $rc = $sth->bind_col($column_number, \$var_to_bind, \%attr ); $rc = $sth->bind_col($column_number, \$var_to_bind, $bind_type ); Binds a Perl variable and/or some attributes to an output column (field) of a C statement. The list of references should have the same number of elements as the number of columns in the C statements, then this attribute holds the number of un-fetched rows in the cache. If the driver doesn't, then it returns C. Note that some drivers pre-fetch rows on execute, whereas others wait till the first fetch. See also the L database handle attribute. =head1 FURTHER INFORMATION =head2 Catalog Methods An application can retrieve metadata information from the DBMS by issuing appropriate queries on the views of the Information Schema. Unfortunately, C views are seldom supported by the DBMS. Special methods (catalog methods) are available to return result sets for a small but important portion of that metadata: column_info foreign_key_info primary_key_info table_info statistics_info All catalog methods accept arguments in order to restrict the result sets. Passing C to an optional argument does not constrain the search for that argument. However, an empty string ('') is treated as a regular search criteria and will only match an empty value. B: SQL/CLI and ODBC differ in the handling of empty strings. An empty string will not restrict the result set in SQL/CLI. Most arguments in the catalog methods accept only I, e.g. the arguments of C. Such arguments are treated as a literal string, i.e. the case is significant and quote characters are taken literally. Some arguments in the catalog methods accept I (strings containing '_' and/or '%'), e.g. the C<$table> argument of C. Passing '%' is equivalent to leaving the argument C. B: The underscore ('_') is valid and often used in SQL identifiers. Passing such a value to a search pattern argument may return more rows than expected! To include pattern characters as literals, they must be preceded by an escape character which can be achieved with $esc = $dbh->get_info( 14 ); # SQL_SEARCH_PATTERN_ESCAPE $search_pattern =~ s/([_%])/$esc$1/g; The ODBC and SQL/CLI specifications define a way to change the default behaviour described above: All arguments (except I) are treated as I if the C attribute is set to C. I are very similar to I, i.e. their body (the string within the quotes) is interpreted literally. I are compared in UPPERCASE. The DBI (currently) does not support the C attribute, i.e. it behaves like an ODBC driver where C is set to C. =head2 Transactions Transactions are a fundamental part of any robust database system. They protect against errors and database corruption by ensuring that sets of related changes to the database take place in atomic (indivisible, all-or-nothing) units. This section applies to databases that support transactions and where C is off. See L for details of using C with various types of databases. The recommended way to implement robust transactions in Perl applications is to use C and S> (which is very fast, unlike S>). For example: $dbh->{AutoCommit} = 0; # enable transactions, if possible $dbh->{RaiseError} = 1; eval { foo(...) # do lots of work here bar(...) # including inserts baz(...) # and updates $dbh->commit; # commit the changes if we get this far }; if ($@) { warn "Transaction aborted because $@"; # now rollback to undo the incomplete changes # but do it in an eval{} as it may also fail eval { $dbh->rollback }; # add other application on-error-clean-up code here } If the C attribute is not set, then DBI calls would need to be manually checked for errors, typically like this: $h->method(@args) or die $h->errstr; With C set, the DBI will automatically C if any DBI method call on that handle (or a child handle) fails, so you don't have to test the return value of each method call. See L for more details. A major advantage of the C approach is that the transaction will be properly rolled back if I code (not just DBI calls) in the inner application dies for any reason. The major advantage of using the C<$h-E{RaiseError}> attribute is that all DBI calls will be checked automatically. Both techniques are strongly recommended. After calling C or C many drivers will not let you fetch from a previously active C statements. See L and L for other important information about transactions. =head2 Handling BLOB / LONG / Memo Fields Many databases support "blob" (binary large objects), "long", or similar datatypes for holding very long strings or large amounts of binary data in a single field. Some databases support variable length long values over 2,000,000,000 bytes in length. Since values of that size can't usually be held in memory, and because databases can't usually know in advance the length of the longest long that will be returned from a C