1DBIx::Class::Manual::ExUasmeprleC(o3n)tributed Perl DocuDmBeInxt:a:tCiloanss::Manual::Example(3)
2
3
4
6 DBIx::Class::Manual::Example - Simple CD database example
7
9 This tutorial will guide you through the process of setting up and
10 testing a very basic CD database using SQLite, with DBIx::Class::Schema
11 as the database frontend.
12
13 The database structure is based on the following rules:
14
15 An artist can have many cds, and each cd belongs to just one artist.
16 A cd can have many tracks, and each track belongs to just one cd.
17
18 The database is implemented with the following:
19
20 table 'artist' with columns: artistid, name
21 table 'cd' with columns: cdid, artistid, title, year
22 table 'track' with columns: trackid, cdid, title
23
24 Each of the table's first columns is the primary key; any subsequent
25 keys are foreign keys.
26
27 Installation
28 You'll need to install DBIx::Class via CPAN, and you'll also need to
29 install sqlite3 (not sqlite) if it's not already intalled.
30
31 The database/tables/data
32
33 Your distribution already comes with a pre-filled SQLite database
34 examples/Schema/db/example.db. You can see it by e.g.
35
36 cpanm --look DBIx::Class
37
38 If for some reason the file is unreadable on your system, you can
39 recreate it as follows:
40
41 cp -a <unpacked-DBIC-tarball>/examples/Schema dbicapp
42 cd dbicapp
43 rm db/example.db
44 sqlite3 db/example.db < db/example.sql
45 perl insertdb.pl
46
47 Testing the database
48
49 Enter the example Schema directory
50
51 cd <unpacked-DBIC-tarball>/examples/Schema
52
53 Run the script testdb.pl, which will test that the database has
54 successfully been filled.
55
56 When this script is run, it should output the following:
57
58 get_tracks_by_cd(Bad):
59 Leave Me Alone
60 Smooth Criminal
61 Dirty Diana
62
63 get_tracks_by_artist(Michael Jackson):
64 Billie Jean (from the CD 'Thriller')
65 Beat It (from the CD 'Thriller')
66 Leave Me Alone (from the CD 'Bad')
67 Smooth Criminal (from the CD 'Bad')
68 Dirty Diana (from the CD 'Bad')
69
70 get_cd_by_track(Stan):
71 The Marshall Mathers LP has the track 'Stan'.
72
73 get_cds_by_artist(Michael Jackson):
74 Thriller
75 Bad
76
77 get_artist_by_track(Dirty Diana):
78 Michael Jackson recorded the track 'Dirty Diana'.
79
80 get_artist_by_cd(The Marshall Mathers LP):
81 Eminem recorded the CD 'The Marshall Mathers LP'.
82
83 Discussion about the results
84
85 The data model defined in this example has an artist with multiple CDs,
86 and a CD with multiple tracks; thus, it's simple to traverse from a
87 track back to a CD, and from there back to an artist. This is
88 demonstrated in the get_tracks_by_artist routine, where we easily walk
89 from the individual track back to the title of the CD that the track
90 came from ($track->cd->title).
91
92 Note also that in the get_tracks_by_cd and get_tracks_by_artist
93 routines, the result set is called multiple times with the 'next'
94 iterator. In contrast, get_cd_by_track uses the 'first' result set
95 method, since only one CD is expected to have a specific track.
96
97 This example uses "load_namespaces" in DBIx::Class::Schema to load in
98 the appropriate Result classes from the "MyApp::Schema::Result"
99 namespace, and any required ResultSet classes from the
100 "MyApp::Schema::ResultSet" namespace (although we did not add, nor
101 needed any such classes in this example).
102
104 Check the list of additional DBIC resources.
105
107 This module is free software copyright by the DBIx::Class (DBIC)
108 authors. You can redistribute it and/or modify it under the same terms
109 as the DBIx::Class library.
110
111
112
113perl v5.36.0 2022-07-22 DBIx::Class::Manual::Example(3)