.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "DBIx::Class::Manual::Example 3pm" .TH DBIx::Class::Manual::Example 3pm "2018-04-19" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" DBIx::Class::Manual::Example \- Simple CD database example .SH "DESCRIPTION" .IX Header "DESCRIPTION" This tutorial will guide you through the process of setting up and testing a very basic \s-1CD\s0 database using SQLite, with DBIx::Class::Schema as the database frontend. .PP The database structure is based on the following rules: .PP .Vb 2 \& An artist can have many cds, and each cd belongs to just one artist. \& A cd can have many tracks, and each track belongs to just one cd. .Ve .PP The database is implemented with the following: .PP .Vb 3 \& table \*(Aqartist\*(Aq with columns: artistid, name \& table \*(Aqcd\*(Aq with columns: cdid, artistid, title, year \& table \*(Aqtrack\*(Aq with columns: trackid, cdid, title .Ve .PP Each of the table's first columns is the primary key; any subsequent keys are foreign keys. .SS "Installation" .IX Subsection "Installation" You'll need to install DBIx::Class via \s-1CPAN,\s0 and you'll also need to install sqlite3 (not sqlite) if it's not already intalled. .PP \fIThe database/tables/data\fR .IX Subsection "The database/tables/data" .PP Your distribution already comes with a pre-filled SQLite database \&\fIexamples/Schema/db/example.db\fR. You can see it by e.g. .PP .Vb 1 \& cpanm \-\-look DBIx::Class .Ve .PP If for some reason the file is unreadable on your system, you can recreate it as follows: .PP .Vb 5 \& cp \-a /examples/Schema dbicapp \& cd dbicapp \& rm db/example.db \& sqlite3 db/example.db < db/example.sql \& perl insertdb.pl .Ve .PP \fITesting the database\fR .IX Subsection "Testing the database" .PP Enter the example Schema directory .PP .Vb 1 \& cd /examples/Schema .Ve .PP Run the script testdb.pl, which will test that the database has successfully been filled. .PP When this script is run, it should output the following: .PP .Vb 4 \& get_tracks_by_cd(Bad): \& Leave Me Alone \& Smooth Criminal \& Dirty Diana \& \& get_tracks_by_artist(Michael Jackson): \& Billie Jean (from the CD \*(AqThriller\*(Aq) \& Beat It (from the CD \*(AqThriller\*(Aq) \& Leave Me Alone (from the CD \*(AqBad\*(Aq) \& Smooth Criminal (from the CD \*(AqBad\*(Aq) \& Dirty Diana (from the CD \*(AqBad\*(Aq) \& \& get_cd_by_track(Stan): \& The Marshall Mathers LP has the track \*(AqStan\*(Aq. \& \& get_cds_by_artist(Michael Jackson): \& Thriller \& Bad \& \& get_artist_by_track(Dirty Diana): \& Michael Jackson recorded the track \*(AqDirty Diana\*(Aq. \& \& get_artist_by_cd(The Marshall Mathers LP): \& Eminem recorded the CD \*(AqThe Marshall Mathers LP\*(Aq. .Ve .PP \fIDiscussion about the results\fR .IX Subsection "Discussion about the results" .PP The data model defined in this example has an artist with multiple CDs, and a \s-1CD\s0 with multiple tracks; thus, it's simple to traverse from a track back to a \s-1CD,\s0 and from there back to an artist. This is demonstrated in the get_tracks_by_artist routine, where we easily walk from the individual track back to the title of the \s-1CD\s0 that the track came from ($track\->cd\->title). .PP Note also that in the get_tracks_by_cd and get_tracks_by_artist routines, the result set is called multiple times with the 'next' iterator. In contrast, get_cd_by_track uses the 'first' result set method, since only one \s-1CD\s0 is expected to have a specific track. .PP This example uses \*(L"load_namespaces\*(R" in DBIx::Class::Schema to load in the appropriate Result classes from the \&\f(CW\*(C`MyApp::Schema::Result\*(C'\fR namespace, and any required ResultSet classes from the \&\f(CW\*(C`MyApp::Schema::ResultSet\*(C'\fR namespace (although we did not add, nor needed any such classes in this example). .SH "FURTHER QUESTIONS?" .IX Header "FURTHER QUESTIONS?" Check the list of additional \s-1DBIC\s0 resources. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This module is free software copyright by the DBIx::Class (\s-1DBIC\s0) authors. You can redistribute it and/or modify it under the same terms as the DBIx::Class library.