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 ({}, { order_by => 'cdid' });
14 # get the defined columns
15 my @dbic_cols = sort $rs->result_source->columns;
17 # use the hashref inflator class as result class
18 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
21 my $datahashref1 = $rs->first;
23 my @hashref_cols = sort keys %$datahashref1;
25 is_deeply( \@dbic_cols, \@hashref_cols, 'returned columns' );
27 my $cd1 = $rs->find ({cdid => 1});
28 is_deeply ( $cd1, $datahashref1, 'first/find return the same thing');
32 my ($dbic_obj, $datahashref) = @_;
34 foreach my $col (keys %$datahashref) {
36 if (not ref ($datahashref->{$col}) ) {
37 is ($datahashref->{$col}, $dbic_obj->get_column($col), 'same value');
39 # related table entry (belongs_to)
40 elsif (ref ($datahashref->{$col}) eq 'HASH') {
41 check_cols_of($dbic_obj->$col, $datahashref->{$col});
43 # multiple related entries (has_many)
44 elsif (ref ($datahashref->{$col}) eq 'ARRAY') {
45 my @dbic_reltable = $dbic_obj->$col;
46 my @hashref_reltable = @{$datahashref->{$col}};
48 is (scalar @hashref_reltable, scalar @dbic_reltable, 'number of related entries');
50 # for my $index (0..scalar @hashref_reltable) {
51 for my $index (0..scalar @dbic_reltable) {
52 my $dbic_reltable_obj = $dbic_reltable[$index];
53 my $hashref_reltable_entry = $hashref_reltable[$index];
55 check_cols_of($dbic_reltable_obj, $hashref_reltable_entry);
61 # create a cd without tracks for testing empty has_many relationship
62 $schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
64 # order_by to ensure both resultsets have the rows in the same order
65 # also check result_class-as-an-attribute syntax
66 my $rs_dbic = $schema->resultset('CD')->search(undef,
68 prefetch => [ qw/ artist tracks / ],
69 order_by => [ 'me.cdid', 'tracks.position' ],
72 my $rs_hashrefinf = $schema->resultset('CD')->search(undef,
74 prefetch => [ qw/ artist tracks / ],
75 order_by => [ 'me.cdid', 'tracks.position' ],
76 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
80 my @dbic = $rs_dbic->all;
81 my @hashrefinf = $rs_hashrefinf->all;
83 for my $index (0 .. $#hashrefinf) {
84 my $dbic_obj = $dbic[$index];
85 my $datahashref = $hashrefinf[$index];
87 check_cols_of($dbic_obj, $datahashref);
90 # sometimes for ultra-mega-speed you want to fetch columns in esoteric ways
91 # check the inflator over a non-fetching join
92 $rs_dbic = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
93 prefetch => { cds => 'tracks' },
94 order_by => [qw/cds.cdid tracks.trackid/],
97 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
98 join => { cds => 'tracks' },
99 select => [qw/name tracks.title tracks.cd /],
100 as => [qw/name cds.tracks.title cds.tracks.cd /],
101 order_by => [qw/cds.cdid tracks.trackid/],
102 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
105 @dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
106 @hashrefinf = $rs_hashrefinf->all;
108 is (scalar @dbic, scalar @hashrefinf, 'Equal number of tracks fetched');
110 for my $index (0 .. $#hashrefinf) {
111 my $track = $dbic[$index];
112 my $datahashref = $hashrefinf[$index];
114 is ($track->cd->artist->name, $datahashref->{name}, 'Brought back correct artist');
115 for my $col (keys %{$datahashref->{cds}{tracks}}) {
116 is ($track->get_column ($col), $datahashref->{cds}{tracks}{$col}, "Correct track '$col'");
120 # check for same query as above but using extended columns syntax
121 $rs_hashrefinf = $schema->resultset ('Artist')->search ({ 'me.artistid' => 1}, {
122 join => { cds => 'tracks' },
123 columns => {name => 'name', 'cds.tracks.title' => 'tracks.title', 'cds.tracks.cd' => 'tracks.cd'},
124 order_by => [qw/cds.cdid tracks.trackid/],
126 $rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
127 is_deeply [$rs_hashrefinf->all], \@hashrefinf, 'Check query using extended columns syntax';
129 # check nested prefetching of has_many relationships which return nothing
130 my $artist = $schema->resultset ('Artist')->create ({ name => 'unsuccessful artist without CDs'});
131 $artist->discard_changes;
132 my $rs_artists = $schema->resultset ('Artist')->search ({ 'me.artistid' => $artist->id}, {
133 prefetch => { cds => 'tracks' }, result_class => 'DBIx::Class::ResultClass::HashRefInflator',
137 [{ $artist->get_columns, cds => [] }],
138 'nested has_many prefetch without entries'