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