| 1 | package DBIx::Class::Helper::TableSample; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Add support for tablesample clauses | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use parent 'DBIx::Class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Ref::Util qw/ is_plain_arrayref is_plain_hashref is_plain_scalarref /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # RECOMMEND PREREQ: Ref::Util::XS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = 'v0.7.0'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 25 | ▶ | sub _resolved_attrs { | ||||||||||||||||||||||||||
|
Subroutine
_resolved_attrs: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||
| 20 | 25 | my $rs = $_[0]; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 25 | $rs->next::method; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 25 | my $attrs = $rs->{_attrs}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 25 | ▶ | if ( my $conf = delete $attrs->{tablesample} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 25 | my $from = $attrs->{from}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 25 | ▶ | $conf = { fraction => $conf } unless is_plain_hashref($conf); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 25 | ▶ | $rs->throw_exception('tablesample must be a hashref') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | unless is_plain_hashref($conf); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 25 | my $sqla = $rs->result_source->storage->sql_maker; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 25 | my $part_sql = " tablesample"; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 25 | ▶ | if (my $type = ($conf->{method} // $conf->{type})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$conf->{"method"} // $conf->{"type"}
MC/DC100%2/2
$conf->{"method"} // $conf->{"type"}
$conf->{"method"}
$conf->{"type"}
Truth table100%3/3
A // B
A= $conf->{"method"}B= $conf->{"type"}
| |||||||||||||||||||||||||||||
| 40 | 13 | $part_sql .= " $type"; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 25 | my $arg = $conf->{fraction}; | |||||||||||||||||||||||||||
| 44 | 25 | ▶ | $arg = $$arg if is_plain_scalarref($arg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 25 | $part_sql .= "($arg)"; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 25 | ▶ | if ( defined $conf->{repeatable} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 4 | my $seed = $conf->{repeatable}; | |||||||||||||||||||||||||||
| 49 | 4 | ▶ | $seed = $$seed if is_plain_scalarref($seed); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 4 | $part_sql .= sprintf( ' repeatable (%s)', $seed ); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 25 | ▶ | if (is_plain_arrayref($from)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 25 | my $sql = $sqla->_from_chunk_to_sql($from->[0]) . $sqla->_sqlcase($part_sql); | |||||||||||||||||||||||||||
| 55 | 25 | $from->[0] = \$sql; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 0 | my $sql = $sqla->_from_chunk_to_sql($from) . $sqla->_sqlcase($part_sql); | |||||||||||||||||||||||||||
| 59 | 0 | $attrs->{from} = \$sql; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 25 | return $attrs; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 14 | ▶ | sub tablesample { | ||||||||||||||||||||||||||
|
Subroutine
tablesample: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 14 | my ( $rs, $frac, $options ) = @_; | |||||||||||||||||||||||||||
| 70 | 14 | ▶ | $options //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options //= {}
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||
| 71 | 14 | ▶ | $options = { method => $options } unless is_plain_hashref($options); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 14 | return $rs->search_rs( | |||||||||||||||||||||||||||
| 73 | undef, | ||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | tablesample => { | ||||||||||||||||||||||||||||
| 76 | fraction => $frac, | ||||||||||||||||||||||||||||
| 77 | %$options | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||