File: ForeignKey.pm

package info (click to toggle)
libdbix-dbschema-perl 0.47-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 244 kB
  • sloc: perl: 1,686; makefile: 2
file content (268 lines) | stat: -rw-r--r-- 4,843 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package DBIx::DBSchema::ForeignKey;

use strict;

our $VERSION = '0.13';
our $DEBUG = 0;

=head1 NAME

DBIx::DBSchema::ForeignKey - Foreign key objects

=head1 SYNOPSIS

  use DBIx::DBSchema::ForeignKey;

  $foreign_key = new DBIx::DBSchema::ForeignKey (
    { 'columns' => [ 'column_name' ],
      'table'   => 'foreign_table',
    }
  );

  $foreign_key = new DBIx::DBSchema::ForeignKey (
    {
      'constraint' => 'constraint_name',
      'columns'    => [ 'column_name', 'column2' ],
      'table'      => 'foreign_table',
      'references' => [ 'foreign_column', 'foreign_column2' ],
      'match'      => 'MATCH FULL', # or MATCH SIMPLE
      'on_delete'  => 'NO ACTION', # on clauses: NO ACTION / RESTRICT /
      'on_update'  => 'RESTRICT',  #           CASCADE / SET NULL / SET DEFAULT
    }
  );

=head1 DESCRIPTION

DBIx::DBSchema::ForeignKey objects represent a foreign key.

=head1 METHODS

=over 4

=item new HASHREF | OPTION, VALUE, ...

Creates a new DBIx::DBschema::ForeignKey object.

Accepts either a hashref or a list of options and values.

Options are:

=over 8

=item constraint - constraint name

=item columns - List reference of column names

=item table - Foreign table name

=item references - List reference of column names in foreign table

=item match - 

=item on_delete - 

=item on_update - 

=back

=cut

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my %opt = ref($_[0]) ? %{$_[0]} : @_; #want a new reference
  my $self = \%opt;
  bless($self, $class);
}

=item constraint [ CONSTRAINT_NAME ]

Returns or sets the constraint name

=cut

sub constraint {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{constraint} = $value;
  } else {
    $self->{constraint};
  }
}

=item table [ TABLE_NAME ]

Returns or sets the foreign table name

=cut

sub table {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{table} = $value;
  } else {
    $self->{table};
  }
}

=item columns [ LISTREF ]

Returns or sets the columns.

=cut

sub columns {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{columns} = $value;
  } else {
    $self->{columns};
  }
}

=item columns_sql

Returns a comma-joined list of columns, suitable for an SQL statement.

=cut

sub columns_sql {
  my $self = shift;
  join(', ', @{ $self->columns } );
}

=item references [ LISTREF ]

Returns or sets the referenced columns.

=cut

sub references {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{references} = $value;
  } else {
    $self->{references};
  }
}

=item references_sql

Returns a comma-joined list of referenced columns, suitable for an SQL
statement.

=cut

sub references_sql {
  my $self = shift;
  join(', ', @{ $self->references || $self->columns } );
}

=item match [ TABLE_NAME ]

Returns or sets the MATCH clause

=cut

sub match {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{match} = $value;
  } else {
    defined($self->{match}) ? $self->{match} : '';
  }
}

=item on_delete [ ACTION ]

Returns or sets the ON DELETE clause

=cut

sub on_delete {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{on_delete} = $value;
  } else {
    defined($self->{on_delete}) ? $self->{on_delete} : '';
  }
}

=item on_update [ ACTION ]

Returns or sets the ON UPDATE clause

=cut

sub on_update {
  my($self, $value) = @_;
  if ( defined($value) ) {
    $self->{on_update} = $value;
  } else {
    defined($self->{on_update}) ? $self->{on_update} : '';
  }
}

=item sql_foreign_key

Returns an SQL FOREIGN KEY statement.

=cut

sub sql_foreign_key {
  my( $self ) = @_;

  my $table = $self->table;
  my $col_sql = $self->columns_sql;
  my $ref_sql = $self->references_sql;

  "FOREIGN KEY ( $col_sql ) REFERENCES $table ( $ref_sql ) ".
    join ' ', map { (my $thing_sql = uc($_) ) =~ s/_/ /g;
                    "$thing_sql ". $self->$_;
                  }
                grep $self->$_, qw( match on_delete on_update );
}

=item cmp OTHER_INDEX_OBJECT

Compares this object to another supplied object.  Returns true if they are
have the same table, columns and references.

=cut

sub cmp {
  my( $self, $other ) = @_;

  $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)
  ;
}

=back

=head1 AUTHOR

Ivan Kohler <ivan-dbix-dbschema@420.am>

Copyright (c) 2013 Freeside Internet Services, Inc.
All rights reserved.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

=head1 BUGS

Should give in and Mo or Moo.

=head1 SEE ALSO

L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBI>

=cut

1;