Alzabo
view release on metacpan or search on metacpan
lib/Alzabo/Intro.pod view on Meta::CPAN
type => 'varchar',
length => 240,
nullable => 0 );
$table->make_index( columns => [ { column => $b_col,
prefix => 10 } ] );
...
$s->save_to_file;
};
if ($@) { handle exceptions }
=head1 USAGE EXAMPLES
Alzabo is a powerful tool but as with many powerful tools it can also
be a bit overwhelming at first. The easiest way to understand some of
its basic capabilities is through some examples. Let's first assume
that you've created the following schema:
TABLE: Movie
movie_id tinyint -- primary key
title varchar(200)
release_year year
TABLE: Person
person_id tinyint -- primary key
name varchar(200)
birthdate date
birthplace_location_id tinyint -- foreign key to location
TABLE: Job
job_id tinyint -- primary key
job varchar(200) -- something like 'actor' or 'director'
TABLE: Credit
movie_id tinyint -- primary key part 1, foreign key to movie
person_id tinyint -- primary key part 2, foreign key to person
job_id tinyint -- primary key part 3, foreign key to job
TABLE: Location
location_id tinyint -- primary key
location varchar(200) -- 'New York City' or 'USA'
parent_location_id tinyint -- foreign key to location
=head2 Fetching data
In Alzabo, data is returned in the form of a L<row
object|Alzabo::Runtime::Row>. This object can be used to access the
data for an individual row.
Unless you are retrieving a row via a unique identifier (usually its
primary key), you will be given a L<cursor|Alzabo::Runtime::RowCursor>
object. This is quite similar to how C<DBI> uses statement handles
and is done for similar reasons.
First of all, let's do something simple. Let's assume I have a
person_id value and I want to find all the movies that they were in
and print the title, year of release, and the job they did in the
movie. Here's what it looks like:
my $schema = Alzabo::Runtime::Schema->load_from_file( name => 'movies' );
my $person_t = $schema->table('Person');
my $credit_t = $schema->table('Credit');
my $movie_t = $schema->table('Movie');
my $job_t = $schema->table('Job');
# returns a row representing this person.
my $person = $person_t->row_by_pk( pk => 42 );
# all the rows in the credit table that have the person_id of 42.
my $cursor =
$person->rows_by_foreign_key
( foreign_key =>
$person_t->foreign_keys_by_table($credit_t) );
print $person->select('name'), " was in the following films:\n\n";
while (my $credit = $cursor->next)
{
# rows_by_foreign_key returns a RowCursor object. We immediately
# call its next method, knowing it will only have one row (if
# it doesn't then our referential integrity is in trouble!)
my $movie =
$credit->rows_by_foreign_key
( foreign_key =>
$credit_t->foreign_keys_by_table($movie_t) )->next;
my $job =
$credit->rows_by_foreign_key
( foreign_key =>
$credit_t->foreign_keys_by_table($job_t) )->next;
print $movie->select('title'), " released in ", $movie->select('release_year'), "\n";
print ' ', $job->('job'), "\n";
}
A more sophisticated version of this code would take into account that
a person can do more than one job in the same movie.
The method names are quite verbose, so let's redo the example using
L<C<Alzabo::MethodMaker>|Alzabo::MethodMaker>:
# Assume that the method_namer() subroutine pluralizes things as one
# would expect.
use Alzabo::MethodMaker( schema => 'movies',
all => 1,
name_maker => \&method_namer );
my $schema = Alzabo::Runtime::Schema->load_from_file( name => 'movies' );
# instantiates a row representing this person.
my $person = $schema->Person->row_by_pk( pk => 42 );
# all the rows in the credit table that have the person_id of 42.
my $cursor = $person->Credits;
print $person->name, " was in the following films:\n\n";
( run in 2.339 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )