File Coverage

blib/lib/DBR/Config/Table.pm
Criterion Covered Total %
statement 105 114 92.1
branch 33 64 51.5
condition 4 12 33.3
subroutine 18 21 85.7
pod 0 14 0.0
total 160 225 71.1


line stmt bran cond sub pod time code
1             # the contents of this file are Copyright (c) 2009 Daniel Norman
2             # This program is free software; you can redistribute it and/or
3             # modify it under the terms of the GNU General Public License as
4             # published by the Free Software Foundation.
5              
6             package DBR::Config::Table;
7              
8 18     18   98 use strict;
  18         108  
  18         987  
9 18     18   98 use base 'DBR::Config::Table::Common';
  18         39  
  18         1893  
10              
11 18     18   22108 use DBR::Config::Field;
  18         67  
  18         1247  
12 18     18   12278 use DBR::Config::Relation;
  18         58  
  18         591  
13 18     18   187 use Carp;
  18         42  
  18         40111  
14              
15             my %TABLES_BY_ID;
16             my %FIELDS_BY_NAME;
17             my %RELATIONS_BY_NAME;
18             my %PK_FIELDS;
19             my %REQ_FIELDS;
20              
21             sub load{
22 51     51 0 129 my( $package ) = shift;
23 51         223 my %params = @_;
24              
25 51         199 my $self = { session => $params{session} };
26 51         186 bless( $self, $package ); # Dummy object
27              
28 51   50     221 my $instance = $params{instance} || return $self->_error('instance is required');
29              
30 51   50     193 my $schema_ids = $params{schema_id} || return $self->_error('schema_id is required');
31 51 50       304 $schema_ids = [$schema_ids] unless ref($schema_ids) eq 'ARRAY';
32              
33 51 50       166 return 1 unless @$schema_ids;
34              
35 51   50     232 my $dbrh = $instance->connect || return $self->_error("Failed to connect to @{[$instance->name]}");
36              
37 51         369 return $self->_error('Failed to select instances') unless
38             my $tables = $dbrh->select(
39             -table => 'dbr_tables',
40             -fields => 'table_id schema_id name',
41 51 50       496 -where => { schema_id => ['d in', @{$schema_ids}] },
42             );
43              
44 51         242 my @table_ids;
45 51         191 foreach my $table (@$tables){
46 82 50       1365 DBR::Config::Schema->_register_table(
47             schema_id => $table->{schema_id},
48             name => $table->{name},
49             table_id => $table->{table_id},
50             ) or return $self->_error('failed to register table');
51              
52 82         416 $table->{conf_instance_guid} = $instance->guid;
53              
54 82         423 $TABLES_BY_ID{ $table->{table_id} } = $table;
55 82         496 push @table_ids, $table->{table_id};
56              
57             #Purge in case this is a reload
58 82         217 $FIELDS_BY_NAME{ $table->{table_id} } = {};
59 82         476 $PK_FIELDS{ $table->{table_id} } = [];
60 82         617 $REQ_FIELDS{ $table->{table_id} } = [];
61             }
62              
63 51 100       463 if(@table_ids){
64 34 50       574 DBR::Config::Field->load(
65             session => $self->{session},
66             instance => $instance,
67             table_id => \@table_ids,
68             ) or return $self->_error('failed to load fields');
69              
70 34 50       425 DBR::Config::Relation->load(
71             session => $self->{session},
72             instance => $instance,
73             table_id => \@table_ids,
74             ) or return $self->_error('failed to load relationships');
75             }
76              
77 51         341 return 1;
78             }
79              
80             sub _register_field{
81 344     344   808 my $package = shift; # no dummy $self object here, for efficiency
82 344         7488 my %params = @_;
83              
84 344 50       999 my $table_id = $params{table_id} or croak('table_id is required');
85 344 50       982 $TABLES_BY_ID{ $table_id } or croak('invalid table_id');
86              
87 344 50       1128 my $name = $params{name} or croak('name is required');
88 344 50       1143 my $field_id = $params{field_id} or croak('field_id is required');
89 344 50       838 defined($params{is_pkey}) or croak('is_pkey is required');
90              
91 344         888 $FIELDS_BY_NAME{ $table_id } -> { $name } = $field_id;
92              
93 344 100       763 if( $params{is_pkey} ){ push @{$PK_FIELDS{ $table_id }}, $field_id }
  82         119  
  82         238  
94 344 100       856 if( $params{is_req} ){ push @{$REQ_FIELDS{ $table_id }}, $field_id }
  90         118  
  90         210  
95              
96 344         1933 return 1;
97             }
98              
99             sub _register_relation{
100 48     48   100 my $package = shift; # no dummy $self object here, for efficiency
101 48         200 my %params = @_;
102              
103 48 50       163 my $table_id = $params{table_id} or croak ('table_id is required');
104 48 50       233 $TABLES_BY_ID{ $table_id } or croak('invalid table_id');
105              
106 48 50       186 my $name = $params{name} or croak('name is required');
107 48 50       856 my $relation_id = $params{relation_id} or croak('relation_id is required');
108              
109 48         178 $RELATIONS_BY_NAME{ $table_id } -> { $name } = $relation_id;
110              
111             return {
112 48         402 %{ $TABLES_BY_ID{ $table_id } }
  48         495  
113             }; # shallow clone
114             }
115              
116             sub new {
117 364     364 0 787 my( $package ) = shift;
118 364         1437 my %params = @_;
119 364         1450 my $self = {
120             session => $params{session},
121             table_id => $params{table_id}
122             };
123              
124 364         1396 bless( $self, $package );
125              
126 364 50       1123 return $self->_error('table_id is required') unless $self->{table_id};
127 364 50       1240 return $self->_error('session is required' ) unless $self->{session};
128              
129 364 50       1600 $TABLES_BY_ID{ $self->{table_id} } or return $self->_error("table_id $self->{table_id} doesn't exist");
130              
131 364         16763 return( $self );
132             }
133              
134             sub clone{
135 0     0 0 0 my $self = shift;
136 0         0 my %params = @_;
137              
138 0 0       0 return bless({
139             session => $self->{session},
140             table_id => $self->{table_id},
141             $params{with_alias} ? ( alias => $self->{alias} ) : (),
142             },
143             ref($self)
144             );
145              
146             }
147              
148              
149 72     72 0 925 sub table_id { $_[0]->{table_id} }
150             sub get_field{
151 284     284 0 666 my $self = shift;
152 284 50       1167 my $name = shift or return $self->_error('name is required');
153              
154 284   50     1542 my $field_id = $FIELDS_BY_NAME{ $self->{table_id} } -> { $name } || return $self->_error("field $name does not exist");
155              
156 284 50       1809 my $field = DBR::Config::Field->new(
157             session => $self->{session},
158             field_id => $field_id,
159             ) or return $self->_error('failed to create table object');
160 284         1250 return $field;
161             }
162              
163             sub fields{
164 27     27 0 64 my $self = shift;
165             [
166 103 50       399 map {
167 27         253 DBR::Config::Field->new(session => $self->{session}, field_id => $_ ) or return $self->_error('failed to create field object')
168 27         55 } values %{$FIELDS_BY_NAME{$self->{table_id}}}
169             ];
170             }
171              
172             sub req_fields{
173 3     3 0 8 my $self = shift;
174             [
175 4 50       16 map {
176 3         14 DBR::Config::Field->new(session => $self->{session}, field_id => $_ ) or return $self->_error('failed to create field object')
177 3         7 } @{ $REQ_FIELDS{ $self->{table_id} } }
178             ];
179              
180             }
181             sub primary_key{
182 37     37 0 100 my $self = shift;
183             [
184 37 50       410 map {
185 37         432 DBR::Config::Field->new(session => $self->{session}, field_id => $_ ) or return $self->_error('failed to create field object')
186 37         87 } @{ $PK_FIELDS{ $self->{table_id} } }
187             ];
188              
189             }
190             sub get_relation{
191 9     9 0 18 my $self = shift;
192 9 50       33 my $name = shift or return $self->_error('name is required');
193              
194 9 50       49 my $relation_id = $RELATIONS_BY_NAME{ $self->{table_id} } -> { $name } or return $self->_error("relationship $name does not exist");
195              
196              
197 9 50       234 my $relation = DBR::Config::Relation->new(
198             session => $self->{session},
199             relation_id => $relation_id,
200             table_id => $self->{table_id},
201             ) or return $self->_error('failed to create relation object');
202              
203 9         116 return $relation;
204             }
205              
206             sub relations{
207 27     27 0 65 my $self = shift;
208              
209 27         53 my @relations;
210              
211 27         50 foreach my $relation_id ( values %{$RELATIONS_BY_NAME{$self->{table_id}}} ) {
  27         173  
212              
213 35 50       441 my $relation = DBR::Config::Relation->new(
214             session => $self->{session},
215             relation_id => $relation_id,
216             table_id => $self->{table_id},
217             ) or return $self->_error('failed to create relation object');
218 35         131 push @relations, $relation;
219             }
220              
221              
222 27         152 return \@relations;
223             }
224              
225              
226 267     267 0 2028 sub name { $TABLES_BY_ID{ $_[0]->{table_id} }->{name} };
227 0     0 0 0 sub schema_id { $TABLES_BY_ID{ $_[0]->{table_id} }->{schema_id} };
228              
229             sub schema{
230 0     0 0 0 my $self = shift;
231 0         0 my %params = @_;
232              
233 0   0     0 my $schema_id = $self->schema_id || return ''; # No schemas here
234              
235 0   0     0 my $schema = DBR::Config::Schema->new(
236             session => $self->{session},
237             schema_id => $schema_id,
238             ) || return $self->_error("failed to fetch schema object for schema_id $schema_id");
239              
240 0         0 return $schema;
241             }
242              
243              
244             sub conf_instance {
245 83     83 0 204 my $self = shift;
246              
247              
248 83         340 my $guid = $TABLES_BY_ID{ $self->{table_id} }->{conf_instance_guid};
249              
250 83 0       688 return DBR::Config::Instance->lookup(
251             session => $self->{session},
252             guid => $guid
253             ) or return $self->_error('Failed to fetch conf instance');
254             }
255              
256              
257             1;