The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::ResultSet::Cookbook - Common recipes for DBIx::ResultSet.

RECIPES

JOINing

Joins are currently not supported. This is mostly because SQL::Abstract does not support them, and I've yet to have the time or impetus to try and dig in to how DBIx::Class::ResultSet does it. Also, in many cases a subselect is a better choice.

Another option is to use DBIx::ResultSet's where_sql() to produce the WHERE portion of the SQL while still being able to write the FROM/JOIN portion of the SQL with raw SQL. For example:

    my ($where_sql, @bind) = $users_rs->search({ 'u.status' => 1});
    
    my $sth = $dbh->prepare(qq[
        SELECT u.user_id, e.sent_date
        FROM users u
        JOIN emails e ON (e.user_id = u.user_id)
        $where_sql
    ]);
    
    $sth->execute( @bind );

Subselects

SQL::Abstract supports subselects natively, and DBIx::ResultSet provides a very elegant way to use them:

    my $emailed_users_rs = $users_rs->search({
        user_id => {-in => \[ $emails_rs->select_sql(['user_id']) ]},
    });

The above example build a SQL query like this:

    SELECT *
    FROM users
    WHERE user_id IN (
        SELECT user_id FROM emails
    );

The SQL::Abstract documentation gives a few more examples of this. Searching for a backslash and an opening square brace "\[" will give you the examples.

bind_param

See bindtype in SQL::Abstract docs. A way to easly integrate this with DBIx::ResultSet has not yet been developed.

Auto PKs

Automatically inrementing primary key values may be retrieved after an insert by calling:

    my $user_id = $users_rs->auto_pk();

This is only supported by a couple database at the moment (MySQL, and SQLte), and more are planned in the future. If you're database isn't yet supported ahn exception will be thrown.

AUTHOR

Aran Clary Deltac <bluefeet@gmail.com>

LICENSE

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.