| 1 | package UR::Object::Join; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use UR; | ||
| 5 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 6 | |||
| 7 | our @CARP_NOT = qw( UR::Object::Property ); | ||
| 8 | |||
| 9 | class UR::Object::Join { | ||
| 10 | #is => 'UR::Value', | ||
| 11 | id_by => [ | ||
| 12 | id => { is => 'Text' }, | ||
| 13 | ], | ||
| 14 | has_optional_transient => [ | ||
| 15 | source_class => { is => 'Text' }, | ||
| 16 | source_property_names => { is => 'Text' }, | ||
| 17 | |||
| 18 | foreign_class => { is => 'Text' }, | ||
| 19 | foreign_property_names => { is => 'Text' }, | ||
| 20 | |||
| 21 | source_name_for_foreign => { is => 'Text' }, | ||
| 22 | foreign_name_for_source => { is => 'Text' }, | ||
| 23 | |||
| 24 | is_optional => { is => 'Boolean' }, | ||
| 25 | |||
| 26 | is_many => { is => 'Boolean' }, | ||
| 27 | |||
| 28 | sub_group_label => { is => 'Text' }, | ||
| 29 | |||
| 30 | where => { is => 'Text' }, | ||
| 31 | ], | ||
| 32 | doc => "join metadata used internally by the ::QueryBuilder" | ||
| 33 | }; | ||
| 34 | |||
| 35 | our %resolve_chain; | ||
| 36 | |||
| 37 | # When a Join is unloaded, we need to remove from the cache any join chain | ||
| 38 | # using this join | ||
| 39 | sub unload { | ||
| 40 | my $self = shift; | ||
| 41 | my $id_to_remove = $self->id; | ||
| 42 | |||
| 43 | foreach my $joins_for_class ( values %resolve_chain ) { | ||
| 44 | foreach my $property_chain ( keys %$joins_for_class ) { | ||
| 45 | # need to skip over DeletedRefs that may already be in the list | ||
| 46 | if (grep { $_->isa('UR::Object::Join') and ($_->id eq $id_to_remove) } | ||
| 47 | @{$joins_for_class->{$property_chain}} | ||
| 48 | ) { | ||
| 49 | # This unloaded join is in the list - nuke the whole list | ||
| 50 | delete $joins_for_class->{$property_chain}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | $self->SUPER::unload(@_); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub resolve_chain { | ||
| 58 | my ($class, $class_name, $property_chain) = @_; | ||
| 59 | |||
| 60 | my $join_chain = | ||
| 61 | $resolve_chain{$class_name}{$property_chain} | ||
| 62 | ||= do { | ||
| 63 | my $class_meta = $class_name->__meta__; | ||
| 64 | my @pmeta = $class_meta->property_meta_for_name($property_chain); | ||
| 65 | my @joins; | ||
| 66 | for my $pmeta (@pmeta) { | ||
| 67 | push @joins, $class->_resolve_chain_for_property_meta($pmeta); | ||
| 68 | } | ||
| 69 | \@joins; | ||
| 70 | }; | ||
| 71 | |||
| 72 | return @$join_chain; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _resolve_chain_for_property_meta { | ||
| 76 | my ($class, $pmeta) = @_; | ||
| 77 | if ($pmeta->via or $pmeta->to) { | ||
| 78 | return $class->_resolve_via_to($pmeta); | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | my $foreign_class = $pmeta->_data_type_as_class_name; | ||
| 82 | unless (defined($foreign_class) and $foreign_class->can('get')) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | if ($pmeta->id_by or $foreign_class->isa("UR::Value")) { | ||
| 86 | return $class->_resolve_forward($pmeta); | ||
| 87 | } | ||
| 88 | elsif (my $reverse_as = $pmeta->reverse_as) { | ||
| 89 | return $class->_resolve_reverse($pmeta); | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | # TODO: handle hard-references to objects here maybe? | ||
| 93 | $pmeta->error_message("Property '" . $pmeta->property_name . "' of class " . $pmeta->class_name | ||
| 94 | . " has no 'id_by' or 'reverse_as' property metadata"); | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _get_or_define { | ||
| 101 | my $class = shift; | ||
| 102 | my %p = @_; | ||
| 103 | my $id = delete $p{id}; | ||
| 104 | delete $p{__get_serial}; | ||
| 105 | delete $p{db_committed}; | ||
| 106 | delete $p{_change_count}; | ||
| 107 | delete $p{__defined}; | ||
| 108 | my $self = $class->get(id => $id); | ||
| 109 | unless ($self) { | ||
| 110 | $self = $class->__define__($id); | ||
| 111 | for my $k (keys %p) { | ||
| 112 | $self->$k($p{$k}); | ||
| 113 | no warnings; | ||
| 114 | unless ($self->{$k} eq $p{$k}) { | ||
| 115 | Carp::confess(Data::Dumper::Dumper($self, \%p)); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | unless ($self) { | ||
| 120 | Carp::confess("Failed to create join???"); | ||
| 121 | } | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _resolve_via_to { | ||
| 126 | my ($class, $pmeta) = @_; | ||
| 127 | |||
| 128 | my $class_name = $pmeta->class_name; | ||
| 129 | my $class_meta = UR::Object::Type->get(class_name => $class_name); | ||
| 130 | |||
| 131 | my @joins; | ||
| 132 | my $via = $pmeta->via; | ||
| 133 | |||
| 134 | my $to = $pmeta->to; | ||
| 135 | if ($via and not $to) { | ||
| 136 | $to = $pmeta->property_name; | ||
| 137 | } | ||
| 138 | |||
| 139 | my $via_meta; | ||
| 140 | if ($via) { | ||
| 141 | if ($via eq '__self__') { | ||
| 142 | my $to_meta = $class_meta->property_meta_for_name($to); | ||
| 143 | unless ($to_meta) { | ||
| 144 | my $property_name = $pmeta->property_name; | ||
| 145 | Carp::croak "Can't resolve joins for property '$property_name' of $class_name: No property metadata 'to' property '$to'"; | ||
| 146 | } | ||
| 147 | return $to_meta->_resolve_join_chain(); | ||
| 148 | } | ||
| 149 | $via_meta = $class_meta->property_meta_for_name($via); | ||
| 150 | unless ($via_meta) { | ||
| 151 | return if $class_name->can($via); # It's via a method, not an actual property | ||
| 152 | |||
| 153 | my $property_name = $pmeta->property_name; | ||
| 154 | Carp::croak "Can't resolve joins for property '$property_name' of $class_name: No property metadata for via property '$via'"; | ||
| 155 | } | ||
| 156 | |||
| 157 | if ($via_meta->to and ($via_meta->to eq '-filter')) { | ||
| 158 | return $via_meta->_resolve_join_chain(); | ||
| 159 | } | ||
| 160 | |||
| 161 | unless ($via_meta->data_type) { | ||
| 162 | my $property_name = $pmeta->property_name; | ||
| 163 | my $class_name = $pmeta->class_name; | ||
| 164 | Carp::croak "Can't resolve joins for property '$property_name' of $class_name: No data type for via property '$via'"; | ||
| 165 | } | ||
| 166 | push @joins, $via_meta->_resolve_join_chain(); | ||
| 167 | |||
| 168 | if (my $where = $pmeta->where) { | ||
| 169 | my $join = pop @joins; | ||
| 170 | unless ($join and $join->{foreign_class}) { | ||
| 171 | my $property_name = $pmeta->property_name; | ||
| 172 | my $class_name = $pmeta->class_name; | ||
| 173 | Carp::croak("Can't resolve joins for property '$property_name' of $class_name: Couldn't determine foreign class for via property '$via'\n" | ||
| 174 | . "join data so far: ". Data::Dumper::Dumper($join, \@joins)); | ||
| 175 | } | ||
| 176 | my $where_rule = UR::BoolExpr->resolve($join->{foreign_class}, @$where); | ||
| 177 | my $id = $join->{id}; | ||
| 178 | $id .= ' ' . $where_rule->id; | ||
| 179 | my %join_data = %$join; | ||
| 180 | push @joins, $class->_get_or_define(%join_data, id => $id, where => $where, sub_group_label => $pmeta->property_name); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $via_meta = $pmeta; | ||
| 185 | } | ||
| 186 | |||
| 187 | if ($to and $to ne '__self__' and $to ne '-filter') { | ||
| 188 | my $to_class_meta = eval { $via_meta->data_type->__meta__ }; | ||
| 189 | unless ($to_class_meta) { | ||
| 190 | Carp::croak("Can't get class metadata for " . $via_meta->data_type | ||
| 191 | . " while resolving property '" . $pmeta->property_name . "' in class " . $pmeta->class_name . "\n" | ||
| 192 | . "Is the data_type for property '" . $via_meta->property_name . "' in class " | ||
| 193 | . $via_meta->class_name . " correct?"); | ||
| 194 | } | ||
| 195 | |||
| 196 | my $to_meta = $to_class_meta->property_meta_for_name($to); | ||
| 197 | unless ($to_meta) { | ||
| 198 | my $property_name = $pmeta->property_name; | ||
| 199 | my $class_name = $pmeta->class_name; | ||
| 200 | Carp::croak "Can't resolve property '$property_name' of $class_name: No '$to' property found on " . $via_meta->data_type; | ||
| 201 | } | ||
| 202 | |||
| 203 | push @joins, $to_meta->_resolve_join_chain(); | ||
| 204 | } | ||
| 205 | |||
| 206 | if (my $return_class_name = $pmeta->_convert_data_type_for_source_class_to_final_class($pmeta->data_type, $pmeta->class_name)) { | ||
| 207 | my $final_class_name = $joins[-1]->foreign_class; | ||
| 208 | if ($return_class_name ne $final_class_name) { | ||
| 209 | if ($return_class_name->isa($final_class_name)) { | ||
| 210 | # the property is a subclass of the one involved in the final join | ||
| 211 | # this happens when there is a via/where/to where say "to" goes-to any "Animal" but this overall property is known to be a "Dog". | ||
| 212 | my $general_join = pop @joins; | ||
| 213 | my $specific_join = UR::Object::Join->_get_or_define( | ||
| 214 | source_class => $general_join->{'source_class'}, | ||
| 215 | source_property_names => $general_join->{'source_property_names'}, | ||
| 216 | foreign_class => $return_class_name, # more specific | ||
| 217 | foreign_property_names => $general_join->{'foreign_property_names'}, # presume the borrow took you into a subclass and these still work | ||
| 218 | is_optional => $general_join->{'is_optional'}, | ||
| 219 | id => $general_join->{id} . ' isa ' . $return_class_name | ||
| 220 | ); | ||
| 221 | push @joins, $specific_join; | ||
| 222 | } | ||
| 223 | elsif ($return_class_name eq 'UR::Value::SloppyPrimitive' or $final_class_name eq 'UR::Value::SloppyPrimitive') { | ||
| 224 | # backward-compatible layer for before there were primitive types | ||
| 225 | } | ||
| 226 | elsif ($final_class_name->isa($return_class_name)) { | ||
| 227 | Carp::carp("Joins for property '" . $pmeta->property_name . "' of class " . $pmeta->class_name | ||
| 228 | . " is declared as data type $return_class_name while its joins connect to a more specific data type $final_class_name!"); | ||
| 229 | } | ||
| 230 | else { | ||
| 231 | #Carp::carp("Discrepant join for property '" . $pmeta->property_name . "' of class " . $pmeta->class_name | ||
| 232 | # . ". Its data type ($return_class_name) does not match the join from property '" | ||
| 233 | # . join("','", @{$joins[-1]->{source_property_names}}) . "' of class " . $joins[-1]->{source_class} | ||
| 234 | # . " with type $final_class_name"); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | return @joins; | ||
| 240 | } | ||
| 241 | |||
| 242 | # code below uses these to convert objects using hash slices | ||
| 243 | my @old = qw/source_class source_property_names foreign_class foreign_property_names source_name_for_foreign foreign_name_for_source is_optional is_many sub_group_label/; | ||
| 244 | my @new = qw/foreign_class foreign_property_names source_class source_property_names foreign_name_for_source source_name_for_foreign is_optional is_many sub_group_label/; | ||
| 245 | |||
| 246 | sub _resolve_forward { | ||
| 247 | my ($class, $pmeta) = @_; | ||
| 248 | |||
| 249 | my $foreign_class = $pmeta->_data_type_as_class_name; | ||
| 250 | unless (defined($foreign_class) and $foreign_class->can('get')) { | ||
| 251 | #Carp::cluck("No metadata?!"); | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | my $source_class = $pmeta->class_name; | ||
| 256 | my $class_meta = UR::Object::Type->get(class_name => $pmeta->class_name); | ||
| 257 | my @joins; | ||
| 258 | my $where = $pmeta->where; | ||
| 259 | my $foreign_class_meta = $foreign_class->__meta__; | ||
| 260 | my $property_name = $pmeta->property_name; | ||
| 261 | |||
| 262 | my $id = $source_class . '::' . $property_name; | ||
| 263 | if ($where) { | ||
| 264 | my $where_rule = UR::BoolExpr->resolve($foreign_class, @$where); | ||
| 265 | $id .= ' ' . $where_rule->id; | ||
| 266 | } | ||
| 267 | |||
| 268 | ##### | ||
| 269 | |||
| 270 | # direct reference (or primitive, which is a direct ref to a value obj) | ||
| 271 | my (@source_property_names, @source_property_types, | ||
| 272 | @foreign_property_names, @foreign_property_types, | ||
| 273 | $source_name_for_foreign, $foreign_name_for_source); | ||
| 274 | |||
| 275 | if ($foreign_class->isa("UR::Value")) { | ||
| 276 | if (my $id_by = $pmeta->id_by) { | ||
| 277 | my @id_by = ref($id_by) eq 'ARRAY' ? @$id_by : ($id_by); | ||
| 278 | foreach my $id_by_name ( @id_by ) { | ||
| 279 | my $id_by_property = $class_meta->property_meta_for_name($id_by_name); | ||
| 280 | push @joins, $id_by_property->_resolve_join_chain(); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | @source_property_names = ($property_name); | ||
| 285 | @foreign_property_names = ('id'); | ||
| 286 | |||
| 287 | $source_name_for_foreign = ($property_name); | ||
| 288 | } | ||
| 289 | elsif (my $id_by = $pmeta->id_by) { | ||
| 290 | my @pairs = $pmeta->get_property_name_pairs_for_join; | ||
| 291 | @source_property_names = map { $_->[0] } @pairs; | ||
| 292 | @foreign_property_names = map { $_->[1] } @pairs; | ||
| 293 | |||
| 294 | if (ref($id_by) eq 'ARRAY') { | ||
| 295 | # satisfying the id_by requires joins of its own | ||
| 296 | # sms: why is this only done on multi-value fks? | ||
| 297 | foreach my $id_by_property_name ( @$id_by ) { | ||
| 298 | my $id_by_property = $class_meta->property_meta_for_name($id_by_property_name); | ||
| 299 | next unless ($id_by_property and $id_by_property->is_delegated); | ||
| 300 | |||
| 301 | push @joins, $id_by_property->_resolve_join_chain(); | ||
| 302 | $source_class = $joins[-1]->{'foreign_class'}; | ||
| 303 | @source_property_names = @{$joins[-1]->{'foreign_property_names'}}; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | $source_name_for_foreign = $pmeta->property_name; | ||
| 308 | my @reverse = $foreign_class_meta->properties(reverse_as => $source_name_for_foreign, data_type => $pmeta->class_name); | ||
| 309 | my $reverse; | ||
| 310 | if (@reverse > 1) { | ||
| 311 | my @reduced = grep { not $_->where } @reverse; | ||
| 312 | if (@reduced != 1) { | ||
| 313 | Carp::confess("Ambiguous results finding reversal for $property_name!" . Data::Dumper::Dumper(\@reverse)); | ||
| 314 | } | ||
| 315 | $reverse = $reduced[0]; | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | $reverse = $reverse[0]; | ||
| 319 | } | ||
| 320 | if ($reverse) { | ||
| 321 | $foreign_name_for_source = $reverse->property_name; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | # the foreign class might NOT have a reverse_as, but | ||
| 326 | # this records what to reverse in this case. | ||
| 327 | $foreign_name_for_source ||= '<' . $source_class . '::' . $source_name_for_foreign; | ||
| 328 | |||
| 329 | push @joins, $class->_get_or_define( | ||
| 330 | id => $id, | ||
| 331 | |||
| 332 | source_class => $source_class, | ||
| 333 | source_property_names => \@source_property_names, | ||
| 334 | |||
| 335 | foreign_class => $foreign_class, | ||
| 336 | foreign_property_names => \@foreign_property_names, | ||
| 337 | |||
| 338 | source_name_for_foreign => $source_name_for_foreign, | ||
| 339 | foreign_name_for_source => $foreign_name_for_source, | ||
| 340 | |||
| 341 | is_optional => ($pmeta->is_optional or $pmeta->is_many), | ||
| 342 | |||
| 343 | is_many => $pmeta->is_many, | ||
| 344 | |||
| 345 | where => $where, | ||
| 346 | ); | ||
| 347 | |||
| 348 | return @joins; | ||
| 349 | } | ||
| 350 | |||
| 351 | sub _resolve_reverse { | ||
| 352 | my ($class, $pmeta) = @_; | ||
| 353 | |||
| 354 | my $foreign_class = $pmeta->_data_type_as_class_name; | ||
| 355 | |||
| 356 | unless (defined($foreign_class) and $foreign_class->can('get')) { | ||
| 357 | #Carp::cluck("No metadata?!"); | ||
| 358 | return; | ||
| 359 | } | ||
| 360 | |||
| 361 | my $source_class = $pmeta->class_name; | ||
| 362 | my $class_meta = UR::Object::Type->get(class_name => $pmeta->class_name); | ||
| 363 | my @joins; | ||
| 364 | my $where = $pmeta->where; | ||
| 365 | my $property_name = $pmeta->property_name; | ||
| 366 | |||
| 367 | my $id = $source_class . '::' . $property_name; | ||
| 368 | if ($where) { | ||
| 369 | my $where_rule = UR::BoolExpr->resolve($foreign_class, @$where); | ||
| 370 | $id .= ' ' . $where_rule->id; | ||
| 371 | } | ||
| 372 | |||
| 373 | ##### | ||
| 374 | |||
| 375 | my $reverse_as = $pmeta->reverse_as; | ||
| 376 | |||
| 377 | my $foreign_class_meta = $foreign_class->__meta__; | ||
| 378 | my $foreign_property_via = $foreign_class_meta->property_meta_for_name($reverse_as); | ||
| 379 | unless ($foreign_property_via) { | ||
| 380 | Carp::confess("No property '$reverse_as' in class $foreign_class, needed to resolve property '" . | ||
| 381 | $pmeta->property_name . "' of class " . $pmeta->class_name); | ||
| 382 | } | ||
| 383 | |||
| 384 | my @join_data = map { { %$_ } } $foreign_property_via->_resolve_join_chain(); | ||
| 385 | my $prev_where = $where; | ||
| 386 | for (@join_data) { | ||
| 387 | @$_{@new} = @$_{@old}; | ||
| 388 | |||
| 389 | my $next_where = $_->{where}; | ||
| 390 | $_->{where} = $prev_where; | ||
| 391 | |||
| 392 | no warnings qw(uninitialized); #source_name_for_foreign can be undefined at the end of the chain | ||
| 393 | my $id = $_->{source_class} . '::' . $_->{source_name_for_foreign}; | ||
| 394 | use warnings qw(uninitialized); | ||
| 395 | if ($prev_where) { | ||
| 396 | my $where_rule = UR::BoolExpr->resolve($foreign_class, @$where); | ||
| 397 | $id .= ' ' . $where_rule->id; | ||
| 398 | |||
| 399 | } | ||
| 400 | $_->{id} = $id; | ||
| 401 | |||
| 402 | $_->{is_optional} = ($pmeta->is_optional || $pmeta->is_many); | ||
| 403 | |||
| 404 | $_->{is_many} = $pmeta->{is_many}; | ||
| 405 | |||
| 406 | $_->{sub_group_label} = $pmeta->property_name; | ||
| 407 | |||
| 408 | $prev_where = $next_where; | ||
| 409 | } | ||
| 410 | @join_data = reverse @join_data; | ||
| 411 | if ($prev_where) { | ||
| 412 | # Having a where clause in the last join is only a problem if testing | ||
| 413 | # the where condition needs more joins. But if it did, then those additional | ||
| 414 | # joins would have already been in the list, right? | ||
| 415 | #Carp::confess("final join needs placement! " . Data::Dumper::Dumper($prev_where)); | ||
| 416 | } | ||
| 417 | |||
| 418 | for my $join_data (@join_data) { | ||
| 419 | push @joins, $class->_get_or_define(%$join_data); | ||
| 420 | } | ||
| 421 | |||
| 422 | return @joins; | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | # Return true if the foreign-end of the join includes all the ID properties of | ||
| 427 | # the foreign class. Used by the ObjectFabricator when it is determining whether or | ||
| 428 | # not to include more rules in the all_params_loaded hash for delegations | ||
| 429 | sub destination_is_all_id_properties { | ||
| 430 | my $self = shift; | ||
| 431 | |||
| 432 | my $foreign_class_meta = $self->{'foreign_class'}->__meta__; | ||
| 433 | my %join_properties = map { $_ => 1 } @{$self->{'foreign_property_names'}}; | ||
| 434 | my $join_has_all_id_props = 1; | ||
| 435 | foreach my $foreign_id_meta ( $foreign_class_meta->all_id_property_metas ) { | ||
| 436 | next if $foreign_id_meta->class_name eq 'UR::Object'; # Skip the manufactured 'id' property | ||
| 437 | next if (delete $join_properties{ $foreign_id_meta->property_name }); | ||
| 438 | $join_has_all_id_props = 0; | ||
| 439 | } | ||
| 440 | return $join_has_all_id_props; | ||
| 441 | } | ||
| 442 | |||
| 443 | |||
| 444 | 1; |