blib/lib/DBIx/DBO.pm

filter: Statementstmt 94.2% 179 / 190 Branchbran 83.3% 105 / 126 Conditioncond 75.0% 27 / 36 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 95.0% 38 / 40 Podpod 89.4% 17 / 19 totaltotal 86.8% 377 / 434 SCAR 45.1
CC
81
Cov
88%
__ANON__
20.0
new
13.0
connect
10.2
CRAP 91.4
« blib/lib/DBIx/DBO/DBD/mysql.pm ↑ Summary blib/lib/DBIx/DBO/DBD/Pg.pm »
1 package DBIx::DBO;
2  
3 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 our $VERSION;
10 12 our %Config = (
11 AutoReconnect => 0,
12 CacheQuery => 0,
13 DebugSQL => 0,
14 OnRowUpdate => 'simple',
15 QuoteIdentifier => 1,
16 );
17 12 my $need_c3_initialize;
18 my @ConnectArgs;
19  
20 BEGIN {
21 12 ▶ $VERSION = '0.40';
Subroutine BEGIN: called CC=2 SCAR=7.6
22 # The C3 method resolution order is required.
23 12 ▶ if ($] < 5.009_005) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.009005) { } 0 12 12
24 0 require MRO::Compat;
25 } else {
26 12 require mro;
27 }
28 }
29  
30 12 ▶use DBIx::DBO::DBD;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12 ▶use DBIx::DBO::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 ▶use DBIx::DBO::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶use DBIx::DBO::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 42 ▶sub _dbd_class { 'DBIx::DBO::DBD' }
Subroutine _dbd_class: called CC=1 SCAR=0.0
36 14 ▶sub _table_class { 'DBIx::DBO::Table' }
Subroutine _table_class: called CC=1 SCAR=0.0
37 9 ▶sub _query_class { 'DBIx::DBO::Query' }
Subroutine _query_class: called CC=1 SCAR=0.0
38 17 ▶sub _row_class { 'DBIx::DBO::Row' }
Subroutine _row_class: called CC=1 SCAR=0.0
39  
40 12 *_isa = \&DBIx::DBO::DBD::_isa;
41  
42 =head1 NAME
43  
44 DBIx::DBO - An OO interface to SQL queries and results. Easily constructs SQL queries, and simplifies processing of the returned data.
45  
46 =head1 SYNOPSIS
47  
48 use DBIx::DBO;
49
50 # Create the DBO
51 my $dbo = DBIx::DBO->connect('DBI:mysql:my_db', 'me', 'mypasswd') or die $DBI::errstr;
52
53 # Create a "read-only" connection (useful for a replicated database)
54 $dbo->connect_readonly('DBI:mysql:my_db', 'me', 'mypasswd') or die $DBI::errstr;
55
56 # Start with a Query object
57 my $query = $dbo->query('my_table');
58
59 # Find records with an 'o' in the name
60 $query->where('name', 'LIKE', '%o%');
61
62 # And with an id that is less than 500
63 $query->where('id', '<', 500);
64
65 # Exluding those with an age range from 20 to 29
66 $query->where('age', 'NOT BETWEEN', [20, 29]);
67
68 # Return only the first 10 rows
69 $query->limit(10);
70
71 # Fetch the rows
72 while (my $row = $query->fetch) {
73
74 # Use the row as an array reference
75 printf "id=%d name=%s status=%s\n", $row->[0], $row->[1], $row->[4];
76
77 # Or as a hash reference
78 print 'id=', $row->{id}, "\n", 'name=', $row->{name};
79
80 # Update/delete rows
81 $row->update(status => 'Fired!') if $row->{name} eq 'Harry';
82 $row->delete if $row->{id} == 27;
83 }
84  
85 =head1 DESCRIPTION
86  
87 This module provides a convenient and efficient way to access a database. It can construct queries for you and returns the results in easy to use methods.
88  
89 Once you've created a C<DBIx::DBO> object using one or both of C<connect> or C<connect_readonly>, you can begin creating L<Query|DBIx::DBO::Query> objects. These are the "workhorse" objects, they encapsulate an entire query with JOINs, WHERE clauses, etc. You need not have to know about what created the C<Query> to be able to use or modify it. This makes it valuable in environments like mod_perl or large projects that prefer an object oriented approach to data.
90  
91 The query is only automatically executed when the data is requested. This is to make it possible to minimise lookups that may not be needed or to delay them as late as possible.
92  
93 The L<Row|DBIx::DBO::Row> object returned can be treated as both an arrayref or a hashref. The data is aliased for efficient use of memory. C<Row> objects can be updated or deleted, even when created by JOINs (If the DB supports it).
94  
95 =head1 METHODS
96  
97 =cut
98  
99 sub import {
100 14 ▶ my $class = shift;
Subroutine import: called CC=4 SCAR=13.9
101 14 ▶ if (@_ & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1) 1 13 14
102 1 my $opt = pop;
103 1 carp "Import option '$opt' passed without a value";
104 }
105 14 ▶ while (my($opt, $val) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 6 14 20
106 6 ▶ if (exists $Config{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Config{$opt}) { } 5 1 6
107 5 DBIx::DBO::DBD->_set_config(\%Config, $opt, $val);
108 } else {
109 1 carp "Unknown import option '$opt'";
110 }
111 }
112 }
113  
114 =head3 C<new>
115  
116 DBIx::DBO->new($dbh);
117 DBIx::DBO->new(undef, $readonly_dbh);
118  
119 Create a new C<DBIx::DBO> object from existsing C<DBI> handles. You must provide one or both of the I<read-write> and I<read-only> C<DBI> handles.
120  
121 =head3 C<connect>
122  
123 $dbo = DBIx::DBO->connect($data_source, $username, $password, \%attr)
124 or die $DBI::errstr;
125  
126 Takes the same arguments as L<DBI-E<gt>connect|DBI/"connect"> for a I<read-write> connection to a database. It returns the C<DBIx::DBO> object if the connection succeeds or undefined on failure.
127  
128 =head3 C<connect_readonly>
129  
130 Takes the same arguments as C<connect> for a I<read-only> connection to a database. It returns the C<DBIx::DBO> object if the connection succeeds or undefined on failure.
131  
132 Both C<connect> & C<connect_readonly> can be called on a C<DBIx::DBO> object to add that respective connection to create a C<DBIx::DBO> with both I<read-write> and I<read-only> connections.
133  
134 my $dbo = DBIx::DBO->connect($master_dsn, $username, $password, \%attr)
135 or die $DBI::errstr;
136 $dbo->connect_readonly($slave_dsn, $username, $password, \%attr)
137 or die $DBI::errstr;
138  
139 =cut
140  
141 sub new {
142 19 ▶ my $me = shift;
Subroutine new: called CC=13 SCAR=25.7
Pod documented
143 19 ▶ croak 'Too many arguments for '.(caller(0))[3] if @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 3 1 18 19
144 18 my($dbh, $rdbh, $new) = @_;
145  
146 18 ▶ if (defined $new and not UNIVERSAL::isa($new, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new and not UNIVERSAL::isa($new, "HASH")) 1 17 18
Condition100%3/3
defined $new and not UNIVERSAL::isa($new, "HASH")
!ll&&!rl&&r
7101
MC/DC100%2/2
defined $new and not UNIVERSAL::isa($new, "HASH")
defined $new not UNIVERSAL::isa($new, "HASH")
Truth table100%3/3
A and not(B)
A= defined $newB= not UNIVERSAL::isa($new, "HASH")
ABresult
0X0
100
111
147 1 croak '3rd argument to '.(caller(0))[3].' is not a HASH reference';
148 }
149 17 ▶ if (defined $dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbh) 11 6 17
150 11 ▶ croak 'Invalid read-write database handle' unless _isa($dbh, 'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
unless _isa($dbh, "DBI::db") 1 10 11
151 10 $new->{dbh} = $dbh;
152 10 ▶ $new->{dbd} ||= $dbh->{Driver}{Name};
Condition66%2/3
$new->{"dbd"} ||= $dbh->{"Driver"}{"Name"}
l!l&&r!l&&!r
370
MC/DC0%0/2
$new->{"dbd"} ||= $dbh->{"Driver"}{"Name"}
$new->{"dbd"} $dbh->{"Driver"}{"Name"}
Truth table66%2/3
A ||= B
A= $new->{"dbd"}B= $dbh->{"Driver"}{"Name"}
ABresult
1X1
011
000
153 }
154 16 ▶ if (defined $rdbh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rdbh) 6 10 16
155 6 ▶ croak 'Invalid read-only database handle' unless _isa($rdbh, 'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
unless _isa($rdbh, "DBI::db") 1 5 6
156 croak 'The read-write and read-only connections must use the same DBI driver'
157 5 ▶ if $dbh and $dbh->{Driver}{Name} ne $rdbh->{Driver}{Name};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh and $dbh->{"Driver"}{"Name"} ne $rdbh->{"Driver"}{"Name"} 1 4 5
Condition100%3/3
$dbh and $dbh->{"Driver"}{"Name"} ne $rdbh->{"Driver"}{"Name"}
!ll&&!rl&&r
311
MC/DC100%2/2
$dbh and $dbh->{"Driver"}{"Name"} ne $rdbh->{"Driver"}{"Name"}
$dbh $dbh->{"Driver"}{"Name"} ne $rdbh->{"Driver"}{"Name"}
Truth table100%3/3
A and B
A= $dbhB= $dbh->{"Driver"}{"Name"} ne $rdbh->{"Driver"}{"Name"}
ABresult
0X0
100
111
158 4 $new->{rdbh} = $rdbh;
159 4 ▶ $new->{dbd} ||= $rdbh->{Driver}{Name};
Condition66%2/3
$new->{"dbd"} ||= $rdbh->{"Driver"}{"Name"}
l!l&&r!l&&!r
130
MC/DC0%0/2
$new->{"dbd"} ||= $rdbh->{"Driver"}{"Name"}
$new->{"dbd"} $rdbh->{"Driver"}{"Name"}
Truth table66%2/3
A ||= B
A= $new->{"dbd"}B= $rdbh->{"Driver"}{"Name"}
ABresult
1X1
011
000
160 }
161 14 ▶ croak "Can't create the DBO, unknown database driver" unless $new->{dbd};
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"dbd"} 1 13 14
162 13 $new->{dbd_class} = $me->_dbd_class->_require_dbd_class($new->{dbd});
163 13 $me->_init($new);
164 }
165  
166 sub _init {
167 13 ▶ my($class, $me) = @_;
Subroutine _init: called CC=1 SCAR=0.0
168 13 bless $me, $class;
169 13 $me->{dbd_class}->_init_dbo($me);
170 }
171  
172 sub connect {
173 8 ▶ my $me = shift;
Subroutine connect: called CC=10 SCAR=23.2
Pod documented
174 8 my $conn;
175 8 ▶ if (ref $me) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $me) 4 4 8
176 4 ▶ croak 'DBO is already connected' if $me->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"dbh"} 1 3 4
177 3 ▶ $me->_check_driver($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 1 3
178 2 ▶ if ($me->config('AutoReconnect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->config('AutoReconnect')) { } 1 1 2
179 1 ▶ $me->{ConnectArgs} = scalar @ConnectArgs unless defined $me->{ConnectArgs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->{"ConnectArgs"} 0 1 1
180 1 $conn = $me->{ConnectArgs};
181 } else {
182 1 ▶ undef $ConnectArgs[$me->{ConnectArgs}] if defined $me->{ConnectArgs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $me->{"ConnectArgs"} 1 0 1
183 1 delete $me->{ConnectArgs};
184 }
185 # $conn = $me->{ConnectArgs} //= scalar @ConnectArgs if $me->config('AutoReconnect');
186 2 ▶ $me->{dbh} = $me->_connect($conn, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
$me->{"dbh"} = $me->_connect($conn, @_) or return 0 2 2
187 2 return $me;
188 }
189 4 my %new;
190 4 ▶ $conn = $new{ConnectArgs} = scalar @ConnectArgs if $me->config('AutoReconnect');
Branch: 1 branch
BranchTrueFalseTotal
if $me->config("AutoReconnect") 1 3 4
191 4 ▶ my $dbh = $me->_connect($conn, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $me->_connect($conn, @_) or return 0 4 4
192 4 $me->new($dbh, undef, \%new);
193 }
194  
195 sub connect_readonly {
196 12 ▶ my $me = shift;
Subroutine connect_readonly: called CC=9 SCAR=22.0
Pod documented
197 12 my $conn;
198 12 ▶ if (ref $me) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $me) 11 1 12
199 11 undef $me->{rdbh};
200 11 ▶ $me->_check_driver($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 4 11
201 9 ▶ if ($me->config('AutoReconnect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->config('AutoReconnect')) { } 4 5 9
202 4 ▶ $me->{ConnectReadOnlyArgs} = scalar @ConnectArgs unless defined $me->{ConnectReadOnlyArgs};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->{"ConnectReadOnlyArgs"} 2 2 4
203 4 $conn = $me->{ConnectReadOnlyArgs};
204 } else {
205 5 ▶ undef $ConnectArgs[$me->{ConnectReadOnlyArgs}] if defined $me->{ConnectReadOnlyArgs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $me->{"ConnectReadOnlyArgs"} 2 3 5
206 5 delete $me->{ConnectReadOnlyArgs};
207 }
208 # $conn = $me->{ConnectReadOnlyArgs} //= scalar @ConnectArgs if $me->config('AutoReconnect');
209 9 ▶ $me->{rdbh} = $me->_connect($conn, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
$me->{"rdbh"} = $me->_connect($conn, @_) or return 0 7 7
210 7 return $me;
211 }
212 1 my %new;
213 1 ▶ $conn = $new{ConnectReadOnlyArgs} = scalar @ConnectArgs if $me->config('AutoReconnect');
Branch: 1 branch
BranchTrueFalseTotal
if $me->config("AutoReconnect") 0 1 1
214 1 ▶ my $dbh = $me->_connect($conn, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $me->_connect($conn, @_) or return 0 1 1
215 1 $me->new(undef, $dbh, \%new);
216 }
217  
218 sub _check_driver {
219 9 ▶ my($me, $dsn) = @_;
Subroutine _check_driver: called CC=4 SCAR=13.9
220  
221 9 ▶ my $driver = (DBI->parse_dsn($dsn))[1] or
Branch: 1 branch
BranchTrueFalseTotal
my $driver = ("DBI"->parse_dsn($dsn))[1] or croak("Can't connect to data source '${dsn}' because I can't work out what driver to use " . q[(it doesn't seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is not set)]) 1 8 9
222 croak "Can't connect to data source '$dsn' because I can't work out what driver to use " .
223 "(it doesn't seem to contain a 'dbi:driver:' prefix and the DBI_DRIVER env var is not set)";
224  
225 ref($me) =~ /::DBD::\Q$driver\E$/ or
226 $driver eq $me->{dbd} or
227 8 ▶ croak "Can't connect to the data source '$dsn'\n" .
Branch: 1 branch
BranchTrueFalseTotal
ref($me) =~ /::DBD::\Q$driver\E$/ or $driver eq $me->{"dbd"} or croak("Can't connect to the data source '${dsn}'\n" . 'The read-write and read-only connections must use the same DBI driver') 2 6 8
Condition66%2/3
ref($me) =~ /::DBD::\Q$driver\E$/ or $driver eq $me->{"dbd"}
l!l&&r!l&&!r
062
MC/DC50%1/2
ref($me) =~ /::DBD::\Q$driver\E$/ or $driver eq $me->{"dbd"}
ref($me) =~ /::DBD::\Q$driver\E$/ $driver eq $me->{"dbd"}
Truth table66%2/3
A or B
A= ref($me) =~ /::DBD::\Q$driver\E$/B= $driver eq $me->{"dbd"}
ABresult
1X1
011
000
228 "The read-write and read-only connections must use the same DBI driver";
229 }
230  
231 sub _connect {
232 17 ▶ my $me = shift;
Subroutine _connect: called CC=7 SCAR=21.6
233 17 my $conn_idx = shift;
234 17 my @conn;
235  
236 17 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 11 6 17
elsif (defined $conn_idx and $ConnectArgs[$conn_idx]) { } 4 2 6
237 11 my($dsn, $user, $auth, $attr) = @_;
238 11 ▶ my %attr = %$attr if ref($attr) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $attr eq "HASH" 2 9 11
239  
240 # Add a stack trace to PrintError & RaiseError
241 $attr{HandleError} = sub {
242 0 ▶ if ($Config{DebugSQL} > 1) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if ($Config{"DebugSQL"} > 1) 0 0 0
243 0 $_[0] = Carp::longmess($_[0]);
244 0 return 0;
245 }
246 0 ▶ carp $_[1]->errstr if $_[1]->{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{"PrintError"} 0 0 0
247 0 ▶ croak $_[1]->errstr if $_[1]->{RaiseError};
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{"RaiseError"} 0 0 0
248 0 return 1;
249 11 ▶ } unless exists $attr{HandleError};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr{"HandleError"} 5 6 11
250  
251 # AutoCommit is always on
252 11 %attr = (PrintError => 0, RaiseError => 1, %attr, AutoCommit => 1);
253 11 @conn = ($dsn, $user, $auth, \%attr);
254  
255 # If a conn index is given then store the connection args
256 11 ▶ $ConnectArgs[$conn_idx] = \@conn if defined $conn_idx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conn_idx 3 8 11
257 ▶ } elsif (defined $conn_idx and $ConnectArgs[$conn_idx]) {
Condition66%2/3
defined $conn_idx and $ConnectArgs[$conn_idx]
!ll&&!rl&&r
204
MC/DC50%1/2
defined $conn_idx and $ConnectArgs[$conn_idx]
defined $conn_idx $ConnectArgs[$conn_idx]
Truth table66%2/3
A and B
A= defined $conn_idxB= $ConnectArgs[$conn_idx]
ABresult
0X0
100
111
258 # If a conn index is given then retrieve the connection args
259 4 @conn = @{$ConnectArgs[$conn_idx]};
260 } else {
261 2 croak "Can't auto-connect as AutoReconnect was not set";
262 }
263  
264 15 local @DBIx::DBO::CARP_NOT = qw(DBI);
265 15 DBI->connect(@conn);
266 }
267  
268 =head3 C<table>
269  
270 $dbo->table($table);
271 $dbo->table([$schema, $table]);
272 $dbo->table($table_object);
273  
274 Create and return a new L<Table|DBIx::DBO::Table> object.
275 Tables can be specified by their name or an arrayref of schema and table name or another L<Table|DBIx::DBO::Table> object.
276  
277 =cut
278  
279 sub table {
280 7 ▶ $_[0]->_table_class->new(@_);
Subroutine table: called CC=1 SCAR=0.0
Pod documented
281 }
282  
283 =head3 C<query>
284  
285 $dbo->query($table, ...);
286 $dbo->query([$schema, $table], ...);
287 $dbo->query($table_object, ...);
288  
289 Create a new L<Query|DBIx::DBO::Query> object from the tables specified.
290 In scalar context, just the C<Query> object will be returned.
291 In list context, the C<Query> object and L<Table|DBIx::DBO::Table> objects will be returned for each table specified.
292  
293 my($query, $table1, $table2) = $dbo->query(['my_schema', 'my_table'], 'my_other_table');
294  
295 =cut
296  
297 sub query {
298 10 ▶ $_[0]->_query_class->new(@_);
Subroutine query: called CC=1 SCAR=0.0
Pod documented
299 }
300  
301 =head3 C<row>
302  
303 $dbo->row($table || $table_object || $query_object);
304  
305 Create and return a new L<Row|DBIx::DBO::Row> object.
306  
307 =cut
308  
309 sub row {
310 5 ▶ $_[0]->_row_class->new(@_);
Subroutine row: called CC=1 SCAR=0.0
Pod documented
311 }
312  
313 =head3 C<selectrow_array>, C<selectrow_arrayref>, C<selectrow_hashref>, C<selectall_arrayref>
314  
315 $dbo->selectrow_array($statement, \%attr, @bind_values);
316 $dbo->selectrow_arrayref($statement, \%attr, @bind_values);
317 $dbo->selectrow_hashref($statement, \%attr, @bind_values);
318 $dbo->selectall_arrayref($statement, \%attr, @bind_values);
319  
320 These convenience methods provide access to L<DBI-E<gt>selectrow_array|DBI/"selectrow_array">, L<DBI-E<gt>selectrow_arrayref|DBI/"selectrow_arrayref">, L<DBI-E<gt>selectrow_hashref|DBI/"selectrow_hashref">, L<DBI-E<gt>selectall_arrayref|DBI/"selectall_arrayref"> methods.
321 They default to using the I<read-only> C<DBI> handle.
322  
323 =cut
324  
325 sub selectrow_array {
326 1 ▶ my $me = shift;
Subroutine selectrow_array: called CC=1 SCAR=0.0
Pod documented
327 1 $me->{dbd_class}->_selectrow_array($me, @_);
328 }
329  
330 sub selectrow_arrayref {
331 2 ▶ my $me = shift;
Subroutine selectrow_arrayref: called CC=1 SCAR=0.0
Pod documented
332 2 $me->{dbd_class}->_selectrow_arrayref($me, @_);
333 }
334  
335 sub selectrow_hashref {
336 0 ▶ my $me = shift;
Subroutine selectrow_hashref: not called CC=1 SCAR=6.9
Pod documented
337 0 $me->{dbd_class}->_selectrow_hashref($me, @_);
338 }
339  
340 sub selectall_arrayref {
341 1 ▶ my $me = shift;
Subroutine selectall_arrayref: called CC=1 SCAR=0.0
Pod documented
342 1 $me->{dbd_class}->_selectall_arrayref($me, @_);
343 }
344  
345 =head3 C<do>
346  
347 $dbo->do($statement) or die $dbo->dbh->errstr;
348 $dbo->do($statement, \%attr) or die $dbo->dbh->errstr;
349 $dbo->do($statement, \%attr, @bind_values) or die ...
350  
351 This provides access to the L<DBI-E<gt>do|DBI/"do"> method. It defaults to using the I<read-write> C<DBI> handle.
352  
353 =cut
354  
355 sub do {
356 6 ▶ my $me = shift;
Subroutine do: called CC=1 SCAR=0.0
Pod documented
357 6 $me->{dbd_class}->_do($me, @_);
358 }
359  
360 =head3 C<table_info>
361  
362 $dbo->table_info($table);
363 $dbo->table_info([$schema, $table]);
364 $dbo->table_info($table_object);
365  
366 Returns a hashref containing C<PrimaryKeys>, C<Columns> and C<Column_Idx> for the table.
367 Mainly for internal use.
368  
369 =cut
370  
371 sub table_info {
372 24 ▶ my($me, $table) = @_;
Subroutine table_info: called CC=10 SCAR=23.1
Pod documented
373 24 ▶ croak 'No table name supplied' unless defined $table and length $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table and length $table 0 24 24
Condition33%1/3
defined $table and length $table
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $table and length $table
defined $table length $table
Truth table33%1/3
A and B
A= defined $tableB= length $table
ABresult
0X0
100
111
374  
375 24 my $schema;
376 24 ▶ if (_isa($table, 'DBIx::DBO::Table')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($table, 'DBIx::DBO::Table')) { } 2 22 24
377 2 ▶ croak 'This table is from a different DBO connection' if $table->{DBO} != $me;
Branch: 1 branch
BranchTrueFalseTotal
if $table->{"DBO"} != $me 1 1 2
378 1 ($schema, $table) = @$table{qw(Schema Name)};
379 } else {
380 22 ▶ ($schema, $table) = ref $table eq 'ARRAY' ? @$table : $me->{dbd_class}->_unquote_table($me, $table);
Branch: 1 branch
BranchTrueFalseTotal
ref $table eq 'ARRAY' ? : 4 18 22
381 22 ▶ defined $schema or $schema = $me->{dbd_class}->_get_table_schema($me, $schema, $table);
Branch: 1 branch
BranchTrueFalseTotal
defined $schema or $schema = $me->{"dbd_class"}->_get_table_schema($me, $schema, $table) 20 2 22
382  
383 $me->{dbd_class}->_get_table_info($me, $schema, $table)
384 22 ▶ unless exists $me->{TableInfo}{defined $schema ? $schema : ''}{$table};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $me->{"TableInfo"}{defined $schema ? $schema : ""}{$table} 6 16 22
defined $schema ? : 11 11 22
385 }
386 22 ▶ return ($schema, $table, $me->{TableInfo}{defined $schema ? $schema : ''}{$table});
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 11 11 22
387 }
388  
389 =head3 C<disconnect>
390  
391 Disconnect both the I<read-write> & I<read-only> connections to the database.
392  
393 =cut
394  
395 sub disconnect {
396 4 ▶ my $me = shift;
Subroutine disconnect: called CC=3 SCAR=11.0
Pod documented
397 4 ▶ if ($me->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"dbh"}) 3 1 4
398 3 $me->{dbh}->disconnect;
399 3 undef $me->{dbh};
400 }
401 4 ▶ if ($me->{rdbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"rdbh"}) 3 1 4
402 3 $me->{rdbh}->disconnect;
403 3 undef $me->{rdbh};
404 }
405 4 delete $me->{TableInfo};
406 4 return;
407 }
408  
409 =head2 Common Methods
410  
411 These methods are accessible from all DBIx::DBO* objects.
412  
413 =head3 C<dbo>
414  
415 This C<DBO> object.
416  
417 =head3 C<dbh>
418  
419 The I<read-write> C<DBI> handle.
420  
421 =head3 C<rdbh>
422  
423 The I<read-only> C<DBI> handle, or if there is no I<read-only> connection, the I<read-write> C<DBI> handle.
424  
425 =cut
426  
427 2 ▶sub dbo { $_[0] }
Subroutine dbo: called CC=1 SCAR=0.0
Pod documented
428  
429 sub _handle {
430 302 ▶ my($me, $type) = @_;
Subroutine _handle: called CC=7 SCAR=19.5
431 # $type can be 'read-only', 'read-write' or false (which means try read-only then read-write)
432 302 ▶ $type ||= defined $me->{rdbh} ? 'read-only' : 'read-write';
Branch: 1 branch
BranchTrueFalseTotal
defined $me->{'rdbh'} ? : 10 258 268
Condition66%2/3
$type ||= defined $me->{"rdbh"} ? "read-only" : "read-write"
l!l&&r!l&&!r
342680
MC/DC0%0/2
$type ||= defined $me->{"rdbh"} ? "read-only" : "read-write"
$type defined $me->{"rdbh"} ? "read-only" : "read-write"
Truth table66%2/3
A ||= B
A= $typeB= defined $me->{"rdbh"} ? "read-only" : "read-write"
ABresult
1X1
011
000
433 302 ▶ my($d, $c) = $type ne 'read-only' ? qw(dbh ConnectArgs) : qw(rdbh ConnectReadOnlyArgs);
Branch: 1 branch
BranchTrueFalseTotal
$type ne 'read-only' ? : 292 10 302
434 302 ▶ croak "No $type handle connected" unless defined $me->{$d};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->{$d} 2 300 302
435 # Automatically reconnect, but only if possible and needed
436 300 ▶ $me->{$d} = $me->_connect($me->{$c}) if exists $me->{$c} and not $me->{$d}->ping;
Branch: 1 branch
BranchTrueFalseTotal
if exists $me->{$c} and not $me->{$d}->ping 1 299 300
Condition100%3/3
exists $me->{$c} and not $me->{$d}->ping
!ll&&!rl&&r
29631
MC/DC100%2/2
exists $me->{$c} and not $me->{$d}->ping
exists $me->{$c} not $me->{$d}->ping
Truth table100%3/3
A and not(B)
A= exists $me->{$c}B= not $me->{$d}->ping
ABresult
0X0
100
111
437 300 $me->{$d};
438 }
439  
440 sub dbh {
441 34 ▶ my $me = shift;
Subroutine dbh: called CC=2 SCAR=7.2
Pod documented
442 34 ▶ $me->_handle($me->config('UseHandle') || 'read-write');
Condition50%1/2
$me->config('UseHandle') || 'read-write'
l!l
034
MC/DC0%0/1
$me->config('UseHandle') || 'read-write'
$me->config('UseHandle')
Truth table50%1/2
A
A= $me->config('UseHandle')
Aresult
11
00
443 }
444  
445 sub rdbh {
446 268 ▶ my $me = shift;
Subroutine rdbh: called CC=1 SCAR=0.0
Pod documented
447 268 $me->_handle($me->config('UseHandle'));
448 }
449  
450 =head3 C<config>
451  
452 $global_setting = DBIx::DBO->config($option);
453 DBIx::DBO->config($option => $global_setting);
454 $dbo_setting = $dbo->config($option);
455 $dbo->config($option => $dbo_setting);
456  
457 Get or set the global or this C<DBIx::DBO> config settings. When setting an option, the previous value is returned. When getting an option's value, if the value is undefined, the global value is returned.
458  
459 =head2 Available C<config> options
460  
461 =over
462  
463 =item C<AutoReconnect>
464  
465 Boolean setting to store the connection details for re-use.
466 Before every operation the connection will be tested via ping() and reconnected automatically if needed.
467 Changing this has no effect after the connection has been made.
468 Defaults to C<false>.
469  
470 =item C<CacheQuery>
471  
472 Boolean setting to cause C<Query> objects to cache their entire result for re-use.
473 The query will only be executed automatically once.
474 To rerun the query, either explicitly call L<run|DBIx::DBO::Query/"run"> or alter the query.
475 Defaults to C<false>.
476  
477 =item C<DebugSQL>
478  
479 Set to C<1> or C<2> to warn about each SQL command executed. C<2> adds a full stack trace.
480 Defaults to C<0> (silent).
481  
482 =item C<OnRowUpdate>
483  
484 Set to C<'empty'>, C<'simple'> or C<'reload'> to define the behaviour of a C<Row> after an L<update|DBIx::DBO::Row/"update">.
485 C<'empty'> will simply leave the C<Row> empty after every update.
486 C<'simple'> will set the values in the C<Row> if they are not complex expressions, otherwise the C<Row> will be empty.
487 C<'reload'> is the same as C<'simple'> except it also tries to reload the C<Row> if possible.
488 Defaults to C<'simple'>.
489  
490 =item C<QuoteIdentifier>
491  
492 Boolean setting to control quoting of SQL identifiers (schema, table and column names).
493  
494 =item C<UseHandle>
495  
496 Set to C<'read-write'> or C<'read-only'> to force using only that handle for all operations.
497 Defaults to C<false> which chooses the I<read-only> handle for reads and the I<read-write> handle otherwise.
498  
499 =back
500  
501 Global options can also be set when C<use>'ing the module:
502  
503 use DBIx::DBO QuoteIdentifier => 0, DebugSQL => 1;
504  
505 =cut
506  
507 sub config {
508 399 ▶ my($me, $opt) = @_;
Subroutine config: called CC=6 SCAR=17.9
Pod documented
509 399 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 37 362 399
510 return ref $me
511 37 ▶ ? $me->{dbd_class}->_set_config($me->{Config} ||= {}, $opt, $_[2])
Branch: 1 branch
BranchTrueFalseTotal
ref $me ? : 15 22 37
Condition100%2/2
$me->{"Config"} ||= {}
l!l
132
MC/DC100%1/1
$me->{"Config"} ||= {}
$me->{"Config"}
Truth table100%2/2
A
A= $me->{"Config"}
Aresult
11
00
512 : $me->_dbd_class->_set_config(\%Config, $opt, $_[2]);
513 }
514 return ref $me
515 362 ▶ ? $me->{dbd_class}->_get_config($opt, $me->{Config} ||= {}, \%Config)
Branch: 1 branch
BranchTrueFalseTotal
ref $me ? : 353 9 362
Condition100%2/2
$me->{"Config"} ||= {}
l!l
3467
MC/DC100%1/1
$me->{"Config"} ||= {}
$me->{"Config"}
Truth table100%2/2
A
A= $me->{"Config"}
Aresult
11
00
516 : $me->_dbd_class->_get_config($opt, \%Config);
517 }
518  
519 sub STORABLE_freeze {
520 16 ▶ my $me = $_[0];
Subroutine STORABLE_freeze: called CC=9 SCAR=22.4
Pod undocumented
521 16 ▶ return unless ref $me->{dbh} or ref $me->{rdbh};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $me->{"dbh"} or ref $me->{"rdbh"} 8 8 16
Condition66%2/3
ref $me->{"dbh"} or ref $me->{"rdbh"}
l!l&&r!l&&!r
808
MC/DC50%1/2
ref $me->{"dbh"} or ref $me->{"rdbh"}
ref $me->{"dbh"} ref $me->{"rdbh"}
Truth table66%2/3
A or B
A= ref $me->{"dbh"}B= ref $me->{"rdbh"}
ABresult
1X1
011
000
522  
523 8 my %stash = map { $_ => delete $me->{$_} } qw(dbh rdbh ConnectArgs ConnectReadOnlyArgs);
524 8 ▶ $me->{dbh} = "$stash{dbh}" if defined $stash{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stash{"dbh"} 8 0 8
525 8 ▶ $me->{rdbh} = "$stash{rdbh}" if defined $stash{rdbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stash{"rdbh"} 0 8 8
526 8 for (qw(ConnectArgs ConnectReadOnlyArgs)) {
527 16 ▶ $me->{$_} = $ConnectArgs[$stash{$_}] if defined $stash{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $stash{$_} 0 16 16
528 }
529  
530 8 my $frozen = Storable::nfreeze($me);
531 8 ▶ defined $stash{$_} and $me->{$_} = $stash{$_} for qw(dbh rdbh ConnectArgs ConnectReadOnlyArgs);
Branch: 1 branch
BranchTrueFalseTotal
defined $stash{$_} and $me->{$_} = $stash{$_} 8 24 32
532 8 return $frozen;
533 }
534  
535 sub STORABLE_thaw {
536 8 ▶ my($me, $cloning, $frozen) = @_;
Subroutine STORABLE_thaw: called CC=5 SCAR=19.9
Pod undocumented
537 8 %$me = %{ Storable::thaw($frozen) };
538 8 ▶ if ($me->config('AutoReconnect')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->config('AutoReconnect')) { } 0 8 8
539 0 for (qw(ConnectArgs ConnectReadOnlyArgs)) {
540 0 ▶ $me->{$_} = push(@ConnectArgs, $me->{$_}) - 1 if $me->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{$_} 0 0 0
541 }
542 } else {
543 8 delete $me->{$_} for qw(ConnectArgs ConnectReadOnlyArgs);
544 }
545 }
546  
547 sub DESTROY {
548 17 ▶ undef %{$_[0]};
Subroutine DESTROY: called CC=1 SCAR=0.0
549 }
550  
551 12 1;
552  
553 __END__
« blib/lib/DBIx/DBO/DBD/mysql.pm ↑ Summary blib/lib/DBIx/DBO/DBD/Pg.pm »