line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ObjectDB::Meta; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
36
|
use strict; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
161
|
|
4
|
5
|
|
|
5
|
|
25
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
157
|
|
5
|
5
|
|
|
5
|
|
25
|
use mro; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
49
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '3.27'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Storable; |
10
|
|
|
|
|
|
|
require Carp; |
11
|
5
|
|
|
5
|
|
262
|
use List::Util qw(first); |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
703
|
|
12
|
|
|
|
|
|
|
|
13
|
5
|
|
|
5
|
|
2335
|
use ObjectDB::Meta::RelationshipFactory; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
13836
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my %OBJECTS; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub find_or_register_meta { |
18
|
42
|
|
|
42
|
0
|
62
|
my $class = shift; |
19
|
42
|
|
|
|
|
76
|
my ($meta_class, @args) = @_; |
20
|
|
|
|
|
|
|
|
21
|
42
|
|
66
|
|
|
165
|
return $OBJECTS{$meta_class} ||= ObjectDB::Meta->new(class => $meta_class, @args); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub find_by_table { |
25
|
0
|
|
|
0
|
0
|
0
|
my $class = shift; |
26
|
0
|
|
|
|
|
0
|
my ($table) = @_; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
0
|
foreach my $meta_class (keys %OBJECTS) { |
29
|
0
|
|
|
|
|
0
|
my $meta = $OBJECTS{$meta_class}; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
0
|
if ($meta->table eq $table) { |
32
|
0
|
|
|
|
|
0
|
return $meta; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new { |
40
|
28
|
|
|
28
|
0
|
53117
|
my $class = shift; |
41
|
28
|
|
|
|
|
79
|
my (%params) = @_; |
42
|
|
|
|
|
|
|
|
43
|
28
|
100
|
|
|
|
162
|
Carp::croak('Class is required when building meta') unless $params{class}; |
44
|
|
|
|
|
|
|
|
45
|
27
|
100
|
|
|
|
76
|
if (my $parent = $class->_is_inheriting($params{class})) { |
46
|
2
|
|
|
|
|
13
|
return $parent; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
25
|
100
|
|
|
|
242
|
Carp::croak('Table is required when building meta') unless $params{table}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $self = { |
52
|
|
|
|
|
|
|
class => $params{class}, |
53
|
|
|
|
|
|
|
table => $params{table} |
54
|
24
|
|
|
|
|
66
|
}; |
55
|
24
|
|
|
|
|
45
|
bless $self, $class; |
56
|
|
|
|
|
|
|
|
57
|
24
|
50
|
|
|
|
49
|
if ($params{discover_schema}) { |
58
|
0
|
|
|
|
|
0
|
$self->discover_schema; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
24
|
100
|
|
|
|
57
|
$self->set_columns($params{columns}) if $params{columns}; |
62
|
24
|
50
|
|
|
|
62
|
$self->set_primary_key($params{primary_key}) if $params{primary_key}; |
63
|
24
|
50
|
|
|
|
45
|
$self->set_unique_keys($params{unique_keys}) if $params{unique_keys}; |
64
|
|
|
|
|
|
|
$self->set_auto_increment($params{auto_increment}) |
65
|
24
|
50
|
|
|
|
45
|
if $params{auto_increment}; |
66
|
|
|
|
|
|
|
|
67
|
24
|
|
|
|
|
77
|
$self->_build_relationships($params{relationships}); |
68
|
|
|
|
|
|
|
|
69
|
24
|
100
|
|
|
|
74
|
if ($params{generate_columns_methods}) { |
70
|
1
|
|
|
|
|
5
|
$self->generate_columns_methods; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
24
|
100
|
|
|
|
51
|
if ($params{generate_related_methods}) { |
74
|
1
|
|
|
|
|
9
|
$self->generate_related_methods; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
24
|
|
|
|
|
86
|
return $self; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
2
|
|
|
2
|
0
|
17
|
sub class { $_[0]->{class} } |
81
|
0
|
|
|
0
|
1
|
0
|
sub table { $_[0]->{table} } |
82
|
1
|
|
|
1
|
1
|
6
|
sub relationships { $_[0]->{relationships} } |
83
|
0
|
|
|
0
|
0
|
0
|
sub column { shift->get_column(@_); } |
84
|
0
|
|
|
0
|
1
|
0
|
sub columns { $_[0]->get_columns; } |
85
|
0
|
|
|
0
|
1
|
0
|
sub primary_key { $_[0]->get_primary_key; } |
86
|
0
|
|
|
0
|
1
|
0
|
sub auto_increment { $_[0]->get_auto_increment; } |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub is_primary_key { |
89
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
90
|
0
|
|
|
|
|
0
|
my ($name) = @_; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
0
|
|
0
|
return !!first { $name eq $_ } $self->get_primary_key; |
|
0
|
|
|
|
|
0
|
|
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub is_unique_key { |
96
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
97
|
0
|
|
|
|
|
0
|
my ($name) = @_; |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
0
|
foreach my $key (@{ $self->{unique_keys} }) { |
|
0
|
|
|
|
|
0
|
|
100
|
0
|
0
|
|
0
|
|
0
|
return 1 if first { $name eq $_ } @$key; |
|
0
|
|
|
|
|
0
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
return 0; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub is_nullable { |
107
|
2
|
|
|
2
|
0
|
4
|
my $self = shift; |
108
|
2
|
|
|
|
|
4
|
my ($name) = @_; |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
5
|
my $column = $self->get_column($name); |
111
|
|
|
|
|
|
|
|
112
|
2
|
|
|
|
|
12
|
return $column->{is_null}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub get_class { |
116
|
5
|
|
|
5
|
0
|
12
|
my $self = shift; |
117
|
|
|
|
|
|
|
|
118
|
5
|
|
|
|
|
24
|
return $self->{class}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub get_table { |
122
|
2
|
|
|
2
|
0
|
11
|
my $self = shift; |
123
|
|
|
|
|
|
|
|
124
|
2
|
|
|
|
|
11
|
return $self->{table}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub set_table { |
128
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
129
|
0
|
|
|
|
|
0
|
my ($value) = @_; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
0
|
$self->{table} = $value; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
0
|
return $self; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub is_column { |
137
|
104
|
|
|
104
|
0
|
138
|
my $self = shift; |
138
|
104
|
|
|
|
|
191
|
my ($name) = @_; |
139
|
|
|
|
|
|
|
|
140
|
104
|
50
|
|
|
|
189
|
Carp::croak('Name is required') unless $name; |
141
|
|
|
|
|
|
|
|
142
|
104
|
|
|
158
|
|
268
|
return !!first { $name eq $_->{name} } @{ $self->{columns} }; |
|
158
|
|
|
|
|
729
|
|
|
104
|
|
|
|
|
301
|
|
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub get_column { |
146
|
10
|
|
|
10
|
0
|
17
|
my $self = shift; |
147
|
10
|
|
|
|
|
14
|
my ($name) = @_; |
148
|
|
|
|
|
|
|
|
149
|
10
|
50
|
|
|
|
19
|
Carp::croak("Unknown column '$name'") unless $self->is_column($name); |
150
|
|
|
|
|
|
|
|
151
|
10
|
|
|
25
|
|
34
|
return first { $_->{name} eq $name } @{ $self->{columns} }; |
|
25
|
|
|
|
|
54
|
|
|
10
|
|
|
|
|
25
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub get_columns { |
155
|
5
|
|
|
5
|
0
|
21
|
my $self = shift; |
156
|
|
|
|
|
|
|
|
157
|
5
|
|
|
|
|
8
|
return map { $_->{name} } @{ $self->{columns} }; |
|
13
|
|
|
|
|
43
|
|
|
5
|
|
|
|
|
9
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub get_regular_columns { |
161
|
1
|
|
|
1
|
0
|
7
|
my $self = shift; |
162
|
|
|
|
|
|
|
|
163
|
1
|
|
|
|
|
2
|
my @columns; |
164
|
|
|
|
|
|
|
|
165
|
1
|
|
|
|
|
4
|
foreach my $column ($self->get_columns) { |
166
|
3
|
100
|
|
3
|
|
9
|
next if first { $column eq $_ } $self->get_primary_key; |
|
3
|
|
|
|
|
9
|
|
167
|
|
|
|
|
|
|
|
168
|
2
|
|
|
|
|
6
|
push @columns, $column; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
1
|
|
|
|
|
10
|
return @columns; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub set_columns { |
175
|
20
|
|
|
20
|
0
|
86
|
my $self = shift; |
176
|
|
|
|
|
|
|
|
177
|
20
|
|
|
|
|
43
|
$self->{columns} = []; |
178
|
|
|
|
|
|
|
|
179
|
20
|
|
|
|
|
53
|
$self->add_columns(@_); |
180
|
|
|
|
|
|
|
|
181
|
20
|
|
|
|
|
43
|
return $self; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub add_columns { |
185
|
20
|
|
|
20
|
0
|
29
|
my $self = shift; |
186
|
20
|
100
|
66
|
|
|
87
|
my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; |
|
4
|
|
|
|
|
9
|
|
187
|
|
|
|
|
|
|
|
188
|
20
|
|
|
|
|
33
|
my $count = 0; |
189
|
20
|
|
|
|
|
65
|
while (my ($name, $options) = @columns[ $count, $count + 1 ]) { |
190
|
76
|
100
|
|
|
|
145
|
last unless $name; |
191
|
|
|
|
|
|
|
|
192
|
56
|
100
|
|
|
|
106
|
if (ref $options eq 'HASH') { |
193
|
2
|
|
|
|
|
4
|
$self->add_column($name, $options); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
else { |
196
|
54
|
|
|
|
|
152
|
$self->add_column($name); |
197
|
|
|
|
|
|
|
|
198
|
54
|
|
|
|
|
66
|
$count++; |
199
|
54
|
|
|
|
|
147
|
next; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
2
|
|
|
|
|
6
|
$count += 2; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
20
|
|
|
|
|
32
|
return $self; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub add_column { |
209
|
59
|
|
|
59
|
0
|
149
|
my $self = shift; |
210
|
59
|
|
|
|
|
94
|
my ($name, $attributes) = @_; |
211
|
|
|
|
|
|
|
|
212
|
59
|
50
|
|
|
|
93
|
Carp::croak('Name is required') unless $name; |
213
|
59
|
100
|
|
|
|
114
|
Carp::croak("Column '$name' already exists") if $self->is_column($name); |
214
|
|
|
|
|
|
|
|
215
|
58
|
|
100
|
|
|
275
|
$attributes ||= {}; |
216
|
|
|
|
|
|
|
|
217
|
58
|
|
|
|
|
76
|
push @{ $self->{columns} }, { name => $name, %$attributes }; |
|
58
|
|
|
|
|
157
|
|
218
|
|
|
|
|
|
|
|
219
|
58
|
|
|
|
|
115
|
return $self; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub remove_column { |
223
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
224
|
0
|
|
|
|
|
0
|
my ($name) = @_; |
225
|
|
|
|
|
|
|
|
226
|
0
|
0
|
0
|
|
|
0
|
return unless $name && $self->is_column($name); |
227
|
|
|
|
|
|
|
|
228
|
0
|
|
|
|
|
0
|
$self->{columns} = [ grep { $_->{name} ne $name } @{ $self->{columns} } ]; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
0
|
return $self; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
sub get_primary_key { |
234
|
4
|
|
|
4
|
0
|
10
|
my $self = shift; |
235
|
|
|
|
|
|
|
|
236
|
4
|
50
|
|
|
|
5
|
return @{ $self->{primary_key} || [] }; |
|
4
|
|
|
|
|
19
|
|
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub set_primary_key { |
240
|
6
|
|
|
6
|
0
|
143
|
my $self = shift; |
241
|
6
|
50
|
33
|
|
|
33
|
my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; |
|
0
|
|
|
|
|
0
|
|
242
|
|
|
|
|
|
|
|
243
|
6
|
|
|
|
|
12
|
foreach my $column (@columns) { |
244
|
6
|
100
|
|
|
|
13
|
Carp::croak("Unknown column '$column' set as primary key") |
245
|
|
|
|
|
|
|
unless $self->is_column($column); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
4
|
|
|
|
|
11
|
$self->{primary_key} = [@columns]; |
249
|
|
|
|
|
|
|
|
250
|
4
|
|
|
|
|
11
|
return $self; |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
sub get_unique_keys { |
254
|
3
|
|
|
3
|
0
|
13
|
my $self = shift; |
255
|
|
|
|
|
|
|
|
256
|
3
|
|
|
|
|
5
|
return @{ $self->{unique_keys} }; |
|
3
|
|
|
|
|
19
|
|
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
sub set_unique_keys { |
260
|
3
|
|
|
3
|
0
|
25
|
my $self = shift; |
261
|
3
|
50
|
66
|
|
|
20
|
my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; |
|
0
|
|
|
|
|
0
|
|
262
|
|
|
|
|
|
|
|
263
|
3
|
|
|
|
|
6
|
$self->{unique_keys} = []; |
264
|
|
|
|
|
|
|
|
265
|
3
|
|
|
|
|
12
|
$self->add_unique_keys(@columns); |
266
|
|
|
|
|
|
|
|
267
|
3
|
|
|
|
|
6
|
return $self; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub add_unique_keys { |
271
|
3
|
|
|
3
|
0
|
3
|
my $self = shift; |
272
|
3
|
50
|
66
|
|
|
15
|
my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; |
|
0
|
|
|
|
|
0
|
|
273
|
|
|
|
|
|
|
|
274
|
3
|
|
|
|
|
6
|
foreach my $column (@columns) { |
275
|
5
|
|
|
|
|
11
|
$self->add_unique_key($column); |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
3
|
|
|
|
|
5
|
return $self; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub add_unique_key { |
282
|
5
|
|
|
5
|
0
|
9
|
my $self = shift; |
283
|
5
|
100
|
66
|
|
|
20
|
my (@columns) = @_ == 1 && ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_; |
|
2
|
|
|
|
|
6
|
|
284
|
|
|
|
|
|
|
|
285
|
5
|
|
|
|
|
10
|
foreach my $column (@columns) { |
286
|
6
|
50
|
|
|
|
13
|
Carp::croak("Unknown column '$column' set as unique key") |
287
|
|
|
|
|
|
|
unless $self->is_column($column); |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
5
|
|
|
|
|
8
|
push @{ $self->{unique_keys} }, [@columns]; |
|
5
|
|
|
|
|
14
|
|
291
|
|
|
|
|
|
|
|
292
|
5
|
|
|
|
|
21
|
return $self; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub get_auto_increment { |
296
|
1
|
|
|
1
|
0
|
4
|
my $self = shift; |
297
|
|
|
|
|
|
|
|
298
|
1
|
|
|
|
|
5
|
return $self->{auto_increment}; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
sub set_auto_increment { |
302
|
2
|
|
|
2
|
0
|
86
|
my $self = shift; |
303
|
2
|
|
|
|
|
5
|
my ($column) = @_; |
304
|
|
|
|
|
|
|
|
305
|
2
|
100
|
|
|
|
5
|
Carp::croak("Unknown column '$column' set as auto increment") |
306
|
|
|
|
|
|
|
unless $self->is_column($column); |
307
|
|
|
|
|
|
|
|
308
|
1
|
|
|
|
|
5
|
$self->{auto_increment} = $column; |
309
|
|
|
|
|
|
|
|
310
|
1
|
|
|
|
|
3
|
return $self; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
sub is_relationship { |
314
|
8
|
|
|
8
|
0
|
27
|
my $self = shift; |
315
|
8
|
|
|
|
|
53
|
my ($name) = @_; |
316
|
|
|
|
|
|
|
|
317
|
8
|
|
|
|
|
73
|
return exists $self->{relationships}->{$name}; |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
sub get_relationship { |
321
|
1
|
|
|
1
|
0
|
19
|
my $self = shift; |
322
|
1
|
|
|
|
|
3
|
my ($name) = @_; |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
Carp::croak("Unknown relationship '$name'") |
325
|
1
|
50
|
|
|
|
4
|
unless exists $self->{relationships}->{$name}; |
326
|
|
|
|
|
|
|
|
327
|
1
|
|
|
|
|
5
|
return $self->{relationships}->{$name}; |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
sub add_relationship { |
331
|
4
|
|
|
4
|
0
|
16
|
my $self = shift; |
332
|
4
|
|
|
|
|
9
|
my ($name, $options) = @_; |
333
|
|
|
|
|
|
|
|
334
|
4
|
50
|
33
|
|
|
18
|
Carp::croak('Name and options are required') unless $name && $options; |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
$self->{relationships}->{$name} = ObjectDB::Meta::RelationshipFactory->new->build( |
337
|
4
|
|
|
|
|
29
|
$options->{type}, %{$options}, |
|
4
|
|
|
|
|
15
|
|
338
|
|
|
|
|
|
|
orig_class => $self->get_class, |
339
|
|
|
|
|
|
|
name => $name |
340
|
|
|
|
|
|
|
); |
341
|
|
|
|
|
|
|
} |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
sub add_relationships { |
344
|
2
|
|
|
2
|
0
|
13
|
my $self = shift; |
345
|
|
|
|
|
|
|
|
346
|
2
|
|
|
|
|
5
|
my $count = 0; |
347
|
2
|
|
|
|
|
10
|
while (my ($name, $options) = @_[ $count, $count + 1 ]) { |
348
|
4
|
100
|
66
|
|
|
17
|
last unless $name && $options; |
349
|
|
|
|
|
|
|
|
350
|
2
|
|
|
|
|
7
|
$self->add_relationship($name, $options); |
351
|
|
|
|
|
|
|
|
352
|
2
|
|
|
|
|
10
|
$count += 2; |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
sub discover_schema { |
357
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
358
|
|
|
|
|
|
|
|
359
|
0
|
0
|
|
|
|
0
|
eval { require DBIx::Inspector; 1 } or do { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
360
|
0
|
|
|
|
|
0
|
Carp::croak('DBIx::Inspector is required for auto discover'); |
361
|
|
|
|
|
|
|
}; |
362
|
|
|
|
|
|
|
|
363
|
0
|
|
|
|
|
0
|
my $dbh = $self->class->init_db; |
364
|
|
|
|
|
|
|
|
365
|
0
|
|
|
|
|
0
|
my $inspector = DBIx::Inspector->new(dbh => $dbh); |
366
|
|
|
|
|
|
|
|
367
|
0
|
|
|
|
|
0
|
my $table = $inspector->table($self->table); |
368
|
0
|
0
|
|
|
|
0
|
Carp::croak('auto discovery failed') unless $table; |
369
|
|
|
|
|
|
|
|
370
|
0
|
|
|
|
|
0
|
$self->{columns} = []; |
371
|
0
|
|
|
|
|
0
|
foreach my $column ($table->columns) { |
372
|
0
|
|
|
|
|
0
|
my $default_value = undef; |
373
|
|
|
|
|
|
|
|
374
|
0
|
0
|
|
|
|
0
|
if (defined $column->column_def) { |
375
|
0
|
0
|
|
|
|
0
|
$default_value = $column->column_def =~ /^'(.*?)'/ ? $1 : $column->column_def; |
376
|
|
|
|
|
|
|
|
377
|
0
|
0
|
|
|
|
0
|
if ($column->type_name =~ m/^bool/i) { |
378
|
0
|
0
|
|
|
|
0
|
if ($column->column_def =~ m/^\d/) { |
379
|
0
|
|
|
|
|
0
|
$default_value = $column->column_def; |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
else { |
382
|
0
|
0
|
|
|
|
0
|
$default_value = $column->column_def =~ m/^t/ ? 1 == 1 : 1 == 0; |
383
|
|
|
|
|
|
|
} |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
|
387
|
0
|
|
|
|
|
0
|
my $type = 'string'; |
388
|
0
|
0
|
|
|
|
0
|
if ( $column->type_name =~ m/^bool/i ) { |
|
|
0
|
|
|
|
|
|
389
|
0
|
|
|
|
|
0
|
$type = 'bool'; |
390
|
|
|
|
|
|
|
} |
391
|
|
|
|
|
|
|
elsif ( $column->type_name =~ m/^(?:int|num|float|real|decimal)/i ) { |
392
|
0
|
|
|
|
|
0
|
$type = 'number'; |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
0
|
0
|
|
|
|
0
|
my $is_null = $column->is_nullable eq 'YES' ? 1 : 0; |
396
|
|
|
|
|
|
|
|
397
|
0
|
|
|
|
|
0
|
$self->add_column($column->name, { default => $default_value, is_null => $is_null, type => $type }); |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
|
400
|
0
|
|
|
|
|
0
|
$self->set_primary_key(map { $_->name } $table->primary_key); |
|
0
|
|
|
|
|
0
|
|
401
|
|
|
|
|
|
|
|
402
|
0
|
|
|
|
|
0
|
return $self; |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
sub generate_columns_methods { |
406
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
407
|
|
|
|
|
|
|
|
408
|
5
|
|
|
5
|
|
45
|
no strict 'refs'; |
|
5
|
|
|
|
|
16
|
|
|
5
|
|
|
|
|
226
|
|
409
|
5
|
|
|
5
|
|
29
|
no warnings 'redefine'; |
|
5
|
|
|
|
|
18
|
|
|
5
|
|
|
|
|
755
|
|
410
|
1
|
|
|
|
|
4
|
foreach my $column ($self->get_columns) { |
411
|
1
|
|
|
|
|
4
|
*{ $self->class . '::' . $column } = |
412
|
1
|
|
|
0
|
|
5
|
sub { shift->column($column, @_) }; |
|
0
|
|
|
|
|
0
|
|
413
|
|
|
|
|
|
|
} |
414
|
|
|
|
|
|
|
|
415
|
1
|
|
|
|
|
3
|
return $self; |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
sub generate_related_methods { |
419
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
420
|
|
|
|
|
|
|
|
421
|
5
|
|
|
5
|
|
44
|
no strict 'refs'; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
198
|
|
422
|
5
|
|
|
5
|
|
28
|
no warnings 'redefine'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
1688
|
|
423
|
1
|
|
|
|
|
3
|
foreach my $rel_name (keys %{ $self->relationships }) { |
|
1
|
|
|
|
|
4
|
|
424
|
1
|
|
|
|
|
3
|
*{ $self->class . '::' . $rel_name } = |
425
|
1
|
|
|
0
|
|
5
|
sub { shift->related($rel_name, @_) }; |
|
0
|
|
|
|
|
0
|
|
426
|
|
|
|
|
|
|
} |
427
|
|
|
|
|
|
|
|
428
|
1
|
|
|
|
|
3
|
return $self; |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
sub _build_relationships { |
432
|
24
|
|
|
24
|
|
35
|
my $self = shift; |
433
|
24
|
|
|
|
|
51
|
my ($relationships) = @_; |
434
|
|
|
|
|
|
|
|
435
|
24
|
|
50
|
|
|
118
|
$self->{relationships} ||= {}; |
436
|
|
|
|
|
|
|
|
437
|
24
|
|
|
|
|
33
|
foreach my $rel (keys %{$relationships}) { |
|
24
|
|
|
|
|
85
|
|
438
|
|
|
|
|
|
|
$self->{relationships}->{$rel} = ObjectDB::Meta::RelationshipFactory->new->build( |
439
|
1
|
|
|
|
|
6
|
$relationships->{$rel}->{type}, %{ $relationships->{$rel} }, |
440
|
|
|
|
|
|
|
orig_class => $self->{class}, |
441
|
1
|
|
|
|
|
7
|
name => $rel |
442
|
|
|
|
|
|
|
); |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
sub _is_inheriting { |
447
|
27
|
|
|
27
|
|
46
|
my $class = shift; |
448
|
27
|
|
|
|
|
50
|
my ($for_class) = @_; |
449
|
|
|
|
|
|
|
|
450
|
27
|
|
|
|
|
128
|
my $parents = mro::get_linear_isa($for_class); |
451
|
27
|
|
|
|
|
54
|
foreach my $parent (@$parents) { |
452
|
34
|
100
|
|
|
|
89
|
if (my $parent_meta = $OBJECTS{$parent}) { |
453
|
2
|
|
|
|
|
205
|
my $meta = Storable::dclone($parent_meta); |
454
|
|
|
|
|
|
|
|
455
|
2
|
|
|
|
|
10
|
$meta->{class} = $for_class; |
456
|
|
|
|
|
|
|
|
457
|
2
|
|
|
|
|
9
|
return $meta; |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
|
461
|
25
|
|
|
|
|
77
|
return; |
462
|
|
|
|
|
|
|
} |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
1; |
465
|
|
|
|
|
|
|
__END__ |