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

NAME

ObjectDB::Table - actions on tables

SYNOPSIS

    package MyDB;
    use base 'ObjectDB';

    sub init_db {
        ...
        return $dbh;
    }

    package MyAuthor;
    use base 'MyDB';

    __PACKAGE__->meta(
        table          => 'author',
        columns        => [qw/id name/],
        primary_key    => 'id',
        auto_increment => 'id',
        relationships  => {
            books => {
                type = 'one to many',
                class => 'MyBook',
                map   => {id => 'author_id'}
            }
        }
    );

    package MyBook;
    use base 'MyDB';

    __PACKAGE__->meta(
        table          => 'book',
        columns        => [qw/id author_id title/],
        primary_key    => 'id',
        auto_increment => 'id',
        relationships  => {
            author => {
                type = 'many to one',
                class => 'MyAuthor',
                map   => {author_id => 'id'}
            }
        }
    );

    my @books = MyBook->table->find(
        with     => 'author',
        order_by => [title => 'ASC'],
        page     => 1,
        per_page => 10
    );

DESCRIPTION

ObjectDB::Table allows to perform actions on table: find, update, delete many rows at a time.

Methods

find

Finds specific rows. Query builder is SQL::Composer.

    my @books = MyBook->table->find;
    my @books = MyBook->table->find(where => [...]);
    my @books = MyBook->table->find(where => [...], order_by => [...]);
    my @books =
      MyBook->table->find(where => [...], order_by => [...], group_by => [...]);

When using rows_as_hashes returns array of hashes instead of objects.

find_by_compose

Finds by using raw query.

    my @books = MyBook->find_by_compose(table => 'book', columns => ['id', 'title']);

When using rows_as_hashes returns array of hashes instead of objects.

find_by_sql

Finds by using raw SQL.

    my @books = MyBook->find_by_sql('SELECT * FROM books WHERE title = ?', ['About Everything']);
    my @books = MyBook->find_by_sql('SELECT * FROM books WHERE title = :title', { title => 'About Everything' });

When using rows_as_hashes returns array of hashes instead of objects.

count

A convenient method for counting.

    my $total_books = MyBook->table->count;
update

Updates many rows at a time.

    MyBook->table->update(set => {author_id => 1}, where => [author_id => 2]);
delete

Deletes many rows at a time.

    MyBook->table->delete;
    MyBook->table->delete(where => [...]);