Protect DBIC as best we can from the failure mode in 7cb35852
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated / WithDSN.pm
1 package DBIx::Class::Storage::DBI::Replicated::WithDSN;
2
3 use Moose::Role;
4 use Scalar::Util 'reftype';
5 requires qw/_query_start/;
6
7 use DBIx::Class::_Util 'dbic_internal_try';
8
9 use namespace::clean -except => 'meta';
10
11 =head1 NAME
12
13 DBIx::Class::Storage::DBI::Replicated::WithDSN - A DBI Storage Role with DSN
14 information in trace output
15
16 =head1 SYNOPSIS
17
18 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>.
19
20 =head1 DESCRIPTION
21
22 This role adds C<DSN: > info to storage debugging output.
23
24 =head1 METHODS
25
26 This class defines the following methods.
27
28 =head2 around: _query_start
29
30 Add C<DSN: > to debugging output.
31
32 =cut
33
34 around '_query_start' => sub {
35   my ($method, $self, $sql, @bind) = @_;
36
37   my $dsn = (dbic_internal_try { $self->dsn }) || $self->_dbi_connect_info->[0];
38
39   my($op, $rest) = (($sql=~m/^(\w+)(.+)$/),'NOP', 'NO SQL');
40   my $storage_type = $self->can('active') ? 'REPLICANT' : 'MASTER';
41
42   my $query = do {
43     if ((reftype($dsn)||'') ne 'CODE') {
44       "$op [DSN_$storage_type=$dsn]$rest";
45     }
46     elsif (my $id = dbic_internal_try { $self->id }) {
47       "$op [$storage_type=$id]$rest";
48     }
49     else {
50       "$op [$storage_type]$rest";
51     }
52   };
53
54   $self->$method($query, @bind);
55 };
56
57 =head1 ALSO SEE
58
59 L<DBIx::Class::Storage::DBI>
60
61 =head1 FURTHER QUESTIONS?
62
63 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
64
65 =head1 COPYRIGHT AND LICENSE
66
67 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
68 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
69 redistribute it and/or modify it under the same terms as the
70 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
71
72 =cut
73
74 1;