File Coverage

lib/DBIx/Schema/Changelog/Action/Constraint.pm
Criterion Covered Total %
statement 64 64 100.0
branch 21 28 75.0
condition 8 9 88.8
subroutine 13 13 100.0
pod 3 3 100.0
total 109 117 93.1


line stmt bran cond sub pod time code
1             package DBIx::Schema::Changelog::Action::Constraint;
2              
3             =head1 NAME
4              
5             DBIx::Schema::Changelog::Action::Constraint - Action handler for constraint
6              
7             =head1 VERSION
8              
9             Version 0.7.1
10              
11             =cut
12              
13             our $VERSION = '0.7.1';
14              
15 7     7   2139 use strict;
  7         79  
  7         262  
16 7     7   31 use warnings;
  7         16  
  7         189  
17 7     7   31 use Data::Dumper;
  7         9  
  7         392  
18 7     7   31 use Moose;
  7         10  
  7         43  
19 7     7   39723 use Method::Signatures::Simple;
  7         12653  
  7         60  
20 7     7   6405 use DBIx::Schema::Changelog::Action::Default;
  7         19  
  7         427  
21              
22             with 'DBIx::Schema::Changelog::Action';
23              
24             has default => (
25             is => 'rw',
26             lazy => 1,
27             does => 'DBIx::Schema::Changelog::Action',
28             default => method {
29             DBIx::Schema::Changelog::Action::Default->new(
30             driver => $self->driver(),
31             dbh => $self->dbh()
32             )
33             },
34             );
35              
36             =head1 SUBROUTINES/METHODS
37              
38             =head2 add
39 103     103 1 1756  
40 103 50       280 =cut
41              
42 103         4432 sub add {
43 103 100 100     389 my ( $self, $col, $constr_ref, $debug ) = @_;
44 1         6 print __PACKAGE__, ' (', __LINE__, ') ', $/, Dumper( $col, $constr_ref )
45 1         5 if ($debug);
46             my $consts = $self->driver()->constraints;
47 102 100       245 if ( defined $col->{primarykey} && ref $col->{primarykey} eq 'ARRAY' ) {
48 1         7 push( @$constr_ref, $self->_primary($col) );
49 1         5 return;
50             }
51             if ( defined $col->{unique} ) {
52 101 100       202 push( @$constr_ref, $self->_unique($col) );
53 101 100 100     432 return;
54             }
55              
56 101 50 66     251 my $must_nn = ( defined $col->{primarykey} ) ? 1 : 0;
57 101 100       246 my $isnt_nn =
58             ( !defined $col->{default} && !defined $col->{foreign} ) ? 1 : 0;
59 101 100       262  
60 101 100       186 die "No default value set for $col->{name}" if ( $must_nn && $isnt_nn );
61             push( @$constr_ref, $self->_foreign($col) ) if ( defined $col->{foreign} );
62 101         4332  
63 101         426 my $not_null = ( $col->{notnull} ) ? $consts->{not_null} : '';
64             my $primarykey =
65             ( defined $col->{primarykey} ) ? $consts->{primary_key} : '';
66             my $default = $self->default()->add( $col, $debug );
67             return qq~$not_null $primarykey $default~;
68             }
69              
70             =head2 alter
71 1     1 1 878  
72             =cut
73              
74             sub alter {
75             my ( $self, $table_name, $col, $constr_ref ) = @_;
76              
77             #$self->table_action()->add($_) if (uc $constraint->{type} eq 'NOT_NULL' );
78             #$self->table_action()->drop($_) if (uc $constraint_->{type} eq 'UNIQUE' );
79             #$self->table_action()->alter($_) if (uc $constraint_->{type} eq 'PRIMARY' );
80             #$self->index_action()->add($_) if (uc $constraint_->{type} eq 'FOREIGN' );
81             #$self->index_action()->alter($_) if (uc $constraint_->{type} eq 'CHECK' );
82             #$self->index_action()->drop($_) if (uc $constraint_->{type} eq 'DEFAULT' );
83             }
84              
85             =head2 drop
86 1     1 1 7  
87             =cut
88              
89             sub drop {
90             my ( $self, $table_name, $col, $constraints ) = @_;
91              
92             #$self->table_action()->add($_) if (uc $constraint->{type} eq 'NOT_NULL' );
93             #$self->table_action()->drop($_) if (uc $constraint_->{type} eq 'UNIQUE' );
94             #$self->table_action()->alter($_) if (uc $constraint_->{type} eq 'PRIMARY' );
95             #$self->index_action()->add($_) if (uc $constraint_->{type} eq 'FOREIGN' );
96             #$self->index_action()->alter($_) if (uc $constraint_->{type} eq 'CHECK' );
97             #$self->index_action()->drop($_) if (uc $constraint_->{type} eq 'DEFAULT' );
98             }
99              
100             =head1 AUXILIARY SUBROUTINES/METHODS
101              
102             =head2 _foreign
103              
104             Private sub to handle foreign keys constraints
105 20     20   41  
106 20         905 =cut
107 20 50       86  
108 20         49 sub _foreign {
109 20         44 my ( $self, $col, $constr_ref ) = @_;
110 20         44 my $actions = $self->driver()->actions;
111 20         45 die "Foreign key is not supported!", $/ unless $actions->{foreign_key};
112             my $table = '' . $col->{table};
113 20         37 my $ref_table = $col->{foreign}->{reftable};
114 20         43 my $name = $col->{name};
115 20         39 my $refcolumn = $col->{foreign}->{refcolumn};
116              
117 20         183 $table =~ s/"//g;
118             $ref_table =~ s/"//g;
119             $name =~ s/"//g;
120              
121             return _replace_spare(
122             $actions->{foreign_key},
123             [
124             $col->{name}, $ref_table,
125             $col->{foreign}->{refcolumn},
126             "fkey_$table" . "_$refcolumn" . "_$name"
127             ]
128             );
129             }
130              
131             =head2 _unique
132              
133             Private sub to handle unique constraints
134 1     1   2  
135 1         56 =cut
136 1 50       7  
137 1 50       9 sub _unique {
138 1         4 my ( $self, $col, $constr_ref ) = @_;
139 1 50       5 my $actions = $self->driver()->actions;
140 1         12 return unless $actions->{unique};
141 1         6 my $table = ( defined $col->{table} ) ? $col->{table} . '' : '';
142             $table =~ s/"//g;
143             my $name = ( defined $col->{name} ) ? $col->{name} : time();
144             return _replace_spare( $actions->{unique},
145             [ qq~unique_$name~, join( ',', @{ $col->{unique} } ) ] );
146             }
147              
148             =head2 _primary
149              
150             Private sub to to handle primary key with more than one column
151 1     1   2  
152 1         56 =cut
153 1 50       7  
154 1         9 sub _primary {
155 1         5 my ( $self, $col, $constr_ref ) = @_;
156             my $actions = $self->driver()->actions;
157             my $name = ( defined $col->{name} ) ? $col->{name} : time() . '_gen';
158 7     7   6969 return _replace_spare( $actions->{primary},
  7         25  
  7         47  
159             [ qq~pkay_multi_$name~, join( ',', @{ $col->{primarykey} } ) ] );
160             }
161              
162             no Moose;
163             __PACKAGE__->meta->make_immutable;
164              
165             1;
166              
167             __END__
168              
169             =head1 AUTHOR
170              
171             Mario Zieschang, C<< <mario.zieschang at combase.de> >>
172              
173             =head1 LICENSE AND COPYRIGHT
174              
175             Copyright 2015 Mario Zieschang.
176              
177             This program is free software; you can redistribute it and/or modify it
178             under the terms of the the Artistic License (2.0). You may obtain a
179             copy of the full license at:
180              
181             L<http://www.perlfoundation.org/artistic_license_2_0>
182              
183             Any use, modification, and distribution of the Standard or Modified
184             Versions is governed by this Artistic License. By using, modifying or
185             distributing the Package, you accept this license. Do not use, modify,
186             or distribute the Package, if you do not accept this license.
187              
188             If your Modified Version has been derived from a Modified Version made
189             by someone other than you, you are nevertheless required to ensure that
190             your Modified Version complies with the requirements of this license.
191              
192             This license does not grant you the right to use any trademark, service
193             mark, trade name, or logo of the Copyright Holder.
194              
195             This license includes the non-exclusive, worldwide, free-of-charge
196             patent license to make, have made, use, offer to sell, sell, import and
197             otherwise transfer the Package with respect to any patent claims
198             licensable by the Copyright Holder that are necessarily infringed by the
199             Package. If you institute patent litigation (including a cross-claim or
200             counterclaim) against any party alleging that the Package constitutes
201             direct or contributory patent infringement, then this Artistic License
202             to you shall terminate on the date that such litigation is filed.
203              
204             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
205             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
206             THE IMPLIED WARRANTIES OF MERCHANT ABILITY, FITNESS FOR A PARTICULAR
207             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
208             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
209             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
210             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
211             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
212              
213             =cut