9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
13 my $rs = $schema->resultset("CD");
18 $rs->search ({}, {columns => ['year'], distinct => 1})->count,
19 'At least one year is the same in rs'
22 my $rs_title = $rs->get_column('title');
23 my $rs_year = $rs->get_column('year');
24 my $max_year = $rs->get_column(\'MAX (year)');
26 is($rs_title->next, 'Spoonful of bees', "next okay");
27 is_deeply( [ sort $rs_year->func('DISTINCT') ], [ 1997, 1998, 1999, 2001 ], "wantarray context okay");
28 ok ($max_year->next == $rs_year->max, q/get_column (\'FUNC') ok/);
30 my @all = $rs_title->all;
31 cmp_ok(scalar @all, '==', 5, "five titles returned");
33 cmp_ok($rs_year->max, '==', 2001, "max okay for year");
34 is($rs_title->min, 'Caterwaulin\' Blues', "min okay for title");
36 cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
39 is($rs_year->next, 1999, "reset okay");
41 is($rs_year->first, 1999, "first okay");
44 is($rs_year->single, 1999, "single okay");
45 }, qr/Query returned more than one row/, 'single warned');
48 # test distinct propagation
50 [$rs->search ({}, { distinct => 1 })->get_column ('year')->all],
51 [$rs_year->func('distinct')],
52 'distinct => 1 is passed through properly',
55 # test +select/+as for single column
56 my $psrs = $schema->resultset('CD')->search({},
58 '+select' => \'MAX(year)',
62 lives_ok(sub { $psrs->get_column('last_year')->next }, '+select/+as additional column "last_year" present (scalar)');
63 dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
65 # test +select/+as for overriding a column
66 $psrs = $schema->resultset('CD')->search({},
68 'select' => \"'The Final Countdown'",
72 is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
75 # test +select/+as for multiple columns
76 $psrs = $schema->resultset('CD')->search({},
78 '+select' => [ \'LENGTH(title) AS title_length', 'title' ],
79 '+as' => [ 'tlength', 'addedtitle' ]
82 lives_ok(sub { $psrs->get_column('tlength')->next }, '+select/+as multiple additional columns, "tlength" column present');
83 lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
85 # test that +select/+as specs do not leak
87 $psrs->get_column('year')->as_query,
88 '(SELECT me.year FROM cd me)',
90 'Correct SQL for get_column/as'
94 $psrs->get_column('addedtitle')->as_query,
95 '(SELECT me.title FROM cd me)',
97 'Correct SQL for get_column/+as col'
101 $psrs->get_column('tlength')->as_query,
102 '(SELECT LENGTH(title) AS title_length FROM cd me)',
104 'Correct SQL for get_column/+as func'
107 # test that order_by over a function forces a subquery
110 [ $psrs->search ({}, { order_by => { -desc => 'title_length' } })->get_column ('title')->all ],
112 "Generic Manufactured Singles",
113 "Come Be Depressed With Us",
114 "Caterwaulin' Blues",
118 'Subquery count induced by aliased ordering function',
122 # test for prefetch not leaking
124 my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
125 my $rsc = $rs->get_column('year');
126 is( $rsc->{_parent_resultset}->{attrs}->{prefetch}, undef, 'prefetch wiped' );
130 is ($schema->resultset('BooksInLibrary')->get_column ('price')->sum, 125, 'Sum of a resultset works correctly');
132 # test sum over search_related
133 my $owner = $schema->resultset('Owners')->find ({ name => 'Newton' });
134 ok ($owner->books->count > 1, 'Owner Newton has multiple books');
135 is ($owner->search_related ('books')->get_column ('price')->sum, 60, 'Correctly calculated price of all owned books');
138 # make sure joined/prefetched get_column of a PK dtrt
141 my $j_rs = $rs->search ({}, { join => 'tracks' })->get_column ('cdid');
144 [ map { my $c = $rs->next; ( ($c->id) x $c->tracks->count ) } (1 .. $rs->count) ],
145 'join properly explodes amount of rows from get_column',
149 my $p_rs = $rs->search ({}, { prefetch => 'tracks' })->get_column ('cdid');
152 [ $rs->get_column ('cdid')->all ],
153 'prefetch properly collapses amount of rows from get_column',