File Coverage

blib/lib/DBICx/Indexing.pm
Criterion Covered Total %
statement 41 41 100.0
branch 13 14 92.8
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 62 63 98.4


line stmt bran cond sub pod time code
1             package DBICx::Indexing;
2             our $VERSION = '0.002';
3              
4 5     5   815016 use strict;
  5         12  
  5         248  
5 5     5   27 use warnings;
  5         10  
  5         176  
6 5     5   28 use base qw/DBIx::Class/;
  5         18  
  5         4849  
7              
8             __PACKAGE__->mk_classdata('_dbicx_indexing');
9              
10             sub indices {
11 6     6 1 305831 my $class = shift;
12              
13 6 100       29 if (@_) {
14 4         8 my $idxs;
15 4 100       16 if (scalar(@_) == 1) { $idxs = $_[0] }
  2         5  
16 2         11 else { $idxs = {@_} }
17              
18 4         14 for my $cols (values %$idxs) {
19 16 100       49 $cols = [$cols] unless ref $cols eq 'ARRAY';
20             }
21              
22 4         154 $class->_dbicx_indexing($idxs);
23             }
24              
25 6         271 return $class->_dbicx_indexing;
26             }
27              
28             sub sqlt_deploy_hook {
29 2     2 1 473388 my $self = shift;
30 2         5 my ($table) = @_;
31              
32 2 50       15 $self->next::method(@_) if $self->next::can;
33              
34 2         182 my $indexes = $self->_dbicx_indexing;
35 2         93 for my $name (keys %$indexes) {
36 8         3356 my $cols = $indexes->{$name};
37              
38 8 100       18 next if _has_cover_index($table, $cols);
39              
40 4         16 $table->add_index(name => $name, fields => $cols);
41             }
42             }
43              
44             sub _has_cover_index {
45 17     17   33099 my ($table, $cols) = @_;
46              
47 17         27 my @idxs;
48 17         49 push @idxs, map { [$_->fields] } $table->get_indices;
  23         1483  
49 17         1317 push @idxs, map { [$_->field_names] } $table->unique_constraints;
  17         5585  
50 17         1299 push @idxs, [$table->primary_key->field_names];
51              
52 17         4574 IDXS: for my $flst (@idxs) {
53 44         501 for my $c (@$cols) {
54 59 100       117 next IDXS unless @$flst;
55 54 100       144 next IDXS unless $flst->[0] eq $c;
56              
57 24         48 shift @$flst;
58             }
59              
60 9         57 return 1;
61             }
62              
63 8         38 return 0;
64             }
65              
66             1;
67              
68             __END__