5 eval "use DBIx::Class::CDBICompat; require Class::DBI::Plugin::DeepAbstractSearch;";
7 plan (skip_all => "Class::DBI::Plugin::DeepAbstractSearch, Class::Trigger and DBIx::ContextualFetch required: $@");
12 my $DB = "t/var/cdbi_testdb";
15 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 });
18 use base qw(DBIx::Class::CDBICompat);
19 use Class::DBI::Plugin::DeepAbstractSearch;
20 __PACKAGE__->connection(@DSN);
22 my $sql = <<'SQL_END';
24 ---------------------------------------
26 ---------------------------------------
27 CREATE TABLE artists (
28 id INTEGER NOT NULL PRIMARY KEY,
32 INSERT INTO artists VALUES (1, "Willie Nelson");
33 INSERT INTO artists VALUES (2, "Patsy Cline");
35 ---------------------------------------
37 ---------------------------------------
39 id INTEGER NOT NULL PRIMARY KEY,
43 INSERT INTO labels VALUES (1, "Columbia");
44 INSERT INTO labels VALUES (2, "Sony");
45 INSERT INTO labels VALUES (3, "Supraphon");
47 ---------------------------------------
49 ---------------------------------------
51 id INTEGER NOT NULL PRIMARY KEY,
57 INSERT INTO cds VALUES (1, 1, 1, "Songs", 2005);
58 INSERT INTO cds VALUES (2, 2, 1, "Read Headed Stanger", 2000);
59 INSERT INTO cds VALUES (3, 1, 1, "Wanted! The Outlaws", 2004);
60 INSERT INTO cds VALUES (4, 2, 1, "The Very Best of Willie Nelson", 1999);
62 INSERT INTO cds VALUES (5, 1, 2, "12 Greates Hits", 1999);
63 INSERT INTO cds VALUES (6, 2, 2, "Sweet Dreams", 1995);
64 INSERT INTO cds VALUES (7, 3, 2, "The Best of Patsy Cline", 1991);
66 ---------------------------------------
68 ---------------------------------------
70 id INTEGER NOT NULL PRIMARY KEY,
75 INSERT INTO tracks VALUES (1, 1, 1, "Songs: Track 1");
76 INSERT INTO tracks VALUES (2, 1, 2, "Songs: Track 2");
77 INSERT INTO tracks VALUES (3, 1, 3, "Songs: Track 3");
78 INSERT INTO tracks VALUES (4, 1, 4, "Songs: Track 4");
80 INSERT INTO tracks VALUES (5, 2, 1, "Read Headed Stanger: Track 1");
81 INSERT INTO tracks VALUES (6, 2, 2, "Read Headed Stanger: Track 2");
82 INSERT INTO tracks VALUES (7, 2, 3, "Read Headed Stanger: Track 3");
83 INSERT INTO tracks VALUES (8, 2, 4, "Read Headed Stanger: Track 4");
85 INSERT INTO tracks VALUES (9, 3, 1, "Wanted! The Outlaws: Track 1");
86 INSERT INTO tracks VALUES (10, 3, 2, "Wanted! The Outlaws: Track 2");
88 INSERT INTO tracks VALUES (11, 4, 1, "The Very Best of Willie Nelson: Track 1");
89 INSERT INTO tracks VALUES (12, 4, 2, "The Very Best of Willie Nelson: Track 2");
90 INSERT INTO tracks VALUES (13, 4, 3, "The Very Best of Willie Nelson: Track 3");
91 INSERT INTO tracks VALUES (14, 4, 4, "The Very Best of Willie Nelson: Track 4");
92 INSERT INTO tracks VALUES (15, 4, 5, "The Very Best of Willie Nelson: Track 5");
93 INSERT INTO tracks VALUES (16, 4, 6, "The Very Best of Willie Nelson: Track 6");
95 INSERT INTO tracks VALUES (17, 5, 1, "12 Greates Hits: Track 1");
96 INSERT INTO tracks VALUES (18, 5, 2, "12 Greates Hits: Track 2");
97 INSERT INTO tracks VALUES (19, 5, 3, "12 Greates Hits: Track 3");
98 INSERT INTO tracks VALUES (20, 5, 4, "12 Greates Hits: Track 4");
100 INSERT INTO tracks VALUES (21, 6, 1, "Sweet Dreams: Track 1");
101 INSERT INTO tracks VALUES (22, 6, 2, "Sweet Dreams: Track 2");
102 INSERT INTO tracks VALUES (23, 6, 3, "Sweet Dreams: Track 3");
103 INSERT INTO tracks VALUES (24, 6, 4, "Sweet Dreams: Track 4");
105 INSERT INTO tracks VALUES (25, 7, 1, "The Best of Patsy Cline: Track 1");
106 INSERT INTO tracks VALUES (26, 7, 2, "The Best of Patsy Cline: Track 2");
110 foreach my $statement (split /;/, $sql) {
111 $statement =~ s/^\s*//gs;
112 $statement =~ s/\s*$//gs;
113 next unless $statement;
114 Music::DBI->db_Main->do($statement) or die "$@ $!";
117 Music::DBI->dbi_commit;
119 package Music::Artist;
120 use base 'Music::DBI';
121 Music::Artist->table('artists');
122 Music::Artist->columns(All => qw/id name/);
125 package Music::Label;
126 use base 'Music::DBI';
127 Music::Label->table('labels');
128 Music::Label->columns(All => qw/id name/);
131 use base 'Music::DBI';
132 Music::CD->table('cds');
133 Music::CD->columns(All => qw/id label artist title year/);
136 package Music::Track;
137 use base 'Music::DBI';
138 Music::Track->table('tracks');
139 Music::Track->columns(All => qw/id cd position title/);
141 Music::Artist->has_many(cds => 'Music::CD');
142 Music::Label->has_many(cds => 'Music::CD');
143 Music::CD->has_many(tracks => 'Music::Track');
144 Music::CD->has_a(artist => 'Music::Artist');
145 Music::CD->has_a(label => 'Music::Label');
146 Music::Track->has_a(cd => 'Music::CD');
153 my @artists = Music::Artist->deep_search_where($where, $attr);
154 is_deeply [ sort @artists ], [ 1, 2 ], "all without order";
159 my $attr = { order_by => 'name' };
160 my @artists = Music::Artist->deep_search_where($where, $attr);
161 is_deeply \@artists, [ 2, 1 ], "all with ORDER BY name";
166 my $attr = { order_by => 'name DESC' };
167 my @artists = Music::Artist->deep_search_where($where, $attr);
168 is_deeply \@artists, [ 1, 2 ], "all with ORDER BY name DESC";
172 my $where = { name => { -like => 'Patsy Cline' }, };
174 my @artists = Music::Artist->deep_search_where($where, $attr);
175 is_deeply \@artists, [ 2 ], "simple search";
179 my $where = { 'artist.name' => 'Patsy Cline' };
181 my @cds = Music::CD->deep_search_where($where, $attr);
182 is_deeply [ sort @cds ], [ 5, 6, 7 ], "Patsy's CDs";
186 my $where = { 'artist.name' => 'Patsy Cline' };
187 my $attr = { order_by => "title" } ;
188 my @cds = Music::CD->deep_search_where($where, $attr);
189 is_deeply [ @cds ], [ 5, 6, 7 ], "Patsy's CDs by title";
191 my $count = Music::CD->count_deep_search_where($where);
192 is_deeply $count, 3, "count Patsy's CDs by title";
196 my $where = { 'cd.title' => { -like => 'S%' }, };
197 my $attr = { order_by => "cd.title, title" } ;
198 my @cds = Music::Track->deep_search_where($where, $attr);
199 is_deeply [ @cds ], [1, 2, 3, 4, 21, 22, 23, 24 ], "Tracks from CDs whose name starts with 'S'";
204 'cd.artist.name' => { -like => 'W%' },
205 'cd.year' => { '>' => 2000 },
206 'position' => { '<' => 3 }
208 my $attr = { order_by => "cd.title DESC, title" } ;
209 my @cds = Music::Track->deep_search_where($where, $attr);
210 is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000 ";
212 my $count = Music::Track->count_deep_search_where($where);
213 is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000";
218 'cd.artist.name' => { -like => 'W%' },
219 'cd.year' => { '>' => 2000 },
220 'position' => { '<' => 3 }
222 my $attr = { order_by => [ 'cd.title DESC' , 'title' ] } ;
223 my @cds = Music::Track->deep_search_where($where, $attr);
224 is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000, array ref order ";
226 my $count = Music::Track->count_deep_search_where($where);
227 is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000, array ref order";
231 my $where = { 'cd.title' => [ -and => { -like => '%o%' }, { -like => '%W%' } ] };
232 my $attr = { order_by => [ 'cd.id' ] } ;
234 my @tracks = Music::Track->deep_search_where($where, $attr);
235 is_deeply [ @tracks ], [ 3, 3, 4, 4, 4, 4, 4, 4 ], "Tracks from CD titles containing 'o' AND 'W'";
239 my $where = { 'cd.year' => [ 1995, 1999 ] };
240 my $attr = { order_by => [ 'cd.id' ] } ;
242 my @tracks = Music::Track->deep_search_where($where, $attr);
243 is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ],
244 "Tracks from CDs from 1995, 1999";
248 my $where = { 'cd.year' => { -in => [ 1995, 1999 ] } };
249 my $attr = { order_by => [ 'cd.id' ] } ;
251 my @tracks = Music::Track->deep_search_where($where, $attr);
252 is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ],
253 "Tracks from CDs in 1995, 1999";
257 my $where = { -and => [ 'cd.year' => [ 1995, 1999 ], position => { '<=', 2 } ] };
258 my $attr = { order_by => [ 'cd.id' ] } ;
260 my @tracks = Music::Track->deep_search_where($where, $attr);
261 is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ],
262 "First 2 tracks Tracks from CDs from 1995, 1999";
266 my $where = { -and => [ 'cd.year' => { -in => [ 1995, 1999 ] }, position => { '<=', 2 } ] };
267 my $attr = { order_by => [ 'cd.id' ] } ;
269 my @tracks = Music::Track->deep_search_where($where, $attr);
270 is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ],
271 "First 2 tracks Tracks from CDs in 1995, 1999";
275 my $where = { 'label.name' => { -in => [ 'Sony', 'Supraphon', 'Bogus' ] } };
276 my $attr = { order_by => [ 'id' ] } ;
278 my @cds = Music::CD->deep_search_where($where, $attr);
279 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
280 "CDs from Sony or Supraphon";
284 my $where = { 'label.name' => [ 'Sony', 'Supraphon', 'Bogus' ] };
285 my $attr = { order_by => [ 'id' ] } ;
287 my @cds = Music::CD->deep_search_where($where, $attr);
288 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
289 "CDs from Sony or Supraphon";
292 END { unlink $DB if -e $DB }