use namespace::clean w/ Try::Tiny
[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 Try::Tiny;
8 use namespace::clean -except => 'meta';
9
10 =head1 NAME
11
12 DBIx::Class::Storage::DBI::Replicated::WithDSN - A DBI Storage Role with DSN
13 information in trace output
14
15 =head1 SYNOPSIS
16
17 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>.
18
19 =head1 DESCRIPTION
20
21 This role adds C<DSN: > info to storage debugging output.
22
23 =head1 METHODS
24
25 This class defines the following methods.
26
27 =head2 around: _query_start
28
29 Add C<DSN: > to debugging output.
30
31 =cut
32
33 around '_query_start' => sub {
34   my ($method, $self, $sql, @bind) = @_;
35
36   my $dsn = (try { $self->dsn }) || $self->_dbi_connect_info->[0];
37
38   my($op, $rest) = (($sql=~m/^(\w+)(.+)$/),'NOP', 'NO SQL');
39   my $storage_type = $self->can('active') ? 'REPLICANT' : 'MASTER';
40
41   my $query = do {
42     if ((reftype($dsn)||'') ne 'CODE') {
43       "$op [DSN_$storage_type=$dsn]$rest";
44     }
45     elsif (my $id = try { $self->id }) {
46       "$op [$storage_type=$id]$rest";
47     }
48     else {
49       "$op [$storage_type]$rest";
50     }
51   };
52
53   $self->$method($query, @bind);
54 };
55
56 =head1 ALSO SEE
57
58 L<DBIx::Class::Storage::DBI>
59
60 =head1 AUTHOR
61
62 John Napiorkowski <john.napiorkowski@takkle.com>
63
64 =head1 LICENSE
65
66 You may distribute this code under the same terms as Perl itself.
67
68 =cut
69
70 1;