File Coverage

blib/lib/DBIx/NinjaORM/Schema/Table.pm
Criterion Covered Total %
statement 43 43 100.0
branch 10 16 62.5
condition n/a
subroutine 11 11 100.0
pod 7 7 100.0
total 71 77 92.2


line stmt bran cond sub pod time code
1             package DBIx::NinjaORM::Schema::Table;
2              
3 9     9   74161 use strict;
  9         20  
  9         413  
4 9     9   52 use warnings;
  9         20  
  9         241  
5              
6 9     9   50 use Carp;
  9         20  
  9         630  
7 9     9   8445 use DBIx::Inspector;
  9         133375  
  9         5089  
8              
9              
10             =head1 NAME
11              
12             DBIx::NinjaORM::Schema::Table - Store information about a table used by L.
13              
14              
15             =head1 VERSION
16              
17             Version 3.0.2
18              
19             =cut
20              
21             our $VERSION = '3.0.2';
22              
23              
24             =head1 DESCRIPTION
25              
26             L currently uses L 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             {
72 10     10 1 109433 my ( $class, %args ) = @_;
73 10         39 my $name = delete( $args{'name'} );
74 10         30 my $dbh = delete( $args{'dbh'} );
75 10 50       62 croak 'The following arguments are not valid: ' . join( ', ', keys %args )
76             if scalar( keys %args ) != 0;
77              
78             # Check for the mandatory parameters.
79 10 100       62 croak 'The argument "name" is mandatory'
80             if !defined( $name );
81 9 100       60 croak 'The argument "dbh" is mandatory'
82             if !defined( $dbh );
83              
84             # Return a blessed object.
85 8         90 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             {
105 5     5 1 17 my ( $self ) = @_;
106              
107 5         72 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             {
121 6     6 1 17 my ( $self ) = @_;
122              
123 6         67 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             {
137 1     1 1 41 my ( $self ) = @_;
138              
139 1         7 my $columns = $self->get_columns();
140              
141 1         4 return [ map { $_->name() } @$columns ];
  6         28  
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.
154              
155             my $inspector = $table_schema->get_inspector();
156              
157             =cut
158              
159             sub get_inspector
160             {
161 5     5 1 58 my ( $self ) = @_;
162              
163 5 50       35 if ( !defined( $self->{'inspector'} ) )
164             {
165 5         34 $self->{'inspector'} = DBIx::Inspector->new( dbh => $self->get_dbh() );
166 5 50       21591 croak 'Failed to create the DBIx::Inspector object'
167             if !defined( $self->{'inspector'} );
168             }
169              
170 5         28 return $self->{'inspector'};
171             }
172              
173              
174             =head2 get_table()
175              
176             Return the cached L object associated with the
177             underlying table.
178              
179             my $table = $table_schema->get_table();
180              
181             =cut
182              
183             sub get_table
184             {
185 4     4 1 44 my ( $self ) = @_;
186              
187 4 50       22 if ( !defined( $self->{'table'} ) )
188             {
189 4         17 my $inspector = $self->get_inspector();
190 4         27 $self->{'table'} = $inspector->table(
191             $self->get_name()
192             );
193              
194 4 50       5217 croak 'Failed to retrieve the table object'
195             if !defined( $self->{'table'} );
196             }
197              
198 4         25 return $self->{'table'};
199             }
200              
201             =head2 get_columns()
202              
203             Return the cached arrayref of L 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             {
212 3     3 1 49 my ( $self ) = @_;
213              
214 3 50       30 if ( !defined( $self->{'columns'} ) )
215             {
216 3         13 my $table = $self->get_table();
217 3         19 $self->{'columns'} = [ $table->columns() ];
218             }
219              
220 3         14168 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.
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
246              
247             =item * AnnoCPAN: Annotated CPAN documentation
248              
249             L
250              
251             =item * CPAN Ratings
252              
253             L
254              
255             =item * MetaCPAN
256              
257             L
258              
259             =back
260              
261              
262             =head1 AUTHOR
263              
264             Guillaume Aubert, C<< >>.
265              
266              
267             =head1 COPYRIGHT & LICENSE
268              
269             Copyright 2009-2014 Guillaume Aubert.
270              
271             This program is free software: you can redistribute it and/or modify it under
272             the terms of the GNU General Public License version 3 as published by the Free
273             Software Foundation.
274              
275             This program is distributed in the hope that it will be useful, but WITHOUT ANY
276             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
277             PARTICULAR PURPOSE. See the GNU General Public License for more details.
278              
279             You should have received a copy of the GNU General Public License along with
280             this program. If not, see http://www.gnu.org/licenses/
281              
282             =cut
283              
284             1;