File Coverage

blib/lib/DBIx/Class/Smooth/ResultBase.pm
Criterion Covered Total %
statement 14 36 38.8
branch 0 14 0.0
condition 0 3 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 19 60 31.6


line stmt bran cond sub pod time code
1 2     2   2242 use 5.20.0;
  2         7  
2 2     2   13 use strict;
  2         3  
  2         53  
3 2     2   11 use warnings;
  2         4  
  2         136  
4              
5             package DBIx::Class::Smooth::ResultBase;
6              
7             # ABSTRACT: Short intro
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.0101';
10              
11 2     2   23 use parent 'DBIx::Class::Core';
  2         12  
  2         16  
12              
13 2     2   196784 use experimental qw/signatures/;
  2         5  
  2         30  
14              
15 0     0 0   sub sqlt_deploy_hook($self, $table) {
  0            
  0            
  0            
16 0           my $indices = {};
17 0           for my $column_name ($self->columns) {
18 0           my $info = $self->column_info($column_name);
19              
20 0 0         if($info->{'indexed'}) {
21              
22 0 0         my $indexvalues = ref $info->{'indexed'} ne 'ARRAY' ? [ $info->{'indexed'} ] : $info->{'indexed'};
23              
24 0           for my $indexvalue (@$indexvalues) {
25              
26 0 0 0       if(length $indexvalue == 1 && $indexvalue) {
    0          
27 0           my $index_name = sprintf '%s_idxa_%s', $table, $column_name;
28 0           $indices->{ $index_name } = [$column_name];
29             }
30             elsif(length $indexvalue > 1) {
31 0           my $index_name = sprintf '%s_idxm_%s', $table, $indexvalue;
32              
33 0 0         if(!exists $indices->{ $index_name }) {
34 0           $indices->{ $index_name } = [];
35             }
36 0           push @{ $indices->{ $index_name } } => $column_name;
  0            
37             }
38             }
39             }
40             }
41              
42 0 0         if(scalar keys %$indices) {
43 0           for my $index_name (keys %$indices) {
44 0           $table->add_index(name => $index_name, fields => $indices->{ $index_name });
45             }
46             }
47 0 0         $self->next::method(@_) if $self->next::can;
48             }
49              
50             1;
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             DBIx::Class::Smooth::ResultBase - Short intro
61              
62             =head1 VERSION
63              
64             Version 0.0101, released 2018-11-29.
65              
66             =head1 SYNOPSIS
67              
68             # in MyApp::Schema::Result::YourResultClass, instead of inheriting from DBIx::Class::Core
69             use base 'DBIx::Class::Sweeten::Result::Base';
70              
71             # DBIx::Class::Candy is always nice
72             use DBIx::Class::Candy;
73              
74             column last_name => {
75             data_type => 'varchar',
76             size => 150,
77             indexed => 1,
78             };
79              
80             =head1 DESCRIPTION
81              
82             Adding indices (apart from primary keys and unique constraints) requires creating a C<sqlt_deploy_hook> method and calling C<add_index> manually. This module
83             adds the C<indexed> column attribute.
84              
85             =head2 Possible values
86              
87             C<indexed> behaves differently depending on the value it is given:
88              
89             =over 4
90              
91             =item *
92              
93             If given a one-character value, that evaluates to true, an index is created named C<[table_name]_idxa_[column_name]>.
94              
95             =item *
96              
97             If given a more-than-one-character value an index is created name C<[table_name]_idxm_[index_name]>. If multiple columns are given the same name a composite index is created.
98              
99             =item *
100              
101             If given an array reference each value in it is treated according to the two rules above.
102              
103             =back
104              
105             With these column definitions:
106              
107             table('Author');
108             column first_name => {
109             data_type => 'varchar',
110             size => 150,
111             indexed => 'name',
112             };
113             column last_name => {
114             data_type => 'varchar',
115             size => 150,
116             indexed => [1, 'name'],
117             };
118             column country => {
119             data_type => 'varchar',
120             size => 150,
121             indexed => 1,
122             };
123              
124             The following indices are created:
125              
126             =over 4
127              
128             =item *
129              
130             C<Author_idxm_name> for C<first_name> and C<last_name>
131              
132             =item *
133              
134             C<Author_idxa_last_name> for C<last_name>
135              
136             =item *
137              
138             C<Author_idxa_country> for C<country>
139              
140             =back
141              
142             =head2 Still need a custom sqlt_deploy_hook?
143              
144             If you need an C<sqlt_deploy_hook> method in a result source just call the parent's C<sqlt_deploy_hook> in your local sqlt_deploy_hook:
145              
146             sub sqlt_deploy_hook {
147             my $self = shift;
148             my $table = shift;
149              
150             $self->next::method($table);
151              
152             ...
153              
154             }
155              
156             =head1 SOURCE
157              
158             L<https://github.com/Csson/p5-DBIx-Class-Smooth>
159              
160             =head1 HOMEPAGE
161              
162             L<https://metacpan.org/release/DBIx-Class-Smooth>
163              
164             =head1 AUTHOR
165              
166             Erik Carlsson <info@code301.com>
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is copyright (c) 2018 by Erik Carlsson.
171              
172             This is free software; you can redistribute it and/or modify it under
173             the same terms as the Perl 5 programming language system itself.
174              
175             =cut