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 foreign table 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;
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;
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;
208 =item sql_foreign_key
210 Returns an SQL FOREIGN KEY statement.
214 sub sql_foreign_key {
217 my $table = $self->table;
218 my $col_sql = $self->columns_sql;
219 my $ref_sql = $self->references_sql;
221 "FOREIGN KEY ( $col_sql ) REFERENCES $table ( $ref_sql ) ".
222 join ' ', grep $_, map $self->$_, qw( match on_delete on_update );
225 =item cmp OTHER_INDEX_OBJECT
227 Compares this object to another supplied object. Returns true if they are
228 have the same table, columns and references.
233 my( $self, $other ) = @_;
235 $self->table eq $other->table
236 and $self->columns_sql eq $other->columns_sql
237 and $self->references_sql eq $other->references_sql
245 Ivan Kohler <ivan-dbix-dbschema@420.am>
247 Copyright (c) 2013 Freeside Internet Services, Inc.
249 This program is free software; you can redistribute it and/or modify it under
250 the same terms as Perl itself.
254 Should give in and Mo or Moo.
258 L<DBIx::DBSchema::Table>, L<DBIx::DBSchema>, L<DBI>