blib/lib/DBIx/DBO.pm

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