Helper/ResultSet/Random.pm

filter: Statementstmt 97.3% 37 / 38 Branchbran 50.0% 2 / 4 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 90.0% 54 / 60 CC 5 SCAR 7.5
CC
5
Cov
91%
rand
4.1
_rand_order_by
2.0
__ANON__
1.0
CRAP 2.1
« Helper/Row/ProxyResultSetMethod.pm ↑ Summary Helper/Schema/Verifier/ColumnInfo.pm »
1 package DBIx::Class::Helper::ResultSet::Random;
2 55 $DBIx::Class::Helper::ResultSet::Random::VERSION = '2.033001';
3 # ABSTRACT: Get random rows from a ResultSet
4  
5 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 55 ▶use parent 'DBIx::Class::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 55 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub _introspector {
14 1 ▶ my $d = use_module('DBIx::Introspector')
Subroutine _introspector: called CC=1 SCAR=0.0
15 ->new(drivers => '2013-12.01');
16  
17 1 $d->decorate_driver_unconnected(ACCESS => rand_sql => 'RND()' );
18 1 $d->decorate_driver_unconnected(Oracle => rand_sql => 'dbms_random.value' );
19 1 $d->decorate_driver_unconnected(Pg => rand_sql => 'RANDOM()' );
20 1 $d->decorate_driver_unconnected(MSSQL => rand_sql => 'NEWID()' );
21 1 $d->decorate_driver_unconnected(SQLite => rand_sql => 'RANDOM()' );
22  
23 1 $d
24 }
25  
26 55 my $d;
27 sub _rand_order_by {
28 2 ▶ my $self = shift;
Subroutine _rand_order_by: called CC=2 SCAR=7.1
29 2 my $storage = $self->result_source->storage;
30 2 $storage->ensure_connected;
31  
32 2 ▶ $d ||= $self->_introspector;
Condition66%2/3
$d ||= $self->_introspector
l!l&&r!l&&!r
110
MC/DC0%0/2
$d ||= $self->_introspector
$d $self->_introspector
Truth table66%2/3
A ||= B
A= $dB= $self->_introspector
ABresult
1X1
011
000
33 2 ▶ return try { $d->get($storage->dbh, undef, 'rand_sql') } catch { 'RAND()' };
Subroutine __ANON__: called CC=1 SCAR=0.4
34 }
35  
36 sub rand {
37 2 ▶ my $self = shift;
Subroutine rand: called CC=4 SCAR=14.0
Pod documented
38 2 ▶ my $amount = shift || 1;
Condition100%2/2
shift() || 1
l!l
11
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
39  
40 2 ▶ $self->throw_exception('rand can only return a positive amount of rows')
Branch: 1 branch
BranchTrueFalseTotal
unless $amount > 0 0 2 2
41 unless $amount > 0;
42  
43 2 ▶ $self->throw_exception('rand can only return an integer amount of rows')
Branch: 1 branch
BranchTrueFalseTotal
unless $amount == int $amount 0 2 2
44 unless $amount == int $amount;
45  
46 2 my $order_by = $self->_rand_order_by;
47  
48 2 return $self->search(undef, { rows=> $amount, order_by => \$order_by});
49 }
50  
51 55 1;
52  
53 __END__
54  
55 =pod
56  
57 =head1 NAME
58  
59 DBIx::Class::Helper::ResultSet::Random - Get random rows from a ResultSet
60  
61 =head1 SYNOPSIS
62  
63 # note that this is normally a component for a ResultSet
64 package MySchema::ResultSet::Bar;
65  
66 use strict;
67 use warnings;
68  
69 use parent 'DBIx::Class::ResultSet';
70  
71 __PACKAGE__->load_components('Helper::ResultSet::Random');
72  
73 # in code using resultset:
74 my $random_row = $schema->resultset('Bar')->rand->single;
75  
76 =head1 DESCRIPTION
77  
78 This component allows convenient selection of random rows. See
79 L<DBIx::Class::Helper::ResultSet/NOTE> for a nice way to apply it to your
80 entire schema.
81  
82 Currently this works by doing something akin to
83  
84 SELECT TOP($x) from $table ORDER BY RANDOM()
85  
86 Lots of people think this is slow. My own benchmarks show that doing the above,
87 for 10 rows in a table with just over 8 million rows, is nearly instant.
88 Although that was with SQL Server, and different databases will handle that
89 differently.
90  
91 So please, if you have performance issues and want this to work with your
92 database, get in touch and I will do what I can to get it to work quickly enough
93 to suite your needs.
94  
95 =head1 METHODS
96  
97 =head2 rand
98  
99 This method takes a single argument, being the size of the random ResultSet
100 to return. It defaults to 1. This Component will throw exceptions if the
101 argument is not an integer or not greater than zero.
102  
103 =head2 _rand_order_by
104  
105 This module currently does an C<ORDER BY> on some db specific function. If for
106 some reason it guesses incorrectly for your database the easiest way to fix
107 that in the short-term (ie without patching upstream) is to override this
108 method. So for example, if your db uses C<RAND()> instead of C<RANDOM()> and
109 it's not in the predefined list of dbs you could just do the following in your
110 ResultSet class:
111  
112 sub _rand_order_by { 'RAND()' }
113  
114 =head1 AUTHOR
115  
116 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
117  
118 =head1 COPYRIGHT AND LICENSE
119  
120 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
121  
122 This is free software; you can redistribute it and/or modify it under
123 the same terms as the Perl 5 programming language system itself.
124  
125 =cut
« Helper/Row/ProxyResultSetMethod.pm ↑ Summary Helper/Schema/Verifier/ColumnInfo.pm »