7 my $schema = DBICTest->init_schema();
9 # Under some versions of SQLite if the $rs is left hanging around it will lock
10 # So we create a scope here cos I'm lazy
12 my $rs = $schema->resultset('CD')->search ({}, {
14 # use the hashref inflator class as result class
15 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
18 # get the defined columns
19 my @dbic_cols = sort $rs->result_source->columns;
22 my $datahashref1 = $rs->first;
24 my @hashref_cols = sort keys %$datahashref1;
26 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
28 my $cd1 = $rs->find ({cdid => 1});
29 is_deeply ( $cd1, $datahashref1, 'first/find return the same thing');
31 my $cd2 = $rs->search({ cdid => 1 })->single;
32 is_deeply ( $cd2, $datahashref1, 'first/search+single return the same thing');
34 $rs->result_class('DBIx::Class::Row');
36 is( $rs->result_class, 'DBIx::Class::Row', 'result_class set' );
39 $rs->search->result_class, 'DBIx::Class::ResultClass::HashRefInflator',
40 'result_class set using accessor does not propagate over search'
46 my ($dbic_obj, $datahashref) = @_;
48 foreach my $col (keys %$datahashref) {
50 if (not ref ($datahashref->{$col}) ) {
51 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
53 # related table entry (belongs_to)
54 elsif (ref ($datahashref->{$col}) eq 'HASH') {
55 check_cols_of($dbic_obj->$col, $datahashref->{$col});
57 # multiple related entries (has_many)
58 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
59 my @dbic_reltable = $dbic_obj->$col;
60 my @hashref_reltable = @{$datahashref->{$col}};
62 is (scalar @dbic_reltable, scalar @hashref_reltable, 'number of related entries');
64 # for my $index (0..scalar @hashref_reltable) {
65 for my $index (0..scalar @dbic_reltable) {
66 my $dbic_reltable_obj = $dbic_reltable[$index];
67 my $hashref_reltable_entry = $hashref_reltable[$index];
69 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
75 # create a cd without tracks for testing empty has_many relationship
76 $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
78 # order_by to ensure both resultsets have the rows in the same order
79 # also check result_class-as-an-attribute syntax
80 my $rs_dbic = $schema->resultset('CD')->search(undef,
82 prefetch => [ qw/ artist tracks / ],
83 order_by => [ 'me.cdid', 'tracks.position' ],
86 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
88 prefetch => [ qw/ artist tracks / ],
89 order_by => [ 'me.cdid', 'tracks.position' ],
90 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
94 my @dbic = $rs_dbic->all;
95 my @hashrefinf = $rs_hashrefinf->all;
97 for my $index (0 .. $#hashrefinf) {
98 my $dbic_obj = $dbic[$index];
99 my $datahashref = $hashrefinf[$index];
101 check_cols_of($dbic_obj, $datahashref);
104 # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
105 # check the inflator over a non-fetching join
106 $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
107 prefetch => { cds => 'tracks' },
108 order_by => [qw/cds.cdid tracks.trackid/],
111 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
112 join => { cds => 'tracks' },
113 select => [qw/name tracks.title tracks.cd /],
114 as => [qw/name cds.tracks.title cds.tracks.cd /],
115 order_by => [qw/cds.cdid tracks.trackid/],
116 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
119 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
120 @hashrefinf = $rs_hashrefinf->all;
122 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
124 for my $index (0 .. $#hashrefinf) {
125 my $track = $dbic[$index];
126 my $datahashref = $hashrefinf[$index];
128 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
129 for my $col (keys %{$datahashref->{cds}{tracks}}) {
130 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
134 # check for same query as above but using extended columns syntax
135 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
136 join => { cds => 'tracks' },
137 columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'},
138 order_by => [qw/cds.cdid tracks.trackid/],
140 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
141 is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
143 # check nested prefetching of has_many relationships which return nothing
144 my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'});
145 $artist->discard_changes;
146 my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, {
147 prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator',
151 [{ $artist->get_columns, cds => [] }],
152 'nested has_many prefetch without entries'