File: VirtualView.pm

package info (click to toggle)
libdbix-class-helpers-perl 2.023007-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 792 kB
  • ctags: 294
  • sloc: perl: 3,872; sql: 161; makefile: 7
file content (110 lines) | stat: -rw-r--r-- 3,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package DBIx::Class::Helper::ResultSet::VirtualView;
$DBIx::Class::Helper::ResultSet::VirtualView::VERSION = '2.023007';
use strict;
use warnings;
use vars qw($VERSION);

# ABSTRACT: Clean up your SQL namespace (DEPRECATED)

sub as_virtual_view {
   my $self = shift;

   return $self->as_subselect_rs;
}

use Carp::Clan;
carp 'This module is deprecated!  Please use DBIx::Class::ResultSet::as_subselect_rs instead!' if $VERSION >= 3;
croak 'This module is deprecated!  Please use DBIx::Class::ResultSet::as_subselect_rs instead!' if $VERSION >= 4;

1;

__END__

=pod

=head1 NAME

DBIx::Class::Helper::ResultSet::VirtualView - Clean up your SQL namespace (DEPRECATED)

=head1 SYNOPSIS

 # note that this is normally a component for a ResultSet
 package MySchema::ResultSet::Bar;

 use strict;
 use warnings;

 use parent 'DBIx::Class::ResultSet';

 __PACKAGE__->load_components('Helper::ResultSet::VirtualView');

 # and then in code that uses the ResultSet Join with relation x
 my $rs = $schema->resultset('Bar')->search({'x.name' => 'abc'},{ join => 'x' });

 # 'x' now pollutes the query namespace

 # So the following works as expected
 my $ok_rs = $rs->search({'x.other' => 1});

 # But this doesn't: instead of finding a 'Bar' related to two x rows (abc and
 # def) we look for one row with contradictory terms and join in another table
 # (aliased 'x_2') which we never use
 my $broken_rs = $rs->search({'x.name' => 'def'});

 my $rs2 = $rs->as_virtual_view;

 # doesn't work - 'x' is no longer accessible in $rs2, having been sealed away
 my $not_joined_rs = $rs2->search({'x.other' => 1});

 # works as expected: finds a 'table' row related to two x rows (abc and def)
 my $correctly_joined_rs = $rs2->search({'x.name' => 'def'});

=head1 DESCRIPTION

This component is will allow you to clean up your SQL namespace.  See
L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it to your
entire schema.

=head1 DEPRECATED

This component has been suplanted by
L<DBIx::Class::ResultSet::as_subselect_rs>.  In the next major version
(3) we will begin issuing a warning on it's use.  In the major version after
that (4) we will remove it entirely.

=head1 METHODS

=head2 as_virtual_view

Act as a barrier to SQL symbols.  The resultset provided will be made into a
"virtual view" by including it as a subquery within the from clause.  From this
point on, any joined tables are inaccessible to ->search on the resultset (as if
it were simply where-filtered without joins).  See L</SYNOPSIS> for example.

=head1 NOTE

You don't I<have> to use this as a Component.  If you prefer you can use it
in the following manner:

 # in code using ResultSet:
 use DBIx::Class:Helper::VirtualView;

 my $new_rs = DBIx::Class::Helper::VirtualView::as_virtual_view($rs);

=head1 THANKS

Thanks to ijw from #dbix-class for the idea for this helper (originally called
seal), most of the code, and most of the documentation.

=head1 AUTHOR

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Arthur Axel "fREW" Schmidt.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut