blib/lib/Class/DBI/FormBuilder/Meta/Table.pm

filter: Statementstmt 19.3% 22 / 114 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 35.2% 6 / 17 Podpod 100% 5 / 5 totaltotal 15.9% 33 / 207 CC 29 SCAR 33.4
CC
29
Cov
13%
instance
72.0
column_deep_type
42.0
_load_type_meta
30.0
CRAP 28.1
« blib/lib/Class/DBI/FormBuilder.pm ↑ Summary blib/lib/Class/DBI/FormBuilder/Meta/Column.pm »
1 package Class::DBI::FormBuilder::Meta::Table;
2 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 32 ▶use List::Util();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 32 ▶use Class::DBI::FormBuilder::Meta::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 ▶use base qw( Class::Accessor );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 __PACKAGE__->mk_accessors( qw( dbh cdbi_class _columns_hash catalog schema ) );
13  
14 =head1 NAME
15  
16 Class::DBI::FormBuilder::Meta::Table
17  
18 =head1 DESCRIPTION
19  
20 Access to column metadata.
21  
22 =head1 METHODS
23  
24 =over 4
25  
26 =item instance( $cdbi, %args )
27  
28 Returns an instance for the C<$cdbi> class (C<$cdbi> can be a class name or object).
29  
30 The C<%args> hash is optional. Keys can be C<catalog> and C<schema>, which are also
31 available as accessors. Both default to C<undef>.
32  
33 =item catalog
34  
35 Get/set the catalog.
36  
37 =item schema
38  
39 Get/set the schema.
40  
41 =item dbh
42  
43 Get/set the DBI database handle (you probably don't want to set it).
44  
45 =item cdbi_class
46  
47 Get/set the CDBI class (you probably don't want to set it).
48  
49 =cut
50  
51 {
52 # per-process instances, keyed by CDBI class
53 32 my %Instances;
54
55 # must be a singleton, loading meta is a fairly expensive operation (the queries can return
56 # lots of data)
57 sub instance
58 0 ▶ {
Subroutine instance: not called CC=8 SCAR=42.8
Pod documented
59 0 my ( $proto, $cdbi, %args ) = @_;
60
61 0 ▶ $cdbi && UNIVERSAL::isa( $cdbi, 'Class::DBI' ) or
Branch: 1 branch
BranchTrueFalseTotal
$cdbi and UNIVERSAL::isa($cdbi, "Class::DBI") or Carp::croak("Must supply a CDBI class or object (got '${cdbi}')") 0 0 0
Condition0%0/3
$cdbi and UNIVERSAL::isa($cdbi, "Class::DBI")
!ll&&!rl&&r
000
MC/DC0%0/2
$cdbi and UNIVERSAL::isa($cdbi, "Class::DBI")
$cdbi UNIVERSAL::isa($cdbi, "Class::DBI")
Truth table0%0/3
A and B
A= $cdbiB= UNIVERSAL::isa($cdbi, "Class::DBI")
ABresult
0X0
100
111
62 Carp::croak( "Must supply a CDBI class or object (got '$cdbi')" );
63
64 0 ▶ my $cdbi_class = ref $cdbi || $cdbi;
Condition0%0/3
ref $cdbi || $cdbi
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $cdbi || $cdbi
ref $cdbi $cdbi
Truth table0%0/3
A || B
A= ref $cdbiB= $cdbi
ABresult
1X1
011
000
65
66 0 ▶ return $Instances{$cdbi_class} if $Instances{$cdbi_class};
Branch: 1 branch
BranchTrueFalseTotal
if $Instances{$cdbi_class} 0 0 0
67
68 # first time - build a new object
69
70 0 ▶ my $self = bless { _columns_hash => {} }, ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
71
72 0 $self->cdbi_class($cdbi_class);
73
74 0 $self->dbh( $cdbi_class->db_Main );
75
76 0 ▶ $self->catalog( $args{catalog} || undef );
Condition0%0/2
$args{'catalog'} || undef
l!l
00
MC/DC0%0/1
$args{'catalog'} || undef
$args{'catalog'}
Truth table0%0/2
A
A= $args{'catalog'}
Aresult
11
00
77
78 0 ▶ $self->schema( $args{schema} || undef );
Condition0%0/2
$args{'schema'} || undef
l!l
00
MC/DC0%0/1
$args{'schema'} || undef
$args{'schema'}
Truth table0%0/2
A
A= $args{'schema'}
Aresult
11
00
79
80 0 $self->_load_meta;
81
82 0 $Instances{$cdbi_class} = $self;
83
84 0 return $self;
85 }
86 }
87  
88 sub _load_meta
89 32 ▶{
Subroutine _load_meta: not called CC=4 SCAR=28.4
90 0 my ( $self ) = @_;
91
92 0 ▶ die "Meta already loaded" if keys %{ $self->_columns_hash };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->_columns_hash;} 0 0 0
93
94 0 my $dbh = $self->dbh;
95 0 my $table = $self->cdbi_class->table;
96
97 #$dbh->{FetchHashKeyName} = 'NAME_lc';
98
99 # undef does not constrain the data returned for that key
100 # I'm suspicious that setting catalog and schema to undef might break RDBMSs that actually
101 # do supply that information.
102
103 # '%' is a search pattern for columns - matches all columns
104 0 ▶ if ( my $sth = $dbh->column_info( $self->catalog, $self->schema, $table, '%' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = $dbh->column_info($self->catalog, $self->schema, $table, '%')) { } 0 0 0
105 {
106 0 ▶ $dbh->errstr && die "Error getting column info sth: " . $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->errstr and die 'Error getting column info sth: ' . $dbh->errstr 0 0 0
107 0 $self->_load_type_meta( $sth );
108 }
109 else
110 {
111 0 $self->_load_typeless_meta;
112 }
113 }
114  
115 # typeless db e.g. sqlite
116 sub _load_typeless_meta
117 0 ▶{
Subroutine _load_typeless_meta: not called CC=4 SCAR=30.0
118 0 my ( $self ) = @_;
119  
120 0 ▶ $self->cdbi_class->set_sql( fb_meta_dummy => 'SELECT * FROM __TABLE__ WHERE 1=0' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cdbi_class->can("sql_fb_meta_dummy") 0 0 0
121 unless $self->cdbi_class->can( 'sql_fb_meta_dummy' );
122  
123 0 my $sth = $self->cdbi_class->sql_fb_meta_dummy;
124
125 0 ▶ $sth->execute or die "Error executing column info: " . $sth->errstr;;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die 'Error executing column info: ' . $sth->errstr 0 0 0
126
127 # see 'Statement Handle Attributes' in the DBI docs for a list of available attributes
128 0 my $cols = $sth->{NAME};
129 0 my $types = $sth->{TYPE};
130 # my $sizes = $sth->{PRECISION}; # empty
131 # my $nulls = $sth->{NULLABLE}; # empty
132
133 # we haven't actually fetched anything from the sth, so need to tell DBI we're not going to
134 0 $sth->finish;
135
136 0 my $order = 0;
137
138 0 foreach my $col ( @$cols )
139 {
140 0 my $meta;
141
142 0 $meta->{nullable} = 1;
143 0 $meta->{is_nullable} = 'yes';
144
145 # in my limited testing, the columns are returned in the same order as they were defined in the schema
146 0 $meta->{ordinal_position} = $order++;
147
148 # type_name is taken literally from the schema, but is not actually used by sqlite,
149 # so it can be anything, e.g. varchar or varchar(xxx) or VARCHAR etc.
150 0 $meta->{type_name} = _fixup_type( shift( @$types ) );
151
152 0 $self->_add_column( $col, $meta );
153 }
154 }
155  
156 # $type may be something like varchar(255) from sqlite
157 sub _fixup_type
158 0 ▶{
Subroutine _fixup_type: not called CC=1 SCAR=6.9
159 0 my ( $type ) = @_;
160 0 $type =~ s/\(.+$//;
161 0 return $type;
162 }
163  
164 sub _load_type_meta
165 0 ▶{
Subroutine _load_type_meta: not called CC=5 SCAR=34.0
166 0 my ($self, $sth) = @_;
167
168 0 ▶ while ( my $row = $sth->fetchrow_hashref )
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
169 {
170 0 my ($meta, $col_name);
171
172 0 foreach my $key ( @{ Class::DBI::FormBuilder::Meta::Column->column_attributes } )
173 {
174 0 ▶ my $value = $row->{$key} || $row->{ uc $key };
Condition0%0/3
$row->{$key} || $$row{uc $key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{$key} || $$row{uc $key}
$row->{$key} $$row{uc $key}
Truth table0%0/3
A || B
A= $row->{$key}B= $$row{uc $key}
ABresult
1X1
011
000
175 0 $meta->{$key} = $value;
176 0 ▶ $col_name = $row->{COLUMN_NAME} || $row->{column_name};
Condition0%0/3
$row->{'COLUMN_NAME'} || $row->{'column_name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row->{'COLUMN_NAME'} || $row->{'column_name'}
$row->{'COLUMN_NAME'} $row->{'column_name'}
Truth table0%0/3
A || B
A= $row->{'COLUMN_NAME'}B= $row->{'column_name'}
ABresult
1X1
011
000
177 }
178
179 0 $self->_add_column($col_name, $meta);
180 }
181 }
182  
183 sub _add_column
184 0 ▶{
Subroutine _add_column: not called CC=1 SCAR=6.9
185 0 my ( $self, $name, $meta ) = @_;
186
187 0 $self->_columns_hash->{$name} = Class::DBI::FormBuilder::Meta::Column->new($self, $name, $meta);
188 }
189  
190 =item column_deep_type( $field )
191  
192 Returns the type of the field. If C<$field> refers to a relationship (e.g. C<has_many> or
193 C<might_have>), returns the type of the column in the related table.
194  
195 =cut
196  
197 # $col might be a related (has_many or might_have) accessor - i.e. it refers to a column in
198 # another table, in which case, the type of the column in that table is returned
199 sub column_deep_type
200 0 ▶{
Subroutine column_deep_type: not called CC=6 SCAR=37.4
Pod documented
201 0 my ($self, $field) = @_;
202
203 0 ▶ Carp::croak "Must supply a column name - got a ref - '$field' " . ref($field) if ref $field;
Branch: 1 branch
BranchTrueFalseTotal
if ref $field 0 0 0
204
205 0 my $them = $self->cdbi_class;
206
207 0 my $column = $self->column($field);
208
209 0 ▶ return $column->type if $column;
Branch: 1 branch
BranchTrueFalseTotal
if $column 0 0 0
210
211 # no such column - must be a related accessor
212
213 0 my ($other, $rel_type) = $self->related_class_and_rel_type($field);
214
215 0 ▶ Carp::croak "Non-existent column '$field' in '$them' is not related to anything" unless $other;
Branch: 1 branch
BranchTrueFalseTotal
unless $other 0 0 0
216
217 0 my $meta = $them->meta_info($rel_type, $field);
218
219 0 my $fk = $meta->{args}->{foreign_key};
220
221 0 my $other_meta = $self->instance($other);
222
223 0 ▶ my $type = $other_meta->column($fk)->type if $fk;
Branch: 1 branch
BranchTrueFalseTotal
if $fk 0 0 0
224  
225 0 ▶ die "No type detected for column '$field' in '$them' or column '$fk' in '$other'" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
226
227 0 return $type;
228 }
229  
230 =item related_class_and_rel_type( $field )
231  
232 =cut
233  
234 sub related_class_and_rel_type
235 0 ▶{
Subroutine related_class_and_rel_type: not called CC=4 SCAR=30.0
Pod documented
236 0 my ( $self, $field ) = @_;
237
238 0 my $them = $self->cdbi_class;
239
240 0 my @rel_types = keys %{ $them->meta_info };
241  
242 0 ▶ my $related_meta = List::Util::first { $_ } map { $them->meta_info( $_ => $field ) } @rel_types;
Subroutine __ANON__: not called CC=1 SCAR=6.9
243
244 0 ▶ return unless $related_meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $related_meta 0 0 0
245  
246 0 my $rel_type = $related_meta->name;
247
248 0 ▶ my $mapping = $related_meta->{args}->{mapping} || [];
Condition0%0/2
$related_meta->{'args'}{'mapping'} || []
l!l
00
MC/DC0%0/1
$related_meta->{'args'}{'mapping'} || []
$related_meta->{'args'}{'mapping'}
Truth table0%0/2
A
A= $related_meta->{'args'}{'mapping'}
Aresult
11
00
249
250 0 my $related_class;
251
252 0 ▶ if ( @$mapping )
Branch: 1 branch
BranchTrueFalseTotal
if (@$mapping) { } 0 0 0
253 {
254 #use Data::Dumper;
255 #my $foreign_meta = $related_meta->foreign_class->meta_info( 'has_a' );
256 #die Dumper( [ $mapping, $rel_type, $related_meta, $foreign_meta ] );
257 $related_class = $related_meta->foreign_class
258 ->meta_info( 'has_a' )
259 0 ->{ $$mapping[0] }
260 ->foreign_class;
261
262 #my $accessor = $related_meta->accessor;
263 #my $map = $$mapping[0];
264 }
265 else
266 {
267 0 $related_class = $related_meta->foreign_class;
268 }
269
270 0 return ($related_class, $rel_type);
271 }
272  
273 =item column( $col_name )
274  
275 If C<$col_name> is a column in this class, returns a L<Class::DBI::FormBuilder::Meta::Column>
276 object for that column. Otherwise, returns C<undef>.
277  
278 =cut
279  
280 # returns a CDBI::FB::Meta::Column object or undef - e.g. if asked for a has_many field
281 # note: column_deep_type relies on the undef for related columns
282 sub column
283 0 ▶{
Subroutine column: not called CC=2 SCAR=17.9
Pod documented
284 0 my ($self, $col_name) = @_;
285
286 0 my $h = $self->_columns_hash;
287
288 0 ▶ Carp::croak "meta not loaded" unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
289
290 0 return $h->{ $col_name };
291 }
292  
293 =item columns()
294  
295 Returns L<Class::DBI::Column> objects, in the same order as defined in the database.
296  
297 =back
298  
299 =cut
300  
301 sub columns
302 0 ▶{
Subroutine columns: not called CC=3 SCAR=24.8
Pod documented
303 0 my ( $self, $group ) = @_;
304  
305 0 ▶ $group ||= 'All';
Condition0%0/2
$group ||= "All"
l!l
00
MC/DC0%0/1
$group ||= "All"
$group
Truth table0%0/2
A
A= $group
Aresult
11
00
306  
307 0 my @columns = $self->cdbi_class->columns( $group );
308
309 0 my @ordered = map { $_->[0] }
310 0 sort { $a->[1] <=> $b->[1] }
311 0 ▶ grep { Carp::croak "Bad column " . $_->[0] . " has order: " . $_->[1] unless defined $_->[1]; $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_->[1] 0 0 0
312 0 map { [ $_, $self->column( $_->name )->order ] }
313 @columns;
314
315 0 return @ordered;
316 }
317  
318 32 1;
319  
320  
321 __END__
« blib/lib/Class/DBI/FormBuilder.pm ↑ Summary blib/lib/Class/DBI/FormBuilder/Meta/Column.pm »