NinjaORM/Schema/Table.pm

filter: Statementstmt 100% 45 / 45 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 7 / 7 totaltotal 92.4% 73 / 79 CC 9 SCAR 8.7
CC
9
Cov
90%
new
4.0
get_inspector
3.1
get_table
3.1
CRAP 2.4
« NinjaORM.pm ↑ Summary NinjaORM/StaticClassInfo.pm »
1 package DBIx::NinjaORM::Schema::Table;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use DBIx::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 =head1 NAME
11  
12 DBIx::NinjaORM::Schema::Table - Store information about a table used by L<DBIx::NinjaORM>.
13  
14  
15 =head1 VERSION
16  
17 Version 3.1.0
18  
19 =cut
20  
21 9 our $VERSION = '3.1.0';
22  
23  
24 =head1 DESCRIPTION
25  
26 L<DBIx::NinjaORM::Schema::Table> currently uses L<DBIx::Inspector> to retrieve
27 various information about the tables used. This may however change in the
28 future, so this package encapsulates various functions to make it easier to
29 replace the dependencies and internals later if needed.
30  
31  
32 =head1 SYNOPSIS
33  
34 use DBIx::NinjaORM::Schema::Table;
35 my $table_schema = DBIx::NinjaORM::Schema::Table->new(
36 dbh => $dbh,
37 name => $name,
38 );
39  
40 my $column_names = $table_schema->get_column_names();
41  
42  
43 =head1 METHODS
44  
45 =head2 new()
46  
47 Create a new DBIx::NinjaORM::Schema::Table object.
48  
49 my $table_schema = DBIx::NinjaORM::Schema::Table->new(
50 dbh => $dbh,
51 name => $name,
52 );
53  
54 Arguments:
55  
56 =over 4
57  
58 =item * dbh (mandatory)
59  
60 The database handle to use to access the table.
61  
62 =item * name (mandatory)
63  
64 The name of the table.
65  
66 =back
67  
68 =cut
69  
70 sub new
71 10 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
72 10 my ( $class, %args ) = @_;
73 10 my $name = delete( $args{'name'} );
74 10 my $dbh = delete( $args{'dbh'} );
75 10 ▶ croak 'The following arguments are not valid: ' . join( ', ', keys %args )
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %args != 0 0 10 10
76 if scalar( keys %args ) != 0;
77  
78 # Check for the mandatory parameters.
79 10 ▶ croak 'The argument "name" is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 9 10
80 if !defined( $name );
81 9 ▶ croak 'The argument "dbh" is mandatory'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh 1 8 9
82 if !defined( $dbh );
83  
84 # Return a blessed object.
85 8 return bless(
86 {
87 name => $name,
88 dbh => $dbh,
89 },
90 $class,
91 );
92 }
93  
94  
95 =head2 get_name()
96  
97 Return the table name.
98  
99 my $table_name = $table_schema->get_name();
100  
101 =cut
102  
103 sub get_name
104 5 ▶{
Subroutine get_name: called CC=1 SCAR=0.0
Pod documented
105 5 my ( $self ) = @_;
106  
107 5 return $self->{'name'};
108 }
109  
110  
111 =head2 get_dbh()
112  
113 Return the database handle associated with the table.
114  
115 my $dbh = $table_schema->get_dbh();
116  
117 =cut
118  
119 sub get_dbh
120 6 ▶{
Subroutine get_dbh: called CC=1 SCAR=0.0
Pod documented
121 6 my ( $self ) = @_;
122  
123 6 return $self->{'dbh'};
124 }
125  
126  
127 =head2 get_column_names()
128  
129 Return the name of the columns that exist in the underlying table.
130  
131 my $column_names = $table_schema->get_column_names();
132  
133 =cut
134  
135 sub get_column_names
136 1 ▶{
Subroutine get_column_names: called CC=1 SCAR=0.0
Pod documented
137 1 my ( $self ) = @_;
138  
139 1 my $columns = $self->get_columns();
140  
141 1 return [ map { $_->name() } @$columns ];
142 }
143  
144  
145 =head1 INTERNAL METHODS
146  
147 Warning: the API for the internal methods may change in the future. Use or
148 subclass with caution.
149  
150  
151 =head2 get_inspector()
152  
153 Return a cached L<DBIx::Inspector object>.
154  
155 my $inspector = $table_schema->get_inspector();
156  
157 =cut
158  
159 sub get_inspector
160 5 ▶{
Subroutine get_inspector: called CC=3 SCAR=11.3
Pod documented
161 5 my ( $self ) = @_;
162  
163 5 ▶ if ( !defined( $self->{'inspector'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"inspector"}) 5 0 5
164 {
165 5 $self->{'inspector'} = DBIx::Inspector->new( dbh => $self->get_dbh() );
166 croak 'Failed to create the DBIx::Inspector object'
167 5 ▶ if !defined( $self->{'inspector'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"inspector"} 0 5 5
168 }
169  
170 5 return $self->{'inspector'};
171 }
172  
173  
174 =head2 get_table()
175  
176 Return the cached L<DBIx::Inspector::Table> object associated with the
177 underlying table.
178  
179 my $table = $table_schema->get_table();
180  
181 =cut
182  
183 sub get_table
184 4 ▶{
Subroutine get_table: called CC=3 SCAR=11.2
Pod documented
185 4 my ( $self ) = @_;
186  
187 4 ▶ if ( !defined( $self->{'table'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"table"}) 4 0 4
188 {
189 4 my $inspector = $self->get_inspector();
190 4 $self->{'table'} = $inspector->table(
191 $self->get_name()
192 );
193  
194 croak 'Failed to retrieve the table object'
195 4 ▶ if !defined( $self->{'table'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"table"} 0 4 4
196 }
197  
198 4 return $self->{'table'};
199 }
200  
201 =head2 get_columns()
202  
203 Return the cached arrayref of L<DBIx::Inspector::Column> objects corresponding
204 to the columns of the underlying table.
205  
206 my $columns = $table_schema->get_columns();
207  
208 =cut
209  
210 sub get_columns
211 3 ▶{
Subroutine get_columns: called CC=2 SCAR=7.0
Pod documented
212 3 my ( $self ) = @_;
213  
214 3 ▶ if ( !defined( $self->{'columns'} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"columns"}) 3 0 3
215 {
216 3 my $table = $self->get_table();
217 3 $self->{'columns'} = [ $table->columns() ];
218 }
219  
220 3 return $self->{'columns'};
221 }
222  
223  
224 =head1 BUGS
225  
226 Please report any bugs or feature requests through the web interface at
227 L<https://github.com/guillaumeaubert/DBIx-NinjaORM/issues/new>.
228 I will be notified, and then you'll automatically be notified of progress on
229 your bug as I make changes.
230  
231  
232 =head1 SUPPORT
233  
234 You can find documentation for this module with the perldoc command.
235  
236 perldoc DBIx::NinjaORM::Schema::Table
237  
238  
239 You can also look for information at:
240  
241 =over 4
242  
243 =item * GitHub's request tracker
244  
245 L<https://github.com/guillaumeaubert/DBIx-NinjaORM/issues>
246  
247 =item * AnnoCPAN: Annotated CPAN documentation
248  
249 L<http://annocpan.org/dist/DBIx-NinjaORM>
250  
251 =item * CPAN Ratings
252  
253 L<http://cpanratings.perl.org/d/DBIx-NinjaORM>
254  
255 =item * MetaCPAN
256  
257 L<https://metacpan.org/release/DBIx-NinjaORM>
258  
259 =back
260  
261  
262 =head1 AUTHOR
263  
264 Guillaume Aubert, C<< <aubertg at cpan.org> >>.
265  
266  
267 =head1 COPYRIGHT & LICENSE
268  
269 Copyright 2009-2017 Guillaume Aubert.
270  
271 This code is free software; you can redistribute it and/or modify it under the
272 same terms as Perl 5 itself.
273  
274 This program is distributed in the hope that it will be useful, but WITHOUT ANY
275 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
276 PARTICULAR PURPOSE. See the LICENSE file for more details.
277  
278 =cut
279  
280 9 1;
« NinjaORM.pm ↑ Summary NinjaORM/StaticClassInfo.pm »