X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FExample.pod;h=b56f85ea554bbd11ddc2794eb4d0bc844a8a8850;hb=880a1a0cc6a48a3165656fe5daf7ec288901c3e2;hp=9b7763f543cf512d29ff16c1cf27e3deb84c901b;hpb=a15b805965f79e5f819e387878179392dba4f8ca;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/Example.pod b/lib/DBIx/Class/Manual/Example.pod index 9b7763f..b56f85e 100644 --- a/lib/DBIx/Class/Manual/Example.pod +++ b/lib/DBIx/Class/Manual/Example.pod @@ -4,7 +4,7 @@ DBIx::Class::Manual::Example - Simple CD database example =head1 DESCRIPTION -This tutorial will guide you through the proeccess of setting up and +This tutorial will guide you through the process of setting up and testing a very basic CD database using SQLite, with DBIx::Class::Schema as the database frontend. @@ -46,12 +46,12 @@ Save the following into a example.sql CREATE TABLE cd ( cdid INTEGER PRIMARY KEY, - artist INTEGER NOT NULL REFERENCES artist(id) + artist INTEGER NOT NULL REFERENCES artist(artistid), title TEXT NOT NULL); CREATE TABLE track ( trackid INTEGER PRIMARY KEY, - cd INTEGER NOT NULL REFERENCES cd(id), + cd INTEGER NOT NULL REFERENCES cd(cdid), title TEXT NOT NULL) ; and create the sqlite database file: @@ -62,56 +62,56 @@ sqlite3 example.db < example.sql First, create some dirs and change working directory: - mkdir DB - mkdir DB/Main + mkdir MyDatabase + mkdir MyDatabase/Main Then, create the following DBIx::Class::Schema classes: -DB/Main.pm: +MyDatabase/Main.pm: - package DB::Main; + package MyDatabase::Main; use base qw/DBIx::Class::Schema/; - __PACKAGE__->load_classes(qw/Artist CD Track/); + __PACKAGE__->load_classes(qw/Artist Cd Track/); 1; -DB/Main/Artist.pm: +MyDatabase/Main/Artist.pm: - package DB::Main::Artist; + package MyDatabase::Main::Artist; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/Core/); __PACKAGE__->table('artist'); __PACKAGE__->add_columns(qw/ artistid name /); __PACKAGE__->set_primary_key('artistid'); - __PACKAGE__->has_many('cds' => 'DB::Main::CD'); + __PACKAGE__->has_many('cds' => 'MyDatabase::Main::Cd'); 1; -DB/Main/CD.pm: +MyDatabase/Main/Cd.pm: - package DB::Main::CD; + package MyDatabase::Main::Cd; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/Core/); __PACKAGE__->table('cd'); __PACKAGE__->add_columns(qw/ cdid artist title/); __PACKAGE__->set_primary_key('cdid'); - __PACKAGE__->belongs_to('artist' => 'DB::Main::Artist'); - __PACKAGE__->has_many('tracks' => 'DB::Main::Track'); + __PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Artist'); + __PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Track'); 1; -DB/Main/Track.pm: +MyDatabase/Main/Track.pm: - package DB::Main::Track; + package MyDatabase::Main::Track; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/Core/); __PACKAGE__->table('track'); __PACKAGE__->add_columns(qw/ trackid cd title/); __PACKAGE__->set_primary_key('trackid'); - __PACKAGE__->belongs_to('cd' => 'DB::Main::CD'); + __PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Cd'); 1; @@ -122,10 +122,10 @@ insertdb.pl #!/usr/bin/perl -w - use DB::Main; + use MyDatabase::Main; use strict; - my $schema = DB::Main->connect('dbi:SQLite:dbic_test.db'); + my $schema = MyDatabase::Main->connect('dbi:SQLite:example.db'); # here's some of the sql that is going to be generated by the schema # INSERT INTO artist VALUES (NULL,'Michael Jackson'); @@ -151,7 +151,7 @@ insertdb.pl push @cds, [$lp, $artist->first]; } - $schema->populate('CD', [ + $schema->populate('Cd', [ [qw/title artist/], @cds, ]); @@ -169,7 +169,7 @@ insertdb.pl my @tracks; foreach my $track (keys %tracks) { - my $cdname = $schema->resultset('CD')->search({ + my $cdname = $schema->resultset('Cd')->search({ title => $tracks{$track}, }); push @tracks, [$cdname->first, $track]; @@ -186,10 +186,10 @@ testdb.pl: #!/usr/bin/perl -w - use DB::Main; + use MyDatabase::Main; use strict; - my $schema = DB::Main->connect('dbi:mysql:cdtestdb', 'testuser', 'testpass'); + my $schema = MyDatabase::Main->connect('dbi:SQLite:example.db'); get_tracks_by_cd('Bad'); get_tracks_by_artist('Michael Jackson'); @@ -243,7 +243,7 @@ testdb.pl: sub get_cd_by_track { my $tracktitle = shift; print "get_cd_by_track($tracktitle):\n"; - my $rs = $schema->resultset('CD')->search( + my $rs = $schema->resultset('Cd')->search( { 'tracks.title' => $tracktitle }, @@ -258,7 +258,7 @@ testdb.pl: sub get_cds_by_artist { my $artistname = shift; print "get_cds_by_artist($artistname):\n"; - my $rs = $schema->resultset('CD')->search( + my $rs = $schema->resultset('Cd')->search( { 'artist.name' => $artistname }, @@ -339,8 +339,8 @@ It should output: =head1 Notes With these scripts we're relying on @INC looking in the current -working directory. You may want to add the DB namespaces to @INC in a -different way when it comes to deployemnt. +working directory. You may want to add the MyDatabase namespaces to +@INC in a different way when it comes to deployment. The testdb.pl script is an excellent start for testing your database model. @@ -349,7 +349,7 @@ model. =head1 AUTHOR - sc_ from IRC. Please credit yourself properly! + sc_ from irc.perl.org#dbix-class Kieren Diment =cut