File Coverage

blib/lib/DBIx/DataModel/Source.pm
Criterion Covered Total %
statement 103 110 93.6
branch 21 28 75.0
condition 8 11 72.7
subroutine 22 24 91.6
pod 4 9 44.4
total 158 182 86.8


line stmt bran cond sub pod time code
1             #----------------------------------------------------------------------
2             package DBIx::DataModel::Source;
3             #----------------------------------------------------------------------
4              
5             # see POD doc at end of file
6              
7 20     20   12149 use warnings;
  20         74  
  20         1183  
8 20     20   162 no warnings 'uninitialized';
  20         33  
  20         805  
9 20     20   104 use strict;
  20         33  
  20         570  
10 20     20   95 use mro 'c3';
  20         45  
  20         298  
11 20     20   825 use List::MoreUtils qw/firstval/;
  20         35  
  20         401  
12 20     20   16514 use Module::Load qw/load/;
  20         54  
  20         229  
13 20     20   1628 use Scalar::Util qw/refaddr/;
  20         41  
  20         1295  
14 20     20   7642 use Storable qw/freeze/;
  20         52180  
  20         1591  
15 20     20   132 use DBIx::DataModel::Carp;
  20         36  
  20         185  
16 20     20   1181 use DBIx::DataModel::Meta::Utils qw/does/;
  20         37  
  20         1039  
17              
18 20     20   103 use namespace::clean;
  20         32  
  20         193  
19              
20              
21              
22             #----------------------------------------------------------------------
23             # accessors
24             #----------------------------------------------------------------------
25              
26             sub schema {
27 2235     2235 1 3064 my $self = shift;
28             return (ref $self && $self->{__schema})
29 2235   66     10608 || $self->metadm->schema->class->singleton;
30             }
31              
32              
33             sub primary_key {
34 34     34 1 4503 my $self = shift;
35              
36             # get primary key columns
37 34         103 my @primary_key = $self->metadm->primary_key;
38              
39             # if called as instance method, get values in those columns
40 34 50       111 @primary_key = @{$self}{@primary_key} if !$self->_is_called_as_class_method;
  0         0  
41              
42             # choose what to return depending on context
43 34 50       81 if (wantarray) {
44 34         103 return @primary_key;
45             }
46             else {
47 0 0       0 @primary_key == 1
48             or croak "cannot return a multi-column primary key in a scalar context";
49 0         0 return $primary_key[0];
50             }
51             }
52              
53             #----------------------------------------------------------------------
54             # select and fetch
55             #----------------------------------------------------------------------
56              
57             # methods delegated to the Statement class
58             foreach my $method (qw/select bless_from_DB/) {
59 20     20   10224 no strict 'refs';
  20         59  
  20         4355  
60             *{$method} = sub {
61 159     159   106574 my $self = shift;
62              
63 159 50       726 $self->_is_called_as_class_method
64             or croak "$method() should be called as a class method";
65              
66 159         663 my $stmt_class = $self->metadm->schema->statement_class;
67 159         785 load $stmt_class;
68 159         12645 my $statement = $stmt_class->new($self);
69 159         693 return $statement->$method(@_);
70             };
71             }
72              
73              
74             sub fetch {
75 10     10 0 20608 my $self = shift;
76              
77 10 50       35 $self->_is_called_as_class_method
78             or croak "fetch() should be called as a class method";
79              
80 10         20 my %select_args;
81              
82             # if last argument is a hashref, it contains arguments to the select() call
83 20     20   172 no warnings 'uninitialized';
  20         72  
  20         19563  
84 10 100       42 if (does $_[-1], 'HASH') {
85 2         22 %select_args = %{pop @_};
  2         11  
86             }
87              
88 10         95 return $self->select(-fetch => \@_, %select_args);
89             }
90              
91              
92             sub fetch_cached {
93 4     4 0 4909 my $self = shift;
94 4         13 my $dbh_addr = refaddr $self->schema->dbh;
95 4         20 my $freeze_args = freeze \@_;
96 4   66     379 return $self->metadm->{fetch_cached}{$dbh_addr}{$freeze_args}
97             ||= $self->fetch(@_);
98             }
99              
100              
101              
102             #----------------------------------------------------------------------
103             # join
104             #----------------------------------------------------------------------
105              
106              
107             sub join {
108 37     37 1 70185 my ($self, $first_role, @other_roles) = @_;
109              
110             # direct references to utility objects
111 37         145 my $schema = $self->schema;
112 37         107 my $meta_schema = $schema->metadm;
113              
114             # find first join information
115 37 100       172 my $path = $self->metadm->path($first_role)
116             or croak "could not find role $first_role in " . $self->metadm->class;
117              
118             # build search criteria on %$self from first join information
119 35         77 my (%criteria, @left_cols);
120 35         159 my $prefix = $schema->placeholder_prefix;
121 35         72 while (my ($left_col, $right_col) = each %{$path->{on}}) {
  70         285  
122 35         118 $criteria{$right_col} = "$prefix$left_col";
123 35         94 push @left_cols, $left_col;
124             }
125              
126             # choose meta_source (just a table or build a join)
127             my $meta_source = @other_roles ? $meta_schema->define_join($path->{to}{name},
128             @other_roles)
129 35 100       182 : $path->{to};
130              
131             # build args for the statement
132 33         278 my $source = bless {__schema => $schema}, $meta_source->class;
133 33         102 my @stmt_args = ($source, -where => \%criteria);
134              
135             # TODO: should add -select_as => 'firstrow' if all multiplicities are 1
136              
137             # build and return the new statement
138 33         145 my $statement = $meta_schema->statement_class->new(@stmt_args);
139              
140 33 100       151 if (!$self->_is_called_as_class_method) { # called as instance method
141             # check that all foreign keys are present
142 24         57 my $missing = join ", ", grep {not exists $self->{$_}} @left_cols;
  24         92  
143 24 100       77 not $missing
144             or croak "cannot follow role '$first_role': missing column '$missing'";
145              
146             # bind to foreign keys
147 22         40 $statement->bind(map {($_ => $self->{$_})} @left_cols);
  22         94  
148             }
149              
150 31         232 return $statement;
151             }
152              
153              
154             #----------------------------------------------------------------------
155             # column handlers and column expansion
156             #----------------------------------------------------------------------
157              
158              
159             sub expand {
160 0     0 1 0 my ($self, $path, @options) = @_;
161 0         0 $self->{$path} = $self->$path(@options);
162             }
163              
164       0 0   sub auto_expand {} # overridden in subclasses through define_auto_expand()
165              
166              
167             sub apply_column_handler {
168 63     63 0 167 my ($self, $handler_name, $objects) = @_;
169              
170 63   50     299 my $targets = $objects || [$self];
171 63         196 my %column_handlers = $self->metadm->_consolidate_hash('column_handlers');
172 63         142 my $results = {};
173              
174             # iterate over all registered columnHandlers
175             COLUMN:
176 63         182 while (my ($column_name, $handlers) = each %column_handlers) {
177              
178             # is $handler_name registered in this column ?
179 142 100       397 my $handler = $handlers->{$handler_name} or next COLUMN;
180              
181             # apply that handler to all targets that possess the $column_name
182 105         155 foreach my $obj (@$targets) {
183             my $result = exists $obj->{$column_name}
184 105 100       255 ? $handler->($obj->{$column_name}, $obj, $column_name, $handler_name)
185             : undef;
186 105 50       483 if ($objects) { push(@{$results->{$column_name}}, $result); }
  0         0  
  0         0  
187 105         345 else { $results->{$column_name} = $result; }
188             }
189             }
190              
191 63         256 return $results;
192             }
193              
194              
195             #----------------------------------------------------------------------
196             # utilities
197             #----------------------------------------------------------------------
198              
199              
200             sub _is_called_as_class_method {
201 314     314   571 my $self = shift;
202              
203             # class method call in the usual Perl sense
204 314 100       1222 return 1 if ! ref $self;
205              
206             # fake class method call : an object with only one field '__schema'
207 161         630 my @k = keys %$self;
208 161   100     1175 return @k == 1 && $k[0] eq '__schema';
209             }
210              
211              
212             sub TO_JSON {
213 3     3 0 8 my $self = shift;
214 3         10 my $clone = {%$self};
215 3         7 delete $clone->{__schema};
216 3         75 return $clone;
217             }
218              
219              
220             1; # End of DBIx::DataModel::Source
221              
222             __END__