Cleanup shebang lines of all maint/example scripts, remove from tests entirely
[dbsrgits/DBIx-Class.git] / examples / Schema / insertdb.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use MyDatabase::Main;
7
8 my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
9
10 #  here's some of the sql that is going to be generated by the schema
11 #  INSERT INTO artist VALUES (NULL,'Michael Jackson');
12 #  INSERT INTO artist VALUES (NULL,'Eminem');
13
14 my @artists = (['Michael Jackson'], ['Eminem']);
15 $schema->populate('Artist', [
16     [qw/name/],
17     @artists,
18 ]);
19
20 my %albums = (
21     'Thriller' => 'Michael Jackson',
22     'Bad' => 'Michael Jackson',
23     'The Marshall Mathers LP' => 'Eminem',
24 );
25
26 my @cds;
27 foreach my $lp (keys %albums) {
28     my $artist = $schema->resultset('Artist')->find({
29         name => $albums{$lp}
30     });
31     push @cds, [$lp, $artist->id];
32 }
33
34 $schema->populate('Cd', [
35     [qw/title artist/],
36     @cds,
37 ]);
38
39
40 my %tracks = (
41     'Beat It'         => 'Thriller',
42     'Billie Jean'     => 'Thriller',
43     'Dirty Diana'     => 'Bad',
44     'Smooth Criminal' => 'Bad',
45     'Leave Me Alone'  => 'Bad',
46     'Stan'            => 'The Marshall Mathers LP',
47     'The Way I Am'    => 'The Marshall Mathers LP',
48 );
49
50 my @tracks;
51 foreach my $track (keys %tracks) {
52     my $cd = $schema->resultset('Cd')->find({
53         title => $tracks{$track},
54     });
55     push @tracks, [$cd->id, $track];
56 }
57
58 $schema->populate('Track',[
59     [qw/cd title/],
60     @tracks,
61 ]);