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