File Coverage

blib/lib/DBIx/Class/Helper/TableSample.pm
Criterion Covered Total %
statement 37 39 94.8
branch 14 18 77.7
condition 4 6 66.6
subroutine 6 6 100.0
pod n/a
total 61 69 88.4


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   221921 use v5.14;
  1         5  
6 1     1   5 use warnings;
  1         3  
  1         30  
7              
8 1     1   5 use parent 'DBIx::Class::ResultSet';
  1         2  
  1         8  
9              
10 1     1   563 use Ref::Util qw/ is_plain_arrayref is_plain_hashref is_plain_scalarref /;
  1         1751  
  1         100  
11              
12             # RECOMMEND PREREQ: Ref::Util::XS
13              
14 1     1   8 use namespace::clean;
  1         3  
  1         8  
15              
16             our $VERSION = 'v0.4.0';
17              
18              
19             sub _resolved_attrs {
20 9     9   954915 my $rs = $_[0];
21              
22 9         52 $rs->next::method;
23              
24 9         1920 my $attrs = $rs->{_attrs};
25              
26 9 50       45 if ( my $conf = delete $attrs->{tablesample} ) {
27              
28 9         23 my $from = $attrs->{from};
29              
30 9 50 33     61 $rs->throw_exception('tablesample on joins is not supported')
31             if is_plain_arrayref($from) && @$from > 1;
32              
33 9 100       37 $conf = { fraction => $conf } unless is_plain_hashref($conf);
34              
35 9 50       28 $rs->throw_exception('tablesample must be a hashref')
36             unless is_plain_hashref($conf);
37              
38 9         49 my $sqla = $rs->result_source->storage->sql_maker;
39              
40 9         804 my $part_sql = " tablesample";
41              
42 9 100 100     76 if (my $type = ($conf->{method} // $conf->{type})) {
43 4         14 $part_sql .= " $type";
44             }
45              
46 9         26 my $arg = $conf->{fraction};
47 9 100       31 $arg = $$arg if is_plain_scalarref($arg);
48 9         59 $part_sql .= "($arg)";
49              
50 9 100       35 if ( defined $conf->{repeatable} ) {
51 2         21 my $seed = $conf->{repeatable};
52 2 100       11 $seed = $$seed if is_plain_scalarref($seed);
53 2         12 $part_sql .= sprintf( ' repeatable (%s)', $seed );
54             }
55              
56 9 50       31 if (is_plain_arrayref($from)) {
57 9         70 my $sql = $sqla->_from_chunk_to_sql($from->[0]) . $sqla->_sqlcase($part_sql);
58 9         1011 $from->[0] = \$sql;
59             }
60             else {
61 0         0 my $sql = $sqla->_from_chunk_to_sql($from) . $sqla->_sqlcase($part_sql);
62 0         0 $attrs->{from} = \$sql;
63             }
64              
65             }
66              
67 9         72 return $attrs;
68             }
69              
70              
71             1;
72              
73             __END__