File Coverage

blib/lib/DBIx/Class/Helper/TableSample.pm
Criterion Covered Total %
statement 40 42 95.2
branch 15 18 83.3
condition 4 5 80.0
subroutine 7 7 100.0
pod 1 1 100.0
total 67 73 91.7


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   757863 use v5.14;
  1         5  
6 1     1   7 use warnings;
  1         2  
  1         92  
7              
8 1     1   7 use parent 'DBIx::Class';
  1         1  
  1         9  
9              
10 1     1   74 use Ref::Util qw/ is_plain_arrayref is_plain_hashref is_plain_scalarref /;
  1         2  
  1         140  
11              
12             # RECOMMEND PREREQ: Ref::Util::XS
13              
14 1     1   8 use namespace::clean;
  1         1  
  1         6  
15              
16             our $VERSION = 'v0.7.0';
17              
18              
19             sub _resolved_attrs {
20 25     25   1577545 my $rs = $_[0];
21              
22 25         221 $rs->next::method;
23              
24 25         7329 my $attrs = $rs->{_attrs};
25              
26 25 50       129 if ( my $conf = delete $attrs->{tablesample} ) {
27              
28 25         125 my $from = $attrs->{from};
29              
30 25 100       103 $conf = { fraction => $conf } unless is_plain_hashref($conf);
31              
32 25 50       70 $rs->throw_exception('tablesample must be a hashref')
33             unless is_plain_hashref($conf);
34              
35 25         180 my $sqla = $rs->result_source->storage->sql_maker;
36              
37 25         3047 my $part_sql = " tablesample";
38              
39 25 100 100     209 if (my $type = ($conf->{method} // $conf->{type})) {
40 13         47 $part_sql .= " $type";
41             }
42              
43 25         66 my $arg = $conf->{fraction};
44 25 100       103 $arg = $$arg if is_plain_scalarref($arg);
45 25         122 $part_sql .= "($arg)";
46              
47 25 100       100 if ( defined $conf->{repeatable} ) {
48 4         13 my $seed = $conf->{repeatable};
49 4 100       18 $seed = $$seed if is_plain_scalarref($seed);
50 4         23 $part_sql .= sprintf( ' repeatable (%s)', $seed );
51             }
52              
53 25 50       80 if (is_plain_arrayref($from)) {
54 25         178 my $sql = $sqla->_from_chunk_to_sql($from->[0]) . $sqla->_sqlcase($part_sql);
55 25         3432 $from->[0] = \$sql;
56             }
57             else {
58 0         0 my $sql = $sqla->_from_chunk_to_sql($from) . $sqla->_sqlcase($part_sql);
59 0         0 $attrs->{from} = \$sql;
60             }
61              
62             }
63              
64 25         288 return $attrs;
65             }
66              
67              
68             sub tablesample {
69 14     14 1 185447 my ( $rs, $frac, $options ) = @_;
70 14   50     57 $options //= {};
71 14 100       58 $options = { method => $options } unless is_plain_hashref($options);
72 14         106 return $rs->search_rs(
73             undef,
74             {
75             tablesample => {
76             fraction => $frac,
77             %$options
78             }
79             }
80             );
81             }
82              
83              
84             1;
85              
86             __END__