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 (
23 { 'columns' => [ 'column_name', 'column2' ],
24 'table' => 'foreign_table',
25 'references' => [ 'foreign_column', 'foreign_column2' ],
26 'match' => 'MATCH FULL', # or MATCH SIMPLE
27 'on_delete' => 'NO ACTION', # on clauses: NO ACTION / RESTRICT /
28 'on_update' => 'RESTRICT', # CASCADE / SET NULL / SET DEFAULT
34 DBIx::DBSchema::ForeignKey objects represent a foreign key.
40 =item new HASHREF | OPTION, VALUE, ...
42 Creates a new DBIx::DBschema::ForeignKey object.
44 Accepts either a hashref or a list of options and values.
50 =item constraint - constraint name
52 =item columns - List reference of column names
54 =item table - Foreign table name
56 =item references - List reference of column names in foreign table
70 my $class = ref($proto) || $proto;
71 my %opt = ref($_[0]) ? %{$_[0]} : @_; #want a new reference
76 =item constraint [ CONSTRAINT_NAME ]
78 Returns or sets the foreign table name
83 my($self, $value) = @_;
84 if ( defined($value) ) {
85 $self->{constraint} = $value;
91 =item table [ TABLE_NAME ]
93 Returns or sets the foreign table name
98 my($self, $value) = @_;
99 if ( defined($value) ) {
100 $self->{table} = $value;
106 =item columns [ LISTREF ]
108 Returns or sets the columns.
113 my($self, $value) = @_;
114 if ( defined($value) ) {
115 $self->{columns} = $value;
123 Returns a comma-joined list of columns, suitable for an SQL statement.
129 join(', ', @{ $self->columns } );
132 =item references [ LISTREF ]
134 Returns or sets the referenced columns.
139 my($self, $value) = @_;
140 if ( defined($value) ) {
141 $self->{references} = $value;
149 Returns a comma-joined list of referenced columns, suitable for an SQL
156 join(', ', @{ $self->references || $self->columns } );
159 =item match [ TABLE_NAME ]
161 Returns or sets the MATCH clause
166 my($self, $value) = @_;
167 if ( defined($value) ) {
168 $self->{match} = $value;
174 =item on_delete [ ACTION ]
176 Returns or sets the ON DELETE clause
181 my($self, $value) = @_;
182 if ( defined($value) ) {
183 $self->{on_delete} = $value;
189 =item on_update [ ACTION ]
191 Returns or sets the ON UPDATE clause
196 my($self, $value) = @_;
197 if ( defined($value) ) {
198 $self->{on_update} = $value;
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 ' ', grep $_, map $self->$_, qw( match on_delete on_update );
223 =item cmp OTHER_INDEX_OBJECT
225 Compares this object to another supplied object. Returns true if they are
226 have the same table, columns and references.
231 my( $self, $other ) = @_;
233 $self->table eq $other->table
234 and $self->columns_sql eq $other->columns_sql
235 and $self->references_sql eq $other->references_sql
243 Ivan Kohler <ivan-dbix-dbschema@420.am>
245 Copyright (c) 2013 Freeside Internet Services, Inc.
247 This program is free software; you can redistribute it and/or modify it under
248 the same terms as Perl itself.
252 Should give in and Mo or Moo.
256 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBI>