| 1 | package UR::DataSource::QueryPlan; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use UR; | ||
| 5 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 6 | |||
| 7 | # this class is an evolving attempt to formalize | ||
| 8 | # the blob of cached value used for query construction | ||
| 9 | |||
| 10 | class UR::DataSource::QueryPlan { | ||
| 11 | is => 'UR::Value', | ||
| 12 | id_by => [ | ||
| 13 | rule_template => { is => 'UR::BoolExpr::Template', id_by => ['subject_class_name','logic_type','logic_detail','constant_values_id'] }, | ||
| 14 | data_source => { is => 'UR::DataSource', id_by => 'data_source_id' }, | ||
| 15 | ], | ||
| 16 | has => [ | ||
| 17 | limit => { is => 'Integer', via => 'rule_template', to => 'limit' }, | ||
| 18 | offset => { is => 'Integer', via => 'rule_template', to => 'offset' }, | ||
| 19 | ], | ||
| 20 | has_transient => [ | ||
| 21 | _is_initialized => { is => 'Boolean' }, | ||
| 22 | |||
| 23 | needs_further_boolexpr_evaluation_after_loading => { is => 'Boolean' }, | ||
| 24 | |||
| 25 | # data tracked for the whole query by property,alias,join_id | ||
| 26 | _delegation_chain_data => { is => 'HASH' }, | ||
| 27 | _alias_data => { is => 'HASH' }, | ||
| 28 | _join_data => { is => 'HASH' }, | ||
| 29 | |||
| 30 | # the old $alias_num | ||
| 31 | _alias_count => { is => 'Number' }, | ||
| 32 | |||
| 33 | # the old @sql_joins | ||
| 34 | _db_joins => { is => 'ARRAY' }, | ||
| 35 | |||
| 36 | # the new @obj_joins | ||
| 37 | _obj_joins => { is => 'ARRAY' }, | ||
| 38 | |||
| 39 | # the old all_table_properties, which has a small array of loading info | ||
| 40 | _db_column_data => { is => 'ARRAY' }, | ||
| 41 | |||
| 42 | # the old hashes by the same names | ||
| 43 | _group_by_property_names => { is => 'HASH' }, | ||
| 44 | _order_by_property_names => { is => 'HASH' }, | ||
| 45 | |||
| 46 | _sql_filters => { is => 'ARRAY' }, | ||
| 47 | _sql_params => { is => 'ARRAY' }, | ||
| 48 | |||
| 49 | lob_column_names => {}, | ||
| 50 | lob_column_positions => {}, | ||
| 51 | query_config => {}, | ||
| 52 | post_process_results_callback => {}, | ||
| 53 | |||
| 54 | select_clause => {}, | ||
| 55 | select_hint => {}, | ||
| 56 | from_clause => {}, | ||
| 57 | where_clause => {}, | ||
| 58 | connect_by_clause => {}, | ||
| 59 | group_by_clause => {}, | ||
| 60 | order_by_columns => {}, | ||
| 61 | order_by_non_column_data => {}, # flag that's true if asked to order_by something not in the data source | ||
| 62 | |||
| 63 | sql_params => {}, | ||
| 64 | filter_specs => {}, | ||
| 65 | |||
| 66 | property_names_in_resultset_order => {}, | ||
| 67 | |||
| 68 | rule_template_id => {}, | ||
| 69 | rule_template_id_without_recursion_desc => {}, | ||
| 70 | rule_template_without_recursion_desc => {}, | ||
| 71 | |||
| 72 | joins => {}, | ||
| 73 | recursion_desc => {}, | ||
| 74 | recurse_property_on_this_row => {}, | ||
| 75 | recurse_property_referencing_other_rows => {}, | ||
| 76 | recurse_resolution_by_iteration => {}, # For data sources that don't support recursive queries | ||
| 77 | |||
| 78 | joins_across_data_sources => {}, # context _resolve_query_plan_for_ds_and_bxt | ||
| 79 | loading_templates => {}, | ||
| 80 | class_name => {}, | ||
| 81 | rule_matches_all => {}, | ||
| 82 | rule_template_is_id_only => {}, | ||
| 83 | |||
| 84 | sub_typing_property => {}, | ||
| 85 | class_table_name => {}, | ||
| 86 | rule_template_specifies_value_for_subtype => {}, | ||
| 87 | sub_classification_meta_class_name => {}, | ||
| 88 | ] | ||
| 89 | }; | ||
| 90 | |||
| 91 | |||
| 92 | sub _load { | ||
| 93 | my $class = shift; | ||
| 94 | my $rule = shift; | ||
| 95 | |||
| 96 | # See if the requested object is loaded. | ||
| 97 | my @loaded = $UR::Context::current->get_objects_for_class_and_rule($class,$rule,0); | ||
| 98 | return $class->context_return(@loaded) if @loaded; | ||
| 99 | |||
| 100 | # Auto generate the object on the fly. | ||
| 101 | my $id = $rule->value_for_id; | ||
| 102 | unless (defined $id) { | ||
| 103 | #$DB::single = 1; | ||
| 104 | Carp::croak "No id specified for loading members of an infinite set ($class)!" | ||
| 105 | } | ||
| 106 | my $class_meta = $class->__meta__; | ||
| 107 | my @p = (id => $id); | ||
| 108 | if (my $alt_ids = $class_meta->{id_by}) { | ||
| 109 | if (@$alt_ids == 1) { | ||
| 110 | push @p, $alt_ids->[0] => $id; | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | my ($rule, %extra) = UR::BoolExpr->resolve_normalized($class, $rule); | ||
| 114 | push @p, $rule->params_list; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | my $obj = $UR::Context::current->_construct_object($class, @p); | ||
| 119 | |||
| 120 | if (my $method_name = $class_meta->sub_classification_method_name) { | ||
| 121 | my($rule, %extra) = UR::BoolExpr->resolve_normalized($class, $rule); | ||
| 122 | my $sub_class_name = $obj->$method_name; | ||
| 123 | if ($sub_class_name ne $class) { | ||
| 124 | # delegate to the sub-class to create the object | ||
| 125 | $UR::Context::current->_abandon_object($obj); | ||
| 126 | $obj = $UR::Context::current->_construct_object($sub_class_name,$rule); | ||
| 127 | $obj->__signal_change__("load"); | ||
| 128 | return $obj; | ||
| 129 | } | ||
| 130 | # fall through if the class names match | ||
| 131 | } | ||
| 132 | |||
| 133 | $obj->__signal_change__("load"); | ||
| 134 | return $obj; | ||
| 135 | } | ||
| 136 | |||
| 137 | # these hash keys are probably removable | ||
| 138 | # because they are not above, they will be deleted if _init sets them | ||
| 139 | # this exists primarily as a cleanup target list | ||
| 140 | my @extra = qw( | ||
| 141 | id_properties | ||
| 142 | direct_table_properties | ||
| 143 | all_table_properties | ||
| 144 | sub_classification_method_name | ||
| 145 | subclassify_by | ||
| 146 | properties_meta_in_resultset_order | ||
| 147 | all_properties | ||
| 148 | rule_specifies_id | ||
| 149 | all_id_property_names | ||
| 150 | id_property_sorter | ||
| 151 | properties_for_params | ||
| 152 | first_table_name | ||
| 153 | base_joins | ||
| 154 | parent_class_objects | ||
| 155 | ); | ||
| 156 | |||
| 157 | sub _init { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | Carp::confess("already initialized???") if $self->_is_initialized; | ||
| 161 | |||
| 162 | # We could have this sub-classify by data source type, but right | ||
| 163 | # now it's conditional logic because we'll likely remove the distinctions. | ||
| 164 | # This will work because we'll separate out the ds-specific portion | ||
| 165 | # and call methods on the DS to get that part. | ||
| 166 | my $ds = $self->data_source; | ||
| 167 | if ($ds->isa("UR::DataSource::RDBMS")) { | ||
| 168 | $self->_init_light(); | ||
| 169 | $self->_init_rdbms(); | ||
| 170 | } | ||
| 171 | elsif ($ds->isa('UR::DataSource::Filesystem')) { | ||
| 172 | $self->_init_core(); | ||
| 173 | $self->_init_filesystem(); | ||
| 174 | } | ||
| 175 | else { | ||
| 176 | # Once all callers are using the API for this we won't need "_init". | ||
| 177 | $self->_init_core(); | ||
| 178 | $self->_init_default() if $ds->isa("UR::DataSource::Default"); | ||
| 179 | #$self->_init_remote_cache() if $ds->isa("UR::DataSource::RemoteCache"); | ||
| 180 | } | ||
| 181 | |||
| 182 | # This object is currently still used as a hashref, but the properties | ||
| 183 | # are a declaration of the part of the hashref data we are still dependent upon. | ||
| 184 | # This removes the other properties to ensure this is the case. | ||
| 185 | # Next steps are to clean up the code below to not produce the data, | ||
| 186 | # then this loop can throw an exception if extra untracked data is found. | ||
| 187 | for my $key (keys %$self) { | ||
| 188 | next if $self->can($key); | ||
| 189 | delete $self->{$key}; | ||
| 190 | } | ||
| 191 | |||
| 192 | $self->_is_initialized(1); | ||
| 193 | return $self; | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | sub _determine_complete_order_by_list { | ||
| 198 | my($self, $rule_template, $class_data, $db_property_data) = @_; | ||
| 199 | |||
| 200 | my $class_meta = $rule_template->subject_class_name->__meta__; | ||
| 201 | my $order_by_columns = $class_data->{order_by_columns} || []; | ||
| 202 | my $order_by = $rule_template->order_by; | ||
| 203 | my $ds = $self->data_source; | ||
| 204 | |||
| 205 | my %order_by_property_names; | ||
| 206 | my $order_by_non_column_data; | ||
| 207 | if ($order_by) { | ||
| 208 | my %db_property_data_map = map { $_->[1]->property_name => $_ } @$db_property_data; | ||
| 209 | |||
| 210 | # we only pull back columns we're ordering by if there is ordering happening | ||
| 211 | my %is_descending; | ||
| 212 | my @column_data; | ||
| 213 | for my $name (@$order_by) { | ||
| 214 | my $order_by_prop = $name; | ||
| 215 | if ($order_by_prop =~ m/^(-|\+)(.*)$/) { | ||
| 216 | $order_by_prop = $2; | ||
| 217 | $is_descending{$order_by_prop} = $1 eq '-'; | ||
| 218 | } | ||
| 219 | |||
| 220 | my($order_by_prop_meta) = $class_meta->_concrete_property_meta_for_class_and_name($order_by_prop); | ||
| 221 | unless ($order_by_prop_meta) { | ||
| 222 | Carp::croak("Cannot order by '$name': Class " | ||
| 223 | . $class_meta->class_name | ||
| 224 | . " has no property named '$order_by_prop'"); | ||
| 225 | } | ||
| 226 | |||
| 227 | $name = ( $is_descending{$order_by_prop} ? '-' : '' ) . $order_by_prop_meta->property_name; | ||
| 228 | if ($order_by_property_names{$name} = $db_property_data_map{$order_by_prop_meta->property_name}) { # yes, single = | ||
| 229 | push @column_data, $order_by_property_names{$name}; | ||
| 230 | |||
| 231 | my $table_column_names = $ds->_select_clause_columns_for_table_property_data($column_data[-1]); | ||
| 232 | $is_descending{$table_column_names->[0]} = $is_descending{$order_by_prop}; # copy for table.column designation | ||
| 233 | $order_by_property_names{$table_column_names->[0]} = $order_by_property_names{$name}; | ||
| 234 | } else { | ||
| 235 | $order_by_non_column_data = 1; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | if (@column_data) { | ||
| 240 | my $additional_order_by_columns = $ds->_select_clause_columns_for_table_property_data(@column_data); | ||
| 241 | |||
| 242 | # Strip out columns named in the original $order_by_columns list that now appear in the | ||
| 243 | # additional order by list so we don't duplicate columns names, and the additional columns | ||
| 244 | # appear earlier in the list | ||
| 245 | my %additional_order_by_columns = map { $_ => 1 } @$additional_order_by_columns; | ||
| 246 | my @existing_order_by_columns = grep { ! $additional_order_by_columns{$_} } @$order_by_columns; | ||
| 247 | $order_by_columns = [ map { $is_descending{$_} ? '-'. $_ : $_ } ( @$additional_order_by_columns, @existing_order_by_columns ) ]; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | $self->_order_by_property_names(\%order_by_property_names); | ||
| 251 | return ($order_by_columns, $order_by_non_column_data); | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub _init_rdbms { | ||
| 256 | my $self = shift; | ||
| 257 | my $rule_template = $self->rule_template; | ||
| 258 | my $ds = $self->data_source; | ||
| 259 | |||
| 260 | # class-based values | ||
| 261 | my $class_name = $rule_template->subject_class_name; | ||
| 262 | my $class_meta = $class_name->__meta__; | ||
| 263 | my $class_data = $ds->_get_class_data_for_loading($class_meta); | ||
| 264 | |||
| 265 | my @parent_class_objects = @{ $class_data->{parent_class_objects} }; | ||
| 266 | my @all_id_property_names = @{ $class_data->{all_id_property_names} }; | ||
| 267 | my @id_properties = @{ $class_data->{id_properties} }; | ||
| 268 | |||
| 269 | #my $first_table_name = $class_data->{first_table_name}; | ||
| 270 | |||
| 271 | #my $id_property_sorter = $class_data->{id_property_sorter}; | ||
| 272 | #my @lob_column_names = @{ $class_data->{lob_column_names} }; | ||
| 273 | my @lob_column_positions = @{ $class_data->{lob_column_positions} }; | ||
| 274 | #my $query_config = $class_data->{query_config}; | ||
| 275 | #my $post_process_results_callback = $class_data->{post_process_results_callback}; | ||
| 276 | #my $class_table_name = $class_data->{class_table_name}; | ||
| 277 | |||
| 278 | # individual template based | ||
| 279 | my $hints = $rule_template->hints; | ||
| 280 | my %hints = map { $_ => 1 } @$hints; | ||
| 281 | my $order_by = $rule_template->order_by; | ||
| 282 | my $group_by = $rule_template->group_by; | ||
| 283 | my $aggregate = $rule_template->aggregate; | ||
| 284 | my $recursion_desc = $rule_template->recursion_desc; | ||
| 285 | |||
| 286 | my ($first_table_name, @db_joins) = _resolve_db_joins_for_inheritance($class_meta); | ||
| 287 | |||
| 288 | $self->_db_joins(\@db_joins); | ||
| 289 | $self->_obj_joins([]); | ||
| 290 | |||
| 291 | # an array of arrays, containing $table_name, $column_name, $alias, $object_num | ||
| 292 | # as joins are done we extend this, and then condense it into object fabricators | ||
| 293 | my @db_property_data = @{ $class_data->{all_table_properties} }; | ||
| 294 | |||
| 295 | my %group_by_property_names; | ||
| 296 | if ($group_by) { | ||
| 297 | # we only pull back columns we're grouping by or aggregating if there is grouping happening | ||
| 298 | for my $name (@$group_by) { | ||
| 299 | unless ($class_name->can($name)) { | ||
| 300 | Carp::croak("Cannot group by '$name': Class $class_name has no property/method by that name"); | ||
| 301 | } | ||
| 302 | $group_by_property_names{$name} = 1; | ||
| 303 | } | ||
| 304 | for my $data (@db_property_data) { | ||
| 305 | my $name = $data->[1]->property_name; | ||
| 306 | if ($group_by_property_names{$name}) { | ||
| 307 | $group_by_property_names{$name} = $data; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | @db_property_data = grep { ref($_) } values %group_by_property_names; | ||
| 311 | } | ||
| 312 | |||
| 313 | my($order_by_columns, $order_by_non_column_data) | ||
| 314 | = $self->_determine_complete_order_by_list($rule_template, $class_data,\@db_property_data); | ||
| 315 | |||
| 316 | $self->_db_column_data(\@db_property_data); | ||
| 317 | $self->_group_by_property_names(\%group_by_property_names); | ||
| 318 | |||
| 319 | # Find out what delegated properties we'll be dealing with | ||
| 320 | my @sql_filters; | ||
| 321 | my @delegated_properties; | ||
| 322 | do { | ||
| 323 | my %filters = | ||
| 324 | map { $_ => $rule_template->operator_for($_) } | ||
| 325 | grep { substr($_,0,1) ne '-' } | ||
| 326 | $rule_template->_property_names; | ||
| 327 | |||
| 328 | unless (@all_id_property_names == 1 && $all_id_property_names[0] eq "id") { | ||
| 329 | delete $filters{'id'}; | ||
| 330 | } | ||
| 331 | |||
| 332 | # Remove the flag for descending/ascending sort | ||
| 333 | my @order_by_properties = $order_by ? @$order_by : (); | ||
| 334 | s/^-|\+// foreach @order_by_properties; | ||
| 335 | |||
| 336 | my %properties_involved = map { $_ => 1 } | ||
| 337 | keys(%filters), | ||
| 338 | ($hints ? @$hints : ()), | ||
| 339 | @order_by_properties, | ||
| 340 | ($group_by ? @$group_by : ()); | ||
| 341 | |||
| 342 | my @properties_involved = sort keys(%properties_involved); | ||
| 343 | my @errors; | ||
| 344 | while (my $property_name = shift @properties_involved) { | ||
| 345 | if (index($property_name,'.') != -1) { | ||
| 346 | push @delegated_properties, $property_name; | ||
| 347 | next; | ||
| 348 | } | ||
| 349 | |||
| 350 | my (@pmeta) = $class_meta->property_meta_for_name($property_name); | ||
| 351 | unless (@pmeta) { | ||
| 352 | if ($class_name->can($property_name)) { | ||
| 353 | # method, not property | ||
| 354 | next; | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | push @errors, "Class ".$class_meta->id." has no property or method named '$property_name'"; | ||
| 358 | next; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | # For each property in this list, go up the inheritance and find the right property | ||
| 363 | # to query on. Give priority to properties that actually have columns | ||
| 364 | FIND_PROPERTY_WITH_COLUMN: | ||
| 365 | foreach my $pmeta ( @pmeta ) { | ||
| 366 | foreach my $candidate_class ( $class_meta->all_class_metas ) { | ||
| 367 | my $candidate_prop_meta = UR::Object::Property->get(class_name => $candidate_class->class_name, | ||
| 368 | property_name => $property_name); | ||
| 369 | next unless $candidate_prop_meta; | ||
| 370 | if ($candidate_prop_meta->column_name) { | ||
| 371 | $pmeta = $candidate_prop_meta; | ||
| 372 | next FIND_PROPERTY_WITH_COLUMN; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | my $property = $pmeta[0]; | ||
| 378 | my $table_name = $property->class_meta->first_table_name; | ||
| 379 | my $operator = $rule_template->operator_for($property_name); | ||
| 380 | my $value_position = $rule_template->value_position_for_property_name($property_name); | ||
| 381 | |||
| 382 | if ($property->can("expr_sql")) { | ||
| 383 | unless ($table_name) { | ||
| 384 | $ds->warning_message("Property '$property_name' of class '$class_name' can 'expr_sql' but has no table!"); | ||
| 385 | next; | ||
| 386 | } | ||
| 387 | my $expr_sql = $property->expr_sql; | ||
| 388 | if (exists $filters{$property_name}) { | ||
| 389 | my @coercion = $self->data_source->cast_for_data_conversion( | ||
| 390 | $property->_data_type_as_class_name, | ||
| 391 | 'UR::Value::String', # We can't know here what the type should be | ||
| 392 | $operator, | ||
| 393 | 'where'); | ||
| 394 | push @sql_filters, | ||
| 395 | $table_name => { | ||
| 396 | # cheap hack of prefixing with a whitespace differentiates | ||
| 397 | # from a regular column below | ||
| 398 | " " . $expr_sql => { | ||
| 399 | operator => $operator, | ||
| 400 | value_position => $value_position, | ||
| 401 | left_coercion => $coercion[0], | ||
| 402 | right_coercion => $coercion[1], | ||
| 403 | } | ||
| 404 | }; | ||
| 405 | } | ||
| 406 | next; | ||
| 407 | } | ||
| 408 | |||
| 409 | # If the property is calculate and has a calculate_from list, add the | ||
| 410 | # calculate_from things to the internal hints list, but not the template | ||
| 411 | if ($property->is_calculated and $property->calculate_from) { | ||
| 412 | my $calculate_from = $property->calculate_from; | ||
| 413 | push @properties_involved, @$calculate_from; | ||
| 414 | push @$hints, @$calculate_from; | ||
| 415 | $hints{$_} = 1 foreach @$calculate_from; | ||
| 416 | } | ||
| 417 | |||
| 418 | if (exists($filters{$property_name}) and $filters{$property_name} eq 'isa') { | ||
| 419 | # RDBMS databases can't do 'isa' | ||
| 420 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 421 | next; | ||
| 422 | } | ||
| 423 | elsif (my $column_name = $property->column_name) { | ||
| 424 | # normal column: filter on it | ||
| 425 | unless ($table_name) { | ||
| 426 | $ds->warning_message("Property '$property_name' of class '$class_name' has column '$column_name' but has no table!"); | ||
| 427 | next; | ||
| 428 | } | ||
| 429 | if (exists $filters{$property_name}) { | ||
| 430 | my @coercion = $self->data_source->cast_for_data_conversion( | ||
| 431 | $property->_data_type_as_class_name, | ||
| 432 | 'UR::Value::String', # We can't know here what the type should be | ||
| 433 | $operator, | ||
| 434 | 'where'); | ||
| 435 | push @sql_filters, | ||
| 436 | $table_name => { | ||
| 437 | $column_name => { | ||
| 438 | operator => $operator, | ||
| 439 | value_position => $value_position, | ||
| 440 | left_coercion => $coercion[0], | ||
| 441 | right_coercion => $coercion[1], | ||
| 442 | } | ||
| 443 | }; | ||
| 444 | } | ||
| 445 | } | ||
| 446 | elsif ($property->is_delegated) { | ||
| 447 | push @delegated_properties, $property->property_name; | ||
| 448 | } | ||
| 449 | elsif ( ! exists($hints{$property_name}) or exists($filters{$property_name}) ) { | ||
| 450 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 451 | } | ||
| 452 | else { | ||
| 453 | next; | ||
| 454 | } | ||
| 455 | |||
| 456 | } # end of properties in the expression which control the query content | ||
| 457 | |||
| 458 | if (@errors) { | ||
| 459 | my $class_name = $class_meta->class_name; | ||
| 460 | $ds->error_message("ERRORS PROCESSING PARAMTERS: (" . join("\n", @errors) . ") used to generate SQL for $class_name!"); | ||
| 461 | #print Data::Dumper::Dumper($rule_template); | ||
| 462 | Carp::croak("Can't continue"); | ||
| 463 | } | ||
| 464 | }; | ||
| 465 | |||
| 466 | my $object_num = 0; | ||
| 467 | $self->_alias_count(0); | ||
| 468 | |||
| 469 | my %hints_included; | ||
| 470 | my @select_hint; | ||
| 471 | |||
| 472 | # FIXME - this needs to be broken out into delegated-property-join-resolver | ||
| 473 | # and inheritance-join-resolver methods that can be called recursively. | ||
| 474 | # It would better encapsulate what's going on and avoid bugs with complicated | ||
| 475 | # get()s | ||
| 476 | |||
| 477 | # one iteration per target value involved in the query, | ||
| 478 | # including values needed for filtering, ordering, grouping, and hints (selecting more) | ||
| 479 | # these "properties" may be a single property name or an ad-hoc "chain" | ||
| 480 | DELEGATED_PROPERTY: | ||
| 481 | for my $delegated_property (sort @delegated_properties) { | ||
| 482 | my $property_name = $delegated_property; | ||
| 483 | my $delegation_chain_data = $self->_delegation_chain_data || $self->_delegation_chain_data({}); | ||
| 484 | $delegation_chain_data->{"__all__"}{table_alias} = {}; | ||
| 485 | $delegation_chain_data->{"__all__"}{class_alias} = { $first_table_name => $class_meta }; | ||
| 486 | |||
| 487 | my ($final_accessor, $is_optional, @joins) = _resolve_object_join_data_for_property_chain($rule_template,$property_name,$property_name); | ||
| 488 | |||
| 489 | # when there is no "final_accessor" it often means we have an object-accessor in a hint | ||
| 490 | # we want that to go through the join process, and only be left out at filter construction time | ||
| 491 | #unless ($final_accessor) { | ||
| 492 | #$self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 493 | #next; | ||
| 494 | #} | ||
| 495 | |||
| 496 | # this is gathered here and used below, but previously was gathered internally to the methods which take it | ||
| 497 | # since it is no longer needed directly in this method it might be refactored into the places which use it | ||
| 498 | my %ds_for_class; | ||
| 499 | for my $join (@joins) { | ||
| 500 | my $source_class_object = $join->{'source_class'}->__meta__; | ||
| 501 | my ($source_data_source) = UR::Context->resolve_data_sources_for_class_meta_and_rule($source_class_object, $rule_template); | ||
| 502 | $ds_for_class{$join->{'source_class'}} = $source_data_source; | ||
| 503 | |||
| 504 | my $foreign_class_object = $join->{'foreign_class'}->__meta__; | ||
| 505 | my ($foreign_data_source) = UR::Context->resolve_data_sources_for_class_meta_and_rule($foreign_class_object, $rule_template); | ||
| 506 | $ds_for_class{$join->{'foreign_class'}} = $foreign_data_source; | ||
| 507 | } | ||
| 508 | |||
| 509 | |||
| 510 | # Splice out joins that go through a UR::Value class and back out to the DB, since UR::Value-types | ||
| 511 | # don't get stored in the DB | ||
| 512 | # TODO: move this into the join creation logic | ||
| 513 | for (my $i = 0; $i < @joins; $i++) { | ||
| 514 | if ( | ||
| 515 | $i < $#joins | ||
| 516 | and | ||
| 517 | ( | ||
| 518 | # db -> UR::Value -> db : shortcut | ||
| 519 | $joins[$i]->{'foreign_class'}->isa('UR::Value') | ||
| 520 | and $joins[$i+1]->{'source_class'}->isa('UR::Value') | ||
| 521 | #and $joins[$i]->{'foreign_class'}->isa($joins[$i+1]->{'source_class'}) ## remove this? | ||
| 522 | ) | ||
| 523 | ) { | ||
| 524 | my $fixed_join = UR::Object::Join->_get_or_define( | ||
| 525 | source_class => $joins[$i]->{'source_class'}, | ||
| 526 | source_property_names => $joins[$i]->{'source_property_names'}, | ||
| 527 | foreign_class => $joins[$i+1]->{'foreign_class'}, | ||
| 528 | foreign_property_names => $joins[$i+1]->{'foreign_property_names'}, | ||
| 529 | is_optional => $joins[$i]->{'is_optional'}, | ||
| 530 | id => $joins[$i]->{id} . "->" . $joins[$i+1]->{id}); | ||
| 531 | if ($joins[$i+1]->{where}) { | ||
| 532 | # If there's a where involved, it will always be on the second thing, | ||
| 533 | # where the foreign_class is NOT a UR::Value | ||
| 534 | $fixed_join->{where} = $joins[$i+1]->{where}; | ||
| 535 | } | ||
| 536 | splice(@joins, $i, 2, $fixed_join); | ||
| 537 | } | ||
| 538 | } | ||
| 539 | |||
| 540 | if (@joins and $joins[-1]{foreign_class}->isa("UR::Value")) { | ||
| 541 | # the final join in a chain is often the link between a primitive value | ||
| 542 | # and the UR::Value subclass into which it falls ...irrelevent for db joins | ||
| 543 | $final_accessor = $joins[-1]->source_property_names->[0]; | ||
| 544 | pop @joins; | ||
| 545 | next DELEGATED_PROPERTY unless @joins; | ||
| 546 | } | ||
| 547 | |||
| 548 | my $last_class_object_excluding_inherited_joins; | ||
| 549 | my $alias_for_property_value; | ||
| 550 | |||
| 551 | # one iteration per table between the start table and target | ||
| 552 | while (my $object_join = shift @joins) { | ||
| 553 | $object_num++; | ||
| 554 | my @joins_for_object = ($object_join); | ||
| 555 | |||
| 556 | # one iteration per layer of inheritance for this object | ||
| 557 | # or per case of a join having additional filtering | ||
| 558 | my $current_inheritance_depth_for_this_target_join = 0; | ||
| 559 | while (my $join = shift @joins_for_object) { | ||
| 560 | |||
| 561 | my $where = $join->{where}; | ||
| 562 | |||
| 563 | $current_inheritance_depth_for_this_target_join++; | ||
| 564 | |||
| 565 | my $foreign_class_name = $join->{foreign_class}; | ||
| 566 | my $foreign_class_object = $join->{'foreign_class_meta'} || $foreign_class_name->__meta__; | ||
| 567 | |||
| 568 | if ($foreign_class_object->join_hint and !($hints_included{$foreign_class_name}++)) { | ||
| 569 | push @select_hint, $foreign_class_object->join_hint; | ||
| 570 | } | ||
| 571 | |||
| 572 | if (not exists $ds_for_class{$foreign_class_name}) { | ||
| 573 | # error: we should have at least a key with an empty value if we tried to find the ds | ||
| 574 | die "no data source key for $foreign_class_name when adding a join?" | ||
| 575 | } | ||
| 576 | |||
| 577 | my $ds = $ds_for_class{$foreign_class_name}; | ||
| 578 | |||
| 579 | if (not $ds) { | ||
| 580 | # no ds for the next piece of data: we will have to resolve this on the client side | ||
| 581 | # this is where things may get slow if the query is insufficiently filtered | ||
| 582 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 583 | next DELEGATED_PROPERTY; | ||
| 584 | } | ||
| 585 | |||
| 586 | my $alias = $self->_add_join( | ||
| 587 | $delegated_property, | ||
| 588 | $join, | ||
| 589 | $object_num, | ||
| 590 | $is_optional, | ||
| 591 | $final_accessor, | ||
| 592 | $ds_for_class{$foreign_class_name}, | ||
| 593 | ); | ||
| 594 | |||
| 595 | if (not $alias) { | ||
| 596 | # unable to add a join for another reason | ||
| 597 | # TODO: is the above the only valid case of a join being impossible? | ||
| 598 | # Can we remove this? | ||
| 599 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 600 | next DELEGATED_PROPERTY; | ||
| 601 | } | ||
| 602 | |||
| 603 | # set these for after all of the joins are done | ||
| 604 | my $last_class_name = $foreign_class_name; | ||
| 605 | my $last_class_object = $foreign_class_object; | ||
| 606 | |||
| 607 | # on the first iteration, we figure out the remaining inherited iterations | ||
| 608 | # if there is inheritance to do, unshift those onto the stack ahead of other things | ||
| 609 | if ($current_inheritance_depth_for_this_target_join == 1) { | ||
| 610 | if ($final_accessor and $last_class_object->property_meta_for_name($final_accessor)) { | ||
| 611 | $last_class_object_excluding_inherited_joins = $last_class_object; | ||
| 612 | } | ||
| 613 | my @parents = grep { $_->table_name } $foreign_class_object->ancestry_class_metas; | ||
| 614 | if (@parents) { | ||
| 615 | my @last_id_property_names = $foreign_class_object->id_property_names; | ||
| 616 | for my $parent (@parents) { | ||
| 617 | my @parent_id_property_names = $parent->id_property_names; | ||
| 618 | die if @parent_id_property_names > 1; | ||
| 619 | my $parent_join_foreign_class_name = $parent->class_name; | ||
| 620 | my $inheritance_join = UR::Object::Join->_get_or_define( | ||
| 621 | source_class => $last_class_name, | ||
| 622 | source_property_names => [@last_id_property_names], # we change content below | ||
| 623 | foreign_class => $parent_join_foreign_class_name, | ||
| 624 | foreign_property_names => \@parent_id_property_names, | ||
| 625 | is_optional => $is_optional, | ||
| 626 | id => "${last_class_name}::" . join(',',@last_id_property_names), | ||
| 627 | ); | ||
| 628 | unshift @joins_for_object, $inheritance_join; | ||
| 629 | @last_id_property_names = @parent_id_property_names; | ||
| 630 | $last_class_name = $foreign_class_name; | ||
| 631 | |||
| 632 | my $foreign_class_object = $parent_join_foreign_class_name->__meta__; | ||
| 633 | my ($foreign_data_source) = UR::Context->resolve_data_sources_for_class_meta_and_rule($foreign_class_object, $rule_template); | ||
| 634 | $ds_for_class{$parent_join_foreign_class_name} = $foreign_data_source; | ||
| 635 | } | ||
| 636 | next; | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | if (!@joins and not $alias_for_property_value) { | ||
| 641 | # we are out of joins for this delegated property | ||
| 642 | # setting $alias_for_property_value helps map to exactly where we do real filter/order/etc. | ||
| 643 | my $foreign_class_loading_data = $ds->_get_class_data_for_loading($foreign_class_object); | ||
| 644 | if ($final_accessor and | ||
| 645 | grep { $_->[1]->property_name eq $final_accessor } @{ $foreign_class_loading_data->{direct_table_properties} } | ||
| 646 | ) { | ||
| 647 | $alias_for_property_value = $alias; | ||
| 648 | #print "found alias for $property_name on $foreign_class_name: $alias\n"; | ||
| 649 | } | ||
| 650 | else { | ||
| 651 | # The thing we're joining to isn't a database-backed column (maybe calculated?) | ||
| 652 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 653 | next DELEGATED_PROPERTY; | ||
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | } # next join in the inheritance for this object | ||
| 658 | |||
| 659 | } # next join across objects from the query subject to the delegated property target | ||
| 660 | |||
| 661 | # done adding any new joins for this delegated property/property-chain | ||
| 662 | |||
| 663 | # now see if anything in the where-clause needs to filter on the item joined-to | ||
| 664 | my $value_position = $rule_template->value_position_for_property_name($property_name); | ||
| 665 | if (defined $value_position) { | ||
| 666 | # this property _is_ used to filter results | ||
| 667 | if (not $final_accessor) { | ||
| 668 | # on the client side :( | ||
| 669 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 670 | next; | ||
| 671 | } | ||
| 672 | else { | ||
| 673 | # at the database level :) | ||
| 674 | my $final_accessor_property_meta = $last_class_object_excluding_inherited_joins->property_meta_for_name($final_accessor); | ||
| 675 | unless ($final_accessor_property_meta) { | ||
| 676 | Carp::croak("No property metadata for property named '$final_accessor' in class " | ||
| 677 | . $last_class_object_excluding_inherited_joins->class_name | ||
| 678 | . " while resolving joins for property '" . $delegated_property->property_name . "' in class " | ||
| 679 | . $delegated_property->class_name); | ||
| 680 | } | ||
| 681 | |||
| 682 | my $sql_lvalue; | ||
| 683 | if ($final_accessor_property_meta->is_calculated) { | ||
| 684 | $sql_lvalue = $final_accessor_property_meta->calculate_sql; | ||
| 685 | unless (defined($sql_lvalue)) { | ||
| 686 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 687 | next; | ||
| 688 | } | ||
| 689 | } | ||
| 690 | else { | ||
| 691 | $sql_lvalue = $final_accessor_property_meta->column_name; | ||
| 692 | unless (defined($sql_lvalue)) { | ||
| 693 | Carp::confess("No column name set for non-delegated/calculated property $property_name of $class_name"); | ||
| 694 | } | ||
| 695 | } | ||
| 696 | |||
| 697 | my $operator = $rule_template->operator_for($property_name); | ||
| 698 | |||
| 699 | unless ($alias_for_property_value) { | ||
| 700 | die "No alias found for $property_name?!"; | ||
| 701 | } | ||
| 702 | |||
| 703 | my @coercion = $self->data_source->cast_for_data_conversion( | ||
| 704 | $final_accessor_property_meta->_data_type_as_class_name, | ||
| 705 | 'UR::Value::String', # We can't know here what the type should be | ||
| 706 | $operator, | ||
| 707 | 'where'); | ||
| 708 | |||
| 709 | push @sql_filters, | ||
| 710 | $alias_for_property_value => { | ||
| 711 | $sql_lvalue => { | ||
| 712 | operator => $operator, | ||
| 713 | value_position => $value_position, | ||
| 714 | left_coercion => $coercion[0], | ||
| 715 | right_coercion => $coercion[1], | ||
| 716 | } | ||
| 717 | }; | ||
| 718 | } | ||
| 719 | } | ||
| 720 | |||
| 721 | } # next delegated property | ||
| 722 | |||
| 723 | # the columns to query | ||
| 724 | my $db_property_data = $self->_db_column_data; | ||
| 725 | |||
| 726 | # the following two sets of variables hold the net result of the logic | ||
| 727 | my $select_clause; | ||
| 728 | my $from_clause; | ||
| 729 | my $connect_by_clause; | ||
| 730 | my $group_by_clause; | ||
| 731 | |||
| 732 | # Build the SELECT clause explicitly. | ||
| 733 | $select_clause = $ds->_select_clause_for_table_property_data(@$db_property_data); | ||
| 734 | |||
| 735 | # Oracle places group_by in a comment in the select | ||
| 736 | unshift(@select_hint, $class_meta->select_hint) if $class_meta->select_hint; | ||
| 737 | |||
| 738 | # Build the FROM clause base. | ||
| 739 | # Add joins to the from clause as necessary, then | ||
| 740 | $from_clause = (defined $first_table_name ? "$first_table_name" : ''); | ||
| 741 | |||
| 742 | my $cnt = 0; | ||
| 743 | my @sql_params; | ||
| 744 | my @sql_joins = @{ $self->_db_joins }; | ||
| 745 | while (@sql_joins) { | ||
| 746 | my $table_name = shift (@sql_joins); | ||
| 747 | my $condition = shift (@sql_joins); | ||
| 748 | my ($table_alias) = ($table_name =~ /(\S+)\s*$/s); | ||
| 749 | |||
| 750 | my $join_type; | ||
| 751 | if ($condition->{-is_required}) { | ||
| 752 | $join_type = 'INNER'; | ||
| 753 | } | ||
| 754 | else { | ||
| 755 | $join_type = 'LEFT'; | ||
| 756 | } | ||
| 757 | |||
| 758 | $from_clause .= "\n$join_type join " . $table_name . " on "; | ||
| 759 | # Restart the counter on each join for the from clause, | ||
| 760 | # but for the where clause keep counting w/o reset. | ||
| 761 | $cnt = 0; | ||
| 762 | |||
| 763 | for my $column_name (keys %$condition) { | ||
| 764 | next if substr($column_name,0,1) eq '-'; | ||
| 765 | |||
| 766 | my $linkage_data = $condition->{$column_name}; | ||
| 767 | my $expr_sql = (substr($column_name,0,1) eq " " ? $column_name : "${table_alias}.${column_name}"); | ||
| 768 | my ($operator, $value_position, $value, $link_table_name, $link_column_name, $left_coercion, $right_coercion) | ||
| 769 | = @$linkage_data{qw/operator value_position value link_table_name link_column_name left_coercion right_coercion/}; | ||
| 770 | |||
| 771 | $expr_sql = sprintf($right_coercion, $expr_sql) if ($right_coercion); | ||
| 772 | |||
| 773 | $from_clause .= "\n and " if ($cnt++); | ||
| 774 | |||
| 775 | if ($link_table_name and $link_column_name) { | ||
| 776 | # the linkage data is a join specifier | ||
| 777 | my $link_sql = "${link_table_name}.${link_column_name}"; | ||
| 778 | $link_sql = sprintf($left_coercion, $link_sql) if ($left_coercion); | ||
| 779 | $from_clause .= "$link_sql = $expr_sql"; | ||
| 780 | } | ||
| 781 | elsif (defined $value_position) { | ||
| 782 | Carp::croak("Joins cannot use variable values currently!"); | ||
| 783 | } | ||
| 784 | else { | ||
| 785 | my ($more_sql, @more_params) = $ds->_extend_sql_for_column_operator_and_value($expr_sql, $operator, $value); | ||
| 786 | if ($more_sql) { | ||
| 787 | $from_clause .= $more_sql; | ||
| 788 | push @sql_params, @more_params; | ||
| 789 | } | ||
| 790 | else { | ||
| 791 | # error | ||
| 792 | return; | ||
| 793 | } | ||
| 794 | } | ||
| 795 | } # next column | ||
| 796 | } # next db join | ||
| 797 | |||
| 798 | # build the WHERE clause by making a data structure which will be parsed outside of this module | ||
| 799 | # special handling of different size lists, and NULLs, make a completely reusable SQL template very hard. | ||
| 800 | my @filter_specs; | ||
| 801 | while (@sql_filters) { | ||
| 802 | my $table_name = shift (@sql_filters); | ||
| 803 | my $condition = shift (@sql_filters); | ||
| 804 | my ($table_alias) = ($table_name =~ /(\S+)\s*$/s); | ||
| 805 | |||
| 806 | for my $column_name (keys %$condition) { | ||
| 807 | my $linkage_data = $condition->{$column_name}; | ||
| 808 | my $expr_sql = (substr($column_name,0,1) eq " " ? $column_name : "${table_alias}.${column_name}"); | ||
| 809 | my ($operator, $value_position, $value, $link_table_name, $link_column_name, $left_coercion, $right_coercion) | ||
| 810 | = @$linkage_data{qw/operator value_position value link_table_name | ||
| 811 | link_column_name left_coercion right_coercion/}; | ||
| 812 | |||
| 813 | if ($link_table_name and $link_column_name) { | ||
| 814 | # the linkage data is a join specifier | ||
| 815 | Carp::confess("explicit column linkage in where clause?"); | ||
| 816 | #$sql .= "${link_table_name}.${link_column_name} = $expr_sql"; | ||
| 817 | } | ||
| 818 | else { | ||
| 819 | # the linkage data is a value position from the @values list | ||
| 820 | unless (defined $value_position) { | ||
| 821 | Carp::confess("No value position for $column_name in query!"); | ||
| 822 | } | ||
| 823 | |||
| 824 | $expr_sql = sprintf($left_coercion, $expr_sql); | ||
| 825 | |||
| 826 | push @filter_specs, [$expr_sql, $operator, $value_position]; | ||
| 827 | } | ||
| 828 | } # next column | ||
| 829 | } # next db filter | ||
| 830 | |||
| 831 | $connect_by_clause = ''; | ||
| 832 | my $recurse_resolution_by_iteration = 0; | ||
| 833 | if ($recursion_desc) { | ||
| 834 | unless (ref($recursion_desc) eq 'ARRAY') { | ||
| 835 | Carp::croak("Recursion description must be an arrayref with exactly 2 items"); | ||
| 836 | } | ||
| 837 | if (@$recursion_desc != 2) { | ||
| 838 | Carp::croak("Recursion description must contain exactly 2 items; got ".scalar(@$recursion_desc) | ||
| 839 | . ': ' . join(', ',@$recursion_desc)); | ||
| 840 | } | ||
| 841 | |||
| 842 | # Oracle supports "connect by" queries. | ||
| 843 | if ($ds->does_support_recursive_queries eq 'connect by') { | ||
| 844 | my ($this,$prior) = @{ $recursion_desc }; | ||
| 845 | |||
| 846 | my $this_property_meta = $class_meta->property_meta_for_name($this); | ||
| 847 | unless ($this_property_meta) { | ||
| 848 | Carp::croak("Class ".$class_meta->class_name." has no property named '$this', named in the recursion description"); | ||
| 849 | } | ||
| 850 | my $prior_property_meta = $class_meta->property_meta_for_name($prior); | ||
| 851 | unless ($prior_property_meta) { | ||
| 852 | Carp::croak("Class ".$class_meta->class_name." has no property named '$prior', named in the recursion description"); | ||
| 853 | } | ||
| 854 | |||
| 855 | my $this_class_meta = $this_property_meta->class_meta; | ||
| 856 | my $prior_class_meta = $prior_property_meta->class_meta; | ||
| 857 | |||
| 858 | my $this_table_name = $this_class_meta->table_name; | ||
| 859 | unless ($this_table_name) { | ||
| 860 | Carp::croak("Cannot resolve table name from class ".$class_meta->class_name." and property '$this', named in the recursion description"); | ||
| 861 | } | ||
| 862 | my $prior_table_name = $prior_class_meta->table_name; | ||
| 863 | unless ($prior_table_name) { | ||
| 864 | Carp::croak("Cannot resolve table name from class ".$class_meta->class_name." and property '$prior', named in the recursion description"); | ||
| 865 | } | ||
| 866 | |||
| 867 | my $this_column_name = $this_property_meta->column_name || $this; | ||
| 868 | my $prior_column_name = $prior_property_meta->column_name || $prior; | ||
| 869 | |||
| 870 | $connect_by_clause = "connect by $this_table_name.$this_column_name = prior $prior_table_name.$prior_column_name\n"; | ||
| 871 | } else { | ||
| 872 | $recurse_resolution_by_iteration = 1; | ||
| 873 | } | ||
| 874 | } | ||
| 875 | |||
| 876 | my @property_names_in_resultset_order; | ||
| 877 | for my $property_meta_array (@$db_property_data) { | ||
| 878 | push @property_names_in_resultset_order, $property_meta_array->[1]->property_name; | ||
| 879 | } | ||
| 880 | |||
| 881 | # this is only used when making a real instance object instead of a "set" | ||
| 882 | my $per_object_in_resultset_loading_detail; | ||
| 883 | unless ($group_by) { | ||
| 884 | $per_object_in_resultset_loading_detail = $ds->_generate_loading_templates_arrayref(\@$db_property_data, $self->_obj_joins); | ||
| 885 | } | ||
| 886 | |||
| 887 | if ($group_by) { | ||
| 888 | # when grouping, we're making set objects instead of regular objects | ||
| 889 | # this means that we re-constitute the select clause and add a group_by clause | ||
| 890 | $group_by_clause = 'group by ' . $select_clause if (scalar(@$group_by)); | ||
| 891 | |||
| 892 | # Q: - does it even make sense for the user to specify an order_by in the | ||
| 893 | # get() request for Set objects? If so, then we need to concatonate these order_by_columns | ||
| 894 | # with the ones that already exist in $order_by_columns from the class data | ||
| 895 | # A: - yes, because group by means "return a list of subsets", and this lets you sort the subsets | ||
| 896 | $order_by_columns = $ds->_select_clause_columns_for_table_property_data(@$db_property_data); | ||
| 897 | |||
| 898 | $select_clause .= ', ' if $select_clause; | ||
| 899 | $select_clause .= 'count(*) count'; | ||
| 900 | for my $ag (@$aggregate) { | ||
| 901 | next if $ag eq 'count'; | ||
| 902 | # TODO: translate property names to column names, and skip non-column properties | ||
| 903 | $select_clause .= ', ' . $ag; | ||
| 904 | } | ||
| 905 | unless (@$group_by == @$db_property_data) { | ||
| 906 | print "mismatch table properties vs group by!\n"; | ||
| 907 | } | ||
| 908 | } | ||
| 909 | |||
| 910 | %$self = ( | ||
| 911 | %$self, | ||
| 912 | |||
| 913 | # custom for RDBMS | ||
| 914 | select_clause => $select_clause, | ||
| 915 | select_hint => scalar(@select_hint) ? \@select_hint : undef, | ||
| 916 | from_clause => $from_clause, | ||
| 917 | connect_by_clause => $connect_by_clause, | ||
| 918 | group_by_clause => $group_by_clause, | ||
| 919 | order_by_columns => $order_by_columns, | ||
| 920 | order_by_non_column_data => $order_by_non_column_data, | ||
| 921 | filter_specs => \@filter_specs, | ||
| 922 | sql_params => \@sql_params, | ||
| 923 | recurse_resolution_by_iteration => $recurse_resolution_by_iteration, | ||
| 924 | |||
| 925 | # override defaults in the regular datasource $parent_template_data | ||
| 926 | property_names_in_resultset_order => \@property_names_in_resultset_order, | ||
| 927 | properties_meta_in_resultset_order => $db_property_data, # duplicate?! | ||
| 928 | loading_templates => $per_object_in_resultset_loading_detail, | ||
| 929 | ); | ||
| 930 | |||
| 931 | my $template_data = $rule_template->{loading_data_cache} = $self; | ||
| 932 | return $self; | ||
| 933 | } | ||
| 934 | |||
| 935 | sub _init_filesystem { | ||
| 936 | my $self = shift; | ||
| 937 | my $rule_template = $self->rule_template; | ||
| 938 | my $ds = $self->data_source; | ||
| 939 | |||
| 940 | # class-based values | ||
| 941 | my $class_name = $rule_template->subject_class_name; | ||
| 942 | my $class_meta = $class_name->__meta__; | ||
| 943 | my $class_data = $ds->_get_class_data_for_loading($class_meta); | ||
| 944 | |||
| 945 | my @db_property_data = @{ $class_data->{all_table_properties} }; | ||
| 946 | |||
| 947 | my($order_by_columns, $order_by_non_column_data) | ||
| 948 | = $self->_determine_complete_order_by_list($rule_template, $class_data, \@db_property_data); | ||
| 949 | |||
| 950 | %$self = ( | ||
| 951 | %$self, | ||
| 952 | |||
| 953 | order_by_columns => $order_by_columns, | ||
| 954 | order_by_non_column_data => $order_by_non_column_data, | ||
| 955 | ); | ||
| 956 | |||
| 957 | my $template_data = $rule_template->{loading_data_cache} = $self; | ||
| 958 | return $self; | ||
| 959 | } | ||
| 960 | |||
| 961 | sub _add_join { | ||
| 962 | my ($self, | ||
| 963 | $property_name, | ||
| 964 | $join, | ||
| 965 | $object_num, | ||
| 966 | $is_optional, | ||
| 967 | $final_accessor, | ||
| 968 | $foreign_data_source, | ||
| 969 | ) = @_; | ||
| 970 | |||
| 971 | my $delegation_chain_data = $self->_delegation_chain_data || $self->_delegation_chain_data({}); | ||
| 972 | my $table_alias = $delegation_chain_data->{"__all__"}{table_alias} ||= {}; | ||
| 973 | my $source_table_and_column_names = $delegation_chain_data->{$property_name}{latest_source_table_and_column_names} ||= []; | ||
| 974 | |||
| 975 | my $source_class_name = $join->{source_class}; | ||
| 976 | my $source_class_object = $join->{'source_class_meta'} || $source_class_name->__meta__; | ||
| 977 | |||
| 978 | my $class_alias = $delegation_chain_data->{"__all__"}{class_alias} ||= {}; | ||
| 979 | if (! %$class_alias and $source_class_object->table_name) { | ||
| 980 | $class_alias->{$source_class_object->table_name} = $source_class_object; | ||
| 981 | } | ||
| 982 | |||
| 983 | my $foreign_class_name = $join->{foreign_class}; | ||
| 984 | my $foreign_class_object = $join->{'foreign_class_meta'} || $foreign_class_name->__meta__; | ||
| 985 | |||
| 986 | my $rule_template = $self->rule_template; | ||
| 987 | my $ds = $self->data_source; | ||
| 988 | |||
| 989 | my $group_by = $rule_template->group_by; | ||
| 990 | |||
| 991 | #my($foreign_data_source) = UR::Context->resolve_data_sources_for_class_meta_and_rule($foreign_class_object, $rule_template); | ||
| 992 | if (!$foreign_data_source or ($foreign_data_source ne $ds)) { | ||
| 993 | # FIXME - do something smarter in the future where it can do a join-y thing in memory | ||
| 994 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 995 | return; | ||
| 996 | } | ||
| 997 | |||
| 998 | my $foreign_class_loading_data = $ds->_get_class_data_for_loading($foreign_class_object); | ||
| 999 | |||
| 1000 | # This will get filled in during the first pass, and every time after we've successfully | ||
| 1001 | # performed a join - ie. that the delegated property points directly to a class/property | ||
| 1002 | # that is a real table/column, and not a tableless class or another delegated property | ||
| 1003 | my @source_property_names; | ||
| 1004 | unless (@$source_table_and_column_names) { | ||
| 1005 | @source_property_names = @{ $join->{source_property_names} }; | ||
| 1006 | |||
| 1007 | @$source_table_and_column_names = | ||
| 1008 | map { | ||
| 1009 | if (my($view, $alias) = $ds->parse_view_and_alias_from_inline_view($_->[0])) { | ||
| 1010 | # This "table_name" was actually a bit of SQL with an inline view and an alias | ||
| 1011 | $_->[0] = $view; | ||
| 1012 | $_->[2] = $alias; | ||
| 1013 | } | ||
| 1014 | $_; | ||
| 1015 | } | ||
| 1016 | map { | ||
| 1017 | my($p) = $source_class_object->_concrete_property_meta_for_class_and_name($_); | ||
| 1018 | unless ($p) { | ||
| 1019 | Carp::croak("No property $_ for class ".$source_class_object->class_name); | ||
| 1020 | } | ||
| 1021 | my($table_name,$column_name) = $p->table_and_column_name_for_property(); | ||
| 1022 | if ($table_name && $column_name) { | ||
| 1023 | [$table_name, $column_name]; | ||
| 1024 | } else { | ||
| 1025 | #Carp::confess("Can't determine table and column for property $_ in class " . | ||
| 1026 | # $source_class_object->class_name); | ||
| 1027 | (); | ||
| 1028 | } | ||
| 1029 | } | ||
| 1030 | @source_property_names; | ||
| 1031 | } | ||
| 1032 | return unless @$source_table_and_column_names; | ||
| 1033 | |||
| 1034 | #my @source_property_names = @{ $join->{source_property_names} }; | ||
| 1035 | #my ($source_table_name, $fcols, $fprops) = $self->_resolve_table_and_column_data($source_class_object, @source_property_names); | ||
| 1036 | #my @source_column_names = @$fcols; | ||
| 1037 | #my @source_property_meta = @$fprops; | ||
| 1038 | |||
| 1039 | my @foreign_property_names = @{ $join->{foreign_property_names} }; | ||
| 1040 | my ($foreign_table_name, $fcols, $fprops) = $self->_resolve_table_and_column_data($foreign_class_object, @foreign_property_names); | ||
| 1041 | my @foreign_column_names = @$fcols; | ||
| 1042 | my @foreign_property_meta = @$fprops; | ||
| 1043 | |||
| 1044 | unless (@foreign_column_names) { | ||
| 1045 | # all calculated properties: don't try to join any further | ||
| 1046 | return; | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | unless ($foreign_table_name) { | ||
| 1050 | # If we can't make the join because there is no datasource representation | ||
| 1051 | # for this class, we're done following the joins for this property | ||
| 1052 | # and will NOT try to filter on it at the datasource level | ||
| 1053 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 1054 | return; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | unless (@foreign_column_names == @foreign_property_meta) { | ||
| 1058 | # some calculated properties, be sure to re-check for a match after loading the object | ||
| 1059 | $self->needs_further_boolexpr_evaluation_after_loading(1); | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | my $alias = $self->_get_join_alias($join, $property_name); | ||
| 1063 | |||
| 1064 | unless ($alias) { | ||
| 1065 | my $alias_num = $self->_alias_count($self->_alias_count+1); | ||
| 1066 | |||
| 1067 | my $alias_name = $join->sub_group_label || $property_name; | ||
| 1068 | if (substr($alias_name,-1) eq '?') { | ||
| 1069 | chop($alias_name) if substr($alias_name,-1) eq '?'; | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | my $alias_length = length($alias_name)+length($alias_num)+1; | ||
| 1073 | my $alias_max_length = 29; | ||
| 1074 | if ($alias_length > $alias_max_length) { | ||
| 1075 | $alias = substr($alias_name,0,$alias_max_length-length($alias_num)-1); | ||
| 1076 | } | ||
| 1077 | else { | ||
| 1078 | $alias = $alias_name; | ||
| 1079 | } | ||
| 1080 | $alias =~ s/\./_/g; | ||
| 1081 | $alias .= '_' . $alias_num; | ||
| 1082 | |||
| 1083 | $self->_set_join_alias($join, $property_name, $alias); | ||
| 1084 | |||
| 1085 | if ($foreign_class_object->table_name) { | ||
| 1086 | my @extra_db_filters; | ||
| 1087 | my @extra_obj_filters; | ||
| 1088 | |||
| 1089 | # TODO: when "flatten" correctly feeds the "ON" clause we can remove this | ||
| 1090 | # This will crash if the "where" happens to use indirect things | ||
| 1091 | my $where = $join->{where}; | ||
| 1092 | if ($where) { | ||
| 1093 | for (my $n = 0; $n < @$where; $n += 2) { | ||
| 1094 | my $key =$where->[$n]; | ||
| 1095 | my ($name,$op) = ($key =~ /^(\S+)\s*(.*)/); | ||
| 1096 | |||
| 1097 | if(index($name, '-') == 0) { | ||
| 1098 | #skip '-order_by', '-hint' and the like for joins | ||
| 1099 | next; | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | #my $meta = $foreign_class_object->property_meta_for_name($name); | ||
| 1103 | #my $column = $meta->is_calculated ? (defined($meta->calculate_sql) ? ($meta->calculate_sql) : () ) : ($meta->column_name); | ||
| 1104 | my ($table_name, $column_names, $property_metas) = $self->_resolve_table_and_column_data($foreign_class_object, $name); | ||
| 1105 | my $column = $column_names->[0]; | ||
| 1106 | |||
| 1107 | if (not $column) { | ||
| 1108 | Carp::confess("No column for $foreign_class_object->{id} $name? Indirect property flattening must be enabled to use indirect filters in where with via/to."); | ||
| 1109 | } | ||
| 1110 | |||
| 1111 | my $value = $where->[$n+1]; | ||
| 1112 | push @extra_db_filters, $column => { value => $value, ($op ? (operator => $op) : ()) }; | ||
| 1113 | push @extra_obj_filters, $name => { value => $value, ($op ? (operator => $op) : ()) }; | ||
| 1114 | } | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | my @db_join_data; | ||
| 1118 | for (my $n = 0; $n < @foreign_column_names; $n++) { | ||
| 1119 | |||
| 1120 | my $link_table_name = $table_alias->{$source_table_and_column_names->[$n][0]} | ||
| 1121 | || $source_table_and_column_names->[$n][2] | ||
| 1122 | || $source_table_and_column_names->[$n][0]; | ||
| 1123 | |||
| 1124 | my $link_column_name = $source_table_and_column_names->[$n][1]; | ||
| 1125 | |||
| 1126 | my $foreign_column_name = $foreign_column_names[$n]; | ||
| 1127 | |||
| 1128 | my $link_class_meta = $class_alias->{$link_table_name} || $source_class_object; | ||
| 1129 | my $link_property_name = $link_class_meta->property_for_column($link_column_name); | ||
| 1130 | |||
| 1131 | # _concrete_property_meta_for_class_and_name returns a list :( | ||
| 1132 | # since we're inspecting the joins by their "real" names and not the generic | ||
| 1133 | # "id", it will only ever return a 1-element list | ||
| 1134 | my($link_prop) = $link_class_meta->_concrete_property_meta_for_class_and_name($link_property_name); | ||
| 1135 | my $left_type = $link_prop->_data_type_as_class_name; | ||
| 1136 | my $right_type = $foreign_property_meta[$n]->_data_type_as_class_name; | ||
| 1137 | my @coercion = $self->data_source->cast_for_data_conversion($left_type, $right_type, '=', 'join'); | ||
| 1138 | |||
| 1139 | push @db_join_data, | ||
| 1140 | $foreign_column_name => { | ||
| 1141 | link_table_name => $link_table_name, | ||
| 1142 | link_column_name => $link_column_name, | ||
| 1143 | left_coercion => $coercion[0], | ||
| 1144 | right_coercion => $coercion[1], | ||
| 1145 | }; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | $self->_add_db_join( | ||
| 1149 | "$foreign_table_name $alias" => { | ||
| 1150 | @db_join_data, | ||
| 1151 | @extra_db_filters, | ||
| 1152 | } | ||
| 1153 | ); | ||
| 1154 | |||
| 1155 | $self->_add_obj_join( | ||
| 1156 | "$alias" => { | ||
| 1157 | ( | ||
| 1158 | map { | ||
| 1159 | $foreign_property_names[$_] => { | ||
| 1160 | link_class_name => $source_class_name, | ||
| 1161 | link_alias => $table_alias->{$source_table_and_column_names->[$_][0]} # join alias | ||
| 1162 | || $source_table_and_column_names->[$_][2] # SQL inline view alias | ||
| 1163 | || $source_table_and_column_names->[$_][0], # table_name | ||
| 1164 | link_property_name => $source_property_names[$_] | ||
| 1165 | } | ||
| 1166 | } | ||
| 1167 | (0..$#foreign_property_names) | ||
| 1168 | ), | ||
| 1169 | @extra_obj_filters, | ||
| 1170 | } | ||
| 1171 | ); | ||
| 1172 | |||
| 1173 | # Add all of the columns in the join table to the return list | ||
| 1174 | # Note that we increment the object numbers. | ||
| 1175 | # Note: we add grouping columns individually instead of in chunks | ||
| 1176 | unless ($group_by) { | ||
| 1177 | $self->_add_columns( | ||
| 1178 | map { | ||
| 1179 | my $new = [@$_]; | ||
| 1180 | $new->[2] = $alias; | ||
| 1181 | $new->[3] = $object_num; | ||
| 1182 | $new | ||
| 1183 | } | ||
| 1184 | @{ $foreign_class_loading_data->{direct_table_properties} } | ||
| 1185 | ); | ||
| 1186 | } | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | |||
| 1190 | if ($group_by) { | ||
| 1191 | if ($self->_groups_by_property($property_name)) { | ||
| 1192 | my ($p) = | ||
| 1193 | map { | ||
| 1194 | my $new = [@$_]; | ||
| 1195 | $new->[2] = $alias; | ||
| 1196 | $new->[3] = 0; | ||
| 1197 | $new | ||
| 1198 | } | ||
| 1199 | grep { $_->[1]->property_name eq $final_accessor } | ||
| 1200 | @{ $foreign_class_loading_data->{direct_table_properties} }; | ||
| 1201 | $self->_add_columns($p); | ||
| 1202 | } | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | |||
| 1206 | if ($self->_orders_by_property($property_name)) { | ||
| 1207 | my ($p) = | ||
| 1208 | map { | ||
| 1209 | my $new = [@$_]; | ||
| 1210 | $new->[2] = $alias; | ||
| 1211 | $new->[3] = 0; | ||
| 1212 | $new | ||
| 1213 | } | ||
| 1214 | grep { $_->[1]->property_name eq $final_accessor } | ||
| 1215 | @{ $foreign_class_loading_data->{direct_table_properties} }; | ||
| 1216 | # ??? what do we do here now with $p? | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | unless ($is_optional) { | ||
| 1220 | # if _any_ part requires this, mark it required | ||
| 1221 | $self->_set_alias_required($alias); | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | } # done adding a new join alias for a join which has not yet been done | ||
| 1225 | |||
| 1226 | if ($foreign_class_object->table_name) { | ||
| 1227 | $table_alias->{$foreign_table_name} = $alias; | ||
| 1228 | $class_alias->{$alias} = $foreign_class_object; | ||
| 1229 | @$source_table_and_column_names = (); # Flag that we need to re-derive this at the top of the loop | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | return $alias; | ||
| 1233 | } | ||
| 1234 | |||
| 1235 | sub _resolve_table_and_column_data { | ||
| 1236 | my ($class, $class_meta, @property_names) = @_; | ||
| 1237 | my @property_meta = | ||
| 1238 | map { $class_meta->_concrete_property_meta_for_class_and_name($_) } | ||
| 1239 | @property_names; | ||
| 1240 | my $table_name; | ||
| 1241 | my @column_names = | ||
| 1242 | map { | ||
| 1243 | # TODO: encapsulate | ||
| 1244 | if ($_->is_calculated) { | ||
| 1245 | if ($_->calculate_sql) { | ||
| 1246 | $_->calculate_sql; | ||
| 1247 | } else { | ||
| 1248 | (); | ||
| 1249 | } | ||
| 1250 | } else { | ||
| 1251 | my $column_name; | ||
| 1252 | ($table_name, $column_name) = $_->table_and_column_name_for_property(); | ||
| 1253 | $column_name; | ||
| 1254 | } | ||
| 1255 | } | ||
| 1256 | @property_meta; | ||
| 1257 | |||
| 1258 | if ($table_name and $table_name =~ /^(.*)\s+(\w+)\s*$/s) { | ||
| 1259 | $table_name = $1; | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | return ($table_name, \@column_names, \@property_meta); | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | sub _set_join_alias { | ||
| 1266 | my ($self, $join, $property_name, $alias) = @_; | ||
| 1267 | $self->_join_data->{$join->id}{$property_name}{alias} = $alias; | ||
| 1268 | $self->_alias_data({}) unless $self->_alias_data(); | ||
| 1269 | $self->_alias_data->{$alias}{join_id} = $join->id; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | sub _get_join_alias { | ||
| 1273 | my ($self,$join,$property_name) = @_; | ||
| 1274 | $self->_join_data({}) unless $self->_join_data(); | ||
| 1275 | return $self->_join_data->{$join->id}{$property_name}{alias}; | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub _get_alias_join { | ||
| 1279 | my ($self,$alias) = @_; | ||
| 1280 | my $alias_data = $self->_alias_data; | ||
| 1281 | return if (! $alias_data or ! exists($alias_data->{$alias})); | ||
| 1282 | my $join_id = $self->_alias_data->{$alias}{join_id}; | ||
| 1283 | UR::Object::Join->get($join_id); | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | sub _add_db_join { | ||
| 1287 | my ($self, $key, $data) = @_; | ||
| 1288 | |||
| 1289 | my ($alias) = ($key =~/\w+$/); | ||
| 1290 | my $alias_data = $self->_alias_data || $self->_alias_data({}); | ||
| 1291 | $alias_data->{$alias}{db_join} = $data; | ||
| 1292 | |||
| 1293 | my $db_joins = $self->_db_joins || $self->_db_joins([]); | ||
| 1294 | push @$db_joins, $key, $data; | ||
| 1295 | } | ||
| 1296 | |||
| 1297 | sub _add_obj_join { | ||
| 1298 | my ($self, $key, $data) = @_; | ||
| 1299 | |||
| 1300 | Carp::confess() unless ref $data; | ||
| 1301 | my $alias_data = $self->_alias_data || $self->_alias_data({}); | ||
| 1302 | $alias_data->{$key}{obj_join} = $data; # the key is the alias here | ||
| 1303 | |||
| 1304 | my $obj_joins = $self->_obj_joins || $self->_obj_joins([]); | ||
| 1305 | push @$obj_joins, $key, $data; | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | sub _set_alias_required { | ||
| 1309 | my ($self, $alias) = @_; | ||
| 1310 | my $alias_data = $self->_alias_data || $self->_alias_data({}); | ||
| 1311 | $alias_data->{$alias}{is_required} = 1; | ||
| 1312 | $alias_data->{$alias}{db_join}{-is_required} = 1; | ||
| 1313 | $alias_data->{$alias}{obj_join}{-is_required} = 1; | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | sub _add_columns { | ||
| 1317 | my $self = shift; | ||
| 1318 | my @new = @_; | ||
| 1319 | my $old = $self->_db_column_data; | ||
| 1320 | my $pos = @$old; | ||
| 1321 | my $lob_column_positions = $self->{lob_column_positions}; | ||
| 1322 | my $lob_column_names = $self->{lob_column_names}; | ||
| 1323 | for my $class_property (@new) { | ||
| 1324 | my ($sql_class,$sql_property,$sql_table_name) = @$class_property; | ||
| 1325 | my $data_type = $sql_property->data_type || ''; | ||
| 1326 | if ($data_type =~ /LOB$/) { | ||
| 1327 | push @$lob_column_names, $sql_property->column_name; | ||
| 1328 | push @$lob_column_positions, $pos; | ||
| 1329 | } | ||
| 1330 | $pos++; | ||
| 1331 | } | ||
| 1332 | push @$old, @new; | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | # Used by the object fabricator to find out which resultset column a | ||
| 1336 | # property's data is stored | ||
| 1337 | sub column_index_for_class_property_and_object_num { | ||
| 1338 | my($self, $class_name, $property_name, $object_num) = @_; | ||
| 1339 | |||
| 1340 | $object_num ||= 0; | ||
| 1341 | |||
| 1342 | my $db_column_data = $self->_db_column_data; | ||
| 1343 | for (my $resultset_col = 0; $resultset_col < @$db_column_data; $resultset_col++) { | ||
| 1344 | if ($db_column_data->[$resultset_col]->[1]->class_name eq $class_name | ||
| 1345 | and $db_column_data->[$resultset_col]->[1]->property_name eq $property_name | ||
| 1346 | and $db_column_data->[$resultset_col]->[3] == $object_num | ||
| 1347 | ) { | ||
| 1348 | return $resultset_col; | ||
| 1349 | } | ||
| 1350 | } | ||
| 1351 | return undef; | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | # used by the object fabricator to determine the resultset column | ||
| 1355 | # the source property of a join is stored. | ||
| 1356 | sub column_index_for_class_and_property_before_object_num { | ||
| 1357 | my($self, $class_name, $property_name, $object_num) = @_; | ||
| 1358 | return unless $object_num; | ||
| 1359 | |||
| 1360 | my $db_column_data = $self->_db_column_data; | ||
| 1361 | my $index; | ||
| 1362 | for (my $resultset_col = 0; $resultset_col < @$db_column_data; $resultset_col++) { | ||
| 1363 | last if ($db_column_data->[$resultset_col]->[3] >= $object_num); | ||
| 1364 | if ($db_column_data->[$resultset_col]->[1]->class_name eq $class_name | ||
| 1365 | and | ||
| 1366 | $db_column_data->[$resultset_col]->[1]->property_name eq $property_name | ||
| 1367 | ) { | ||
| 1368 | $index = $resultset_col; | ||
| 1369 | } | ||
| 1370 | } | ||
| 1371 | return $index; | ||
| 1372 | } | ||
| 1373 | |||
| 1374 | |||
| 1375 | sub _groups_by_property { | ||
| 1376 | my ($self, $property_name) = @_; | ||
| 1377 | return $self->_group_by_property_names->{$property_name}; | ||
| 1378 | } | ||
| 1379 | |||
| 1380 | sub _orders_by_property { | ||
| 1381 | my ($self, $property_name) = @_; | ||
| 1382 | return $self->_order_by_property_names->{$property_name}; | ||
| 1383 | } | ||
| 1384 | |||
| 1385 | sub _resolve_db_joins_for_inheritance { | ||
| 1386 | my $class_meta = $_[0]; | ||
| 1387 | |||
| 1388 | my $first_table_name; | ||
| 1389 | my @sql_joins; | ||
| 1390 | |||
| 1391 | my $prev_table_name; | ||
| 1392 | my $prev_id_column_name; | ||
| 1393 | my $prev_property_meta; | ||
| 1394 | |||
| 1395 | my @parent_class_objects = $class_meta->ancestry_class_metas; | ||
| 1396 | |||
| 1397 | my %seen; | ||
| 1398 | for my $co ( $class_meta, @parent_class_objects ) { | ||
| 1399 | my $class_name = $co->class_name; | ||
| 1400 | next if $seen{$class_name}++; | ||
| 1401 | |||
| 1402 | my @id_property_objects = $co->direct_id_property_metas; | ||
| 1403 | my %id_properties = map { $_->property_name => 1 } @id_property_objects; | ||
| 1404 | my @id_column_names = | ||
| 1405 | map { $_->column_name } | ||
| 1406 | @id_property_objects; | ||
| 1407 | |||
| 1408 | my $table_name = $co->table_name; | ||
| 1409 | if ($table_name) { | ||
| 1410 | $first_table_name ||= $table_name; | ||
| 1411 | if ($prev_table_name) { | ||
| 1412 | die "Database-level inheritance cannot be used with multi-value-id classes ($class_name)!" if @id_property_objects > 1; | ||
| 1413 | my $prev_table_alias; | ||
| 1414 | if ($prev_table_name =~ /.*\s+(\w+)\s*$/) { | ||
| 1415 | $prev_table_alias = $1; | ||
| 1416 | } | ||
| 1417 | else { | ||
| 1418 | $prev_table_alias = $prev_table_name; | ||
| 1419 | } | ||
| 1420 | |||
| 1421 | my @coercion = $co->data_source->cast_for_data_conversion( | ||
| 1422 | $prev_property_meta->_data_type_as_class_name, | ||
| 1423 | $id_property_objects[0]->_data_type_as_class_name, | ||
| 1424 | '=', | ||
| 1425 | 'join'); | ||
| 1426 | push @sql_joins, | ||
| 1427 | $table_name => | ||
| 1428 | { | ||
| 1429 | $id_property_objects[0]->column_name => { | ||
| 1430 | link_table_name => $prev_table_alias, | ||
| 1431 | link_column_name => $prev_id_column_name, | ||
| 1432 | left_coercion => $coercion[0], | ||
| 1433 | right_coercion => $coercion[1], | ||
| 1434 | }, | ||
| 1435 | -is_required => 1, | ||
| 1436 | }; | ||
| 1437 | } | ||
| 1438 | $prev_table_name = $table_name; | ||
| 1439 | $prev_id_column_name = $id_property_objects[0]->column_name; | ||
| 1440 | $prev_property_meta = $id_property_objects[0]; | ||
| 1441 | } | ||
| 1442 | } | ||
| 1443 | |||
| 1444 | return ($first_table_name, @sql_joins); | ||
| 1445 | } | ||
| 1446 | |||
| 1447 | sub _resolve_object_join_data_for_property_chain { | ||
| 1448 | my ($rule_template, $property_name) = @_; | ||
| 1449 | my $class_meta = $rule_template->subject_class_name->__meta__; | ||
| 1450 | |||
| 1451 | my @joins; | ||
| 1452 | my $is_optional; | ||
| 1453 | my $final_accessor; | ||
| 1454 | |||
| 1455 | my @pmeta = $class_meta->_concrete_property_meta_for_class_and_name($property_name); | ||
| 1456 | |||
| 1457 | my $last_class_meta = $class_meta; | ||
| 1458 | for my $meta (@pmeta) { | ||
| 1459 | if (!$meta) { | ||
| 1460 | Carp::croak "Can't resolve joins for ".$rule_template->subject_class_name . " property '$property_name': No property metadata found for that class and property_name"; | ||
| 1461 | } | ||
| 1462 | #id is a special property that we want to look up, but isn't necessarily on a table | ||
| 1463 | #so if it aliases another property, we look at that instead | ||
| 1464 | if($meta->property_name eq 'id' and $meta->class_name eq 'UR::Object') { | ||
| 1465 | my @id_properties = grep {$_->class_name ne 'UR::Object'} $last_class_meta->id_properties; | ||
| 1466 | if(@id_properties == 1) { | ||
| 1467 | $meta = $id_properties[0]; | ||
| 1468 | $last_class_meta = $meta->class_name->__meta__; | ||
| 1469 | next; | ||
| 1470 | } | ||
| 1471 | elsif (@id_properties > 1) { | ||
| 1472 | Carp::confess "can't join to class " . $last_class_meta->class_name . " with multiple id properties: @id_properties"; | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | if($meta->data_type and $meta->data_type =~ /::/) { | ||
| 1476 | $last_class_meta = UR::Object::Type->get($meta->data_type); | ||
| 1477 | } else { | ||
| 1478 | $last_class_meta = UR::Object::Type->get($meta->class_name); | ||
| 1479 | } | ||
| 1480 | last unless $last_class_meta; | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | # we can't actually get this from the joins because | ||
| 1484 | # a bunch of optional things can be chained together to form | ||
| 1485 | # something non-optional | ||
| 1486 | $is_optional = 0; | ||
| 1487 | for my $pmeta (@pmeta) { | ||
| 1488 | push @joins, $pmeta->_resolve_join_chain(); | ||
| 1489 | $is_optional = 1 if $pmeta->is_optional or $pmeta->is_many; | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | return unless @joins; | ||
| 1493 | return ($joins[-1]->{source_name_for_foreign}, $is_optional, @joins) | ||
| 1494 | }; | ||
| 1495 | |||
| 1496 | sub _init_light { | ||
| 1497 | my $self = shift; | ||
| 1498 | my $rule_template = $self->rule_template; | ||
| 1499 | my $ds = $self->data_source; | ||
| 1500 | |||
| 1501 | my $class_name = $rule_template->subject_class_name; | ||
| 1502 | my $class_meta = $class_name->__meta__; | ||
| 1503 | my $class_data = $ds->_get_class_data_for_loading($class_meta); | ||
| 1504 | |||
| 1505 | my @parent_class_objects = @{ $class_data->{parent_class_objects} }; | ||
| 1506 | my @all_properties = @{ $class_data->{all_properties} }; | ||
| 1507 | my $sub_classification_meta_class_name = $class_data->{sub_classification_meta_class_name}; | ||
| 1508 | my $subclassify_by = $class_data->{subclassify_by}; | ||
| 1509 | |||
| 1510 | my @all_id_property_names = @{ $class_data->{all_id_property_names} }; | ||
| 1511 | my @id_properties = @{ $class_data->{id_properties} }; | ||
| 1512 | my $id_property_sorter = $class_data->{id_property_sorter}; | ||
| 1513 | my $sub_typing_property = $class_data->{sub_typing_property}; | ||
| 1514 | my $class_table_name = $class_data->{class_table_name}; | ||
| 1515 | |||
| 1516 | my $recursion_desc = $rule_template->recursion_desc; | ||
| 1517 | my $recurse_property_on_this_row; | ||
| 1518 | my $recurse_property_referencing_other_rows; | ||
| 1519 | my $recurse_resolution_by_iteration; | ||
| 1520 | if ($recursion_desc) { | ||
| 1521 | ($recurse_property_on_this_row,$recurse_property_referencing_other_rows) = @$recursion_desc; | ||
| 1522 | $recurse_resolution_by_iteration = ! $ds->does_support_recursive_queries; | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | my $needs_further_boolexpr_evaluation_after_loading; | ||
| 1526 | |||
| 1527 | my $is_join_across_data_source; | ||
| 1528 | |||
| 1529 | my @sql_params; | ||
| 1530 | my @filter_specs; | ||
| 1531 | my @property_names_in_resultset_order; | ||
| 1532 | my $object_num = 0; # 0-based, usually zero unless there are joins | ||
| 1533 | |||
| 1534 | my @filters = $rule_template->_property_names; | ||
| 1535 | my %filters = | ||
| 1536 | map { $_ => 0 } | ||
| 1537 | grep { substr($_,0,1) ne '-' } | ||
| 1538 | @filters; | ||
| 1539 | |||
| 1540 | unless (@all_id_property_names == 1 && $all_id_property_names[0] eq "id") { | ||
| 1541 | delete $filters{'id'}; | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | my ( | ||
| 1545 | @sql_joins, | ||
| 1546 | @sql_filters, | ||
| 1547 | $prev_table_name, | ||
| 1548 | $prev_id_column_name, | ||
| 1549 | $pk_used, | ||
| 1550 | @delegated_properties, | ||
| 1551 | %chain_delegates, | ||
| 1552 | ); | ||
| 1553 | |||
| 1554 | for my $key (keys %filters) { | ||
| 1555 | if (index($key,'.') != -1) { | ||
| 1556 | $chain_delegates{$key} = delete $filters{$key}; | ||
| 1557 | } | ||
| 1558 | } | ||
| 1559 | |||
| 1560 | for my $co ( $class_meta, @parent_class_objects ) { | ||
| 1561 | my $class_name = $co->class_name; | ||
| 1562 | last if ( ($class_name eq 'UR::Object') or (not $class_name->isa("UR::Object")) ); | ||
| 1563 | my @id_property_objects = $co->direct_id_property_metas; | ||
| 1564 | if (@id_property_objects == 0) { | ||
| 1565 | @id_property_objects = $co->property_meta_for_name("id"); | ||
| 1566 | if (@id_property_objects == 0) { | ||
| 1567 | Carp::confess("Couldn't determine ID properties for $class_name\n"); | ||
| 1568 | } | ||
| 1569 | } | ||
| 1570 | my %id_properties = map { $_->property_name => 1 } @id_property_objects; | ||
| 1571 | my @id_column_names = | ||
| 1572 | map { $_->column_name } | ||
| 1573 | @id_property_objects; | ||
| 1574 | for my $property_name (sort keys %filters) { | ||
| 1575 | my $property = UR::Object::Property->get(class_name => $class_name, property_name => $property_name); | ||
| 1576 | next unless $property; | ||
| 1577 | my $operator = $rule_template->operator_for($property_name); | ||
| 1578 | my $value_position = $rule_template->value_position_for_property_name($property_name); | ||
| 1579 | delete $filters{$property_name}; | ||
| 1580 | $pk_used = 1 if $id_properties{ $property_name }; | ||
| 1581 | if ($property->is_legacy_eav) { | ||
| 1582 | die "Old GSC EAV can be handled with a via/to/where/is_mutable=1"; | ||
| 1583 | } | ||
| 1584 | elsif ($property->is_delegated) { | ||
| 1585 | push @delegated_properties, $property; | ||
| 1586 | } | ||
| 1587 | elsif ($property->is_calculated || $property->is_transient) { | ||
| 1588 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1589 | } | ||
| 1590 | else { | ||
| 1591 | push @sql_filters, | ||
| 1592 | $class_name => | ||
| 1593 | { | ||
| 1594 | $property_name => { operator => $operator, value_position => $value_position } | ||
| 1595 | }; | ||
| 1596 | } | ||
| 1597 | } | ||
| 1598 | $prev_id_column_name = $id_property_objects[0]->column_name; | ||
| 1599 | } # end of inheritance loop | ||
| 1600 | |||
| 1601 | if ( my @errors = keys(%filters) ) { | ||
| 1602 | my $class_name = $class_meta->class_name; | ||
| 1603 | $ds->error_message('Unknown param(s) (' . join(',',@errors) . ") used to generate SQL for $class_name!"); | ||
| 1604 | Carp::confess(); | ||
| 1605 | } | ||
| 1606 | |||
| 1607 | my $last_class_name = $class_name; | ||
| 1608 | my $last_class_object = $class_meta; | ||
| 1609 | my $alias_num = 1; | ||
| 1610 | my %joins_done; | ||
| 1611 | my $joins_across_data_sources; | ||
| 1612 | |||
| 1613 | DELEGATED_PROPERTY: | ||
| 1614 | for my $delegated_property (@delegated_properties) { | ||
| 1615 | my $last_alias_for_this_chain; | ||
| 1616 | my $property_name = $delegated_property->property_name; | ||
| 1617 | my @joins = $delegated_property->_resolve_join_chain($property_name); | ||
| 1618 | my $relationship_name = $delegated_property->via; | ||
| 1619 | unless ($relationship_name) { | ||
| 1620 | $relationship_name = $property_name; | ||
| 1621 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1622 | } | ||
| 1623 | |||
| 1624 | my $delegate_class_meta = $delegated_property->class_meta; | ||
| 1625 | my($via_accessor_meta) = $delegate_class_meta->_concrete_property_meta_for_class_and_name($relationship_name); | ||
| 1626 | next unless $via_accessor_meta; | ||
| 1627 | my $final_accessor = $delegated_property->to; | ||
| 1628 | |||
| 1629 | my $data_type = $via_accessor_meta->data_type; | ||
| 1630 | unless ($data_type) { | ||
| 1631 | Carp::croak "Can't resolve delegation for $property_name on class $class_name: via property $relationship_name has no data type"; | ||
| 1632 | } | ||
| 1633 | |||
| 1634 | my $data_type_meta = UR::Object::Type->get($via_accessor_meta->data_type); | ||
| 1635 | unless ($data_type_meta) { | ||
| 1636 | Carp::croak "No class meta data for " . $via_accessor_meta->data_type . | ||
| 1637 | " while resolving property $property_name on class $class_name"; | ||
| 1638 | } | ||
| 1639 | my($final_accessor_meta) = $data_type_meta->_concrete_property_meta_for_class_and_name( | ||
| 1640 | $final_accessor | ||
| 1641 | ); | ||
| 1642 | unless ($final_accessor_meta) { | ||
| 1643 | Carp::croak("No property '$final_accessor' on class " . $via_accessor_meta->data_type . | ||
| 1644 | " while resolving property $property_name on class $class_name"); | ||
| 1645 | } | ||
| 1646 | |||
| 1647 | # Follow the chain of via/to delegation down to where the data ultimately lives | ||
| 1648 | while($final_accessor_meta->is_delegated) { | ||
| 1649 | # May have been 'to' an id_by/id_class_by property. Stop chaining and do two queries | ||
| 1650 | # If we had access to the value at this point, we could continue joining through that | ||
| 1651 | # value's class and id | ||
| 1652 | next DELEGATED_PROPERTY if ($final_accessor_meta->id_by or $final_accessor_meta->id_class_by); | ||
| 1653 | |||
| 1654 | my $prev_accessor_meta = $final_accessor_meta; | ||
| 1655 | $final_accessor_meta = $final_accessor_meta->to_property_meta(); | ||
| 1656 | unless ($final_accessor_meta) { | ||
| 1657 | Carp::croak("Can't resolve property '$final_accessor' of class " . $via_accessor_meta->data_type | ||
| 1658 | . ": Resolution involved property '" . $prev_accessor_meta->property_name . "' of class " | ||
| 1659 | . $prev_accessor_meta->class_name | ||
| 1660 | . " which is delegated, but its via/to metadata does not resolve to a known class and property"); | ||
| 1661 | } | ||
| 1662 | } | ||
| 1663 | $final_accessor = $final_accessor_meta->property_name; | ||
| 1664 | for my $join (@joins) { | ||
| 1665 | my $source_class_name = $join->{source_class}; | ||
| 1666 | my $source_class_object = $join->{'source_class_meta'} || $source_class_name->__meta__; | ||
| 1667 | |||
| 1668 | my $foreign_class_name = $join->{foreign_class}; | ||
| 1669 | next DELEGATED_PROPERTY if ($foreign_class_name->isa('UR::Value')); | ||
| 1670 | my $foreign_class_object = $join->{'foreign_class_meta'} || $foreign_class_name->__meta__; | ||
| 1671 | my($foreign_data_source) = $UR::Context::current->resolve_data_sources_for_class_meta_and_rule($foreign_class_object, $rule_template); | ||
| 1672 | if (! $foreign_data_source) { | ||
| 1673 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1674 | next DELEGATED_PROPERTY; | ||
| 1675 | |||
| 1676 | } elsif ($foreign_data_source ne $ds or | ||
| 1677 | ! $ds->does_support_joins or | ||
| 1678 | ! $foreign_data_source->does_support_joins | ||
| 1679 | ) | ||
| 1680 | { | ||
| 1681 | push(@{$joins_across_data_sources->{$foreign_data_source->id}}, $delegated_property); | ||
| 1682 | next DELEGATED_PROPERTY; | ||
| 1683 | } | ||
| 1684 | my @source_property_names = @{ $join->{source_property_names} }; | ||
| 1685 | my @source_table_and_column_names = | ||
| 1686 | map { | ||
| 1687 | my($p) = $source_class_object->_concrete_property_meta_for_class_and_name($_); | ||
| 1688 | unless ($p) { | ||
| 1689 | Carp::confess("No property $_ for class $source_class_object->{class_name}\n"); | ||
| 1690 | } | ||
| 1691 | unless ($p->class_name->__meta__) { | ||
| 1692 | Carp::croak("Can't get class metadata for " . $p->class_name); | ||
| 1693 | } | ||
| 1694 | [$p->class_name->__meta__->class_name, $p->property_name]; | ||
| 1695 | } | ||
| 1696 | @source_property_names; | ||
| 1697 | my $foreign_table_name = $foreign_class_name; | ||
| 1698 | unless ($foreign_table_name) { | ||
| 1699 | # If we can't make the join because there is no datasource representation | ||
| 1700 | # for this class, we're done following the joins for this property | ||
| 1701 | # and will NOT try to filter on it at the datasource level | ||
| 1702 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1703 | next DELEGATED_PROPERTY; | ||
| 1704 | } | ||
| 1705 | my @foreign_property_names = @{ $join->{foreign_property_names} }; | ||
| 1706 | my @foreign_property_meta = | ||
| 1707 | map { | ||
| 1708 | $foreign_class_object->_concrete_property_meta_for_class_and_name($_) | ||
| 1709 | } | ||
| 1710 | @foreign_property_names; | ||
| 1711 | |||
| 1712 | my @foreign_column_names = | ||
| 1713 | map { | ||
| 1714 | # TODO: encapsulate | ||
| 1715 | $_->is_calculated ? (defined($_->calculate_sql) ? ($_->calculate_sql) : () ) : ($_->property_name) | ||
| 1716 | } | ||
| 1717 | @foreign_property_meta; | ||
| 1718 | |||
| 1719 | unless (@foreign_column_names) { | ||
| 1720 | # all calculated properties: don't try to join any further | ||
| 1721 | last; | ||
| 1722 | } | ||
| 1723 | unless (@foreign_column_names == @foreign_property_meta) { | ||
| 1724 | # some calculated properties, be sure to re-check for a match after loading the object | ||
| 1725 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1726 | } | ||
| 1727 | my $alias = $joins_done{$join->{id}}; | ||
| 1728 | unless ($alias) { | ||
| 1729 | $alias = "${relationship_name}_${alias_num}"; | ||
| 1730 | $alias_num++; | ||
| 1731 | $object_num++; | ||
| 1732 | |||
| 1733 | push @sql_joins, | ||
| 1734 | "$foreign_table_name $alias" => | ||
| 1735 | { | ||
| 1736 | map { | ||
| 1737 | $foreign_property_names[$_] => { | ||
| 1738 | link_table_name => $last_alias_for_this_chain || $source_table_and_column_names[$_][0], | ||
| 1739 | link_column_name => $source_table_and_column_names[$_][1] | ||
| 1740 | } | ||
| 1741 | } | ||
| 1742 | (0..$#foreign_property_names) | ||
| 1743 | }; | ||
| 1744 | |||
| 1745 | # Add all of the columns in the join table to the return list. | ||
| 1746 | push @all_properties, | ||
| 1747 | map { [$foreign_class_object, $_, $alias, $object_num] } | ||
| 1748 | map { $_->[1] } # These three lines are to get around a bug in perl | ||
| 1749 | sort { $a->[0] cmp $b->[0] } # 5.8's sort involving method calls within the sort | ||
| 1750 | map { [ $_->property_name, $_ ] } # sub that do sorts of their own | ||
| 1751 | grep { defined($_->column_name) && $_->column_name ne '' } | ||
| 1752 | UR::Object::Property->get( class_name => $foreign_class_name ); | ||
| 1753 | |||
| 1754 | $joins_done{$join->{id}} = $alias; | ||
| 1755 | |||
| 1756 | } | ||
| 1757 | # Set these for after all of the joins are done | ||
| 1758 | $last_class_name = $foreign_class_name; | ||
| 1759 | $last_class_object = $foreign_class_object; | ||
| 1760 | $last_alias_for_this_chain = $alias; | ||
| 1761 | } # next join | ||
| 1762 | unless ($delegated_property->via) { | ||
| 1763 | next; | ||
| 1764 | } | ||
| 1765 | my($final_accessor_property_meta) = $last_class_object->_concrete_property_meta_for_class_and_name($final_accessor); | ||
| 1766 | unless ($final_accessor_property_meta) { | ||
| 1767 | Carp::croak("No property metadata for property named '$final_accessor' in class " . $last_class_object->class_name | ||
| 1768 | . " while resolving joins for property '" .$delegated_property->property_name . "' in class " | ||
| 1769 | . $delegated_property->class_name); | ||
| 1770 | } | ||
| 1771 | my $sql_lvalue; | ||
| 1772 | if ($final_accessor_property_meta->is_calculated) { | ||
| 1773 | $sql_lvalue = $final_accessor_property_meta->calculate_sql; | ||
| 1774 | unless (defined($sql_lvalue)) { | ||
| 1775 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1776 | next; | ||
| 1777 | } | ||
| 1778 | } | ||
| 1779 | else { | ||
| 1780 | $sql_lvalue = $final_accessor_property_meta->column_name; | ||
| 1781 | unless (defined($sql_lvalue)) { | ||
| 1782 | Carp::confess("No column name set for non-delegated/calculated property $property_name of $class_name"); | ||
| 1783 | } | ||
| 1784 | } | ||
| 1785 | my $operator = $rule_template->operator_for($property_name); | ||
| 1786 | my $value_position = $rule_template->value_position_for_property_name($property_name); | ||
| 1787 | } # next delegated property | ||
| 1788 | for my $property_meta_array (@all_properties) { | ||
| 1789 | push @property_names_in_resultset_order, $property_meta_array->[1]->property_name; | ||
| 1790 | } | ||
| 1791 | my $rule_template_without_recursion_desc = ($recursion_desc ? $rule_template->remove_filter('-recurse') : $rule_template); | ||
| 1792 | my $rule_template_specifies_value_for_subtype; | ||
| 1793 | if ($sub_typing_property) { | ||
| 1794 | $rule_template_specifies_value_for_subtype = $rule_template->specifies_value_for($sub_typing_property) | ||
| 1795 | } | ||
| 1796 | #my $per_object_in_resultset_loading_detail = $ds->_generate_loading_templates_arrayref(\@all_properties); | ||
| 1797 | %$self = ( | ||
| 1798 | %$self, | ||
| 1799 | %$class_data, | ||
| 1800 | properties_for_params => \@all_properties, | ||
| 1801 | property_names_in_resultset_order => \@property_names_in_resultset_order, | ||
| 1802 | joins => \@sql_joins, | ||
| 1803 | rule_template_id => $rule_template->id, | ||
| 1804 | rule_template_without_recursion_desc => $rule_template_without_recursion_desc, | ||
| 1805 | rule_template_id_without_recursion_desc => $rule_template_without_recursion_desc->id, | ||
| 1806 | rule_matches_all => $rule_template->matches_all, | ||
| 1807 | rule_specifies_id => ($rule_template->specifies_value_for('id') || undef), | ||
| 1808 | rule_template_is_id_only => $rule_template->is_id_only, | ||
| 1809 | rule_template_specifies_value_for_subtype => $rule_template_specifies_value_for_subtype, | ||
| 1810 | recursion_desc => $rule_template->recursion_desc, | ||
| 1811 | recurse_property_on_this_row => $recurse_property_on_this_row, | ||
| 1812 | recurse_property_referencing_other_rows => $recurse_property_referencing_other_rows, | ||
| 1813 | recurse_resolution_by_iteration => $recurse_resolution_by_iteration, | ||
| 1814 | #loading_templates => $per_object_in_resultset_loading_detail, | ||
| 1815 | joins_across_data_sources => $joins_across_data_sources, | ||
| 1816 | ); | ||
| 1817 | return $self; | ||
| 1818 | } | ||
| 1819 | |||
| 1820 | sub _init_core { | ||
| 1821 | my $self = shift; | ||
| 1822 | my $rule_template = $self->rule_template; | ||
| 1823 | my $ds = $self->data_source; | ||
| 1824 | |||
| 1825 | # TODO: most of this only applies to the RDBMS subclass, | ||
| 1826 | # but some applies to any datasource. It doesn't hurt to have the RDBMS stuff | ||
| 1827 | # here and ignored, but it's not placed correctly. | ||
| 1828 | |||
| 1829 | # class-based values | ||
| 1830 | |||
| 1831 | my $class_name = $rule_template->subject_class_name; | ||
| 1832 | my $class_meta = $class_name->__meta__; | ||
| 1833 | my $class_data = $ds->_get_class_data_for_loading($class_meta); | ||
| 1834 | |||
| 1835 | my @parent_class_objects = @{ $class_data->{parent_class_objects} }; | ||
| 1836 | my @all_properties = @{ $class_data->{all_properties} }; | ||
| 1837 | my $sub_classification_meta_class_name = $class_data->{sub_classification_meta_class_name}; | ||
| 1838 | my $subclassify_by = $class_data->{subclassify_by}; | ||
| 1839 | |||
| 1840 | my @all_id_property_names = @{ $class_data->{all_id_property_names} }; | ||
| 1841 | my @id_properties = @{ $class_data->{id_properties} }; | ||
| 1842 | my $id_property_sorter = $class_data->{id_property_sorter}; | ||
| 1843 | |||
| 1844 | my $sub_typing_property = $class_data->{sub_typing_property}; | ||
| 1845 | my $class_table_name = $class_data->{class_table_name}; | ||
| 1846 | |||
| 1847 | # individual query/boolexpr based | ||
| 1848 | |||
| 1849 | my $recursion_desc = $rule_template->recursion_desc; | ||
| 1850 | my $recurse_property_on_this_row; | ||
| 1851 | my $recurse_property_referencing_other_rows; | ||
| 1852 | if ($recursion_desc) { | ||
| 1853 | ($recurse_property_on_this_row,$recurse_property_referencing_other_rows) = @$recursion_desc; | ||
| 1854 | } | ||
| 1855 | |||
| 1856 | # _usually_ items freshly loaded from the DB don't need to be evaluated through the rule | ||
| 1857 | # because the SQL gets constructed in such a way that all the items returned would pass anyway. | ||
| 1858 | # But in certain cases (a delegated property trying to match a non-object value (which is a bug | ||
| 1859 | # in the caller's code from one point of view) or with calculated non-sql properties, then the | ||
| 1860 | # sql will return a superset of the items we're actually asking for, and the loader needs to | ||
| 1861 | # validate them through the rule | ||
| 1862 | my $needs_further_boolexpr_evaluation_after_loading; | ||
| 1863 | |||
| 1864 | # Does fulfilling this request involve querying more than one data source? | ||
| 1865 | my $is_join_across_data_source; | ||
| 1866 | |||
| 1867 | my @sql_params; | ||
| 1868 | my @filter_specs; | ||
| 1869 | my @property_names_in_resultset_order; | ||
| 1870 | my $object_num = 0; # 0-based, usually zero unless there are joins | ||
| 1871 | |||
| 1872 | my @filters = $rule_template->_property_names; | ||
| 1873 | my %filters = | ||
| 1874 | map { $_ => 0 } | ||
| 1875 | grep { substr($_,0,1) ne '-' } | ||
| 1876 | @filters; | ||
| 1877 | |||
| 1878 | unless (@all_id_property_names == 1 && $all_id_property_names[0] eq "id") { | ||
| 1879 | delete $filters{'id'}; | ||
| 1880 | } | ||
| 1881 | |||
| 1882 | my ( | ||
| 1883 | @sql_joins, | ||
| 1884 | @sql_filters, | ||
| 1885 | $prev_table_name, | ||
| 1886 | $prev_id_column_name, | ||
| 1887 | $pk_used, | ||
| 1888 | @delegated_properties, | ||
| 1889 | %chain_delegates, | ||
| 1890 | ); | ||
| 1891 | |||
| 1892 | for my $key (keys %filters) { | ||
| 1893 | if (index($key,'.') != -1) { | ||
| 1894 | $chain_delegates{$key} = delete $filters{$key}; | ||
| 1895 | } | ||
| 1896 | } | ||
| 1897 | for my $co ( $class_meta, @parent_class_objects ) { | ||
| 1898 | my $class_name = $co->class_name; | ||
| 1899 | |||
| 1900 | last if ( ($class_name eq 'UR::Object') or (not $class_name->isa("UR::Object")) ); | ||
| 1901 | |||
| 1902 | my @id_property_objects = $co->direct_id_property_metas; | ||
| 1903 | |||
| 1904 | if (@id_property_objects == 0) { | ||
| 1905 | @id_property_objects = $co->property_meta_for_name("id"); | ||
| 1906 | if (@id_property_objects == 0) { | ||
| 1907 | Carp::confess("Couldn't determine ID properties for $class_name\n"); | ||
| 1908 | } | ||
| 1909 | } | ||
| 1910 | |||
| 1911 | my %id_properties = map { $_->property_name => 1 } @id_property_objects; | ||
| 1912 | my @id_column_names = | ||
| 1913 | map { $_->column_name } | ||
| 1914 | @id_property_objects; | ||
| 1915 | |||
| 1916 | for my $property_name (sort keys %filters) | ||
| 1917 | { | ||
| 1918 | my $property = UR::Object::Property->get(class_name => $class_name, property_name => $property_name); | ||
| 1919 | next unless $property; | ||
| 1920 | |||
| 1921 | my $operator = $rule_template->operator_for($property_name); | ||
| 1922 | my $value_position = $rule_template->value_position_for_property_name($property_name); | ||
| 1923 | |||
| 1924 | delete $filters{$property_name}; | ||
| 1925 | $pk_used = 1 if $id_properties{ $property_name }; | ||
| 1926 | |||
| 1927 | if ($property->is_legacy_eav) { | ||
| 1928 | die "Old GSC EAV can be handled with a via/to/where/is_mutable=1"; | ||
| 1929 | } | ||
| 1930 | elsif ($property->is_transient) { | ||
| 1931 | die "Query by transient property $property_name on $class_name cannot be done!"; | ||
| 1932 | } | ||
| 1933 | elsif ($property->is_delegated) { | ||
| 1934 | push @delegated_properties, $property; | ||
| 1935 | } | ||
| 1936 | elsif ($property->is_calculated) { | ||
| 1937 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1938 | } | ||
| 1939 | else { | ||
| 1940 | # normal column: filter on it | ||
| 1941 | push @sql_filters, | ||
| 1942 | $class_name => | ||
| 1943 | { | ||
| 1944 | $property_name => { operator => $operator, value_position => $value_position } | ||
| 1945 | }; | ||
| 1946 | } | ||
| 1947 | } | ||
| 1948 | |||
| 1949 | $prev_id_column_name = $id_property_objects[0]->column_name; | ||
| 1950 | |||
| 1951 | } # end of inheritance loop | ||
| 1952 | |||
| 1953 | if ( my @errors = keys(%filters) ) { | ||
| 1954 | my $class_name = $class_meta->class_name; | ||
| 1955 | $ds->error_message('Unknown param(s) (' . join(',',@errors) . ") used to generate SQL for $class_name!"); | ||
| 1956 | Carp::confess(); | ||
| 1957 | } | ||
| 1958 | |||
| 1959 | my $last_class_name = $class_name; | ||
| 1960 | my $last_class_object = $class_meta; | ||
| 1961 | my $alias_num = 1; | ||
| 1962 | |||
| 1963 | my %joins_done; | ||
| 1964 | my $joins_across_data_sources; | ||
| 1965 | |||
| 1966 | DELEGATED_PROPERTY: | ||
| 1967 | for my $delegated_property (@delegated_properties) { | ||
| 1968 | my $last_alias_for_this_chain; | ||
| 1969 | |||
| 1970 | my $property_name = $delegated_property->property_name; | ||
| 1971 | my @joins = $delegated_property->_resolve_join_chain($property_name); | ||
| 1972 | #pop @joins if $joins[-1]->{foreign_class}->isa("UR::Value"); | ||
| 1973 | my $relationship_name = $delegated_property->via; | ||
| 1974 | unless ($relationship_name) { | ||
| 1975 | $relationship_name = $property_name; | ||
| 1976 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 1977 | } | ||
| 1978 | |||
| 1979 | my $delegate_class_meta = $delegated_property->class_meta; | ||
| 1980 | my($via_accessor_meta) = $delegate_class_meta->_concrete_property_meta_for_class_and_name($relationship_name); | ||
| 1981 | my $final_accessor = $delegated_property->to; | ||
| 1982 | my($final_accessor_meta) = $via_accessor_meta->data_type->__meta__->_concrete_property_meta_for_class_and_name( | ||
| 1983 | $final_accessor | ||
| 1984 | ); | ||
| 1985 | unless ($final_accessor_meta) { | ||
| 1986 | Carp::croak("No property '$final_accessor' on class " . $via_accessor_meta->data_type . | ||
| 1987 | " while resolving property $property_name on class $class_name"); | ||
| 1988 | } | ||
| 1989 | while($final_accessor_meta->is_delegated) { | ||
| 1990 | $final_accessor_meta = $final_accessor_meta->to_property_meta(); | ||
| 1991 | unless ($final_accessor_meta) { | ||
| 1992 | Carp::croak("No property '$final_accessor' on class " . $via_accessor_meta->data_type . | ||
| 1993 | " while resolving property $property_name on class $class_name"); | ||
| 1994 | } | ||
| 1995 | } | ||
| 1996 | $final_accessor = $final_accessor_meta->property_name; | ||
| 1997 | |||
| 1998 | for my $join (@joins) { | ||
| 1999 | |||
| 2000 | my $source_class_name = $join->{source_class}; | ||
| 2001 | my $source_class_object = $join->{'source_class_meta'} || $source_class_name->__meta__; | ||
| 2002 | |||
| 2003 | my $foreign_class_name = $join->{foreign_class}; | ||
| 2004 | my $foreign_class_object = $join->{'foreign_class_meta'} || $foreign_class_name->__meta__; | ||
| 2005 | my($foreign_data_source) = $UR::Context::current->resolve_data_sources_for_class_meta_and_rule($foreign_class_object, $rule_template); | ||
| 2006 | if (! $foreign_data_source) { | ||
| 2007 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 2008 | next DELEGATED_PROPERTY; | ||
| 2009 | |||
| 2010 | } elsif ($foreign_data_source ne $ds or | ||
| 2011 | ! $ds->does_support_joins or | ||
| 2012 | ! $foreign_data_source->does_support_joins | ||
| 2013 | ) | ||
| 2014 | { | ||
| 2015 | push(@{$joins_across_data_sources->{$foreign_data_source->id}}, $delegated_property); | ||
| 2016 | next DELEGATED_PROPERTY; | ||
| 2017 | } | ||
| 2018 | |||
| 2019 | my @source_property_names = @{ $join->{source_property_names} }; | ||
| 2020 | |||
| 2021 | my @source_table_and_column_names = | ||
| 2022 | map { | ||
| 2023 | my($p) = $source_class_object->_concrete_property_meta_for_class_and_name($_); | ||
| 2024 | unless ($p) { | ||
| 2025 | Carp::confess("No property $_ for class $source_class_object->{class_name}\n"); | ||
| 2026 | } | ||
| 2027 | [$p->class_name->__meta__->class_name, $p->property_name]; | ||
| 2028 | } | ||
| 2029 | @source_property_names; | ||
| 2030 | |||
| 2031 | |||
| 2032 | my $foreign_table_name = $foreign_class_name; | ||
| 2033 | |||
| 2034 | unless ($foreign_table_name) { | ||
| 2035 | # If we can't make the join because there is no datasource representation | ||
| 2036 | # for this class, we're done following the joins for this property | ||
| 2037 | # and will NOT try to filter on it at the datasource level | ||
| 2038 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 2039 | next DELEGATED_PROPERTY; | ||
| 2040 | } | ||
| 2041 | |||
| 2042 | my @foreign_property_names = @{ $join->{foreign_property_names} }; | ||
| 2043 | my @foreign_property_meta = | ||
| 2044 | map { | ||
| 2045 | $foreign_class_object->_concrete_property_meta_for_class_and_name($_); | ||
| 2046 | } | ||
| 2047 | @foreign_property_names; | ||
| 2048 | |||
| 2049 | my @foreign_column_names = | ||
| 2050 | map { | ||
| 2051 | # TODO: encapsulate | ||
| 2052 | $_->is_calculated ? (defined($_->calculate_sql) ? ($_->calculate_sql) : () ) : ($_->property_name) | ||
| 2053 | } | ||
| 2054 | @foreign_property_meta; | ||
| 2055 | |||
| 2056 | unless (@foreign_column_names) { | ||
| 2057 | # all calculated properties: don't try to join any further | ||
| 2058 | last; | ||
| 2059 | } | ||
| 2060 | unless (@foreign_column_names == @foreign_property_meta) { | ||
| 2061 | # some calculated properties, be sure to re-check for a match after loading the object | ||
| 2062 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 2063 | } | ||
| 2064 | |||
| 2065 | my $alias = $joins_done{$join->{id}}; | ||
| 2066 | unless ($alias) { | ||
| 2067 | $alias = "${relationship_name}_${alias_num}"; | ||
| 2068 | $alias_num++; | ||
| 2069 | $object_num++; | ||
| 2070 | |||
| 2071 | my @source_property_meta = map { $source_class_object->_concrete_property_meta_for_class_and_name($_) } | ||
| 2072 | @source_property_names; | ||
| 2073 | push @sql_joins, | ||
| 2074 | "$foreign_table_name $alias" => | ||
| 2075 | { | ||
| 2076 | map { | ||
| 2077 | my @coercion = $ds->cast_for_data_conversion( | ||
| 2078 | $source_property_meta[$_]->_data_type_as_class_name, | ||
| 2079 | $foreign_property_meta[$_]->_data_type_as_class_name, | ||
| 2080 | '=', | ||
| 2081 | 'join'); | ||
| 2082 | $foreign_property_names[$_] => { | ||
| 2083 | link_table_name => $last_alias_for_this_chain || $source_table_and_column_names[$_][0], | ||
| 2084 | link_column_name => $source_table_and_column_names[$_][1], | ||
| 2085 | left_coercion => $coercion[0], | ||
| 2086 | right_coercion => $coercion[1], | ||
| 2087 | } | ||
| 2088 | } | ||
| 2089 | (0..$#foreign_property_names) | ||
| 2090 | }; | ||
| 2091 | |||
| 2092 | # Add all of the columns in the join table to the return list. | ||
| 2093 | push @all_properties, | ||
| 2094 | map { [$foreign_class_object, $_, $alias, $object_num] } | ||
| 2095 | map { $_->[1] } # These three lines are to get around a bug in perl | ||
| 2096 | sort { $a->[0] cmp $b->[0] } # 5.8's sort involving method calls within the sort | ||
| 2097 | map { [ $_->property_name, $_ ] } # sub that do sorts of their own | ||
| 2098 | grep { defined($_->column_name) && $_->column_name ne '' } | ||
| 2099 | UR::Object::Property->get( class_name => $foreign_class_name ); | ||
| 2100 | |||
| 2101 | $joins_done{$join->{id}} = $alias; | ||
| 2102 | |||
| 2103 | } | ||
| 2104 | |||
| 2105 | # Set these for after all of the joins are done | ||
| 2106 | $last_class_name = $foreign_class_name; | ||
| 2107 | $last_class_object = $foreign_class_object; | ||
| 2108 | $last_alias_for_this_chain = $alias; | ||
| 2109 | |||
| 2110 | } # next join | ||
| 2111 | |||
| 2112 | unless ($delegated_property->via) { | ||
| 2113 | next; | ||
| 2114 | } | ||
| 2115 | |||
| 2116 | my($final_accessor_property_meta) = $last_class_object->_concrete_property_meta_for_class_and_name($id_properties[0]); | ||
| 2117 | unless ($final_accessor_property_meta) { | ||
| 2118 | Carp::croak("No property metadata for property named '$final_accessor' in class " . $last_class_object->class_name | ||
| 2119 | . " while resolving joins for property '" .$delegated_property->property_name . "' in class " | ||
| 2120 | . $delegated_property->class_name); | ||
| 2121 | } | ||
| 2122 | |||
| 2123 | my $sql_lvalue; | ||
| 2124 | if ($final_accessor_property_meta->is_calculated) { | ||
| 2125 | $sql_lvalue = $final_accessor_property_meta->calculate_sql; | ||
| 2126 | unless (defined($sql_lvalue)) { | ||
| 2127 | $needs_further_boolexpr_evaluation_after_loading = 1; | ||
| 2128 | next; | ||
| 2129 | } | ||
| 2130 | } | ||
| 2131 | else { | ||
| 2132 | $sql_lvalue = $final_accessor_property_meta->column_name; | ||
| 2133 | unless (defined($sql_lvalue)) { | ||
| 2134 | Carp::confess("No column name set for non-delegated/calculated property $property_name of $class_name"); | ||
| 2135 | } | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | my $operator = $rule_template->operator_for($property_name); | ||
| 2139 | my $value_position = $rule_template->value_position_for_property_name($property_name); | ||
| 2140 | } # next delegated property | ||
| 2141 | |||
| 2142 | for my $property_meta_array (@all_properties) { | ||
| 2143 | push @property_names_in_resultset_order, $property_meta_array->[1]->property_name; | ||
| 2144 | } | ||
| 2145 | |||
| 2146 | my $rule_template_without_recursion_desc = ($recursion_desc ? $rule_template->remove_filter('-recurse') : $rule_template); | ||
| 2147 | |||
| 2148 | my $rule_template_specifies_value_for_subtype; | ||
| 2149 | if ($sub_typing_property) { | ||
| 2150 | $rule_template_specifies_value_for_subtype = $rule_template->specifies_value_for($sub_typing_property) | ||
| 2151 | } | ||
| 2152 | |||
| 2153 | my @this_ds_properties = grep { ! $_->[1]->is_delegated | ||
| 2154 | and (! $_->[1]->is_calculated or $_->[1]->calculate_sql) | ||
| 2155 | } | ||
| 2156 | @all_properties; | ||
| 2157 | |||
| 2158 | my $per_object_in_resultset_loading_detail = $ds->_generate_loading_templates_arrayref(\@this_ds_properties); | ||
| 2159 | |||
| 2160 | %$self = ( | ||
| 2161 | %$self, | ||
| 2162 | |||
| 2163 | %$class_data, | ||
| 2164 | |||
| 2165 | properties_for_params => \@all_properties, | ||
| 2166 | property_names_in_resultset_order => \@property_names_in_resultset_order, | ||
| 2167 | joins => \@sql_joins, | ||
| 2168 | |||
| 2169 | rule_template_id => $rule_template->id, | ||
| 2170 | rule_template_without_recursion_desc => $rule_template_without_recursion_desc, | ||
| 2171 | rule_template_id_without_recursion_desc => $rule_template_without_recursion_desc->id, | ||
| 2172 | rule_matches_all => $rule_template->matches_all, | ||
| 2173 | rule_specifies_id => ($rule_template->specifies_value_for('id') || undef), | ||
| 2174 | rule_template_is_id_only => $rule_template->is_id_only, | ||
| 2175 | rule_template_specifies_value_for_subtype => $rule_template_specifies_value_for_subtype, | ||
| 2176 | |||
| 2177 | recursion_desc => $rule_template->recursion_desc, | ||
| 2178 | recurse_property_on_this_row => $recurse_property_on_this_row, | ||
| 2179 | recurse_property_referencing_other_rows => $recurse_property_referencing_other_rows, | ||
| 2180 | |||
| 2181 | loading_templates => $per_object_in_resultset_loading_detail, | ||
| 2182 | |||
| 2183 | joins_across_data_sources => $joins_across_data_sources, | ||
| 2184 | ); | ||
| 2185 | |||
| 2186 | return $self; | ||
| 2187 | } | ||
| 2188 | |||
| 2189 | sub _init_default { | ||
| 2190 | my $self = shift; | ||
| 2191 | my $bx_template = $self->rule_template; | ||
| 2192 | $self->{needs_further_boolexpr_evaluation_after_loading} = 1; | ||
| 2193 | my $all_possible_headers = $self->{loading_templates}[0]{property_names}; | ||
| 2194 | my $expected_headers; | ||
| 2195 | my $class_meta = $bx_template->subject_class_name->__meta__; | ||
| 2196 | for my $pname (@$all_possible_headers) { | ||
| 2197 | my $pmeta = $class_meta->property($pname); | ||
| 2198 | if ($pmeta->is_delegated) { | ||
| 2199 | next; | ||
| 2200 | } | ||
| 2201 | push @$expected_headers, $pname; | ||
| 2202 | } | ||
| 2203 | $self->{loading_templates}[0]{property_names} = $expected_headers; | ||
| 2204 | |||
| 2205 | if ($bx_template->subject_class_name->isa('UR::Value')) { | ||
| 2206 | # Hack so the objects get blessed into the proper subclass in the Object Fabricator. | ||
| 2207 | # This is necessary so every possible UR::Value subclass doesn't need its | ||
| 2208 | # own "id" property defined. Without it, the data shows that these objects get | ||
| 2209 | # loaded as the base UR::Value class (since its "id" is defined on UR:Value) | ||
| 2210 | # and then would get automagically subclassed. | ||
| 2211 | $self->{'loading_templates'}->[0]->{'final_class_name'} = $bx_template->subject_class_name | ||
| 2212 | } | ||
| 2213 | |||
| 2214 | return $self; | ||
| 2215 | } | ||
| 2216 | |||
| 2217 | |||
| 2218 | sub _init_remote_cache { | ||
| 2219 | my $self = shift; | ||
| 2220 | my $rule_template = $self->rule_template; | ||
| 2221 | my $ds = $self->data_source; | ||
| 2222 | |||
| 2223 | my $class_name = $rule_template->subject_class_name; | ||
| 2224 | my $class_meta = $class_name->__meta__; | ||
| 2225 | my $class_data = $ds->_get_class_data_for_loading($class_meta); | ||
| 2226 | |||
| 2227 | my $recursion_desc = $rule_template->recursion_desc; | ||
| 2228 | my $rule_template_without_recursion_desc = ($recursion_desc ? $rule_template->remove_filter('-recurse') : $rule_template); | ||
| 2229 | my $rule_template_specifies_value_for_subtype; | ||
| 2230 | my $sub_typing_property = $class_data->{'sub_typing_property'}; | ||
| 2231 | if ($sub_typing_property) { | ||
| 2232 | $rule_template_specifies_value_for_subtype = $rule_template->specifies_value_for($sub_typing_property) | ||
| 2233 | } | ||
| 2234 | |||
| 2235 | my @property_names = $class_name->__meta__->all_property_names; | ||
| 2236 | |||
| 2237 | %$self = ( | ||
| 2238 | %$self, | ||
| 2239 | |||
| 2240 | select_clause => '', | ||
| 2241 | select_hint => undef, | ||
| 2242 | from_clause => '', | ||
| 2243 | where_clause => '', | ||
| 2244 | connect_by_clause => '', | ||
| 2245 | order_by_clause => '', | ||
| 2246 | |||
| 2247 | needs_further_boolexpr_evaluation_after_loading => undef, | ||
| 2248 | loading_templates => [], | ||
| 2249 | |||
| 2250 | sql_params => [], | ||
| 2251 | filter_specs => [], | ||
| 2252 | property_names_in_resultset_order => \@property_names, | ||
| 2253 | properties_for_params => [], | ||
| 2254 | |||
| 2255 | rule_template_id => $rule_template->id, | ||
| 2256 | rule_template_without_recursion_desc => $rule_template_without_recursion_desc, | ||
| 2257 | rule_template_id_without_recursion_desc => $rule_template_without_recursion_desc->id, | ||
| 2258 | rule_matches_all => $rule_template->matches_all, | ||
| 2259 | rule_specifies_id => ($rule_template->specifies_value_for('id') || undef), | ||
| 2260 | rule_template_is_id_only => $rule_template->is_id_only, | ||
| 2261 | rule_template_specifies_value_for_subtype => $rule_template_specifies_value_for_subtype, | ||
| 2262 | |||
| 2263 | recursion_desc => undef, | ||
| 2264 | recurse_property_on_this_row => undef, | ||
| 2265 | recurse_property_referencing_other_rows => undef, | ||
| 2266 | |||
| 2267 | %$class_data, | ||
| 2268 | ); | ||
| 2269 | |||
| 2270 | return $self; | ||
| 2271 | } | ||
| 2272 | |||
| 2273 | sub order_by_column_list { | ||
| 2274 | my $self = shift; | ||
| 2275 | |||
| 2276 | $self->_resolve_order_by_and_descending_data(); | ||
| 2277 | return $self->{_order_by_column_list}; | ||
| 2278 | } | ||
| 2279 | |||
| 2280 | sub _resolve_order_by_and_descending_data { | ||
| 2281 | my $self = shift; | ||
| 2282 | |||
| 2283 | unless ($self->{_order_by_column_list}) { | ||
| 2284 | my %is_descending; | ||
| 2285 | my @order_by_columns = | ||
| 2286 | map { | ||
| 2287 | m/^-(.*)/ | ||
| 2288 | ? $is_descending{$1} = $1 | ||
| 2289 | : $_; | ||
| 2290 | } | ||
| 2291 | @{ $self->order_by_columns || [] }; | ||
| 2292 | |||
| 2293 | $self->{_order_by_column_list} = \@order_by_columns; | ||
| 2294 | $self->{_order_by_column_is_descending} = \%is_descending; | ||
| 2295 | } | ||
| 2296 | } | ||
| 2297 | |||
| 2298 | sub order_by_column_is_descending { | ||
| 2299 | my($self, $column_name) = @_; | ||
| 2300 | |||
| 2301 | $self->_resolve_order_by_and_descending_data(); | ||
| 2302 | return $self->{_order_by_column_is_descending}->{$column_name}; | ||
| 2303 | } | ||
| 2304 | |||
| 2305 | sub property_meta_for_column { | ||
| 2306 | my($self, $table_and_column_name) = @_; | ||
| 2307 | |||
| 2308 | $table_and_column_name = lc($table_and_column_name); | ||
| 2309 | |||
| 2310 | my $data_source = $self->data_source(); | ||
| 2311 | my ($table_name, $column_name) = $data_source->_resolve_table_and_column_from_column_name($table_and_column_name); | ||
| 2312 | |||
| 2313 | if (my $join = $self->_get_alias_join($table_name)) { | ||
| 2314 | # The given $table_name was actually a join alias | ||
| 2315 | my $foreign_class_meta = $join->foreign_class->__meta__; | ||
| 2316 | my $prop_name = $foreign_class_meta->property_for_column($column_name); | ||
| 2317 | return $prop_name | ||
| 2318 | ? $foreign_class_meta->property_meta_for_name($prop_name) | ||
| 2319 | : undef; | ||
| 2320 | |||
| 2321 | } else { | ||
| 2322 | my $class_meta = $self->class_name->__meta__; | ||
| 2323 | my $prop_name = $class_meta->property_for_column($table_and_column_name); | ||
| 2324 | return $class_meta->property_meta_for_name($prop_name); | ||
| 2325 | } | ||
| 2326 | } | ||
| 2327 | |||
| 2328 | 1; | ||
| 2329 |