On 12/3/06 2:02 PM, [EMAIL PROTECTED] wrote:
> I'd like to perform a query like
> 
>     SELECT DISTINCT some_val from some_table
>     WHERE event_time > ...
>
> to get all distinct values of some_val after a given time. Using
> 
>     my $events = WatchLAN::Activity::Manager->get_some_table(
>         query      => [ event_time => { gt => ... },
>                       ],
>         distinct   => 1,
>     );
> 
> yields
> 
>     SELECT DISTINCT id, event_time, some_val from some_table
>     WHERE event_time > ...

This will do what you want:

    $events =
      WatchLAN::Activity::Manager->get_some_table(
        distinct => 1,
        select   => [ 'some_val ' ],
        query    => [ event_time => { gt => ... } ]);

but keep in mind that it'll return objects that have no data in them other
than the value of the "some_val" column.

Really, this is not an appropriate of use of get_objects() since the
returned objects are not linked with their corresponding rows in the
database.  Were you to try to save() one, for example, it'll blow up:

    $events->[0]->save; # fatal error!

because each object *thinks* its a representations of a uniquely identified,
existing row in the database.  Instead, in this case, each object is just a
trivial container for a distinct value of some_val--which many actual rows
in the table may have.

A more appropriate solution would use a "get_results()" Manager method that
does not return RDBO-derived objects, but rather just returns data.  Such a
Manager method does not exist yet, but it's on my TODO list.  In the
meantime you can be a bit more "pure" by making your own wrapper method that
pulls the some_val values out of the objects and returns them "bare."
Something like this:

  package WatchLAN::Activity::Manager;
  ...
  sub get_distinct_values
  {
    my($class) = shift;
  
    my %args   = (@_ == 1) ? (column => $_[0]) : @_;
  
    my $column = delete $args{'column'};
    my $method = Item->meta->column($column)->accessor_method_name;
  
    my $items  = $class->get_items(distinct => 1, select => $column, %args);
    my @vals   = map { $_->$method() } @$items;
  
    return wantarray ? @vals : [EMAIL PROTECTED];
  }

Then you can do this:

  @all_vals = WatchLAN::Activity::Manager->get_distinct_values('some_val');

  @certain_vals = 
    WatchLAN::Activity::Manager->get_distinct_values(
      column => 'some_val',
      query  => [ event_time => { gt => ... } ]);

and so on.  Generalizing get_distinct_values() to work for any Manager class
is left as an exercise for the reader (hint: "object_class" param)

-John



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to