File Coverage

blib/lib/DBIx/Class/Helper/TableSample.pm
Criterion Covered Total %
statement 40 42 95.2
branch 14 18 77.7
condition 4 6 66.6
subroutine 7 7 100.0
pod n/a
total 65 73 89.0


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::TableSample;
2              
3             # ABSTRACT: Add support for tablesample clauses
4              
5 1     1   193182 use v5.10.1;
  1         3  
6              
7 1     1   5 use strict;
  1         3  
  1         29  
8 1     1   6 use warnings;
  1         1  
  1         27  
9              
10 1     1   5 use parent 'DBIx::Class::ResultSet';
  1         2  
  1         8  
11              
12 1     1   474 use Ref::Util qw/ is_plain_arrayref is_plain_hashref is_plain_scalarref /;
  1         1539  
  1         87  
13              
14             # RECOMMEND PREREQ: Ref::Util::XS
15              
16 1     1   8 use namespace::clean;
  1         2  
  1         9  
17              
18             our $VERSION = 'v0.3.1';
19              
20              
21             sub _resolved_attrs {
22 9     9   837103 my $rs = $_[0];
23              
24 9         61 $rs->next::method;
25              
26 9         1626 my $attrs = $rs->{_attrs};
27              
28 9 50       42 if ( my $conf = delete $attrs->{tablesample} ) {
29              
30 9         24 my $from = $attrs->{from};
31              
32 9 50 33     61 $rs->throw_exception('tablesample on joins is not supported')
33             if is_plain_arrayref($from) && @$from > 1;
34              
35 9 100       34 $conf = { fraction => $conf } unless is_plain_hashref($conf);
36              
37 9 50       24 $rs->throw_exception('tablesample must be a hashref')
38             unless is_plain_hashref($conf);
39              
40 9         49 my $sqla = $rs->result_source->storage->sql_maker;
41              
42 9         660 my $part_sql = " tablesample";
43              
44 9 100 100     75 if (my $type = ($conf->{method} // $conf->{type})) {
45 4         17 $part_sql .= " $type";
46             }
47              
48 9         23 my $arg = $conf->{fraction};
49 9 100       47 $arg = $$arg if is_plain_scalarref($arg);
50 9         64 $part_sql .= "($arg)";
51              
52 9 100       32 if ( defined $conf->{repeatable} ) {
53 2         6 my $seed = $conf->{repeatable};
54 2 100       9 $seed = $$seed if is_plain_scalarref($seed);
55 2         13 $part_sql .= sprintf( ' repeatable (%s)', $seed );
56             }
57              
58 9 50       24 if (is_plain_arrayref($from)) {
59 9         56 my $sql = $sqla->_from_chunk_to_sql($from->[0]) . $sqla->_sqlcase($part_sql);
60 9         861 $from->[0] = \$sql;
61             }
62             else {
63 0         0 my $sql = $sqla->_from_chunk_to_sql($from) . $sqla->_sqlcase($part_sql);
64 0         0 $attrs->{from} = \$sql;
65             }
66              
67             }
68              
69 9         70 return $attrs;
70             }
71              
72              
73             1;
74              
75             __END__