ObjectDB::Table - actions on tables
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 );
ObjectDB::Table allows to perform actions on table: find, update, delete many rows at a time.
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.
rows_as_hashes
find_by_compose
Finds by using raw query.
my @books = MyBook->find_by_compose(table => 'book', columns => ['id', 'title']);
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' });
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 => [...]);
To install ObjectDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ObjectDB
CPAN shell
perl -MCPAN -e shell install ObjectDB
For more information on module installation, please visit the detailed CPAN module installation guide.