1 package DBIx::DBSchema::ForeignKey;
10 DBIx::DBSchema::ForeignKey - Foreign key objects
14 use DBIx::DBSchema::ForeignKey;
16 $foreign_key = new DBIx::DBSchema::ForeignKey (
17 { 'columns' => [ 'column_name' ],
18 'table' => 'foreign_table',
22 $foreign_key = new DBIx::DBSchema::ForeignKey (
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
36 DBIx::DBSchema::ForeignKey objects represent a foreign key.
42 =item new HASHREF | OPTION, VALUE, ...
44 Creates a new DBIx::DBschema::ForeignKey object.
46 Accepts either a hashref or a list of options and values.
52 =item constraint - constraint name
54 =item columns - List reference of column names
56 =item table - Foreign table name
58 =item references - List reference of column names in foreign table
72 my $class = ref($proto) || $proto;
73 my %opt = ref($_[0]) ? %{$_[0]} : @_; #want a new reference
78 =item constraint [ CONSTRAINT_NAME ]
80 Returns or sets the constraint name
85 my($self, $value) = @_;
86 if ( defined($value) ) {
87 $self->{constraint} = $value;
93 =item table [ TABLE_NAME ]
95 Returns or sets the foreign table name
100 my($self, $value) = @_;
101 if ( defined($value) ) {
102 $self->{table} = $value;
108 =item columns [ LISTREF ]
110 Returns or sets the columns.
115 my($self, $value) = @_;
116 if ( defined($value) ) {
117 $self->{columns} = $value;
125 Returns a comma-joined list of columns, suitable for an SQL statement.
131 join(', ', @{ $self->columns } );
134 =item references [ LISTREF ]
136 Returns or sets the referenced columns.
141 my($self, $value) = @_;
142 if ( defined($value) ) {
143 $self->{references} = $value;
151 Returns a comma-joined list of referenced columns, suitable for an SQL
158 join(', ', @{ $self->references || $self->columns } );
161 =item match [ TABLE_NAME ]
163 Returns or sets the MATCH clause
168 my($self, $value) = @_;
169 if ( defined($value) ) {
170 $self->{match} = $value;
172 defined($self->{match}) ? $self->{match} : '';
176 =item on_delete [ ACTION ]
178 Returns or sets the ON DELETE clause
183 my($self, $value) = @_;
184 if ( defined($value) ) {
185 $self->{on_delete} = $value;
187 defined($self->{on_delete}) ? $self->{on_delete} : '';
191 =item on_update [ ACTION ]
193 Returns or sets the ON UPDATE clause
198 my($self, $value) = @_;
199 if ( defined($value) ) {
200 $self->{on_update} = $value;
202 defined($self->{on_update}) ? $self->{on_update} : '';
206 =item sql_foreign_key
208 Returns an SQL FOREIGN KEY statement.
212 sub sql_foreign_key {
215 my $table = $self->table;
216 my $col_sql = $self->columns_sql;
217 my $ref_sql = $self->references_sql;
219 "FOREIGN KEY ( $col_sql ) REFERENCES $table ( $ref_sql ) ".
220 join ' ', map { (my $thing_sql = uc($_) ) =~ s/_/ /g;
221 "$thing_sql ". $self->$_;
223 grep $self->$_, qw( match on_delete on_update );
226 =item cmp OTHER_INDEX_OBJECT
228 Compares this object to another supplied object. Returns true if they are
229 have the same table, columns and references.
234 my( $self, $other ) = @_;
236 $self->table eq $other->table
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)
249 Ivan Kohler <ivan-dbix-dbschema@420.am>
251 Copyright (c) 2013 Freeside Internet Services, Inc.
253 This program is free software; you can redistribute it and/or modify it under
254 the same terms as Perl itself.
258 Should give in and Mo or Moo.
262 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBI>