Mose
Mose

Reputation: 563

Rose::DB::Object::Manager query with a list of object ids

I'm trying to write a Rose::DB::Object query string using either an Array or a Hash, however I'm unsure how to do it. I'm trying to write an update function based off of certain ID's in a list that are enumerated in the array. I unfortunately do not have any other unique key to filter on to build the query, so I need to query specific ID's.

Essentially I am trying to programatically write the follow:

my $list = My::DB::Manager->get_items(query => [
     {id => 1},
     {id => 14},
     {id => 210},
     {id => 1102},
     {id => 3151},
]);

This is the code I have so far, but I haven't been able to successfully achieve what I am trying to do:

        use My::DB::Manager;
        my @ary;
        foreach (@_) {
            my %col = ("id", $_);
            push (@ary, \%col);
        }
        my $list = My::DB::Manager->get_items(query => \@ary);
        ...

./test.pl

Now the script just hangs with no output indefinately.

I'm trying to avoid iterating through the DB::Manager and making a DB call on a per record basis as this script will be run via cron every 60 seconds and has the potential to return large sets.

Upvotes: 1

Views: 660

Answers (2)

John Siracusa
John Siracusa

Reputation: 15271

The query parameter takes a reference to an array of name/value pairs, not a reference to an array of hash references. If you want objects where the value of the id column is one of a list of values, then use the name id and a reference to an array of ids as the value. This code should work (assuming the id values are in @_):

$list = My::DB::Manager->get_items(query => [ id => \@_ ]);

Upvotes: 2

ruz
ruz

Reputation: 492

You push strings into @ary when you need to push perl structures:

    use My::DB::Manager;
    my @ary;
    foreach (@_) {
            push (@ary, { id => $_ });
    }
    my $list = My::DB::Manager->get_items(query => [@ary]);
    ...

However, I think you can use query => [ id => [$id1, $id2, ... ], ...]:

    use My::DB::Manager;
    my $list = My::DB::Manager->get_items(query => [ id => \@_ ]);
    ...

Never used Rose, this based on docs of the module.

Upvotes: 0

Related Questions