DBSchema/ForeignKey.pm

filter: Statementstmt 10.9% 6 / 55 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 7.6% 1 / 13 Podpod 100% 12 / 12 totaltotal 14.8% 19 / 128 SCAR 57.6
CC
19
Cov
6%
cmp
42.0
new
12.0
match
12.0
CRAP 316.5
« DBSchema.pm ↑ Summary DBSchema/DBD/SQLite.pm »
1 package DBIx::DBSchema::ForeignKey;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.13';
6 1 our $DEBUG = 0;
7  
8 =head1 NAME
9  
10 DBIx::DBSchema::ForeignKey - Foreign key objects
11  
12 =head1 SYNOPSIS
13  
14 use DBIx::DBSchema::ForeignKey;
15  
16 $foreign_key = new DBIx::DBSchema::ForeignKey (
17 { 'columns' => [ 'column_name' ],
18 'table' => 'foreign_table',
19 }
20 );
21  
22 $foreign_key = new DBIx::DBSchema::ForeignKey (
23 {
24 'constraint' => 'constraint_name',
25 'columns' => [ 'column_name', 'column2' ],
26 'table' => 'foreign_table',
27 'references' => [ 'foreign_column', 'foreign_column2' ],
28 'match' => 'MATCH FULL', # or MATCH SIMPLE
29 'on_delete' => 'NO ACTION', # on clauses: NO ACTION / RESTRICT /
30 'on_update' => 'RESTRICT', # CASCADE / SET NULL / SET DEFAULT
31 }
32 );
33  
34 =head1 DESCRIPTION
35  
36 DBIx::DBSchema::ForeignKey objects represent a foreign key.
37  
38 =head1 METHODS
39  
40 =over 4
41  
42 =item new HASHREF | OPTION, VALUE, ...
43  
44 Creates a new DBIx::DBschema::ForeignKey object.
45  
46 Accepts either a hashref or a list of options and values.
47  
48 Options are:
49  
50 =over 8
51  
52 =item constraint - constraint name
53  
54 =item columns - List reference of column names
55  
56 =item table - Foreign table name
57  
58 =item references - List reference of column names in foreign table
59  
60 =item match -
61  
62 =item on_delete -
63  
64 =item on_update -
65  
66 =back
67  
68 =cut
69  
70 sub new {
71 0 ▶ my $proto = shift;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
72 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
73 0 ▶ my %opt = ref($_[0]) ? %{$_[0]} : @_; #want a new reference
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
74 0 my $self = \%opt;
75 0 bless($self, $class);
76 }
77  
78 =item constraint [ CONSTRAINT_NAME ]
79  
80 Returns or sets the constraint name
81  
82 =cut
83  
84 sub constraint {
85 0 ▶ my($self, $value) = @_;
Subroutine constraint: not called CC=2 SCAR=17.9
Pod documented
86 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
87 0 $self->{constraint} = $value;
88 } else {
89 0 $self->{constraint};
90 }
91 }
92  
93 =item table [ TABLE_NAME ]
94  
95 Returns or sets the foreign table name
96  
97 =cut
98  
99 sub table {
100 0 ▶ my($self, $value) = @_;
Subroutine table: not called CC=2 SCAR=17.9
Pod documented
101 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
102 0 $self->{table} = $value;
103 } else {
104 0 $self->{table};
105 }
106 }
107  
108 =item columns [ LISTREF ]
109  
110 Returns or sets the columns.
111  
112 =cut
113  
114 sub columns {
115 0 ▶ my($self, $value) = @_;
Subroutine columns: not called CC=2 SCAR=17.9
Pod documented
116 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
117 0 $self->{columns} = $value;
118 } else {
119 0 $self->{columns};
120 }
121 }
122  
123 =item columns_sql
124  
125 Returns a comma-joined list of columns, suitable for an SQL statement.
126  
127 =cut
128  
129 sub columns_sql {
130 0 ▶ my $self = shift;
Subroutine columns_sql: not called CC=1 SCAR=6.9
Pod documented
131 0 join(', ', @{ $self->columns } );
132 }
133  
134 =item references [ LISTREF ]
135  
136 Returns or sets the referenced columns.
137  
138 =cut
139  
140 sub references {
141 0 ▶ my($self, $value) = @_;
Subroutine references: not called CC=2 SCAR=17.9
Pod documented
142 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
143 0 $self->{references} = $value;
144 } else {
145 0 $self->{references};
146 }
147 }
148  
149 =item references_sql
150  
151 Returns a comma-joined list of referenced columns, suitable for an SQL
152 statement.
153  
154 =cut
155  
156 sub references_sql {
157 0 ▶ my $self = shift;
Subroutine references_sql: not called CC=2 SCAR=17.9
Pod documented
158 0 ▶ join(', ', @{ $self->references || $self->columns } );
Branch: 1 branch
BranchTrueFalseTotal
$self->references or $self->columns 0 0 0
159 }
160  
161 =item match [ TABLE_NAME ]
162  
163 Returns or sets the MATCH clause
164  
165 =cut
166  
167 sub match {
168 0 ▶ my($self, $value) = @_;
Subroutine match: not called CC=3 SCAR=24.8
Pod documented
169 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
170 0 $self->{match} = $value;
171 } else {
172 0 ▶ defined($self->{match}) ? $self->{match} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'match'} ? : 0 0 0
173 }
174 }
175  
176 =item on_delete [ ACTION ]
177  
178 Returns or sets the ON DELETE clause
179  
180 =cut
181  
182 sub on_delete {
183 0 ▶ my($self, $value) = @_;
Subroutine on_delete: not called CC=3 SCAR=24.8
Pod documented
184 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
185 0 $self->{on_delete} = $value;
186 } else {
187 0 ▶ defined($self->{on_delete}) ? $self->{on_delete} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'on_delete'} ? : 0 0 0
188 }
189 }
190  
191 =item on_update [ ACTION ]
192  
193 Returns or sets the ON UPDATE clause
194  
195 =cut
196  
197 sub on_update {
198 0 ▶ my($self, $value) = @_;
Subroutine on_update: not called CC=3 SCAR=24.8
Pod documented
199 0 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
200 0 $self->{on_update} = $value;
201 } else {
202 0 ▶ defined($self->{on_update}) ? $self->{on_update} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'on_update'} ? : 0 0 0
203 }
204 }
205  
206 =item sql_foreign_key
207  
208 Returns an SQL FOREIGN KEY statement.
209  
210 =cut
211  
212 sub sql_foreign_key {
213 0 ▶ my( $self ) = @_;
Subroutine sql_foreign_key: not called CC=1 SCAR=6.9
Pod documented
214  
215 0 my $table = $self->table;
216 0 my $col_sql = $self->columns_sql;
217 0 my $ref_sql = $self->references_sql;
218  
219 "FOREIGN KEY ( $col_sql ) REFERENCES $table ( $ref_sql ) ".
220 0 join ' ', map { (my $thing_sql = uc($_) ) =~ s/_/ /g;
221 0 "$thing_sql ". $self->$_;
222 }
223 grep $self->$_, qw( match on_delete on_update );
224 }
225  
226 =item cmp OTHER_INDEX_OBJECT
227  
228 Compares this object to another supplied object. Returns true if they are
229 have the same table, columns and references.
230  
231 =cut
232  
233 sub cmp {
234 0 ▶ my( $self, $other ) = @_;
Subroutine cmp: not called CC=6 SCAR=37.4
Pod documented
235  
236 0 ▶ $self->table eq $other->table
Branch: 1 branch
BranchTrueFalseTotal
$self->table eq $other->table and $self->columns_sql eq $other->columns_sql and $self->references_sql eq $other->references_sql and uc $self->match eq uc $other->match and uc $self->on_delete eq uc $other->on_delete and uc $self->on_update eq uc $other->on_update 0 0 0
Condition0%0/12
$self->table eq $other->table and $self->columns_sql eq $other->columns_sql and $self->references_sql eq $other->references_sql and uc $self->match eq uc $other->match and uc $self->on_delete eq uc $other->on_delete
!ll&&!rl&&r
000
$self->table eq $other->table and $self->columns_sql eq $other->columns_sql and $self->references_sql eq $other->references_sql and uc $self->match eq uc $other->match
!ll&&!rl&&r
000
$self->table eq $other->table and $self->columns_sql eq $other->columns_sql and $self->references_sql eq $other->references_sql
!ll&&!rl&&r
000
$self->table eq $other->table and $self->columns_sql eq $other->columns_sql
!ll&&!rl&&r
000
MC/DC0%0/5
$self->table eq $other->table and $self->columns_sql eq $other->columns_sql and $self->references_sql eq $other->references_sql and uc $self->match eq uc $other->match and uc $self->on_delete eq uc $other->on_delete
$self->table eq $other->table $self->columns_sql eq $other->columns_sql $self->references_sql eq $other->references_sql uc $self->match eq uc $other->match uc $self->on_delete eq uc $other->on_delete
Truth table0%0/6
A and B and C and D and E
A= $self->table eq $other->tableB= $self->columns_sql eq $other->columns_sqlC= $self->references_sql eq $other->references_sqlD= uc $self->match eq uc $other->matchE= uc $self->on_delete eq uc $other->on_delete
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
237 and $self->columns_sql eq $other->columns_sql
238 and $self->references_sql eq $other->references_sql
239 and uc($self->match) eq uc($other->match)
240 and uc($self->on_delete) eq uc($other->on_delete)
241 and uc($self->on_update) eq uc($other->on_update)
242 ;
243 }
244  
245 =back
246  
247 =head1 AUTHOR
248  
249 Ivan Kohler <ivan-dbix-dbschema@420.am>
250  
251 Copyright (c) 2013 Freeside Internet Services, Inc.
252 All rights reserved.
253 This program is free software; you can redistribute it and/or modify it under
254 the same terms as Perl itself.
255  
256 =head1 BUGS
257  
258 Should give in and Mo or Moo.
259  
260 =head1 SEE ALSO
261  
262 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBI>
263  
264 =cut
265  
266 1 1;
267  
268  
« DBSchema.pm ↑ Summary DBSchema/DBD/SQLite.pm »