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