DBIO
view release on metacpan or search on metacpan
demo/dbio-demo view on Meta::CPAN
for my $cd_data (@$cds) {
my $tracks = delete $cd_data->{tracks};
my $cd = $artist->create_related('cds', $cd_data);
$cd->create_related('tracks', $_) for @$tracks;
}
printf " + %-16s %d CDs, %d tracks\n",
$artist->name,
$artist->cds->count,
$artist->cds->search_related('tracks')->count;
}
# -- Counts -----------------------------------------------------------------
step("Counts");
show("Artists:", $schema->resultset('Artist')->count);
show("CDs:", $schema->resultset('CD')->count);
show("Tracks:", $schema->resultset('Track')->count);
# -- Queries ----------------------------------------------------------------
step("Query: all tracks by Kraftwerk (join through CD)");
my $rs = $schema->resultset('Track')->search(
{ 'artist.name' => 'Kraftwerk' },
{ join => { cd => 'artist' }, order_by => ['cd.year', 'me.position'] },
);
while (my $t = $rs->next) {
printf " %d. %-24s (%s, %d)\n",
$t->position, $t->title, $t->cd->title, $t->cd->year;
}
step("Query: CDs from the 70s (ordered by year)");
$rs = $schema->resultset('CD')->search(
{ year => { '>=' => 1970, '<' => 1980 } },
{ order_by => 'year', prefetch => 'artist' },
);
while (my $cd = $rs->next) {
printf " %d %-26s by %s\n", $cd->year, $cd->title, $cd->artist->name;
}
step("Query: artists with more than 3 tracks");
$rs = $schema->resultset('Artist')->search(undef, {
join => { cds => 'tracks' },
group_by => ['me.id', 'me.name'],
having => \[ 'COUNT(tracks.id) > ?', 3 ],
});
while (my $a = $rs->next) {
printf " %-16s %d tracks\n", $a->name,
$a->cds->search_related('tracks')->count;
}
# -- Update -----------------------------------------------------------------
step("Update: rename 'Die Mensch-Maschine' to 'The Man-Machine'");
my $cd = $schema->resultset('CD')->find({ title => 'Die Mensch-Maschine' });
$cd->update({ title => 'The Man-Machine' });
show("New title:", $cd->title);
# -- Delete -----------------------------------------------------------------
step("Delete: remove Nina Simone and cascade");
my $nina = $schema->resultset('Artist')->find({ name => 'Nina Simone' });
# manual cascade: delete tracks, then CDs, then artist
for my $nina_cd ($nina->cds->all) {
$nina_cd->tracks->delete;
$nina_cd->delete;
}
$nina->delete;
show("Artists now:", $schema->resultset('Artist')->count);
show("CDs now:", $schema->resultset('CD')->count);
show("Tracks now:", $schema->resultset('Track')->count);
# -- Transaction ------------------------------------------------------------
step("Transaction: add + rollback");
eval {
$schema->txn_do(sub {
$schema->resultset('Artist')->create({ name => 'Ghost Artist' });
show("Inside txn:", $schema->resultset('Artist')->count . " artists");
die "intentional rollback\n";
});
};
show("After rollback:", $schema->resultset('Artist')->count . " artists");
# -- ResultSet chaining -----------------------------------------------------
step("ResultSet chaining demo");
my $bowie_70s_tracks = $schema->resultset('Track')
->search({ 'artist.name' => 'David Bowie' }, { join => { cd => 'artist' } })
->search({ 'cd.year' => { '<' => 1975 } })
->search(undef, { order_by => [{ -asc => 'cd.year' }, { -asc => 'me.position' }] });
say " David Bowie tracks from before 1975:";
while (my $t = $bowie_70s_tracks->next) {
printf " %s - %s (%d) #%d\n",
$t->cd->title, $t->title, $t->cd->year, $t->position;
}
# -- Done -------------------------------------------------------------------
banner("Done");
say " Everything works. DBIO is ready.\n";
( run in 1.337 second using v1.01-cache-2.11-cpan-7fcb06a456a )