X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI.pm;h=ac90066876f7b747187bf41807211200217a25f7;hb=aa0163d42e50f63a1bd40d910654008ee8ca7a09;hp=ed5444b9f168441098973ebe0f161730ee0d61f7;hpb=c8f3ec1d90aa7d80f2a9fa5b08f98cc0625529d2;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index ed5444b..ac90066 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -16,10 +16,12 @@ use List::Util(); use Data::Dumper::Concise(); use Sub::Name (); +use File::Path (); + __PACKAGE__->mk_group_accessors('simple' => qw/_connect_info _dbi_connect_info _dbh _sql_maker _sql_maker_opts _conn_pid _conn_tid transaction_depth _dbh_autocommit _driver_determined savepoints - __server_info/ + _server_info_hash/ ); # the values for these accessors are picked out (and deleted) from @@ -36,11 +38,10 @@ __PACKAGE__->cursor_class('DBIx::Class::Storage::DBI::Cursor'); __PACKAGE__->mk_group_accessors('inherited' => qw/ sql_maker_class - can_insert_returning + _supports_insert_returning /); __PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks'); - # Each of these methods need _determine_driver called before itself # in order to function reliably. This is a purely DRY optimization my @rdbms_specific_methods = qw/ @@ -93,7 +94,7 @@ DBIx::Class::Storage::DBI - DBI storage handler ); $schema->resultset('Book')->search({ - written_on => $schema->storage->datetime_parser(DateTime->now) + written_on => $schema->storage->datetime_parser->format_datetime(DateTime->now) }); =head1 DESCRIPTION @@ -115,9 +116,102 @@ sub new { $new->{_in_dbh_do} = 0; $new->{_dbh_gen} = 0; + # read below to see what this does + $new->_arm_global_destructor; + $new; } +# This is hack to work around perl shooting stuff in random +# order on exit(). If we do not walk the remaining storage +# objects in an END block, there is a *small but real* chance +# of a fork()ed child to kill the parent's shared DBI handle, +# *before perl reaches the DESTROY in this package* +# Yes, it is ugly and effective. +{ + my %seek_and_destroy; + + sub _arm_global_destructor { + my $self = shift; + my $key = Scalar::Util::refaddr ($self); + $seek_and_destroy{$key} = $self; + Scalar::Util::weaken ($seek_and_destroy{$key}); + } + + END { + local $?; # just in case the DBI destructor changes it somehow + + # destroy just the object if not native to this process/thread + $_->_preserve_foreign_dbh for (grep + { defined $_ } + values %seek_and_destroy + ); + } +} + +sub DESTROY { + my $self = shift; + + # destroy just the object if not native to this process/thread + $self->_preserve_foreign_dbh; + + # some databases need this to stop spewing warnings + if (my $dbh = $self->_dbh) { + local $@; + eval { + %{ $dbh->{CachedKids} } = (); + $dbh->disconnect; + }; + } + + $self->_dbh(undef); +} + +sub _preserve_foreign_dbh { + my $self = shift; + + return unless $self->_dbh; + + $self->_verify_tid; + + return unless $self->_dbh; + + $self->_verify_pid; + +} + +# handle pid changes correctly - do not destroy parent's connection +sub _verify_pid { + my $self = shift; + + return if ( defined $self->_conn_pid and $self->_conn_pid == $$ ); + + $self->_dbh->{InactiveDestroy} = 1; + $self->_dbh(undef); + $self->{_dbh_gen}++; + + return; +} + +# very similar to above, but seems to FAIL if I set InactiveDestroy +sub _verify_tid { + my $self = shift; + + if ( ! defined $self->_conn_tid ) { + return; # no threads + } + elsif ( $self->_conn_tid == threads->tid ) { + return; # same thread + } + + #$self->_dbh->{InactiveDestroy} = 1; # why does t/51threads.t fail...? + $self->_dbh(undef); + $self->{_dbh_gen}++; + + return; +} + + =head2 connect_info This method is normally called by L, which @@ -803,19 +897,11 @@ sub connected { sub _seems_connected { my $self = shift; + $self->_preserve_foreign_dbh; + my $dbh = $self->_dbh or return 0; - if(defined $self->_conn_tid && $self->_conn_tid != threads->tid) { - $self->_dbh(undef); - $self->{_dbh_gen}++; - return 0; - } - else { - $self->_verify_pid; - return 0 if !$self->_dbh; - } - return $dbh->FETCH('Active'); } @@ -827,20 +913,6 @@ sub _ping { return $dbh->ping; } -# handle pid changes correctly -# NOTE: assumes $self->_dbh is a valid $dbh -sub _verify_pid { - my ($self) = @_; - - return if defined $self->_conn_pid && $self->_conn_pid == $$; - - $self->_dbh->{InactiveDestroy} = 1; - $self->_dbh(undef); - $self->{_dbh_gen}++; - - return; -} - sub ensure_connected { my ($self) = @_; @@ -873,7 +945,7 @@ sub dbh { # this is the internal "get dbh or connect (don't check)" method sub _get_dbh { my $self = shift; - $self->_verify_pid if $self->_dbh; + $self->_preserve_foreign_dbh; $self->_populate_dbh unless $self->_dbh; return $self->_dbh; } @@ -908,6 +980,7 @@ sub _populate_dbh { my @info = @{$self->_dbi_connect_info || []}; $self->_dbh(undef); # in case ->connected failed we might get sent here + $self->_server_info_hash (undef); $self->_dbh($self->_connect(@info)); $self->_conn_pid($$); @@ -920,8 +993,6 @@ sub _populate_dbh { $self->{transaction_depth} = $self->_dbh_autocommit ? 0 : 1; $self->_run_connection_actions unless $self->{_in_determine_driver}; - - $self->_populate_server_info; } sub _run_connection_actions { @@ -934,36 +1005,46 @@ sub _run_connection_actions { $self->_do_connection_actions(connect_call_ => $_) for @actions; } -sub _populate_server_info { +sub _server_info { my $self = shift; - my %info; - my $dbms_ver = eval { - local $@; - local $SIG{__WARN__} = sub {}; - $self->_get_dbh->get_info(18) - }; + unless ($self->_server_info_hash) { - if (defined $dbms_ver) { - $info{dbms_ver} = $dbms_ver; + my %info; - ($dbms_ver) = $dbms_ver =~ /^(\S+)/; + my $server_version = $self->_get_server_version; - my @verparts = split /\./, $dbms_ver; - $info{dbms_ver_normalized} = sprintf "%d.%03d%03d", @verparts; - } + if (defined $server_version) { + $info{dbms_version} = $server_version; - $self->__server_info(\%info); + my ($numeric_version) = $server_version =~ /^([\d\.]+)/; + my @verparts = split (/\./, $numeric_version); + if ( + @verparts + && + $verparts[0] <= 999 + ) { + # consider only up to 3 version parts, iff not more than 3 digits + my @use_parts; + while (@verparts && @use_parts < 3) { + my $p = shift @verparts; + last if $p > 999; + push @use_parts, $p; + } + push @use_parts, 0 while @use_parts < 3; - return \%info; -} + $info{normalized_dbms_version} = sprintf "%d.%03d%03d", @use_parts; + } + } -sub _server_info { - my $self = shift; + $self->_server_info_hash(\%info); + } - $self->_get_dbh; + return $self->_server_info_hash +} - return $self->__server_info(@_); +sub _get_server_version { + eval { shift->_get_dbh->get_info(18) }; } sub _determine_driver { @@ -989,8 +1070,9 @@ sub _determine_driver { # try to use dsn to not require being connected, the driver may still # force a connection in _rebless to determine version # (dsn may not be supplied at all if all we do is make a mock-schema) - my $dsn = $self->_dbi_connect_info->[0] || ''; + my $dsn = $self->_dbi_connect_info->[0] || $ENV{DBI_DSN} || ''; ($driver) = $dsn =~ /dbi:([^:]+):/i; + $driver ||= $ENV{DBI_DRIVER}; } } @@ -1862,7 +1944,7 @@ sub _select_args { #limited has_many ( $attrs->{rows} && keys %{$attrs->{collapse}} ) || - # limited prefetch with RNO subqueries + # limited prefetch with RNO subqueries (otherwise a risk of column name clashes) ( $attrs->{rows} && @@ -1873,7 +1955,7 @@ sub _select_args { @{$attrs->{_prefetch_select}} ) || - # grouped prefetch + # grouped prefetch (to satisfy group_by == select) ( $attrs->{group_by} && @{$attrs->{group_by}} @@ -1888,6 +1970,8 @@ sub _select_args { } elsif ( + # the RNO limit dialect mangles the SQL such that the join gets lost + # wrap a subquery here ($attrs->{rows} || $attrs->{offset}) && $sql_maker->limit_dialect eq 'RowNumberOver' @@ -1896,8 +1980,6 @@ sub _select_args { && scalar $self->_parse_order_by ($attrs->{order_by}) ) { - # the RNO limit dialect above mangles the SQL such that the join gets lost - # wrap a subquery here push @limit, delete @{$attrs}{qw/rows offset/}; @@ -2324,6 +2406,9 @@ sub create_ddl_dir { unless ($dir) { carp "No directory given, using ./\n"; $dir = './'; + } else { + -d $dir or File::Path::mkpath($dir) + or $self->throw_exception("create_ddl_dir: $! creating dir '$dir'"); } $self->throw_exception ("Directory '$dir' does not exist\n") unless(-d $dir); @@ -2639,23 +2724,6 @@ sub relname_to_table_alias { return $alias; } -sub DESTROY { - my $self = shift; - - $self->_verify_pid if $self->_dbh; - - # some databases need this to stop spewing warnings - if (my $dbh = $self->_dbh) { - local $@; - eval { - %{ $dbh->{CachedKids} } = (); - $dbh->disconnect; - }; - } - - $self->_dbh(undef); -} - 1; =head1 USAGE NOTES