5 plan skip_all => 'needs DBD::SQLite for testing'
6 unless eval { require DBD::SQLite };
8 plan skip_all => 'needs Class::DBI::Plugin::DeepAbstractSearch'
9 unless eval { require Class::DBI::Plugin::DeepAbstractSearch };
17 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 });
20 use base qw(DBIx::Class::CDBICompat);
21 use Class::DBI::Plugin::DeepAbstractSearch;
22 __PACKAGE__->connection(@DSN);
24 my $sql = <<'SQL_END';
26 ---------------------------------------
28 ---------------------------------------
29 CREATE TABLE artists (
30 id INTEGER NOT NULL PRIMARY KEY,
34 INSERT INTO artists VALUES (1, "Willie Nelson");
35 INSERT INTO artists VALUES (2, "Patsy Cline");
37 ---------------------------------------
39 ---------------------------------------
41 id INTEGER NOT NULL PRIMARY KEY,
45 INSERT INTO labels VALUES (1, "Columbia");
46 INSERT INTO labels VALUES (2, "Sony");
47 INSERT INTO labels VALUES (3, "Supraphon");
49 ---------------------------------------
51 ---------------------------------------
53 id INTEGER NOT NULL PRIMARY KEY,
59 INSERT INTO cds VALUES (1, 1, 1, "Songs", 2005);
60 INSERT INTO cds VALUES (2, 2, 1, "Read Headed Stanger", 2000);
61 INSERT INTO cds VALUES (3, 1, 1, "Wanted! The Outlaws", 2004);
62 INSERT INTO cds VALUES (4, 2, 1, "The Very Best of Willie Nelson", 1999);
64 INSERT INTO cds VALUES (5, 1, 2, "12 Greates Hits", 1999);
65 INSERT INTO cds VALUES (6, 2, 2, "Sweet Dreams", 1995);
66 INSERT INTO cds VALUES (7, 3, 2, "The Best of Patsy Cline", 1991);
68 ---------------------------------------
70 ---------------------------------------
72 id INTEGER NOT NULL PRIMARY KEY,
77 INSERT INTO tracks VALUES (1, 1, 1, "Songs: Track 1");
78 INSERT INTO tracks VALUES (2, 1, 2, "Songs: Track 2");
79 INSERT INTO tracks VALUES (3, 1, 3, "Songs: Track 3");
80 INSERT INTO tracks VALUES (4, 1, 4, "Songs: Track 4");
82 INSERT INTO tracks VALUES (5, 2, 1, "Read Headed Stanger: Track 1");
83 INSERT INTO tracks VALUES (6, 2, 2, "Read Headed Stanger: Track 2");
84 INSERT INTO tracks VALUES (7, 2, 3, "Read Headed Stanger: Track 3");
85 INSERT INTO tracks VALUES (8, 2, 4, "Read Headed Stanger: Track 4");
87 INSERT INTO tracks VALUES (9, 3, 1, "Wanted! The Outlaws: Track 1");
88 INSERT INTO tracks VALUES (10, 3, 2, "Wanted! The Outlaws: Track 2");
90 INSERT INTO tracks VALUES (11, 4, 1, "The Very Best of Willie Nelson: Track 1");
91 INSERT INTO tracks VALUES (12, 4, 2, "The Very Best of Willie Nelson: Track 2");
92 INSERT INTO tracks VALUES (13, 4, 3, "The Very Best of Willie Nelson: Track 3");
93 INSERT INTO tracks VALUES (14, 4, 4, "The Very Best of Willie Nelson: Track 4");
94 INSERT INTO tracks VALUES (15, 4, 5, "The Very Best of Willie Nelson: Track 5");
95 INSERT INTO tracks VALUES (16, 4, 6, "The Very Best of Willie Nelson: Track 6");
97 INSERT INTO tracks VALUES (17, 5, 1, "12 Greates Hits: Track 1");
98 INSERT INTO tracks VALUES (18, 5, 2, "12 Greates Hits: Track 2");
99 INSERT INTO tracks VALUES (19, 5, 3, "12 Greates Hits: Track 3");
100 INSERT INTO tracks VALUES (20, 5, 4, "12 Greates Hits: Track 4");
102 INSERT INTO tracks VALUES (21, 6, 1, "Sweet Dreams: Track 1");
103 INSERT INTO tracks VALUES (22, 6, 2, "Sweet Dreams: Track 2");
104 INSERT INTO tracks VALUES (23, 6, 3, "Sweet Dreams: Track 3");
105 INSERT INTO tracks VALUES (24, 6, 4, "Sweet Dreams: Track 4");
107 INSERT INTO tracks VALUES (25, 7, 1, "The Best of Patsy Cline: Track 1");
108 INSERT INTO tracks VALUES (26, 7, 2, "The Best of Patsy Cline: Track 2");
112 foreach my $statement (split /;/, $sql) {
113 $statement =~ s/^\s*//gs;
114 $statement =~ s/\s*$//gs;
115 next unless $statement;
116 Music::DBI->db_Main->do($statement) or die "$@ $!";
119 Music::DBI->dbi_commit;
121 package Music::Artist;
122 use base 'Music::DBI';
123 Music::Artist->table('artists');
124 Music::Artist->columns(All => qw/id name/);
127 package Music::Label;
128 use base 'Music::DBI';
129 Music::Label->table('labels');
130 Music::Label->columns(All => qw/id name/);
133 use base 'Music::DBI';
134 Music::CD->table('cds');
135 Music::CD->columns(All => qw/id label artist title year/);
138 package Music::Track;
139 use base 'Music::DBI';
140 Music::Track->table('tracks');
141 Music::Track->columns(All => qw/id cd position title/);
143 Music::Artist->has_many(cds => 'Music::CD');
144 Music::Label->has_many(cds => 'Music::CD');
145 Music::CD->has_many(tracks => 'Music::Track');
146 Music::CD->has_a(artist => 'Music::Artist');
147 Music::CD->has_a(label => 'Music::Label');
148 Music::Track->has_a(cd => 'Music::CD');
155 my @artists = Music::Artist->deep_search_where($where, $attr);
156 is_deeply [ sort @artists ], [ 1, 2 ], "all without order";
161 my $attr = { order_by => 'name' };
162 my @artists = Music::Artist->deep_search_where($where, $attr);
163 is_deeply \@artists, [ 2, 1 ], "all with ORDER BY name";
168 my $attr = { order_by => 'name DESC' };
169 my @artists = Music::Artist->deep_search_where($where, $attr);
170 is_deeply \@artists, [ 1, 2 ], "all with ORDER BY name DESC";
174 my $where = { name => { -like => 'Patsy Cline' }, };
176 my @artists = Music::Artist->deep_search_where($where, $attr);
177 is_deeply \@artists, [ 2 ], "simple search";
181 my $where = { 'artist.name' => 'Patsy Cline' };
183 my @cds = Music::CD->deep_search_where($where, $attr);
184 is_deeply [ sort @cds ], [ 5, 6, 7 ], "Patsy's CDs";
188 my $where = { 'artist.name' => 'Patsy Cline' };
189 my $attr = { order_by => "title" } ;
190 my @cds = Music::CD->deep_search_where($where, $attr);
191 is_deeply [ @cds ], [ 5, 6, 7 ], "Patsy's CDs by title";
193 my $count = Music::CD->count_deep_search_where($where);
194 is_deeply $count, 3, "count Patsy's CDs by title";
198 my $where = { 'cd.title' => { -like => 'S%' }, };
199 my $attr = { order_by => "cd.title, title" } ;
200 my @cds = Music::Track->deep_search_where($where, $attr);
201 is_deeply [ @cds ], [1, 2, 3, 4, 21, 22, 23, 24 ], "Tracks from CDs whose name starts with 'S'";
206 'cd.artist.name' => { -like => 'W%' },
207 'cd.year' => { '>' => 2000 },
208 'position' => { '<' => 3 }
210 my $attr = { order_by => "cd.title DESC, title" } ;
211 my @cds = Music::Track->deep_search_where($where, $attr);
212 is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000 ";
214 my $count = Music::Track->count_deep_search_where($where);
215 is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000";
220 'cd.artist.name' => { -like => 'W%' },
221 'cd.year' => { '>' => 2000 },
222 'position' => { '<' => 3 }
224 my $attr = { order_by => [ 'cd.title DESC' , 'title' ] } ;
225 my @cds = Music::Track->deep_search_where($where, $attr);
226 is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000, array ref order ";
228 my $count = Music::Track->count_deep_search_where($where);
229 is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000, array ref order";
233 my $where = { 'cd.title' => [ -and => { -like => '%o%' }, { -like => '%W%' } ] };
234 my $attr = { order_by => [ 'cd.id' ] } ;
236 my @tracks = Music::Track->deep_search_where($where, $attr);
237 is_deeply [ @tracks ], [ 3, 3, 4, 4, 4, 4, 4, 4 ], "Tracks from CD titles containing 'o' AND 'W'";
241 my $where = { 'cd.year' => [ 1995, 1999 ] };
242 my $attr = { order_by => [ 'cd.id' ] } ;
244 my @tracks = Music::Track->deep_search_where($where, $attr);
245 is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ],
246 "Tracks from CDs from 1995, 1999";
250 my $where = { 'cd.year' => { -in => [ 1995, 1999 ] } };
251 my $attr = { order_by => [ 'cd.id' ] } ;
253 my @tracks = Music::Track->deep_search_where($where, $attr);
254 is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ],
255 "Tracks from CDs in 1995, 1999";
259 my $where = { -and => [ 'cd.year' => [ 1995, 1999 ], position => { '<=', 2 } ] };
260 my $attr = { order_by => [ 'cd.id' ] } ;
262 my @tracks = Music::Track->deep_search_where($where, $attr);
263 is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ],
264 "First 2 tracks Tracks from CDs from 1995, 1999";
268 my $where = { -and => [ 'cd.year' => { -in => [ 1995, 1999 ] }, position => { '<=', 2 } ] };
269 my $attr = { order_by => [ 'cd.id' ] } ;
271 my @tracks = Music::Track->deep_search_where($where, $attr);
272 is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ],
273 "First 2 tracks Tracks from CDs in 1995, 1999";
277 my $where = { 'label.name' => { -in => [ 'Sony', 'Supraphon', 'Bogus' ] } };
278 my $attr = { order_by => [ 'id' ] } ;
280 my @cds = Music::CD->deep_search_where($where, $attr);
281 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
282 "CDs from Sony or Supraphon";
286 my $where = { 'label.name' => [ 'Sony', 'Supraphon', 'Bogus' ] };
287 my $attr = { order_by => [ 'id' ] } ;
289 my @cds = Music::CD->deep_search_where($where, $attr);
290 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
291 "CDs from Sony or Supraphon";
294 END { unlink $DB if -e $DB }