| 1 | package DBIx::Class::Helper::ResultSet::Random; | ||||||||||||||||||||
| 2 | 57 | $DBIx::Class::Helper::ResultSet::Random::VERSION = '2.037000'; | |||||||||||||||||||
| 3 | # ABSTRACT: Get random rows from a ResultSet | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 57 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 57 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 57 | ▶ | use parent 'DBIx::Class::ResultSet'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 57 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 57 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | sub _introspector { | ||||||||||||||||||
|
Subroutine
_introspector: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | my $d = use_module('DBIx::Introspector') | |||||||||||||||||||
| 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 | 57 | my $d; | |||||||||||||||||||
| 27 | 2 | ▶ | sub _rand_order_by { | ||||||||||||||||||
|
Subroutine
_rand_order_by: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 28 | 2 | my $self = shift; | |||||||||||||||||||
| 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
MC/DC0%0/2
$d ||= $self->_introspector
$d
$self->_introspector
Truth table66%2/3
A ||= B
A= $dB= $self->_introspector
| |||||||||||||||||||||
| 33 | 0 | ▶ | return try { $d->get($storage->dbh, undef, 'rand_sql') } catch { 'RAND()' }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | ▶ | sub rand { | ||||||||||||||||||
|
Subroutine
rand: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 37 | 2 | my $self = shift; | |||||||||||||||||||
| 38 | 2 | ▶ | my $amount = shift || 1; | ||||||||||||||||||
|
Condition100%2/2
shift() || 1
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 2 | ▶ | $self->throw_exception('rand can only return a positive amount of rows') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | unless $amount > 0; | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 2 | ▶ | $self->throw_exception('rand can only return an integer amount of rows') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 57 | 1; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | __END__ | ||||||||||||||||||||