Storage/DBI.pm

filter: Statementstmt 43.1% 41 / 95 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 48.2% 14 / 29 Podpod 100% 13 / 13 totaltotal 39.0% 68 / 174 CC 15 SCAR 18.3
CC
15
Cov
33%
connect_string
12.0
is_connected
12.0
connect
12.0
CRAP 6.2
« Storage/DBIC.pm ↑ Summary Storage/Statement.pm »
1 5 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Storage::DBI;
6 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 $Data::Storage::DBI::VERSION = '1.102720';
8 }
9  
10 # ABSTRACT: Mixin class for storages based on DBI
11 # Mixin class for storages based on a transactional RDBMS. When deriving from
12 # this class, put it before Data::Storage in 'use base' so that its
13 # connect() and disconnect() methods are found before the generic ones from
14 # Data::Storage.
15 5 ▶use DBI ':sql_types';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Data::Storage::DBI::Unrealized;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Data::Storage::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Error::Hierarchy::Util 'assert_defined';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Error::Hierarchy::Internal::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Data::Storage::Exception::Connect;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Error ':try';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use parent qw(Data::Storage Class::Accessor::Complex);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 __PACKAGE__->mk_scalar_accessors(
24 qw(
25 dbh dbname dbuser dbpass dbhost port AutoCommit RaiseError PrintError
26 LongReadLen HandleError ShowErrorStatement schema_prefix)
27 );
28 5 ▶use constant DEFAULTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
29 AutoCommit => 0,
30 RaiseError => 1,
31 PrintError => 0,
32 LongReadLen => 4_096_000,
33 HandleError => Error::Hierarchy::Internal::DBI->handler,
34 ShowErrorStatement => 1,
35 schema_prefix => '',
36 5 );
37  
38 # Subclasses that construct the connect string as given below can simply set
39 # the connect_string_dbi_id; others have to override connect_string().
40 5 ▶use constant connect_string_dbi_id => '?';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 0 ▶sub connect_string {
Subroutine connect_string: not called CC=3 SCAR=24.8
Pod documented
43 0 my $self = shift;
44 0 my $s = sprintf "dbi:%s:dbname=%s",
45 $self->connect_string_dbi_id, $self->dbname;
46 0 ▶ $s .= ';host=' . $self->dbhost if $self->dbhost;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dbhost 0 0 0
47 0 ▶ $s .= ';port=' . $self->port if $self->port;
Branch: 1 branch
BranchTrueFalseTotal
if $self->port 0 0 0
48 0 $s;
49 }
50  
51 0 ▶sub get_connect_options {
Subroutine get_connect_options: not called CC=1 SCAR=6.9
Pod documented
52 0 my $self = shift;
53 0 { RaiseError => $self->RaiseError,
54 PrintError => $self->PrintError,
55 AutoCommit => $self->AutoCommit,
56 LongReadLen => $self->LongReadLen,
57 HandleError => $self->HandleError,
58 ShowErrorStatement => $self->ShowErrorStatement,
59 };
60 }
61  
62 0 ▶sub is_connected {
Subroutine is_connected: not called CC=3 SCAR=24.8
Pod documented
63 0 my $self = shift;
64 0 ▶ my $is_connected = $self->dbh
Condition0%0/6
$self->dbh && (ref $self->dbh eq 'DBI::db' || ref $self->dbh eq 'Apache::DBI::db')
!ll&&!rl&&r
000
ref $self->dbh eq "DBI::db" or ref $self->dbh eq "Apache::DBI::db"
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->dbh && (ref $self->dbh eq 'DBI::db' || ref $self->dbh eq 'Apache::DBI::db')
$self->dbh ref $self->dbh eq "DBI::db" ref $self->dbh eq "Apache::DBI::db"
Truth table0%0/4
A && B or C
A= $self->dbhB= ref $self->dbh eq "DBI::db"C= ref $self->dbh eq "Apache::DBI::db"
ABCresult
0XX0
1000
11X1
1011
65 && ( ref($self->dbh) eq 'DBI::db'
66 || ref($self->dbh) eq 'Apache::DBI::db');
67  
68 # No logging unless it's necessary - this is called often and is expensive
69 # $self->log->debug('storage [%s] is %s',
70 # $self->dbname,
71 # $is_connected ? 'connected' : 'not connected');
72 0 $is_connected;
73 }
74  
75 0 ▶sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
76 0 my $self = shift;
77 0 ▶ return if $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_connected 0 0 0
78 assert_defined $self->$_, sprintf "called without %s argument.", $_
79 0 for qw/dbname dbuser dbpass/;
80 0 $self->log->debug('connecting to storage [%s] as [%s/%s]',
81 $self->dbname, $self->dbuser, $self->dbpass);
82 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 $self->dbh(
84 DBI->connect(
85 $self->connect_string, $self->dbuser,
86 $self->dbpass, $self->get_connect_options,
87 )
88 );
89 }
90 0 ▶ catch Error with {
Subroutine __ANON__: not called CC=3 SCAR=24.8
91 0 my $E = shift;
92 0 $! = 0;
93 0 ▶ if ($self->dbh && ref($self->dbh) eq 'DBI::db') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dbh and ref $self->dbh eq "DBI::db") 0 0 0
Condition0%0/3
$self->dbh and ref $self->dbh eq "DBI::db"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->dbh and ref $self->dbh eq "DBI::db"
$self->dbh ref $self->dbh eq "DBI::db"
Truth table0%0/3
A and B
A= $self->dbhB= ref $self->dbh eq "DBI::db"
ABresult
0X0
100
111
94 0 $self->set_rollback_mode;
95 0 $self->disconnect;
96 }
97 throw Data::Storage::Exception::Connect(
98 0 dbname => $self->dbname,
99 dbuser => $self->dbuser,
100 reason => $E
101 );
102 0 };
103 }
104  
105 0 ▶sub disconnect {
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod documented
106 0 my $self = shift;
107 0 ▶ return unless $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_connected 0 0 0
108 0 ▶ $self->rollback_mode ? $self->rollback : $self->commit;
Branch: 1 branch
BranchTrueFalseTotal
$self->rollback_mode ? : 0 0 0
109 0 $self->log->debug('disconnecting from storage [%s]', $self->dbname);
110 0 $self->dbh->disconnect;
111 0 $self->clear_dbh;
112 }
113  
114 0 ▶sub rollback {
Subroutine rollback: not called CC=1 SCAR=6.9
Pod documented
115 0 my $self = shift;
116 0 $self->dbh->rollback;
117 0 $self->log->debug('did rollback');
118 }
119  
120 0 ▶sub commit {
Subroutine commit: not called CC=3 SCAR=24.8
Pod documented
121 0 my $self = shift;
122 0 ▶ return if $self->rollback_mode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->rollback_mode 0 0 0
123  
124 # avoid "commit ineffective with AutoCommit enabled" error
125 0 ▶ return if $self->AutoCommit;
Branch: 1 branch
BranchTrueFalseTotal
if $self->AutoCommit 0 0 0
126 0 $self->dbh->commit;
127 0 $self->log->debug('did commit');
128 }
129  
130 0 ▶sub lazy_connect {
Subroutine lazy_connect: not called CC=2 SCAR=17.9
Pod documented
131 0 my $self = shift;
132 0 ▶ return if $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_connected 0 0 0
133 0 $self->dbh(Data::Storage::DBI::Unrealized->new(callback => $self));
134 }
135  
136 # Statement handles are wrapped in a special class that forwards most method
137 # calls directly to the statement handle but does some special things like
138 # stringifying potential value objects passed to bind_param.
139 #
140 # If you have a number of registry distributions (like we have NICAT, EnumAT,
141 # CarrierEnumAT etc.), you are probably going to use similar database
142 # schemata. We have a lot of tables that are the same in any registry database
143 # and they only differ in the table name prefix. For example, the table where
144 # the possible TLDs are stored is called cea_tld in CarrierEnumAT, eat_tld in
145 # EnumAT and so on. Therefore, we support a mechanism where you can use a
146 # speclal placeholder in your SQL statements where the database schema's
147 # prefix should be substituted.
148 #
149 # For example:
150 #
151 # SELECT tld_code FROM <P>_tld
152 #
153 # where <P> will be replaced by the schema prefix (given in the schema_prefix
154 # constant).
155 #
156 # This way we can have generic statements that still work in different
157 # database schemata. It's a little bit like generics in C++ and Java except
158 # that here we don't abstract over types, but schema names.
159 0 ▶sub prepare {
Subroutine prepare: not called CC=1 SCAR=6.9
Pod documented
160 0 my ($self, $query) = @_;
161 0 Data::Storage::Statement->new(
162 sth => $self->dbh->prepare($self->rewrite_query($query)));
163 }
164  
165 0 ▶sub prepare_named {
Subroutine prepare_named: not called CC=2 SCAR=17.9
Pod documented
166 0 my ($self, $name, $query) = @_;
167 0 our %cache;
168 0 ▶ $cache{$name} ||= $self->rewrite_query($query);
Condition0%0/3
$Data::Storage::DBI::cache{$name} ||= $self->rewrite_query($query)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Data::Storage::DBI::cache{$name} ||= $self->rewrite_query($query)
$Data::Storage::DBI::cache{$name} $self->rewrite_query($query)
Truth table0%0/3
A ||= B
A= $Data::Storage::DBI::cache{$name}B= $self->rewrite_query($query)
ABresult
1X1
011
000
169 Data::Storage::Statement->new(
170 0 sth => $self->dbh->prepare_cached($cache{$name}));
171 }
172  
173 # Do nothing here; subclasses can override it to rename tables and columns,
174 # for example. rewrite_query() is for a specific storage implementation;
175 # rewrite_query_for_dbd() is for rewriting queries depending on the database
176 # type. For example, the current user is called 'user' in Oracle and
177 # 'CURRENT_USER' in PostgreSQL.
178 0 ▶sub rewrite_query {
Subroutine rewrite_query: not called CC=1 SCAR=6.9
Pod documented
179 0 my ($self, $query) = @_;
180 0 $query = $self->rewrite_query_for_dbd($query);
181 0 my $prefix = $self->schema_prefix;
182 0 $query =~ s/<P>/$prefix/g;
183 0 $query;
184 }
185  
186 0 ▶sub rewrite_query_for_dbd {
Subroutine rewrite_query_for_dbd: not called CC=1 SCAR=6.9
Pod documented
187 0 my ($self, $query) = @_;
188 0 $query;
189 }
190  
191 0 ▶sub signature {
Subroutine signature: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193 0 sprintf "%s,dbname=%s,dbuser=%s",
194 $self->SUPER::signature(), $self->dbname, $self->dbuser;
195 }
196 5 1;
197  
198  
199 __END__
« Storage/DBIC.pm ↑ Summary Storage/Statement.pm »