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
|
18
|
|
|
18
|
|
9690
|
use warnings; |
|
18
|
|
|
|
|
41
|
|
|
18
|
|
|
|
|
623
|
|
8
|
18
|
|
|
18
|
|
98
|
no warnings 'uninitialized'; |
|
18
|
|
|
|
|
39
|
|
|
18
|
|
|
|
|
542
|
|
9
|
18
|
|
|
18
|
|
95
|
use strict; |
|
18
|
|
|
|
|
35
|
|
|
18
|
|
|
|
|
461
|
|
10
|
18
|
|
|
18
|
|
108
|
use mro 'c3'; |
|
18
|
|
|
|
|
37
|
|
|
18
|
|
|
|
|
153
|
|
11
|
18
|
|
|
18
|
|
791
|
use List::MoreUtils qw/firstval/; |
|
18
|
|
|
|
|
43
|
|
|
18
|
|
|
|
|
163
|
|
12
|
18
|
|
|
18
|
|
13330
|
use Module::Load qw/load/; |
|
18
|
|
|
|
|
37
|
|
|
18
|
|
|
|
|
202
|
|
13
|
18
|
|
|
18
|
|
1296
|
use Scalar::Util qw/refaddr/; |
|
18
|
|
|
|
|
60
|
|
|
18
|
|
|
|
|
1125
|
|
14
|
18
|
|
|
18
|
|
10622
|
use Storable qw/freeze/; |
|
18
|
|
|
|
|
54568
|
|
|
18
|
|
|
|
|
1272
|
|
15
|
18
|
|
|
18
|
|
142
|
use Carp::Clan qw[^(DBIx::DataModel::|SQL::Abstract)]; |
|
18
|
|
|
|
|
48
|
|
|
18
|
|
|
|
|
199
|
|
16
|
18
|
|
|
18
|
|
1715
|
use DBIx::DataModel::Meta::Utils qw/does/; |
|
18
|
|
|
|
|
45
|
|
|
18
|
|
|
|
|
803
|
|
17
|
|
|
|
|
|
|
|
18
|
18
|
|
|
18
|
|
130
|
use namespace::clean; |
|
18
|
|
|
|
|
37
|
|
|
18
|
|
|
|
|
164
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
23
|
|
|
|
|
|
|
# accessors |
24
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub schema { |
27
|
1846
|
|
|
1846
|
1
|
3158
|
my $self = shift; |
28
|
|
|
|
|
|
|
return (ref $self && $self->{__schema}) |
29
|
1846
|
|
66
|
|
|
9060
|
|| $self->metadm->schema->class->singleton; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub primary_key { |
34
|
34
|
|
|
34
|
1
|
1814
|
my $self = shift; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# get primary key columns |
37
|
34
|
|
|
|
|
105
|
my @primary_key = $self->metadm->primary_key; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# if called as instance method, get values in those columns |
40
|
34
|
50
|
|
|
|
140
|
@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
|
|
|
|
86
|
if (wantarray) { |
44
|
34
|
|
|
|
|
113
|
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
|
18
|
|
|
18
|
|
9383
|
no strict 'refs'; |
|
18
|
|
|
|
|
66
|
|
|
18
|
|
|
|
|
3555
|
|
60
|
|
|
|
|
|
|
*{$method} = sub { |
61
|
139
|
|
|
139
|
|
115362
|
my $self = shift; |
62
|
|
|
|
|
|
|
|
63
|
139
|
50
|
|
|
|
561
|
$self->_is_called_as_class_method |
64
|
|
|
|
|
|
|
or croak "$method() should be called as a class method"; |
65
|
|
|
|
|
|
|
|
66
|
139
|
|
|
|
|
490
|
my $stmt_class = $self->metadm->schema->statement_class; |
67
|
139
|
|
|
|
|
571
|
load $stmt_class; |
68
|
139
|
|
|
|
|
9547
|
my $statement = $stmt_class->new($self); |
69
|
139
|
|
|
|
|
585
|
return $statement->$method(@_); |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub fetch { |
75
|
10
|
|
|
10
|
0
|
20396
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
10
|
50
|
|
|
|
33
|
$self->_is_called_as_class_method |
78
|
|
|
|
|
|
|
or croak "fetch() should be called as a class method"; |
79
|
|
|
|
|
|
|
|
80
|
10
|
|
|
|
|
24
|
my %select_args; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# if last argument is a hashref, it contains arguments to the select() call |
83
|
18
|
|
|
18
|
|
141
|
no warnings 'uninitialized'; |
|
18
|
|
|
|
|
45
|
|
|
18
|
|
|
|
|
17127
|
|
84
|
10
|
100
|
|
|
|
39
|
if (does $_[-1], 'HASH') { |
85
|
2
|
|
|
|
|
25
|
%select_args = %{pop @_}; |
|
2
|
|
|
|
|
16
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
10
|
|
|
|
|
115
|
return $self->select(-fetch => \@_, %select_args); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub fetch_cached { |
93
|
4
|
|
|
4
|
0
|
2575
|
my $self = shift; |
94
|
4
|
|
|
|
|
15
|
my $dbh_addr = refaddr $self->schema->dbh; |
95
|
4
|
|
|
|
|
25
|
my $freeze_args = freeze \@_; |
96
|
4
|
|
66
|
|
|
400
|
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
|
56574
|
my ($self, $first_role, @other_roles) = @_; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# direct references to utility objects |
111
|
35
|
|
|
|
|
113
|
my $schema = $self->schema; |
112
|
35
|
|
|
|
|
106
|
my $meta_schema = $schema->metadm; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# find first join information |
115
|
35
|
100
|
|
|
|
90
|
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
|
|
|
|
|
72
|
my (%criteria, @left_cols); |
120
|
33
|
|
|
|
|
114
|
my $prefix = $schema->placeholder_prefix; |
121
|
33
|
|
|
|
|
69
|
while (my ($left_col, $right_col) = each %{$path->{on}}) { |
|
66
|
|
|
|
|
269
|
|
122
|
33
|
|
|
|
|
114
|
$criteria{$right_col} = "$prefix$left_col"; |
123
|
33
|
|
|
|
|
93
|
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
|
|
|
|
145
|
: $path->{to}; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# build args for the statement |
132
|
31
|
|
|
|
|
146
|
my $source = bless {__schema => $schema}, $meta_source->class; |
133
|
31
|
|
|
|
|
131
|
my @stmt_args = ($source, -where => \%criteria); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# keep a reference to @left_cols so that Source::join can bind them |
136
|
31
|
|
|
|
|
79
|
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
|
|
|
|
|
102
|
my $statement = $meta_schema->statement_class->new(@stmt_args); |
142
|
|
|
|
|
|
|
|
143
|
31
|
100
|
|
|
|
121
|
if (!$self->_is_called_as_class_method) { # called as instance method |
144
|
|
|
|
|
|
|
my $left_cols = $statement->{args}{-_left_cols} |
145
|
23
|
50
|
|
|
|
88
|
or die "statement had no {left_cols} entry"; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# check that all foreign keys are present |
148
|
23
|
|
|
|
|
78
|
my $missing = join ", ", grep {not exists $self->{$_}} @$left_cols; |
|
23
|
|
|
|
|
101
|
|
149
|
23
|
100
|
|
|
|
78
|
not $missing |
150
|
|
|
|
|
|
|
or croak "cannot follow role '$first_role': missing column '$missing'"; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# bind to foreign keys |
153
|
21
|
|
|
|
|
52
|
$statement->bind(map {($_ => $self->{$_})} @$left_cols); |
|
21
|
|
|
|
|
89
|
|
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
29
|
|
|
|
|
187
|
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
|
159
|
my ($self, $handler_name, $objects) = @_; |
176
|
|
|
|
|
|
|
|
177
|
63
|
|
50
|
|
|
274
|
my $targets = $objects || [$self]; |
178
|
63
|
|
|
|
|
174
|
my %column_handlers = $self->metadm->_consolidate_hash('column_handlers'); |
179
|
63
|
|
|
|
|
170
|
my $results = {}; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# iterate over all registered columnHandlers |
182
|
|
|
|
|
|
|
COLUMN: |
183
|
63
|
|
|
|
|
204
|
while (my ($column_name, $handlers) = each %column_handlers) { |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# is $handler_name registered in this column ? |
186
|
142
|
100
|
|
|
|
411
|
my $handler = $handlers->{$handler_name} or next COLUMN; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# apply that handler to all targets that possess the $column_name |
189
|
105
|
|
|
|
|
211
|
foreach my $obj (@$targets) { |
190
|
|
|
|
|
|
|
my $result = exists $obj->{$column_name} |
191
|
105
|
100
|
|
|
|
306
|
? $handler->($obj->{$column_name}, $obj, $column_name, $handler_name) |
192
|
|
|
|
|
|
|
: undef; |
193
|
105
|
50
|
|
|
|
500
|
if ($objects) { push(@{$results->{$column_name}}, $result); } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
194
|
105
|
|
|
|
|
388
|
else { $results->{$column_name} = $result; } |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
63
|
|
|
|
|
212
|
return $results; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
203
|
|
|
|
|
|
|
# utilities |
204
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub _is_called_as_class_method { |
208
|
292
|
|
|
292
|
|
547
|
my $self = shift; |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# class method call in the usual Perl sense |
211
|
292
|
100
|
|
|
|
955
|
return 1 if ! ref $self; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
# fake class method call : an object with only one field '__schema' |
214
|
141
|
|
|
|
|
609
|
my @k = keys %$self; |
215
|
141
|
|
100
|
|
|
936
|
return @k == 1 && $k[0] eq '__schema'; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub TO_JSON { |
220
|
3
|
|
|
3
|
0
|
6
|
my $self = shift; |
221
|
3
|
|
|
|
|
13
|
my $clone = {%$self}; |
222
|
3
|
|
|
|
|
8
|
delete $clone->{__schema}; |
223
|
3
|
|
|
|
|
19
|
return $clone; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
1; # End of DBIx::DataModel::Source |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
__END__ |