more work on extended set of Pg auto-pk tests
[dbsrgits/DBIx-Class.git] / examples / Schema / testdb.pl
1 #!/usr/bin/perl -w
2
3 use MyDatabase::Main;
4 use strict;
5
6 my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
7 # for other DSNs, e.g. MySql, see the perldoc for the relevant dbd
8 # driver, e.g perldoc L<DBD::mysql>.
9
10 get_tracks_by_cd('Bad');
11 get_tracks_by_artist('Michael Jackson');
12
13 get_cd_by_track('Stan');
14 get_cds_by_artist('Michael Jackson');
15
16 get_artist_by_track('Dirty Diana');
17 get_artist_by_cd('The Marshall Mathers LP');
18
19
20 sub get_tracks_by_cd {
21     my $cdtitle = shift;
22     print "get_tracks_by_cd($cdtitle):\n";
23     my $rs = $schema->resultset('Track')->search(
24         {
25             'cd.title' => $cdtitle
26         },
27         {
28             join     => [qw/ cd /],
29         }
30     );
31     while (my $track = $rs->next) {
32         print $track->title . "\n";
33     }
34     print "\n";
35 }
36
37 sub get_tracks_by_artist {
38     my $artistname = shift;
39     print "get_tracks_by_artist($artistname):\n";
40     my $rs = $schema->resultset('Track')->search(
41         {
42             'artist.name' => $artistname
43         },
44         {
45             join => {
46                 'cd' => 'artist'
47             },
48         }
49     );
50     while (my $track = $rs->next) {
51         print $track->title . "\n";
52     }
53     print "\n";
54 }
55
56
57 sub get_cd_by_track {
58     my $tracktitle = shift;
59     print "get_cd_by_track($tracktitle):\n";
60     my $rs = $schema->resultset('Cd')->search(
61         {
62             'tracks.title' => $tracktitle
63         },
64         {
65             join     => [qw/ tracks /],
66         }
67     );
68     my $cd = $rs->first;
69     print $cd->title . "\n\n";
70 }
71
72 sub get_cds_by_artist {
73     my $artistname = shift;
74     print "get_cds_by_artist($artistname):\n";
75     my $rs = $schema->resultset('Cd')->search(
76         {
77             'artist.name' => $artistname
78         },
79         {
80             join     => [qw/ artist /],
81         }
82     );
83     while (my $cd = $rs->next) {
84         print $cd->title . "\n";
85     }
86     print "\n";
87 }
88
89
90
91 sub get_artist_by_track {
92     my $tracktitle = shift;
93     print "get_artist_by_track($tracktitle):\n";
94     my $rs = $schema->resultset('Artist')->search(
95         {
96             'tracks.title' => $tracktitle
97         },
98         {
99             join => {
100                 'cds' => 'tracks'
101             }
102         }
103     );
104     my $artist = $rs->first;
105     print $artist->name . "\n\n";
106 }
107
108          
109 sub get_artist_by_cd {
110     my $cdtitle = shift;
111     print "get_artist_by_cd($cdtitle):\n";
112     my $rs = $schema->resultset('Artist')->search(
113         {
114             'cds.title' => $cdtitle
115         },
116         {
117             join     => [qw/ cds /],
118         }
119     );
120     my $artist = $rs->first;
121     print $artist->name . "\n\n";
122 }