5 eval "use DBD::SQLite";
6 plan $@ ? (skip_all => 'needs DBD::SQLite for testing')
13 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 });
16 use base qw(DBIx::Class::CDBICompat);
17 use Class::DBI::Plugin::DeepAbstractSearch;
18 __PACKAGE__->connection(@DSN);
20 my $sql = <<'SQL_END';
22 ---------------------------------------
24 ---------------------------------------
25 CREATE TABLE artists (
26 id INTEGER NOT NULL PRIMARY KEY,
30 INSERT INTO artists VALUES (1, "Willie Nelson");
31 INSERT INTO artists VALUES (2, "Patsy Cline");
33 ---------------------------------------
35 ---------------------------------------
37 id INTEGER NOT NULL PRIMARY KEY,
41 INSERT INTO labels VALUES (1, "Columbia");
42 INSERT INTO labels VALUES (2, "Sony");
43 INSERT INTO labels VALUES (3, "Supraphon");
45 ---------------------------------------
47 ---------------------------------------
49 id INTEGER NOT NULL PRIMARY KEY,
55 INSERT INTO cds VALUES (1, 1, 1, "Songs", 2005);
56 INSERT INTO cds VALUES (2, 2, 1, "Read Headed Stanger", 2000);
57 INSERT INTO cds VALUES (3, 1, 1, "Wanted! The Outlaws", 2004);
58 INSERT INTO cds VALUES (4, 2, 1, "The Very Best of Willie Nelson", 1999);
60 INSERT INTO cds VALUES (5, 1, 2, "12 Greates Hits", 1999);
61 INSERT INTO cds VALUES (6, 2, 2, "Sweet Dreams", 1995);
62 INSERT INTO cds VALUES (7, 3, 2, "The Best of Patsy Cline", 1991);
64 ---------------------------------------
66 ---------------------------------------
68 id INTEGER NOT NULL PRIMARY KEY,
73 INSERT INTO tracks VALUES (1, 1, 1, "Songs: Track 1");
74 INSERT INTO tracks VALUES (2, 1, 2, "Songs: Track 2");
75 INSERT INTO tracks VALUES (3, 1, 3, "Songs: Track 3");
76 INSERT INTO tracks VALUES (4, 1, 4, "Songs: Track 4");
78 INSERT INTO tracks VALUES (5, 2, 1, "Read Headed Stanger: Track 1");
79 INSERT INTO tracks VALUES (6, 2, 2, "Read Headed Stanger: Track 2");
80 INSERT INTO tracks VALUES (7, 2, 3, "Read Headed Stanger: Track 3");
81 INSERT INTO tracks VALUES (8, 2, 4, "Read Headed Stanger: Track 4");
83 INSERT INTO tracks VALUES (9, 3, 1, "Wanted! The Outlaws: Track 1");
84 INSERT INTO tracks VALUES (10, 3, 2, "Wanted! The Outlaws: Track 2");
86 INSERT INTO tracks VALUES (11, 4, 1, "The Very Best of Willie Nelson: Track 1");
87 INSERT INTO tracks VALUES (12, 4, 2, "The Very Best of Willie Nelson: Track 2");
88 INSERT INTO tracks VALUES (13, 4, 3, "The Very Best of Willie Nelson: Track 3");
89 INSERT INTO tracks VALUES (14, 4, 4, "The Very Best of Willie Nelson: Track 4");
90 INSERT INTO tracks VALUES (15, 4, 5, "The Very Best of Willie Nelson: Track 5");
91 INSERT INTO tracks VALUES (16, 4, 6, "The Very Best of Willie Nelson: Track 6");
93 INSERT INTO tracks VALUES (17, 5, 1, "12 Greates Hits: Track 1");
94 INSERT INTO tracks VALUES (18, 5, 2, "12 Greates Hits: Track 2");
95 INSERT INTO tracks VALUES (19, 5, 3, "12 Greates Hits: Track 3");
96 INSERT INTO tracks VALUES (20, 5, 4, "12 Greates Hits: Track 4");
98 INSERT INTO tracks VALUES (21, 6, 1, "Sweet Dreams: Track 1");
99 INSERT INTO tracks VALUES (22, 6, 2, "Sweet Dreams: Track 2");
100 INSERT INTO tracks VALUES (23, 6, 3, "Sweet Dreams: Track 3");
101 INSERT INTO tracks VALUES (24, 6, 4, "Sweet Dreams: Track 4");
103 INSERT INTO tracks VALUES (25, 7, 1, "The Best of Patsy Cline: Track 1");
104 INSERT INTO tracks VALUES (26, 7, 2, "The Best of Patsy Cline: Track 2");
108 foreach my $statement (split /;/, $sql) {
109 $statement =~ s/^\s*//gs;
110 $statement =~ s/\s*$//gs;
111 next unless $statement;
112 Music::DBI->db_Main->do($statement) or die "$@ $!";
115 Music::DBI->dbi_commit;
117 package Music::Artist;
118 use base 'Music::DBI';
119 Music::Artist->table('artists');
120 Music::Artist->columns(All => qw/id name/);
123 package Music::Label;
124 use base 'Music::DBI';
125 Music::Label->table('labels');
126 Music::Label->columns(All => qw/id name/);
129 use base 'Music::DBI';
130 Music::CD->table('cds');
131 Music::CD->columns(All => qw/id label artist title year/);
134 package Music::Track;
135 use base 'Music::DBI';
136 Music::Track->table('tracks');
137 Music::Track->columns(All => qw/id cd position title/);
139 Music::Artist->has_many(cds => 'Music::CD');
140 Music::Label->has_many(cds => 'Music::CD');
141 Music::CD->has_many(tracks => 'Music::Track');
142 Music::CD->has_a(artist => 'Music::Artist');
143 Music::CD->has_a(label => 'Music::Label');
144 Music::Track->has_a(cd => 'Music::CD');
151 my @artists = Music::Artist->deep_search_where($where, $attr);
152 is_deeply [ sort @artists ], [ 1, 2 ], "all without order";
157 my $attr = { order_by => 'name' };
158 my @artists = Music::Artist->deep_search_where($where, $attr);
159 is_deeply \@artists, [ 2, 1 ], "all with ORDER BY name";
164 my $attr = { order_by => 'name DESC' };
165 my @artists = Music::Artist->deep_search_where($where, $attr);
166 is_deeply \@artists, [ 1, 2 ], "all with ORDER BY name DESC";
170 my $where = { name => { -like => 'Patsy Cline' }, };
172 my @artists = Music::Artist->deep_search_where($where, $attr);
173 is_deeply \@artists, [ 2 ], "simple search";
177 my $where = { 'artist.name' => 'Patsy Cline' };
179 my @cds = Music::CD->deep_search_where($where, $attr);
180 is_deeply [ sort @cds ], [ 5, 6, 7 ], "Patsy's CDs";
184 my $where = { 'artist.name' => 'Patsy Cline' };
185 my $attr = { order_by => "title" } ;
186 my @cds = Music::CD->deep_search_where($where, $attr);
187 is_deeply [ @cds ], [ 5, 6, 7 ], "Patsy's CDs by title";
189 my $count = Music::CD->count_deep_search_where($where);
190 is_deeply $count, 3, "count Patsy's CDs by title";
194 my $where = { 'cd.title' => { -like => 'S%' }, };
195 my $attr = { order_by => "cd.title, title" } ;
196 my @cds = Music::Track->deep_search_where($where, $attr);
197 is_deeply [ @cds ], [1, 2, 3, 4, 21, 22, 23, 24 ], "Tracks from CDs whose name starts with 'S'";
202 'cd.artist.name' => { -like => 'W%' },
203 'cd.year' => { '>' => 2000 },
204 'position' => { '<' => 3 }
206 my $attr = { order_by => "cd.title DESC, title" } ;
207 my @cds = Music::Track->deep_search_where($where, $attr);
208 is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000 ";
210 my $count = Music::Track->count_deep_search_where($where);
211 is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000";
216 'cd.artist.name' => { -like => 'W%' },
217 'cd.year' => { '>' => 2000 },
218 'position' => { '<' => 3 }
220 my $attr = { order_by => [ 'cd.title DESC' , 'title' ] } ;
221 my @cds = Music::Track->deep_search_where($where, $attr);
222 is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000, array ref order ";
224 my $count = Music::Track->count_deep_search_where($where);
225 is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000, array ref order";
229 my $where = { 'cd.title' => [ -and => { -like => '%o%' }, { -like => '%W%' } ] };
230 my $attr = { order_by => [ 'cd.id' ] } ;
232 my @tracks = Music::Track->deep_search_where($where, $attr);
233 is_deeply [ @tracks ], [ 3, 3, 4, 4, 4, 4, 4, 4 ], "Tracks from CD titles containing 'o' AND 'W'";
237 my $where = { 'cd.year' => [ 1995, 1999 ] };
238 my $attr = { order_by => [ 'cd.id' ] } ;
240 my @tracks = Music::Track->deep_search_where($where, $attr);
241 is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ],
242 "Tracks from CDs from 1995, 1999";
246 my $where = { 'cd.year' => { -in => [ 1995, 1999 ] } };
247 my $attr = { order_by => [ 'cd.id' ] } ;
249 my @tracks = Music::Track->deep_search_where($where, $attr);
250 is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ],
251 "Tracks from CDs in 1995, 1999";
255 my $where = { -and => [ 'cd.year' => [ 1995, 1999 ], position => { '<=', 2 } ] };
256 my $attr = { order_by => [ 'cd.id' ] } ;
258 my @tracks = Music::Track->deep_search_where($where, $attr);
259 is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ],
260 "First 2 tracks Tracks from CDs from 1995, 1999";
264 my $where = { -and => [ 'cd.year' => { -in => [ 1995, 1999 ] }, position => { '<=', 2 } ] };
265 my $attr = { order_by => [ 'cd.id' ] } ;
267 my @tracks = Music::Track->deep_search_where($where, $attr);
268 is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ],
269 "First 2 tracks Tracks from CDs in 1995, 1999";
273 my $where = { 'label.name' => { -in => [ 'Sony', 'Supraphon', 'Bogus' ] } };
274 my $attr = { order_by => [ 'id' ] } ;
276 my @cds = Music::CD->deep_search_where($where, $attr);
277 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
278 "CDs from Sony or Supraphon";
282 my $where = { 'label.name' => [ 'Sony', 'Supraphon', 'Bogus' ] };
283 my $attr = { order_by => [ 'id' ] } ;
285 my @cds = Music::CD->deep_search_where($where, $attr);
286 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
287 "CDs from Sony or Supraphon";
290 END { unlink $DB if -e $DB }