| 1 | |||
| 2 | package Xmldoom::Criteria; | ||
| 3 | |||
| 4 | use Xmldoom::Criteria::Search; | ||
| 5 | use Xmldoom::Criteria::ExplicitJoinVisitor; | ||
| 6 | use Xmldoom::Threads; | ||
| 7 | use DBIx::Romani::Query::SQL::Column; | ||
| 8 | use strict; | ||
| 9 | |||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | # Search types | ||
| 13 | our $AND = 'AND'; | ||
| 14 | our $OR = 'OR'; | ||
| 15 | |||
| 16 | # comparison types | ||
| 17 | our $EQUAL = '='; | ||
| 18 | our $NOT_EQUAL = '<>'; | ||
| 19 | our $GREATER_THAN = '>'; | ||
| 20 | our $GREATER_EQUAL = '>='; | ||
| 21 | our $LESS_THAN = '<'; | ||
| 22 | our $LESS_EQUAL = '<='; | ||
| 23 | our $LIKE = 'LIKE'; | ||
| 24 | our $NOT_LIKE = 'NOT LIKE'; | ||
| 25 | our $ILIKE = 'ILIKE'; | ||
| 26 | our $NOT_ILIKE = 'NOT ILIKE'; | ||
| 27 | our $BETWEEN = 'BETWEEN'; | ||
| 28 | our $IN = 'IN'; | ||
| 29 | our $NOT_IN = 'NOT IN'; | ||
| 30 | our $IS_NULL = 'IS NULL'; | ||
| 31 | our $IS_NOT_NULL = 'IS NOT NULL'; | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my $class = shift; | ||
| 36 | my $args = shift; | ||
| 37 | |||
| 38 | my $parent; | ||
| 39 | my $shared; | ||
| 40 | |||
| 41 | if ( ref($args) eq 'HASH' ) | ||
| 42 | { | ||
| 43 | $parent = $args->{parent}; | ||
| 44 | $shared = $args->{shared}; | ||
| 45 | } | ||
| 46 | else | ||
| 47 | { | ||
| 48 | $parent = $args; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $self = { | ||
| 52 | parent => $parent, | ||
| 53 | search => Xmldoom::Criteria::Search->new({ type => 'AND' }), | ||
| 54 | order_by => [ ], | ||
| 55 | group_by => [ ], | ||
| 56 | limit => undef, | ||
| 57 | offset => undef, | ||
| 58 | }; | ||
| 59 | |||
| 60 | bless $self, $class; | ||
| 61 | |||
| 62 | # we want to move into shared memory as soon as possible | ||
| 63 | $self = Xmldoom::Threads::make_shared($self, $shared); | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub get_parent { return shift->{parent}; } | ||
| 69 | sub get_type { return "AND"; } | ||
| 70 | sub get_order_by { return shift->{order_by}; } | ||
| 71 | sub get_group_by { return shift->{order_by}; } | ||
| 72 | sub get_limit { return shift->{limit}; } | ||
| 73 | sub get_offset { return shift->{offset}; } | ||
| 74 | |||
| 75 | sub set_parent | ||
| 76 | { | ||
| 77 | my ($self, $parent) = @_; | ||
| 78 | $self->{parent} = $parent; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub set_limit | ||
| 82 | { | ||
| 83 | my ($self, $limit, $offset) = @_; | ||
| 84 | $self->{limit} = $limit; | ||
| 85 | $self->{offset} = $offset; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub add | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | $self->{search}->add( @_ ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub add_attr | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | $self->{search}->add_attr( @_ ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub add_prop | ||
| 101 | { | ||
| 102 | my $self = shift; | ||
| 103 | $self->{search}->add_prop( @_ ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub join_attr | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | $self->{search}->join_attr( @_ ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub join_prop | ||
| 113 | { | ||
| 114 | my $self = shift; | ||
| 115 | $self->{search}->join_prop( @_ ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub add_order_by_attr | ||
| 119 | { | ||
| 120 | my ($self, $attr, $dir) = @_; | ||
| 121 | my ($table_name, $column) = split '/', $attr; | ||
| 122 | |||
| 123 | my %order_by = ( | ||
| 124 | attr => { | ||
| 125 | table_name => $table_name, | ||
| 126 | column => $column, | ||
| 127 | }, | ||
| 128 | value => { | ||
| 129 | dir => $dir, | ||
| 130 | } | ||
| 131 | ); | ||
| 132 | |||
| 133 | push @{$self->{order_by}}, \%order_by; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub add_order_by_prop | ||
| 137 | { | ||
| 138 | my ($self, $prop, $dir) = @_; | ||
| 139 | my ($object_name, $prop_name) = split '/', $prop; | ||
| 140 | |||
| 141 | my %order_by = ( | ||
| 142 | prop => { | ||
| 143 | object_name => $object_name, | ||
| 144 | prop_name => $prop_name, | ||
| 145 | }, | ||
| 146 | value => { | ||
| 147 | dir => $dir, | ||
| 148 | } | ||
| 149 | ); | ||
| 150 | |||
| 151 | push @{$self->{order_by}}, \%order_by; | ||
| 152 | } | ||
| 153 | |||
| 154 | # A convenience alias. | ||
| 155 | sub add_order_by | ||
| 156 | { | ||
| 157 | my $self = shift; | ||
| 158 | $self->add_order_by_prop(@_); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub add_group_by_attr | ||
| 162 | { | ||
| 163 | my ($self, $attr) = @_; | ||
| 164 | my ($table_name, $column) = split '/', $attr; | ||
| 165 | |||
| 166 | my %group_by = ( | ||
| 167 | attr => { | ||
| 168 | table_name => $table_name, | ||
| 169 | column => $column, | ||
| 170 | } | ||
| 171 | ); | ||
| 172 | |||
| 173 | push @{$self->{group_by}}, \%group_by; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub add_group_by_prop | ||
| 177 | { | ||
| 178 | my ($self, $prop) = @_; | ||
| 179 | my ($object_name, $prop_name) = split '/', $prop; | ||
| 180 | |||
| 181 | my %group_by = ( | ||
| 182 | prop => { | ||
| 183 | object_name => $object_name, | ||
| 184 | prop_name => $prop_name, | ||
| 185 | } | ||
| 186 | ); | ||
| 187 | |||
| 188 | push @{$self->{group_by}}, \%group_by; | ||
| 189 | } | ||
| 190 | |||
| 191 | # A convenience alias. | ||
| 192 | sub add_group_by | ||
| 193 | { | ||
| 194 | my $self = shift; | ||
| 195 | $self->add_group_by_prop(@_); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _setup_query | ||
| 199 | { | ||
| 200 | my ($self, $database, $query) = @_; | ||
| 201 | |||
| 202 | my $search; | ||
| 203 | |||
| 204 | if ( defined $self->{parent} ) | ||
| 205 | { | ||
| 206 | $search = $self->{search}->clone(); | ||
| 207 | |||
| 208 | # add the values of its primary keys to the criteria | ||
| 209 | foreach my $col ( @{$self->{parent}->_get_definition()->get_table()->get_columns()} ) | ||
| 210 | { | ||
| 211 | if ( $col->{primary_key} ) | ||
| 212 | { | ||
| 213 | my $attr_name = join '/', $self->{parent}->_get_definition()->get_table_name(), $col->{name}; | ||
| 214 | # we use key instead of the attr values, in case they were changed, we | ||
| 215 | # should still query against the current database value. | ||
| 216 | $search->add_attr( $attr_name, $self->{parent}->{key}->{$col->{name}} ); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | else | ||
| 221 | { | ||
| 222 | $search = $self->{search}; | ||
| 223 | } | ||
| 224 | |||
| 225 | # add the from stuff | ||
| 226 | foreach my $table_name ( @{$search->get_tables($database)} ) | ||
| 227 | { | ||
| 228 | $query->add_from( $table_name ); | ||
| 229 | } | ||
| 230 | |||
| 231 | # build the where clause | ||
| 232 | $query->set_where( $search->generate($database) ); | ||
| 233 | |||
| 234 | # set the limit and offset | ||
| 235 | $query->set_limit( $self->{limit}, $self->{offset} ); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub _apply_order_by_to_query | ||
| 239 | { | ||
| 240 | my ($self, $database, $query) = @_; | ||
| 241 | |||
| 242 | foreach my $order_by ( @{$self->{order_by}} ) | ||
| 243 | { | ||
| 244 | if ( defined $order_by->{attr} ) | ||
| 245 | { | ||
| 246 | # add the table to the query | ||
| 247 | $query->add_from( $order_by->{attr}->{table_name} ); | ||
| 248 | |||
| 249 | # add, yo. | ||
| 250 | my $value = DBIx::Romani::Query::SQL::Column->new({ | ||
| 251 | table => $order_by->{attr}->{table_name}, | ||
| 252 | name => $order_by->{attr}->{column} | ||
| 253 | }); | ||
| 254 | $query->add_order_by({ value => $value, dir => $order_by->{value}->{dir} }); | ||
| 255 | } | ||
| 256 | elsif ( defined $order_by->{prop} ) | ||
| 257 | { | ||
| 258 | my $object = $database->get_object( $order_by->{prop}->{object_name} ); | ||
| 259 | if ( not defined $object ) | ||
| 260 | { | ||
| 261 | die "Unable to find object '$order_by->{prop}->{object_name}' in order_by"; | ||
| 262 | } | ||
| 263 | |||
| 264 | my $prop = $object->get_property( $order_by->{prop}->{prop_name} ); | ||
| 265 | if ( not defined $prop ) | ||
| 266 | { | ||
| 267 | die "Unable to find property '$order_by->{prop}->{prop_name}' in object '$order_by->{prop}->{prop_name}' in order_by"; | ||
| 268 | } | ||
| 269 | |||
| 270 | # TODO: this should really "visit" the returned lval to determine what | ||
| 271 | # tables this includes ... | ||
| 272 | $query->add_from( $object->get_table_name() ); | ||
| 273 | |||
| 274 | foreach my $lval ( @{$prop->get_query_lval()} ) | ||
| 275 | { | ||
| 276 | $query->add_order_by({ value => $lval, dir => $order_by->{value}->{dir} }); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | # TODO: This was just copied from _apply_order_by_to_query. These two should be | ||
| 283 | # merged if possible somehow. | ||
| 284 | sub _apply_group_by_to_query | ||
| 285 | { | ||
| 286 | my ($self, $database, $query) = @_; | ||
| 287 | |||
| 288 | foreach my $group_by ( @{$self->{group_by}} ) | ||
| 289 | { | ||
| 290 | if ( defined $group_by->{attr} ) | ||
| 291 | { | ||
| 292 | # add the table to the query | ||
| 293 | $query->add_from( $group_by->{attr}->{table_name} ); | ||
| 294 | |||
| 295 | # add, yo. | ||
| 296 | my $value = DBIx::Romani::Query::SQL::Column->new({ | ||
| 297 | table => $group_by->{attr}->{table_name}, | ||
| 298 | name => $group_by->{attr}->{column} | ||
| 299 | }); | ||
| 300 | $query->add_group_by( $value ); | ||
| 301 | } | ||
| 302 | elsif ( defined $group_by->{prop} ) | ||
| 303 | { | ||
| 304 | my $object = $database->get_object( $group_by->{prop}->{object_name} ); | ||
| 305 | if ( not defined $object ) | ||
| 306 | { | ||
| 307 | die "Unable to find object '$group_by->{prop}->{object_name}' in group_by"; | ||
| 308 | } | ||
| 309 | |||
| 310 | my $prop = $object->get_property( $group_by->{prop}->{prop_name} ); | ||
| 311 | if ( not defined $prop ) | ||
| 312 | { | ||
| 313 | die "Unable to find property '$group_by->{prop}->{prop_name}' in object '$group_by->{prop}->{prop_name}' in group_by"; | ||
| 314 | } | ||
| 315 | |||
| 316 | # TODO: this should really "visit" the returned lval to determine what | ||
| 317 | # tables this includes ... | ||
| 318 | $query->add_from( $object->get_table_name() ); | ||
| 319 | |||
| 320 | foreach my $lval ( @{$prop->get_query_lval()} ) | ||
| 321 | { | ||
| 322 | $query->add_group_by( $lval ); | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | sub _join_to_tables | ||
| 329 | { | ||
| 330 | my ($self, $database, $query) = @_; | ||
| 331 | |||
| 332 | # if there is only one table of this query then we don't have to worry at all. | ||
| 333 | if ( scalar @{$query->get_from()} == 1 ) | ||
| 334 | { | ||
| 335 | return; | ||
| 336 | } | ||
| 337 | |||
| 338 | # get a list of exiplicit joins on the existings query | ||
| 339 | my $visitor = Xmldoom::Criteria::ExplicitJoinVisitor->new(); | ||
| 340 | my $explicit_joins; | ||
| 341 | if ( $query->get_where() ) | ||
| 342 | { | ||
| 343 | $explicit_joins = $query->get_where()->visit( $visitor ); | ||
| 344 | } | ||
| 345 | #print Dumper $explicit_joins; | ||
| 346 | |||
| 347 | my $where = DBIx::Romani::Query::Where->new(); | ||
| 348 | my @tables = @{$query->get_from()}; | ||
| 349 | my $main_table = shift @tables; | ||
| 350 | my %joined_hash = ( $main_table => 1 ); | ||
| 351 | my %tables_hash = map { $_ => 1 } @tables; | ||
| 352 | |||
| 353 | while( scalar keys %tables_hash > 0 ) | ||
| 354 | { | ||
| 355 | my $joined = 0; | ||
| 356 | |||
| 357 | # go through the list of unconnected tables | ||
| 358 | foreach my $table_name ( keys %tables_hash ) | ||
| 359 | { | ||
| 360 | my $links = [ ]; | ||
| 361 | my $new_link; | ||
| 362 | |||
| 363 | # look for connections to already connected tables | ||
| 364 | foreach my $other_table_name ( keys %joined_hash ) | ||
| 365 | { | ||
| 366 | $links = [ @$links, @{$database->find_links( $table_name, $other_table_name )} ]; | ||
| 367 | } | ||
| 368 | |||
| 369 | # reduce the overlapping links | ||
| 370 | $links = Xmldoom::Definition::Link::reduce_shortest( $links ); | ||
| 371 | |||
| 372 | # if there are no links, then we look to see if an explicit link exists | ||
| 373 | if ( scalar @$links == 0 ) | ||
| 374 | { | ||
| 375 | foreach my $explicit ( @$explicit_joins ) | ||
| 376 | { | ||
| 377 | if ( $explicit->{local_table} eq $table_name and | ||
| 378 | $joined_hash{$explicit->{foreign_table}} ) | ||
| 379 | { | ||
| 380 | # cool! We've got an explicit link to one of the joined tables. | ||
| 381 | $joined = 1; | ||
| 382 | last; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | } | ||
| 386 | elsif ( scalar @$links > 1 ) | ||
| 387 | { | ||
| 388 | # attempt to disambiguate the multiple links using the explicit joins. | ||
| 389 | |||
| 390 | LINK: foreach my $link ( @$links ) | ||
| 391 | { | ||
| 392 | my $is_already_explicit = 1; | ||
| 393 | |||
| 394 | # check to see if all of the foreign keys in this link, are covered by | ||
| 395 | # a pre-existing explicit link. | ||
| 396 | foreach my $fn ( @{$link->get_foreign_keys()} ) | ||
| 397 | { | ||
| 398 | foreach my $ref ( @{$fn->get_column_names()} ) | ||
| 399 | { | ||
| 400 | my $has_this_one = 0; | ||
| 401 | |||
| 402 | foreach my $explicit ( @$explicit_joins ) | ||
| 403 | { | ||
| 404 | if ( $ref->{local_table} eq $explicit->{local_table} and | ||
| 405 | $ref->{local_column} eq $explicit->{local_column} and | ||
| 406 | $ref->{foreign_table} eq $explicit->{foreign_table} and | ||
| 407 | $ref->{foreign_column} eq $explicit->{foreign_column} ) | ||
| 408 | { | ||
| 409 | $has_this_one = 1; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | if ( not $has_this_one ) | ||
| 414 | { | ||
| 415 | $is_already_explicit = 0; | ||
| 416 | next LINK; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | if ( $is_already_explicit ) | ||
| 422 | { | ||
| 423 | # we are explicitly joined already, yo! | ||
| 424 | $joined = 1; | ||
| 425 | last; | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | if ( not $joined ) | ||
| 430 | { | ||
| 431 | # This is an error! A serious error, yo! | ||
| 432 | die "There are multiple ways in which these tables could be linked, so an explicit join must be used to select one of them." | ||
| 433 | |||
| 434 | #print STDERR "WARNING: There are multiple ways in which these tables could be linked, but no explicit join was given so the first available link was chosen.\n"; | ||
| 435 | #$new_link = $links->[0]; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | else | ||
| 439 | { | ||
| 440 | # We only have the one possible link, so attempt to automatically join on that. | ||
| 441 | $new_link = $links->[0]; | ||
| 442 | } | ||
| 443 | |||
| 444 | if ( defined $new_link ) | ||
| 445 | { | ||
| 446 | # join the two tables | ||
| 447 | foreach my $fn ( @{$new_link->get_foreign_keys()} ) | ||
| 448 | { | ||
| 449 | foreach my $ref ( @{$fn->get_column_names()} ) | ||
| 450 | { | ||
| 451 | my $is_already_explicit = 0; | ||
| 452 | |||
| 453 | foreach my $explicit ( @$explicit_joins ) | ||
| 454 | { | ||
| 455 | if ( $ref->{local_table} eq $explicit->{local_table} and | ||
| 456 | $ref->{local_column} eq $explicit->{local_column} and | ||
| 457 | $ref->{foreign_table} eq $explicit->{foreign_table} and | ||
| 458 | $ref->{foreign_column} eq $explicit->{foreign_column} ) | ||
| 459 | { | ||
| 460 | $is_already_explicit = 1; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | if ( not $is_already_explicit ) | ||
| 465 | { | ||
| 466 | my $join = DBIx::Romani::Query::Comparison->new(); | ||
| 467 | |||
| 468 | # NOTE: We do this in reverse than expected order because we looping | ||
| 469 | # essentially backwards. The first item on the list of foriegn tables | ||
| 470 | # is thought to be our master table... | ||
| 471 | |||
| 472 | $join->add( DBIx::Romani::Query::SQL::Column->new( $ref->{foreign_table}, $ref->{foreign_column} ) ); | ||
| 473 | $join->add( DBIx::Romani::Query::SQL::Column->new( $ref->{local_table}, $ref->{local_column} ) ); | ||
| 474 | $where->add( $join ); | ||
| 475 | |||
| 476 | # add to the from list if we are dealing with a many-to-many link | ||
| 477 | $query->add_from( $ref->{foreign_table} ); | ||
| 478 | |||
| 479 | # also, remove this from the unjoined tables and add them to the | ||
| 480 | # the joined list! | ||
| 481 | $joined_hash{$ref->{foreign_table}} = 1; | ||
| 482 | delete $tables_hash{$ref->{foreign_table}}; | ||
| 483 | } | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | $joined = 1; | ||
| 488 | } | ||
| 489 | |||
| 490 | if ( $joined ) | ||
| 491 | { | ||
| 492 | # we mark this table as joined and restart | ||
| 493 | $joined_hash{$table_name} = 1; | ||
| 494 | delete $tables_hash{$table_name}; | ||
| 495 | last; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | if ( not $joined ) | ||
| 500 | { | ||
| 501 | die "Unable to join the following tables: " . join(', ', keys %tables_hash); | ||
| 502 | } | ||
| 503 | } | ||
| 504 | |||
| 505 | # merge the old where statement with the connection one | ||
| 506 | if ( scalar @{$where->get_values()} > 0 ) | ||
| 507 | { | ||
| 508 | my $old_where = $query->get_where(); | ||
| 509 | if ( $old_where ) | ||
| 510 | { | ||
| 511 | $where->add ( $old_where ); | ||
| 512 | } | ||
| 513 | $query->set_where( $where ); | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | sub generate_query_for_object | ||
| 518 | { | ||
| 519 | my ($self, $database, $object_name) = @_; | ||
| 520 | |||
| 521 | my $definition = $database->get_object( $object_name ); | ||
| 522 | my $query = $definition->get_select_query()->clone(); | ||
| 523 | |||
| 524 | # setup the query | ||
| 525 | $self->_setup_query( $database, $query ); | ||
| 526 | |||
| 527 | # add the order by | ||
| 528 | $self->_apply_order_by_to_query( $database, $query ); | ||
| 529 | |||
| 530 | # add the group by | ||
| 531 | $self->_apply_group_by_to_query( $database, $query ); | ||
| 532 | |||
| 533 | # make sure all the appropriate connections exist | ||
| 534 | $self->_join_to_tables( $database, $query ); | ||
| 535 | |||
| 536 | return $query; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub generate_query_for_object_count | ||
| 540 | { | ||
| 541 | my ($self, $database, $object_name) = @_; | ||
| 542 | |||
| 543 | my $definition = $database->get_object( $object_name ); | ||
| 544 | |||
| 545 | my $query = $definition->get_select_query()->clone(); | ||
| 546 | my $table = $definition->get_table(); | ||
| 547 | my $table_name = $definition->get_table_name(); | ||
| 548 | |||
| 549 | # make a query for COUNT() of the objects first primary key | ||
| 550 | $query->clear_result(); | ||
| 551 | foreach my $column ( @{$table->get_columns()} ) | ||
| 552 | { | ||
| 553 | if ( $column->{primary_key} ) | ||
| 554 | { | ||
| 555 | my $count = DBIx::Romani::Query::Function::Count->new(); | ||
| 556 | $count->add( DBIx::Romani::Query::SQL::Column->new( $table_name, $column->{name} ) ); | ||
| 557 | $query->add_result( $count, 'count' ); | ||
| 558 | |||
| 559 | # we're cool | ||
| 560 | last; | ||
| 561 | } | ||
| 562 | } | ||
| 563 | |||
| 564 | # setup the query | ||
| 565 | $self->_setup_query( $database, $query ); | ||
| 566 | |||
| 567 | # make sure all the appropriate connections exist | ||
| 568 | $self->_join_to_tables( $database, $query ); | ||
| 569 | |||
| 570 | # we don't want to limit or offset on a count query | ||
| 571 | $query->clear_limit(); | ||
| 572 | |||
| 573 | return $query; | ||
| 574 | } | ||
| 575 | |||
| 576 | sub generate_query_for_attrs | ||
| 577 | { | ||
| 578 | my ($self, $database, $attrs) = (shift, shift, shift); | ||
| 579 | |||
| 580 | # we can put on the list, or use an array hash | ||
| 581 | if ( ref($attrs) ne 'ARRAY' ) | ||
| 582 | { | ||
| 583 | $attrs = [ $attrs, @_ ]; | ||
| 584 | } | ||
| 585 | |||
| 586 | my $query = DBIx::Romani::Query::Select->new(); | ||
| 587 | |||
| 588 | foreach my $attr ( @$attrs ) | ||
| 589 | { | ||
| 590 | my ($table_name, $column) = split '/', $attr; | ||
| 591 | |||
| 592 | # add the column to the result list | ||
| 593 | $query->add_result( DBIx::Romani::Query::SQL::Column->new( $table_name, $column ) ); | ||
| 594 | |||
| 595 | # add to the table list | ||
| 596 | $query->add_from( $table_name ); | ||
| 597 | } | ||
| 598 | |||
| 599 | # setup the query | ||
| 600 | $self->_setup_query( $database, $query ); | ||
| 601 | |||
| 602 | # add the order by stuff | ||
| 603 | $self->_apply_order_by_to_query( $database, $query ); | ||
| 604 | |||
| 605 | # add the group by | ||
| 606 | $self->_apply_group_by_to_query( $database, $query ); | ||
| 607 | |||
| 608 | # make sure all the appropriate connections exist | ||
| 609 | $self->_join_to_tables( $database, $query ); | ||
| 610 | |||
| 611 | return $query; | ||
| 612 | } | ||
| 613 | |||
| 614 | sub generate_description | ||
| 615 | { | ||
| 616 | my $self = shift; | ||
| 617 | |||
| 618 | return $self->{search}->generate_description( @_ ); | ||
| 619 | } | ||
| 620 | |||
| 621 | sub clone | ||
| 622 | { | ||
| 623 | my $self = shift; | ||
| 624 | my $parent = shift; | ||
| 625 | |||
| 626 | if ( not defined $parent ) | ||
| 627 | { | ||
| 628 | $parent = $self->get_parent(); | ||
| 629 | } | ||
| 630 | |||
| 631 | my $criteria = Xmldoom::Criteria->new( $parent ); | ||
| 632 | |||
| 633 | # copy all the deep information | ||
| 634 | $criteria->{search} = $self->{search}->clone(); | ||
| 635 | foreach my $order_by ( @{$self->get_order_by()} ) | ||
| 636 | { | ||
| 637 | push @{$criteria->{order_by}}, $order_by; | ||
| 638 | } | ||
| 639 | foreach my $group_by ( @{$self->get_group_by()} ) | ||
| 640 | { | ||
| 641 | push @{$criteria->{group_by}}, $group_by; | ||
| 642 | } | ||
| 643 | |||
| 644 | # shallow mallow | ||
| 645 | $criteria->set_limit( $self->get_limit(), $self->get_offset() ); | ||
| 646 | |||
| 647 | return $criteria; | ||
| 648 | } | ||
| 649 | |||
| 650 | 1; | ||
| 651 |