| 1 | package DBIx::Class::Report; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Digest::MD5 qw/md5_hex/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 2 | has 'columns' => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | isa => 'ArrayRef[Str]', | ||||||||||
| 13 | required => 1, | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 2 | has 'sql' => ( | |||||||||
| 17 | is => 'ro', | ||||||||||
| 18 | isa => 'Str', | ||||||||||
| 19 | required => 1, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 2 | has 'schema' => ( | |||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | isa => 'DBIx::Class::Schema', | ||||||||||
| 25 | required => 1, | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | has 'methods' => ( | ||||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | isa => 'HashRef[CodeRef]', | ||||||||||
| 31 | 0 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 32 | 2 | required => 0, | |||||||||
| 33 | ); | ||||||||||
| 34 | |||||||||||
| 35 | 2 | has '_resultset' => ( | |||||||||
| 36 | is => 'rw', | ||||||||||
| 37 | isa => 'DBIx::Class::ResultSet', | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | sub BUILD { | ||||||||||
| 41 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 42 | 1 | my $schema_class = ref $self->schema; # XXX There has to be a better way | |||||||||
| 43 | |||||||||||
| 44 | 1 | my $md5 = md5_hex( $self->sql ); | |||||||||
| 45 | 1 | my $table = "table_$md5"; | |||||||||
| 46 | 1 | my $source_name = "View$md5"; | |||||||||
| 47 | 1 | my $view_class = $schema_class . "::$source_name"; | |||||||||
| 48 | |||||||||||
| 49 | # XXX Again, I'll figure out something better after this hack | ||||||||||
| 50 | 1 | ▶ | eval <<"END_VIEW"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | package $view_class; | ||||||||||
| 52 | use base 'DBIx::Class::Core'; | ||||||||||
| 53 | END_VIEW | ||||||||||
| 54 | 1 | ▶ | croak $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | 1 | $view_class->table_class('DBIx::Class::ResultSource::View'); | |||||||||
| 57 | 1 | $view_class->table($table); | |||||||||
| 58 | 1 | $view_class->add_columns( @{ $self->columns } ); | |||||||||
| 59 | 1 | $view_class->result_source_instance->is_virtual(1); | |||||||||
| 60 | 1 | $view_class->result_source_instance->view_definition( $self->sql ); | |||||||||
| 61 | |||||||||||
| 62 | 1 | $self->schema->register_class( $source_name => $view_class ); | |||||||||
| 63 | 1 | $self->_resultset( $self->schema->resultset($source_name) ); | |||||||||
| 64 | 1 | $self->_add_methods($view_class); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub _add_methods { | ||||||||||
| 68 | 1 | ▶ | my ( $self, $view_class ) = @_; | ||||||||
|
Subroutine
_add_methods: called CC=2 SCAR=6.9
| |||||||||||
| 69 | 1 | ▶ | while ( my ( $name, $body ) = each %{ $self->methods } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 1 | *{ $view_class . '::' . $name } = $body; | |||||||||
| 72 | } | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | sub fetch { | ||||||||||
| 76 | 3 | ▶ | my ( $self, @bind_params ) = @_; | ||||||||
|
Subroutine
fetch: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 77 | 3 | return $self->_resultset->search( {}, { bind => [@bind_params] } ); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 81 | |||||||||||
| 82 | =head1 NAME | ||||||||||
| 83 | |||||||||||
| 84 | DBIx::Class::Report - Ad-Hoc reporting from DBIx::Class | ||||||||||
| 85 | |||||||||||
| 86 | =head1 VERSION | ||||||||||
| 87 | |||||||||||
| 88 | Version 0.01 | ||||||||||
| 89 | |||||||||||
| 90 | =head1 SYNOPSIS | ||||||||||
| 91 | |||||||||||
| 92 | use DBIx::Class::Report; | ||||||||||
| 93 | |||||||||||
| 94 | my $report = DBIx::Class::Report->new( | ||||||||||
| 95 | schema => $dbic_schema_object, | ||||||||||
| 96 | sql => $complicated_sql, | ||||||||||
| 97 | columns => \@accessor_names, | ||||||||||
| 98 | methods => { | ||||||||||
| 99 | method1 => sub { ... }, | ||||||||||
| 100 | method1 => sub { ... }, | ||||||||||
| 101 | }, | ||||||||||
| 102 | ); | ||||||||||
| 103 | my $resultset = $report->fetch(@bind_params_for_complicated_sql); | ||||||||||
| 104 | while ( my $result = $resultset->next ) { | ||||||||||
| 105 | # use like a normal dbic result, but it's read only | ||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||
| 109 | |||||||||||
| 110 | B<NOTE:> Experimental ALPHA code. | ||||||||||
| 111 | |||||||||||
| 112 | Sometimes it's nice to be able to run ad-hoc SQL and get back a L<DBIx::Class> | ||||||||||
| 113 | resultset. We can use L<DBIx::Class::ResultSource::View>, but that requires we | ||||||||||
| 114 | hard-code our SQL ahead of time. C<DBIx::Class::Report> allows you to create | ||||||||||
| 115 | your SQL on the fly and generate ad-hoc, read-only dbic resultsets which act | ||||||||||
| 116 | just like normal dbic objects. | ||||||||||
| 117 | |||||||||||
| 118 | my $sql = <<'SQL'; | ||||||||||
| 119 | SELECT var.name, ce.event_type, count(*) | ||||||||||
| 120 | FROM tracking_conversion_event ce | ||||||||||
| 121 | JOIN tracking_visitor visitor ON visitor.tracking_visitor_id = ce.tracking_visitor_id | ||||||||||
| 122 | JOIN tracking_version_variant curr ON curr.tracking_version_variant_id = visitor.tracking_version_variant_id | ||||||||||
| 123 | JOIN tracking_version ver ON ver.tracking_version_id = curr.tracking_version_id | ||||||||||
| 124 | JOIN tracking_variant var ON var.tracking_variant_id = curr.tracking_variant_id | ||||||||||
| 125 | WHERE ver.tracking_id = ? | ||||||||||
| 126 | AND ver.version = ? | ||||||||||
| 127 | GROUP BY 1, 2 | ||||||||||
| 128 | SQL | ||||||||||
| 129 | |||||||||||
| 130 | my $events_per_name = DBIx::Class::Report->new( | ||||||||||
| 131 | schema => $schema, | ||||||||||
| 132 | sql => $sql, | ||||||||||
| 133 | columns => [qw/name event_type total/], | ||||||||||
| 134 | methods => { | ||||||||||
| 135 | tracking_version => sub { | ||||||||||
| 136 | my $self = shift; | ||||||||||
| 137 | return $self->result_source->schema->resultset('TrackingVersion')->find($self->tracking_version_id); | ||||||||||
| 138 | }, | ||||||||||
| 139 | tracking_variant => sub { | ||||||||||
| 140 | my $self = shift; | ||||||||||
| 141 | return $self->result_source->schema->resultset('TrackingVersion')->find($self->tracking_variant_id); | ||||||||||
| 142 | }, | ||||||||||
| 143 | ); | ||||||||||
| 144 | |||||||||||
| 145 | my $resultset = $events_per_name->fetch( $tracking_id, $version ); | ||||||||||
| 146 | say $resultset->count; # yeah, it behaves just like a normal dbic resultset | ||||||||||
| 147 | |||||||||||
| 148 | while ( my $result = $resultset->next ) { | ||||||||||
| 149 | say $result->name; | ||||||||||
| 150 | say $result->event_type; | ||||||||||
| 151 | say $result->total; | ||||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | Note that the C<methods> key installs methods in each returned result object. | ||||||||||
| 155 | This allows us to neatly similate inflation or anthing else we need from a | ||||||||||
| 156 | standard result object. | ||||||||||
| 157 | |||||||||||
| 158 | =head1 AUTHOR | ||||||||||
| 159 | |||||||||||
| 160 | Curtis "Ovid" Poe, C<< <ovid at cpan.org> >> | ||||||||||
| 161 | |||||||||||
| 162 | =head1 BUGS | ||||||||||
| 163 | |||||||||||
| 164 | Please report any bugs or feature requests to through the web interface at | ||||||||||
| 165 | L<https://github.com/Ovid/dbix-class-report/issues>. | ||||||||||
| 166 | |||||||||||
| 167 | =head1 SUPPORT | ||||||||||
| 168 | |||||||||||
| 169 | You can find documentation for this module with the perldoc command. | ||||||||||
| 170 | |||||||||||
| 171 | perldoc DBIx::Class::Report | ||||||||||
| 172 | |||||||||||
| 173 | You can also look for information at: | ||||||||||
| 174 | |||||||||||
| 175 | =over 4 | ||||||||||
| 176 | |||||||||||
| 177 | =item * Bugs | ||||||||||
| 178 | |||||||||||
| 179 | L<https://github.com/Ovid/dbix-class-report/issues> | ||||||||||
| 180 | |||||||||||
| 181 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 182 | |||||||||||
| 183 | L<http://annocpan.org/dist/DBIx-Class-Report> | ||||||||||
| 184 | |||||||||||
| 185 | =item * CPAN Ratings | ||||||||||
| 186 | |||||||||||
| 187 | L<http://cpanratings.perl.org/d/DBIx-Class-Report> | ||||||||||
| 188 | |||||||||||
| 189 | =item * Search CPAN | ||||||||||
| 190 | |||||||||||
| 191 | L<http://search.cpan.org/dist/DBIx-Class-Report/> | ||||||||||
| 192 | |||||||||||
| 193 | =back | ||||||||||
| 194 | |||||||||||
| 195 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 196 | |||||||||||
| 197 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 198 | |||||||||||
| 199 | Copyright 2015 Curtis "Ovid" Poe. | ||||||||||
| 200 | |||||||||||
| 201 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 202 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 203 | copy of the full license at: | ||||||||||
| 204 | |||||||||||
| 205 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 206 | |||||||||||
| 207 | Any use, modification, and distribution of the Standard or Modified | ||||||||||
| 208 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||
| 209 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||
| 210 | or distribute the Package, if you do not accept this license. | ||||||||||
| 211 | |||||||||||
| 212 | If your Modified Version has been derived from a Modified Version made | ||||||||||
| 213 | by someone other than you, you are nevertheless required to ensure that | ||||||||||
| 214 | your Modified Version complies with the requirements of this license. | ||||||||||
| 215 | |||||||||||
| 216 | This license does not grant you the right to use any trademark, service | ||||||||||
| 217 | mark, tradename, or logo of the Copyright Holder. | ||||||||||
| 218 | |||||||||||
| 219 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||
| 220 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||
| 221 | otherwise transfer the Package with respect to any patent claims | ||||||||||
| 222 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||
| 223 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||
| 224 | counterclaim) against any party alleging that the Package constitutes | ||||||||||
| 225 | direct or contributory patent infringement, then this Artistic License | ||||||||||
| 226 | to you shall terminate on the date that such litigation is filed. | ||||||||||
| 227 | |||||||||||
| 228 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||
| 229 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||
| 230 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||
| 231 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||
| 232 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||
| 233 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||
| 234 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||
| 235 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 236 | |||||||||||
| 237 | |||||||||||
| 238 | =cut | ||||||||||
| 239 | |||||||||||
| 240 | 2 | 1; # End of DBIx::Class::Report | |||||||||