From: Kieren Diment Date: Thu, 2 Mar 2006 02:10:03 +0000 (+0000) Subject: Altered example to populate the db with DBIC X-Git-Tag: v0.06000~74 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a15b805965f79e5f819e387878179392dba4f8ca;p=dbsrgits%2FDBIx-Class.git Altered example to populate the db with DBIC --- diff --git a/lib/DBIx/Class/Manual/Example.pod b/lib/DBIx/Class/Manual/Example.pod index 365896e..9b7763f 100644 --- a/lib/DBIx/Class/Manual/Example.pod +++ b/lib/DBIx/Class/Manual/Example.pod @@ -4,7 +4,9 @@ DBIx::Class::Manual::Example - Simple CD database example =head1 DESCRIPTION -This tutorial will guide you through the proeccess of setting up and testing a very basic CD database using Mysql, with DBIx::Class::Schema as the database frontend. +This tutorial will guide you through the proeccess of setting up and +testing a very basic CD database using SQLite, with DBIx::Class::Schema +as the database frontend. The database consists of the following: @@ -23,62 +25,46 @@ And these rules exists: =head2 Installation -=head3 Create the database/tables and populate them with a few records +Install DBIx::Class via CPAN should be sufficient. - CREATE DATABASE cdtestdb ; - USE cdtestdb; +=head3 Create the database/tables. - CREATE TABLE artist ( - artistid INT NOT NULL AUTO_INCREMENT , - name CHAR( 40 ) NOT NULL , - PRIMARY KEY ( artistid ) - ); +First make and change the directory: - CREATE TABLE cd ( - cdid INT NOT NULL AUTO_INCREMENT , - artist INT NOT NULL , - title CHAR( 40 ) NOT NULL , - PRIMARY KEY ( cdid ) - ); + mkdir app + cd app - CREATE TABLE track ( - trackid INT NOT NULL AUTO_INCREMENT , - cd INT NOT NULL , - title CHAR( 40 ) NOT NULL , - PRIMARY KEY ( trackid ) - ; +This example uses SQLite which is a dependency of DBIx::Class, so you +shouldn't have to install extra software. +Save the following into a example.sql - INSERT INTO artist VALUES - (NULL,'Michael Jackson'), - (NULL,'Eminem'); - - INSERT INTO cd VALUES - (NULL,'1','Thriller'), - (NULL,'1','Bad'), - (NULL,'2','The Marshall Mathers LP'); - - INSERT INTO track VALUES - (NULL,'1','Beat it'), - (NULL,'1','Billie Jean'), - (NULL,'2','Dirty Diana'), - (NULL,'2','Smooth Criminal'), - (NULL,'2','Leave Me Alone'), - (NULL,'3','Stan'), - (NULL,'3','The Way I Am'); + CREATE TABLE artist ( + artistid INTEGER PRIMARY KEY, + name TEXT NOT NULL + ); + + CREATE TABLE cd ( + cdid INTEGER PRIMARY KEY, + artist INTEGER NOT NULL REFERENCES artist(id) + title TEXT NOT NULL); + CREATE TABLE track ( + trackid INTEGER PRIMARY KEY, + cd INTEGER NOT NULL REFERENCES cd(id), + title TEXT NOT NULL) ; +and create the sqlite database file: + +sqlite3 example.db < example.sql =head3 Set up DBIx::Class::Schema First, create some dirs and change working directory: - mkdir app - mkdir app/DB - mkdir app/DB/Main - cd app + mkdir DB + mkdir DB/Main - Then, create the following DBIx::Class::Schema classes: DB/Main.pm: @@ -113,10 +99,10 @@ DB/Main/CD.pm: __PACKAGE__->set_primary_key('cdid'); __PACKAGE__->belongs_to('artist' => 'DB::Main::Artist'); __PACKAGE__->has_many('tracks' => 'DB::Main::Track'); - + 1; - + DB/Main/Track.pm: package DB::Main::Track; @@ -126,11 +112,75 @@ DB/Main/Track.pm: __PACKAGE__->add_columns(qw/ trackid cd title/); __PACKAGE__->set_primary_key('trackid'); __PACKAGE__->belongs_to('cd' => 'DB::Main::CD'); - + 1; -=head3 Create and run the test script +=head3 Write a script to insert some records. + +insertdb.pl + + #!/usr/bin/perl -w + + use DB::Main; + use strict; + + my $schema = DB::Main->connect('dbi:SQLite:dbic_test.db'); + + # here's some of the sql that is going to be generated by the schema + # INSERT INTO artist VALUES (NULL,'Michael Jackson'); + # INSERT INTO artist VALUES (NULL,'Eminem'); + + my @artists = (['Michael Jackson'], ['Eminem']); + $schema->populate('Artist', [ + [qw/name/], + @artists, + ]); + + my %albums = ( + 'Thriller' => 'Michael Jackson', + 'Bad' => 'Michael Jackson', + 'The Marshall Mathers LP' => 'Eminem', + ); + + my @cds; + foreach my $lp (keys %albums) { + my $artist = $schema->resultset('Artist')->search({ + name => $albums{$lp} + }); + push @cds, [$lp, $artist->first]; + } + + $schema->populate('CD', [ + [qw/title artist/], + @cds, + ]); + + + my %tracks = ( + 'Beat It' => 'Thriller', + 'Billie Jean' => 'Thriller', + 'Dirty Diana' => 'Bad', + 'Smooth Criminal' => 'Bad', + 'Leave Me Alone' => 'Bad', + 'Stan' => 'The Marshall Mathers LP', + 'The Way I Am' => 'The Marshall Mathers LP', + ); + + my @tracks; + foreach my $track (keys %tracks) { + my $cdname = $schema->resultset('CD')->search({ + title => $tracks{$track}, + }); + push @tracks, [$cdname->first, $track]; + } + + $schema->populate('Track',[ + [qw/cd title/], + @tracks, + ]); + +=head3 Create and run the scripts testdb.pl: @@ -286,8 +336,20 @@ It should output: get_artist_by_cd(The Marshall Mathers LP): Eminem +=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. + +The testdb.pl script is an excellent start for testing your database +model. + +=head1 TODO + =head1 AUTHOR - sc_ + sc_ from IRC. Please credit yourself properly! + Kieren Diment =cut