blib/lib/DBIx/DBO/Table.pm

filter: Statementstmt 96.3% 106 / 110 Branchbran 68.4% 26 / 38 Conditioncond 66.6% 8 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 92.8% 26 / 28 Podpod 100% 19 / 19 totaltotal 87.3% 187 / 214 SCAR 32.7
CC
25
Cov
88%
insert
7.1
name
6.0
column
4.0
CRAP 26.2
« blib/lib/DBIx/DBO/DBD/Pg.pm ↑ Summary blib/lib/DBIx/DBO/DBD/Oracle.pm »
1 package DBIx::DBO::Table;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use overload '**' => \&column, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶sub _row_class { $_[0]{DBO}->_row_class }
Subroutine _row_class: called CC=1 SCAR=0.0
10  
11 12 *_isa = \&DBIx::DBO::DBD::_isa;
12  
13 =head1 NAME
14  
15 DBIx::DBO::Table - An OO interface to SQL queries and results. Encapsulates a table in an object.
16  
17 =head1 SYNOPSIS
18  
19 # Create a Table object
20 my $table = $dbo->table('my_table');
21
22 # Get a column reference
23 my $column = $table ** 'employee_id';
24
25 # Quickly display my employee id
26 print $table->fetch_value('employee_id', name => 'Vernon');
27
28 # Find the IDs of fired employees
29 my @fired = @{ $table->fetch_column('id', status => 'fired');
30
31 # Insert a new row into the table
32 $table->insert(employee_id => 007, name => 'James Bond');
33
34 # Remove rows from the table where the name IS NULL
35 $table->delete(name => undef);
36  
37 =head1 DESCRIPTION
38  
39 C<Table> objects are mostly used for column references in a L<Query|DBIx::DBO::Query>.
40 They can also be used for INSERTs, DELETEs and simple lookups (fetch_*).
41  
42 =head1 METHODS
43  
44 =head3 C<new>
45  
46 DBIx::DBO::Table->new($dbo, $table);
47 # or
48 $dbo->table($table);
49  
50 Create and return a new C<Table> object.
51 The C<$table> argument that specifies the table can be a string containing the table name, C<'customers'> or C<'history.log'>, it can be an arrayref of schema and table name C<['history', 'log']> or as another Table object to clone.
52  
53 =cut
54  
55 sub new {
56 22 ▶ my $proto = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
57 22 ▶ eval { $_[0]->isa('DBIx::DBO') } or croak 'Invalid DBO Object';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_[0]->isa("DBIx::DBO") } } or croak("Invalid DBO Object") 1 21 22
58 21 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
59 21 $class->_init(@_);
60 }
61  
62 sub _init {
63 21 ▶ my($class, $dbo, $table) = @_;
Subroutine _init: called CC=1 SCAR=0.0
64 21 (my $schema, $table, my $info) = $dbo->table_info($table);
65 19 bless { %$info, Schema => $schema, Name => $table, DBO => $dbo, LastInsertID => undef }, $class;
66 }
67  
68 =head3 C<tables>
69  
70 Return a list of C<Table> objects, which will always be this C<Table> object.
71  
72 =cut
73  
74 sub tables {
75 1 ▶ wantarray ? $_[0] : 1;
Subroutine tables: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
76 }
77  
78 sub _table_alias {
79 29 ▶ undef;
Subroutine _table_alias: called CC=1 SCAR=0.0
80 }
81  
82 =head3 C<name>
83  
84 $table_name = $table->name;
85 ($schema_name, $table_name) = $table->name;
86  
87 In scalar context it returns the name of the table in list context the schema and table names are returned.
88  
89 =cut
90  
91 sub name {
92 0 ▶ wantarray ? @{$_[0]}{qw(Schema Name)} : $_[0]->{Name};
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
93 }
94  
95 sub _from {
96 62 ▶ my $me = shift;
Subroutine _from: called CC=2 SCAR=6.9
97 62 ▶ defined $me->{_from} ? $me->{_from} : ($me->{_from} = $me->{DBO}{dbd_class}->_qi($me, @$me{qw(Schema Name)}));
Branch: 1 branch
BranchTrueFalseTotal
defined $me->{'_from'} ? : 45 17 62
98 }
99  
100 =head3 C<columns>
101  
102 Return a list of column names.
103  
104 =cut
105  
106 sub columns {
107 13 ▶ @{$_[0]->{Columns}};
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
108 }
109  
110 =head3 C<column>
111  
112 $table->column($column_name);
113 $table ** $column_name;
114  
115 Returns a reference to a column for use with other methods.
116 The C<**> method is a shortcut for the C<column> method.
117  
118 =cut
119  
120 sub column {
121 117 ▶ my($me, $col) = @_;
Subroutine column: called CC=4 SCAR=13.9
Pod documented
122 117 ▶ croak 'Missing argument for column' unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 117 117
123 croak 'Invalid column '.$me->{DBO}{dbd_class}->_qi($me, $col).' in table '.$me->_from
124 117 ▶ unless exists $me->{Column_Idx}{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"Column_Idx"}{$col} 1 116 117
125 116 ▶ $me->{Column}{$col} ||= bless [$me, $col], 'DBIx::DBO::Column';
Condition100%2/2
$me->{"Column"}{$col} ||= bless([$me, $col], "DBIx::DBO::Column")
l!l
9620
MC/DC100%1/1
$me->{"Column"}{$col} ||= bless([$me, $col], "DBIx::DBO::Column")
$me->{"Column"}{$col}
Truth table100%2/2
A
A= $me->{"Column"}{$col}
Aresult
11
00
126 }
127 12 *_inner_col = \&column;
128  
129 =head3 C<row>
130  
131 Returns a new empty L<Row|DBIx::DBO::Row> object for this table.
132  
133 =cut
134  
135 sub row {
136 3 ▶ my $me = shift;
Subroutine row: called CC=1 SCAR=0.0
Pod documented
137 3 $me->_row_class->new($me->{DBO}, $me);
138 }
139  
140 =head3 C<fetch_row>
141  
142 $table->fetch_row(%where);
143  
144 Fetch the first matching row from the table returning it as a L<Row|DBIx::DBO::Row> object.
145  
146 The C<%where> is a hash of field/value pairs.
147 The value can be a simple SCALAR or C<undef> for C<NULL>
148 It can also be a SCALAR reference, which will be used without quoting, or an ARRAY reference for multiple C<IN> values.
149  
150 $someone = $table->fetch_row(age => 21, join_date => \'CURDATE()', end_date => undef);
151 $a_child = $table->fetch_row(name => \'NOT NULL', age => [5 .. 15]);
152  
153 =cut
154  
155 sub fetch_row {
156 3 ▶ my $me = shift;
Subroutine fetch_row: called CC=1 SCAR=0.0
Pod documented
157 3 $me->row->load(@_);
158 }
159  
160 =head3 C<fetch_value>
161  
162 $table->fetch_value($column, %where);
163  
164 Fetch the first matching row from the table returning the value in one column.
165  
166 =cut
167  
168 sub fetch_value {
169 2 ▶ my($me, $col) = splice @_, 0, 2;
Subroutine fetch_value: called CC=3 SCAR=11.3
Pod documented
170 2 my @bind;
171 2 $col = $me->{DBO}{dbd_class}->_build_val($me, \@bind, $me->{DBO}{dbd_class}->_parse_col_val($me, $col));
172 2 my $sql = "SELECT $col FROM ".$me->_from;
173 2 my $clause;
174 2 ▶ $sql .= ' WHERE '.$clause if $clause = $me->{DBO}{dbd_class}->_build_quick_where($me, \@bind, @_);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $me->{"DBO"}{"dbd_class"}->_build_quick_where($me, \@bind, @_) 2 0 2
175 2 my $ref = $me->{DBO}{dbd_class}->_selectrow_arrayref($me, $sql, undef, @bind);
176 2 ▶ return $ref && $ref->[0];
Condition33%1/3
$ref && $ref->[0]
!ll&&!rl&&r
002
MC/DC0%0/2
$ref && $ref->[0]
$ref $ref->[0]
Truth table33%1/3
A && B
A= $refB= $ref->[0]
ABresult
0X0
100
111
177 }
178  
179 =head3 C<fetch_hash>
180  
181 $table->fetch_hash(%where);
182  
183 Fetch the first matching row from the table returning it as a hashref.
184  
185 =cut
186  
187 sub fetch_hash {
188 1 ▶ my $me = shift;
Subroutine fetch_hash: called CC=2 SCAR=7.0
Pod documented
189 1 my $sql = 'SELECT * FROM '.$me->_from;
190 1 my @bind;
191 my $clause;
192 1 ▶ $sql .= ' WHERE '.$clause if $clause = $me->{DBO}{dbd_class}->_build_quick_where($me, \@bind, @_);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $me->{"DBO"}{"dbd_class"}->_build_quick_where($me, \@bind, @_) 1 0 1
193 1 $me->{DBO}{dbd_class}->_selectrow_hashref($me, $sql, undef, @bind);
194 }
195  
196 =head3 C<fetch_column>
197  
198 $table->fetch_column($column, %where);
199  
200 Fetch all matching rows from the table returning an arrayref of the values in one column.
201  
202 =cut
203  
204 sub fetch_column {
205 1 ▶ my($me, $col) = splice @_, 0, 2;
Subroutine fetch_column: called CC=2 SCAR=7.0
Pod documented
206 1 my @bind;
207 1 $col = $me->{DBO}{dbd_class}->_build_val($me, \@bind, $me->{DBO}{dbd_class}->_parse_col_val($me, $col));
208 1 my $sql = "SELECT $col FROM ".$me->_from;
209 1 my $clause;
210 1 ▶ $sql .= ' WHERE '.$clause if $clause = $me->{DBO}{dbd_class}->_build_quick_where($me, \@bind, @_);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $me->{"DBO"}{"dbd_class"}->_build_quick_where($me, \@bind, @_) 1 0 1
211 1 $me->{DBO}{dbd_class}->_sql($me, $sql, @bind);
212 1 return $me->rdbh->selectcol_arrayref($sql, undef, @bind);
213 }
214  
215 =head3 C<insert>
216  
217 $table->insert(name => 'Richard', age => 103);
218  
219 Insert a row into the table. Returns true on success or C<undef> on failure.
220  
221 On supporting databases you may also use C<$table-E<gt>last_insert_id> to retreive
222 the autogenerated ID (if there was one) from the last inserted row.
223  
224 =cut
225  
226 sub insert {
227 7 ▶ my $me = shift;
Subroutine insert: called CC=7 SCAR=19.6
Pod documented
228 7 ▶ croak 'Called insert() without args on table '.$me->_from unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 7 7
229 7 ▶ croak 'Wrong number of arguments' if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 7 7
230 7 my @cols;
231 my @vals;
232 7 my @bind;
233 7 my %remove_duplicates;
234 7 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 7 21
235 14 my @val = $me->{DBO}{dbd_class}->_parse_val($me, pop);
236 14 my $col = $me->{DBO}{dbd_class}->_build_col($me, $me->{DBO}{dbd_class}->_parse_col($me, pop));
237 14 ▶ next if $remove_duplicates{$col}++;
Branch: 1 branch
BranchTrueFalseTotal
if $remove_duplicates{$col}++ 1 13 14
238 13 push @cols, $col;
239 13 push @vals, $me->{DBO}{dbd_class}->_build_val($me, \@bind, @val);
240 }
241 7 my $sql = 'INSERT INTO '.$me->_from.' ('.join(', ', @cols).') VALUES ('.join(', ', @vals).')';
242 7 $me->{DBO}{dbd_class}->_sql($me, $sql, @bind);
243 7 ▶ my $sth = $me->dbh->prepare($sql) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $me->dbh->prepare($sql) or return undef 0 7 7
244 7 ▶ my $rv = $sth->execute(@bind) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rv = $sth->execute(@bind) or return undef 0 7 7
245 7 $me->{LastInsertID} = $me->{DBO}{dbd_class}->_save_last_insert_id($me, $sth);
246 7 return $rv;
247 }
248  
249 =head3 C<last_insert_id>
250  
251 $table->insert(name => 'Quentin');
252 my $row_id = $table->last_insert_id;
253  
254 Retreive the autogenerated ID (if there was one) from the last inserted row.
255  
256 Returns the ID or undef if it's unavailable.
257  
258 =cut
259  
260 sub last_insert_id {
261 1 ▶ my $me = shift;
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
262 1 $me->{LastInsertID};
263 }
264  
265 =head3 C<bulk_insert>
266  
267 $table->bulk_insert(
268 columns => [qw(id name age)], # Optional
269 rows => [{name => 'Richard', age => 103}, ...]
270 );
271 $table->bulk_insert(
272 columns => [qw(id name age)], # Optional
273 rows => [[ undef, 'Richard', 103 ], ...]
274 );
275  
276 Insert multiple rows into the table.
277 Returns the number of rows inserted or C<undef> on failure.
278  
279 The C<columns> need not be passed in, and will default to all the columns in the table.
280  
281 On supporting databases you may also use C<$table-E<gt>last_insert_id> to retreive
282 the autogenerated ID (if there was one) from the last inserted row.
283  
284 =cut
285  
286 sub bulk_insert {
287 4 ▶ my($me, %opt) = @_;
Subroutine bulk_insert: called CC=3 SCAR=11.0
Pod documented
288 4 ▶ croak 'The "rows" argument must be an arrayref' if ref $opt{rows} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt{"rows"} ne "ARRAY" 0 4 4
289 4 my $sql = 'INSERT INTO '.$me->_from;
290  
291 4 my @cols;
292 4 ▶ if (defined $opt{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'columns'}) { } 2 2 4
293 2 @cols = map $me->column($_), @{$opt{columns}};
294 2 $sql .= ' ('.join(', ', map $me->{DBO}{dbd_class}->_build_col($me, $_), @cols).')';
295 2 @cols = map $_->[1], @cols;
296 } else {
297 2 @cols = @{$me->{Columns}};
298 }
299 4 $sql .= ' VALUES ';
300  
301 4 $me->{DBO}{dbd_class}->_bulk_insert($me, $sql, \@cols, %opt);
302 }
303  
304 =head3 C<delete>
305  
306 $table->delete(name => 'Richard', age => 103);
307  
308 Delete all rows from the table matching the criteria. Returns the number of rows deleted or C<undef> on failure.
309  
310 =cut
311  
312 sub delete {
313 8 ▶ my $me = shift;
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
314 8 my $sql = 'DELETE FROM '.$me->_from;
315 8 my @bind;
316 my $clause;
317 8 ▶ $sql .= ' WHERE '.$clause if $clause = $me->{DBO}{dbd_class}->_build_quick_where($me, \@bind, @_);
Branch: 1 branch
BranchTrueFalseTotal
if $clause = $me->{"DBO"}{"dbd_class"}->_build_quick_where($me, \@bind, @_) 3 4 7
318 7 $me->{DBO}{dbd_class}->_do($me, $sql, undef, @bind);
319 }
320  
321 =head3 C<truncate>
322  
323 $table->truncate;
324  
325 Truncate the table. Returns true on success or C<undef> on failure.
326  
327 =cut
328  
329 sub truncate {
330 0 ▶ my $me = shift;
Subroutine truncate: not called CC=1 SCAR=6.9
Pod documented
331 0 $me->{DBO}{dbd_class}->_do($me, 'TRUNCATE TABLE '.$me->_from);
332 }
333  
334 =head2 Common Methods
335  
336 These methods are accessible from all DBIx::DBO* objects.
337  
338 =head3 C<dbo>
339  
340 The C<DBO> object.
341  
342 =head3 C<dbh>
343  
344 The I<read-write> C<DBI> handle.
345  
346 =head3 C<rdbh>
347  
348 The I<read-only> C<DBI> handle, or if there is no I<read-only> connection, the I<read-write> C<DBI> handle.
349  
350 =cut
351  
352 2 ▶sub dbo { $_[0]{DBO} }
Subroutine dbo: called CC=1 SCAR=0.0
Pod documented
353 18 ▶sub dbh { $_[0]{DBO}->dbh }
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
354 51 ▶sub rdbh { $_[0]{DBO}->rdbh }
Subroutine rdbh: called CC=1 SCAR=0.0
Pod documented
355  
356 =head3 C<config>
357  
358 $table_setting = $table->config($option);
359 $table->config($option => $table_setting);
360  
361 Get or set the C<Table> config settings. When setting an option, the previous value is returned. When getting an option's value, if the value is undefined, the L<DBIx::DBO|DBIx::DBO>'s value is returned.
362  
363 See L<DBIx::DBO/Available_config_options>.
364  
365 =cut
366  
367 sub config {
368 149 ▶ my $me = shift;
Subroutine config: called CC=4 SCAR=13.9
Pod documented
369 149 my $opt = shift;
370 149 ▶ return $me->{DBO}{dbd_class}->_set_config($me->{Config} ||= {}, $opt, shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 34 115 149
Condition50%1/2
$me->{"Config"} ||= {}
l!l
340
MC/DC0%0/1
$me->{"Config"} ||= {}
$me->{"Config"}
Truth table50%1/2
A
A= $me->{"Config"}
Aresult
11
00
371 115 ▶ $me->{DBO}{dbd_class}->_get_config($opt, $me->{Config} ||= {}, $me->{DBO}{Config}, \%DBIx::DBO::Config);
Condition100%2/2
$me->{"Config"} ||= {}
l!l
9817
MC/DC100%1/1
$me->{"Config"} ||= {}
$me->{"Config"}
Truth table100%2/2
A
A= $me->{"Config"}
Aresult
11
00
372 }
373  
374 sub DESTROY {
375 7 ▶ undef %{$_[0]};
Subroutine DESTROY: called CC=1 SCAR=0.0
376 }
377  
378 12 1;
379  
380 __END__
« blib/lib/DBIx/DBO/DBD/Pg.pm ↑ Summary blib/lib/DBIx/DBO/DBD/Oracle.pm »