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
|
17
|
|
|
17
|
|
7137
|
use warnings; |
|
17
|
|
|
|
|
57
|
|
|
17
|
|
|
|
|
512
|
|
8
|
17
|
|
|
17
|
|
140
|
no warnings 'uninitialized'; |
|
17
|
|
|
|
|
53
|
|
|
17
|
|
|
|
|
546
|
|
9
|
17
|
|
|
17
|
|
81
|
use strict; |
|
17
|
|
|
|
|
35
|
|
|
17
|
|
|
|
|
445
|
|
10
|
17
|
|
|
17
|
|
110
|
use mro 'c3'; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
151
|
|
11
|
17
|
|
|
17
|
|
581
|
use List::MoreUtils qw/firstval/; |
|
17
|
|
|
|
|
46
|
|
|
17
|
|
|
|
|
148
|
|
12
|
17
|
|
|
17
|
|
10955
|
use Module::Load qw/load/; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
271
|
|
13
|
17
|
|
|
17
|
|
1167
|
use Scalar::Util qw/refaddr/; |
|
17
|
|
|
|
|
77
|
|
|
17
|
|
|
|
|
1032
|
|
14
|
17
|
|
|
17
|
|
9272
|
use Storable qw/freeze/; |
|
17
|
|
|
|
|
43096
|
|
|
17
|
|
|
|
|
1039
|
|
15
|
17
|
|
|
17
|
|
127
|
use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)]; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
192
|
|
16
|
17
|
|
|
17
|
|
1402
|
use DBIx::DataModel::Meta::Utils qw/does/; |
|
17
|
|
|
|
|
42
|
|
|
17
|
|
|
|
|
611
|
|
17
|
|
|
|
|
|
|
|
18
|
17
|
|
|
17
|
|
88
|
use namespace::clean; |
|
17
|
|
|
|
|
42
|
|
|
17
|
|
|
|
|
151
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
23
|
|
|
|
|
|
|
# accessors |
24
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub schema { |
27
|
1846
|
|
|
1846
|
1
|
2372
|
my $self = shift; |
28
|
|
|
|
|
|
|
return (ref $self && $self->{__schema}) |
29
|
1846
|
|
66
|
|
|
7045
|
|| $self->metadm->schema->class->singleton; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub primary_key { |
34
|
34
|
|
|
34
|
1
|
1310
|
my $self = shift; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# get primary key columns |
37
|
34
|
|
|
|
|
76
|
my @primary_key = $self->metadm->primary_key; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# if called as instance method, get values in those columns |
40
|
34
|
50
|
|
|
|
118
|
@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
|
|
|
|
|
100
|
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
|
17
|
|
|
17
|
|
7496
|
no strict 'refs'; |
|
17
|
|
|
|
|
37
|
|
|
17
|
|
|
|
|
3221
|
|
60
|
|
|
|
|
|
|
*{$method} = sub { |
61
|
139
|
|
|
139
|
|
95636
|
my $self = shift; |
62
|
|
|
|
|
|
|
|
63
|
139
|
50
|
|
|
|
407
|
$self->_is_called_as_class_method |
64
|
|
|
|
|
|
|
or croak "$method() should be called as a class method"; |
65
|
|
|
|
|
|
|
|
66
|
139
|
|
|
|
|
364
|
my $stmt_class = $self->metadm->schema->statement_class; |
67
|
139
|
|
|
|
|
414
|
load $stmt_class; |
68
|
139
|
|
|
|
|
7450
|
my $statement = $stmt_class->new($self); |
69
|
139
|
|
|
|
|
436
|
return $statement->$method(@_); |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub fetch { |
75
|
10
|
|
|
10
|
0
|
16791
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
10
|
50
|
|
|
|
28
|
$self->_is_called_as_class_method |
78
|
|
|
|
|
|
|
or croak "fetch() should be called as a class method"; |
79
|
|
|
|
|
|
|
|
80
|
10
|
|
|
|
|
18
|
my %select_args; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# if last argument is a hashref, it contains arguments to the select() call |
83
|
17
|
|
|
17
|
|
108
|
no warnings 'uninitialized'; |
|
17
|
|
|
|
|
30
|
|
|
17
|
|
|
|
|
13590
|
|
84
|
10
|
100
|
|
|
|
31
|
if (does $_[-1], 'HASH') { |
85
|
2
|
|
|
|
|
21
|
%select_args = %{pop @_}; |
|
2
|
|
|
|
|
11
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
10
|
|
|
|
|
84
|
return $self->select(-fetch => \@_, %select_args); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub fetch_cached { |
93
|
4
|
|
|
4
|
0
|
2005
|
my $self = shift; |
94
|
4
|
|
|
|
|
12
|
my $dbh_addr = refaddr $self->schema->dbh; |
95
|
4
|
|
|
|
|
21
|
my $freeze_args = freeze \@_; |
96
|
4
|
|
66
|
|
|
293
|
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
|
35
|
|
|
35
|
1
|
45513
|
my ($self, $first_role, @other_roles) = @_; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# direct references to utility objects |
111
|
35
|
|
|
|
|
81
|
my $schema = $self->schema; |
112
|
35
|
|
|
|
|
85
|
my $meta_schema = $schema->metadm; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# find first join information |
115
|
35
|
100
|
|
|
|
69
|
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
|
33
|
|
|
|
|
53
|
my (%criteria, @left_cols); |
120
|
33
|
|
|
|
|
80
|
my $prefix = $schema->placeholder_prefix; |
121
|
33
|
|
|
|
|
61
|
while (my ($left_col, $right_col) = each %{$path->{on}}) { |
|
66
|
|
|
|
|
197
|
|
122
|
33
|
|
|
|
|
82
|
$criteria{$right_col} = "$prefix$left_col"; |
123
|
33
|
|
|
|
|
67
|
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
|
33
|
100
|
|
|
|
132
|
: $path->{to}; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# build args for the statement |
132
|
31
|
|
|
|
|
114
|
my $source = bless {__schema => $schema}, $meta_source->class; |
133
|
31
|
|
|
|
|
99
|
my @stmt_args = ($source, -where => \%criteria); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# keep a reference to @left_cols so that Source::join can bind them |
136
|
31
|
|
|
|
|
69
|
push @stmt_args, -_left_cols => \@left_cols; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# TODO: should add -select_as => 'firstrow' if all multiplicities are 1 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# build and return the new statement |
141
|
31
|
|
|
|
|
83
|
my $statement = $meta_schema->statement_class->new(@stmt_args); |
142
|
|
|
|
|
|
|
|
143
|
31
|
100
|
|
|
|
98
|
if (!$self->_is_called_as_class_method) { # called as instance method |
144
|
|
|
|
|
|
|
my $left_cols = $statement->{args}{-_left_cols} |
145
|
23
|
50
|
|
|
|
53
|
or die "statement had no {left_cols} entry"; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# check that all foreign keys are present |
148
|
23
|
|
|
|
|
54
|
my $missing = join ", ", grep {not exists $self->{$_}} @$left_cols; |
|
23
|
|
|
|
|
71
|
|
149
|
23
|
100
|
|
|
|
65
|
not $missing |
150
|
|
|
|
|
|
|
or croak "cannot follow role '$first_role': missing column '$missing'"; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# bind to foreign keys |
153
|
21
|
|
|
|
|
39
|
$statement->bind(map {($_ => $self->{$_})} @$left_cols); |
|
21
|
|
|
|
|
92
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
29
|
|
|
|
|
155
|
return $statement; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
162
|
|
|
|
|
|
|
# column handlers and column expansion |
163
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub expand { |
167
|
0
|
|
|
0
|
1
|
0
|
my ($self, $path, @options) = @_; |
168
|
0
|
|
|
|
|
0
|
$self->{$path} = $self->$path(@options); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
0
|
0
|
|
sub auto_expand {} # overridden in subclasses through define_auto_expand() |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub apply_column_handler { |
175
|
63
|
|
|
63
|
0
|
126
|
my ($self, $handler_name, $objects) = @_; |
176
|
|
|
|
|
|
|
|
177
|
63
|
|
50
|
|
|
197
|
my $targets = $objects || [$self]; |
178
|
63
|
|
|
|
|
150
|
my %column_handlers = $self->metadm->_consolidate_hash('column_handlers'); |
179
|
63
|
|
|
|
|
131
|
my $results = {}; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# iterate over all registered columnHandlers |
182
|
|
|
|
|
|
|
COLUMN: |
183
|
63
|
|
|
|
|
167
|
while (my ($column_name, $handlers) = each %column_handlers) { |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# is $handler_name registered in this column ? |
186
|
142
|
100
|
|
|
|
325
|
my $handler = $handlers->{$handler_name} or next COLUMN; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# apply that handler to all targets that possess the $column_name |
189
|
105
|
|
|
|
|
159
|
foreach my $obj (@$targets) { |
190
|
|
|
|
|
|
|
my $result = exists $obj->{$column_name} |
191
|
105
|
100
|
|
|
|
219
|
? $handler->($obj->{$column_name}, $obj, $column_name, $handler_name) |
192
|
|
|
|
|
|
|
: undef; |
193
|
105
|
50
|
|
|
|
419
|
if ($objects) { push(@{$results->{$column_name}}, $result); } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
194
|
105
|
|
|
|
|
299
|
else { $results->{$column_name} = $result; } |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
63
|
|
|
|
|
176
|
return $results; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
203
|
|
|
|
|
|
|
# utilities |
204
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub _is_called_as_class_method { |
208
|
292
|
|
|
292
|
|
471
|
my $self = shift; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# class method call in the usual Perl sense |
211
|
292
|
100
|
|
|
|
755
|
return 1 if ! ref $self; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
# fake class method call : an object with only one field '__schema' |
214
|
141
|
|
|
|
|
479
|
my @k = keys %$self; |
215
|
141
|
|
100
|
|
|
711
|
return @k == 1 && $k[0] eq '__schema'; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub TO_JSON { |
220
|
3
|
|
|
3
|
0
|
7
|
my $self = shift; |
221
|
3
|
|
|
|
|
9
|
my $clone = {%$self}; |
222
|
3
|
|
|
|
|
4
|
delete $clone->{__schema}; |
223
|
3
|
|
|
|
|
17
|
return $clone; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
1; # End of DBIx::DataModel::Source |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
__END__ |