blib/lib/DBIx/DBO/Table.pm

filter: Statementstmt 95.3% 82 / 86 Branchbran 70.0% 21 / 30 Conditioncond 75.0% 9 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 91.3% 21 / 23 Podpod 100% 14 / 14 totaltotal 86.6% 149 / 172 CC 21 SCAR 11.8
CC
21
Cov
88%
insert
7.1
name
6.0
column
4.0
CRAP 3.2
« blib/lib/DBIx/DBO/DBD/SQLite.pm ↑ Summary blib/lib/DBIx/DBO/DBD/DBM.pm »
1 package DBIx::DBO::Table;
2  
3 14 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use DBIx::DBO;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 ▶use overload '**' => \&column, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 *_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 # Insert a new row into the table
26 $table->insert(employee_id => 007, name => 'James Bond');
27
28 # Remove rows from the table where the name IS NULL
29 $table->delete(name => undef);
30  
31 =head1 DESCRIPTION
32  
33 C<Table> objects are mostly used for column references in a L<Query|DBIx::DBO::Query>.
34 They can also be used for INSERTs and DELETEs.
35  
36 =head1 METHODS
37  
38 =head3 C<new>
39  
40 DBIx::DBO::Table->new($dbo, $table);
41 # or
42 $dbo->table($table);
43  
44 Create and return a new C<Table> object.
45 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.
46  
47 =cut
48  
49 27 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
50 27 my $proto = shift;
51 27 ▶ 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") 26 1 27
52 26 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1250
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
53 26 $class->_init(@_);
54 }
55  
56 26 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
57 26 my($class, $dbo, $table) = @_;
58 26 (my $schema, $table, my $info) = $dbo->table_info($table);
59 21 bless { %$info, Schema => $schema, Name => $table, DBO => $dbo, LastInsertID => undef }, $class;
60 }
61  
62 =head3 C<tables>
63  
64 Return a list of C<Table> objects, which will always be this C<Table> object.
65  
66 =cut
67  
68 1 ▶sub tables {
Subroutine tables: called CC=2 SCAR=7.6
Pod documented
69 1 ▶ wantarray ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
70 }
71  
72 22 ▶sub _table_alias {
Subroutine _table_alias: called CC=1 SCAR=0.0
73 22 undef;
74 }
75  
76 =head3 C<name>
77  
78 $table_name = $table->name;
79 ($schema_name, $table_name) = $table->name;
80  
81 In scalar context it returns the name of the table in list context the schema and table names are returned.
82  
83 =cut
84  
85 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
86 0 ▶ wantarray ? @{$_[0]}{qw(Schema Name)} : $_[0]->{Name};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
87 }
88  
89 134 ▶sub _as_table {
Subroutine _as_table: called CC=2 SCAR=7.1
90 134 my $me = shift;
91 134 ▶ $me->{_as_table} //= $me->{DBO}{dbd_class}->_qi($me, @$me{qw(Schema Name)});
Condition66%2/3
$me->{"_as_table"} //= $me->{"DBO"}{"dbd_class"}->_qi($me, @$me{"Schema", "Name"})
l!l&&r!l&&!r
115190
MC/DC0%0/2
$me->{"_as_table"} //= $me->{"DBO"}{"dbd_class"}->_qi($me, @$me{"Schema", "Name"})
$me->{"_as_table"} $me->{"DBO"}{"dbd_class"}->_qi($me, @$me{"Schema", "Name"})
Truth table66%2/3
A //= B
A= $me->{"_as_table"}B= $me->{"DBO"}{"dbd_class"}->_qi($me, @$me{"Schema", "Name"})
ABresult
1X1
011
000
92 }
93  
94 =head3 C<columns>
95  
96 Return a list of column names.
97  
98 =cut
99  
100 9 ▶sub columns {
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
101 9 @{$_[0]->{Columns}};
102 }
103  
104 =head3 C<column>
105  
106 $table->column($column_name);
107 $table ** $column_name;
108  
109 Returns a reference to a column for use with other methods.
110 The C<**> method is a shortcut for the C<column> method.
111  
112 =cut
113  
114 73 ▶sub column {
Subroutine column: called CC=4 SCAR=13.9
Pod documented
115 73 my($me, $col) = @_;
116 73 ▶ croak 'Missing argument for column' unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 73 73
117 croak 'Invalid column '.$me->{DBO}{dbd_class}->_qi($me, $col).' in table '.$me->_as_table
118 73 ▶ unless exists $me->{Column_Idx}{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"Column_Idx"}{$col} 1 72 73
119 72 ▶ $me->{Column}{$col} //= bless [$me, $col], 'DBIx::DBO::Column';
Condition100%2/2
$me->{"Column"}{$col} //= bless([$me, $col], "DBIx::DBO::Column")
l!l
5913
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
120 }
121 14 *_inner_col = \&column;
122  
123 =head3 C<insert>
124  
125 $table->insert(name => 'Richard', age => 103);
126  
127 Insert a row into the table. Returns true on success or C<undef> on failure.
128  
129 On supporting databases you may also use C<$table-E<gt>last_insert_id> to retreive
130 the autogenerated ID (if there was one) from the last inserted row.
131  
132 =cut
133  
134 7 ▶sub insert {
Subroutine insert: called CC=7 SCAR=19.6
Pod documented
135 7 my $me = shift;
136 7 ▶ croak 'Called insert() without args on table '.$me->_as_table unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 7 7
137 7 ▶ croak 'Wrong number of arguments' if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 7 7
138 7 my @cols;
139 my @vals;
140 7 my @bind;
141 7 my %remove_duplicates;
142 7 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 7 21
143 14 my @val = $me->{DBO}{dbd_class}->_parse_val($me, pop);
144 14 my $col = $me->{DBO}{dbd_class}->_build_col($me, $me->{DBO}{dbd_class}->_parse_col($me, pop));
145 14 ▶ next if $remove_duplicates{$col}++;
Branch: 1 branch
BranchTrueFalseTotal
if $remove_duplicates{$col}++ 1 13 14
146 13 push @cols, $col;
147 13 push @vals, $me->{DBO}{dbd_class}->_build_val($me, \@bind, @val);
148 }
149 7 my $sql = 'INSERT INTO '.$me->_as_table.' ('.join(', ', @cols).') VALUES ('.join(', ', @vals).')';
150 7 $me->{DBO}{dbd_class}->_sql($me, $sql, @bind);
151 7 ▶ my $sth = $me->dbh->prepare($sql) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $me->dbh->prepare($sql) or return undef 7 0 7
152 7 ▶ my $rv = $sth->execute(@bind) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rv = $sth->execute(@bind) or return undef 7 0 7
153 7 $me->{LastInsertID} = $me->{DBO}{dbd_class}->_save_last_insert_id($me, $sth);
154 7 return $rv;
155 }
156  
157 =head3 C<last_insert_id>
158  
159 $table->insert(name => 'Quentin');
160 my $row_id = $table->last_insert_id;
161  
162 Retreive the autogenerated ID (if there was one) from the last inserted row.
163  
164 Returns the ID or undef if it's unavailable.
165  
166 =cut
167  
168 1 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
169 1 my $me = shift;
170 1 $me->{LastInsertID};
171 }
172  
173 =head3 C<bulk_insert>
174  
175 $table->bulk_insert(
176 columns => [qw(id name age)], # Optional
177 rows => [{name => 'Richard', age => 103}, ...]
178 );
179 $table->bulk_insert(
180 columns => [qw(id name age)], # Optional
181 rows => [[ undef, 'Richard', 103 ], ...]
182 );
183  
184 Insert multiple rows into the table.
185 Returns the number of rows inserted or C<undef> on failure.
186  
187 The C<columns> need not be passed in, and will default to all the columns in the table.
188  
189 On supporting databases you may also use C<$table-E<gt>last_insert_id> to retreive
190 the autogenerated ID (if there was one) from the last inserted row.
191  
192 =cut
193  
194 4 ▶sub bulk_insert {
Subroutine bulk_insert: called CC=3 SCAR=11.0
Pod documented
195 4 my($me, %opt) = @_;
196 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
197 4 my $sql = 'INSERT INTO '.$me->_as_table;
198  
199 4 my @cols;
200 4 ▶ if (defined $opt{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'columns'}) { } 2 2 4
201 2 @cols = map $me->column($_), @{$opt{columns}};
202 2 $sql .= ' ('.join(', ', map $me->{DBO}{dbd_class}->_build_col($me, $_), @cols).')';
203 2 @cols = map $_->[1], @cols;
204 } else {
205 2 @cols = @{$me->{Columns}};
206 }
207 4 $sql .= ' VALUES ';
208  
209 4 $me->{DBO}{dbd_class}->_bulk_insert($me, $sql, \@cols, %opt);
210 }
211  
212 =head3 C<delete>
213  
214 $table->delete(name => 'Richard', age => 103);
215  
216 Delete all rows from the table matching the criteria. Returns the number of rows deleted or C<undef> on failure.
217  
218 =cut
219  
220 8 ▶sub delete {
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
221 8 my $me = shift;
222 8 my $sql = 'DELETE FROM '.$me->_as_table;
223 8 my @bind;
224 my $clause;
225 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
226 7 $me->{DBO}{dbd_class}->_do($me, $sql, undef, @bind);
227 }
228  
229 =head3 C<truncate>
230  
231 $table->truncate;
232  
233 Truncate the table. Returns true on success or C<undef> on failure.
234  
235 =cut
236  
237 0 ▶sub truncate {
Subroutine truncate: not called CC=1 SCAR=6.9
Pod documented
238 0 my $me = shift;
239 0 $me->{DBO}{dbd_class}->_do($me, 'TRUNCATE TABLE '.$me->_as_table);
240 }
241  
242 =head2 Common Methods
243  
244 These methods are accessible from all DBIx::DBO* objects.
245  
246 =head3 C<dbo>
247  
248 The C<DBO> object.
249  
250 =head3 C<dbh>
251  
252 The I<read-write> C<DBI> handle.
253  
254 =head3 C<rdbh>
255  
256 The I<read-only> C<DBI> handle, or if there is no I<read-only> connection, the I<read-write> C<DBI> handle.
257  
258 =cut
259  
260 3 ▶sub dbo { $_[0]{DBO} }
Subroutine dbo: called CC=1 SCAR=0.0
Pod documented
261 18 ▶sub dbh { $_[0]{DBO}->dbh }
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
262 36 ▶sub rdbh { $_[0]{DBO}->rdbh }
Subroutine rdbh: called CC=1 SCAR=0.0
Pod documented
263  
264 =head3 C<config>
265  
266 $table_setting = $table->config($option);
267 $table->config($option => $table_setting);
268  
269 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.
270  
271 See L<DBIx::DBO/"Available config options">.
272  
273 =cut
274  
275 132 ▶sub config {
Subroutine config: called CC=4 SCAR=13.9
Pod documented
276 132 my $me = shift;
277 132 my $opt = shift;
278 132 ▶ return $me->{DBO}{dbd_class}->_set_config($me->{Config} //= {}, $opt, shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 30 102 132
Condition50%1/2
$me->{"Config"} //= {}
l!l
300
MC/DC0%0/1
$me->{"Config"} //= {}
$me->{"Config"}
Truth table50%1/2
A
A= $me->{"Config"}
Aresult
11
00
279 102 ▶ $me->{DBO}{dbd_class}->_get_config($opt, $me->{Config} //= {}, $me->{DBO}{Config}, \%DBIx::DBO::Config);
Condition100%2/2
$me->{"Config"} //= {}
l!l
8319
MC/DC100%1/1
$me->{"Config"} //= {}
$me->{"Config"}
Truth table100%2/2
A
A= $me->{"Config"}
Aresult
11
00
280 }
281  
282 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
283 7 undef %{$_[0]};
284 }
285  
286 14 1;
287  
288 __END__
« blib/lib/DBIx/DBO/DBD/SQLite.pm ↑ Summary blib/lib/DBIx/DBO/DBD/DBM.pm »