| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Database Object Interface - ~/lib/DB/Object/Constraint/Foreign.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2023 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2023/11/20 | ||
| 7 | ## Modified 2026/03/22 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package DB::Object::Constraint::Foreign; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use parent qw( Module::Generic ); | ||
| 20 | use vars qw( $VERSION $EXCEPTION_CLASS ); | ||
| 21 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub init | ||
| 29 | { | ||
| 30 | my $self = shift( @_ ); | ||
| 31 | $self->{expr} = undef; | ||
| 32 | $self->{fields} = undef; | ||
| 33 | $self->{match} = undef; | ||
| 34 | $self->{on_delete} = undef; | ||
| 35 | $self->{on_update} = undef; | ||
| 36 | $self->{name} = undef; | ||
| 37 | $self->{table} = undef; | ||
| 38 | $self->{_init_strict_use_sub} = 1; | ||
| 39 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 40 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 41 | return( $self ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub expr { return( shift->_set_get_scalar_as_object( 'expr', @_ ) ); } | ||
| 45 | |||
| 46 | sub fields { return( shift->_set_get_array_as_object( 'fields', @_ ) ); } | ||
| 47 | |||
| 48 | sub match { return( shift->_set_get_scalar_as_object( 'match', @_ ) ); } | ||
| 49 | |||
| 50 | sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); } | ||
| 51 | |||
| 52 | sub on_delete { return( shift->_set_get_scalar_as_object( 'on_delete', @_ ) ); } | ||
| 53 | |||
| 54 | sub on_update { return( shift->_set_get_scalar_as_object( 'on_update', @_ ) ); } | ||
| 55 | |||
| 56 | sub table { return( shift->_set_get_scalar_as_object( 'table', @_ ) ); } | ||
| 57 | |||
| 58 | # NOTE: For CBOR and Sereal | ||
| 59 | sub FREEZE | ||
| 60 | { | ||
| 61 | my $self = CORE::shift( @_ ); | ||
| 62 | my $serialiser = CORE::shift( @_ ) // ''; | ||
| 63 | my $class = CORE::ref( $self ); | ||
| 64 | |||
| 65 | my @props = qw( | ||
| 66 | expr fields match on_deete on_update name table | ||
| 67 | ); | ||
| 68 | |||
| 69 | my $hash = {}; | ||
| 70 | foreach my $prop ( @props ) | ||
| 71 | { | ||
| 72 | if( CORE::exists( $self->{ $prop } ) && | ||
| 73 | defined( $self->{ $prop } ) && | ||
| 74 | CORE::ref( $self->{ $prop } ) ne 'CODE' ) | ||
| 75 | { | ||
| 76 | $hash->{ $prop } = $self->{ $prop }; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | # Return an array reference rather than a list so this works with Sereal and CBOR. | ||
| 81 | # Before Sereal version 4.023, Sereal did not support multiple values returned. | ||
| 82 | if( $serialiser eq 'Sereal' ) | ||
| 83 | { | ||
| 84 | require Sereal::Encoder; | ||
| 85 | require version; | ||
| 86 | |||
| 87 | if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) ) | ||
| 88 | { | ||
| 89 | CORE::return( [$class, $hash] ); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | # But Storable wants a list with the first element being the serialised element | ||
| 94 | CORE::return( $class, $hash ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); } | ||
| 98 | |||
| 99 | sub STORABLE_thaw { return( shift->THAW( @_ ) ); } | ||
| 100 | |||
| 101 | sub STORABLE_thaw_post_processing | ||
| 102 | { | ||
| 103 | my $obj = shift( @_ ); | ||
| 104 | my @keys = %$obj; | ||
| 105 | my $class = ref( $obj ); | ||
| 106 | my $hash = {}; | ||
| 107 | @$hash{ @keys } = @$obj{ @keys }; | ||
| 108 | my $self = bless( $hash => $class ); | ||
| 109 | return( $self ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub THAW | ||
| 113 | { | ||
| 114 | # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue | ||
| 115 | # 'CBOR' as the second value. | ||
| 116 | my( $self, undef, @args ) = @_; | ||
| 117 | my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args; | ||
| 118 | my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self ); | ||
| 119 | my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {}; | ||
| 120 | my $new; | ||
| 121 | # Storable pattern requires to modify the object it created rather than returning a new one | ||
| 122 | if( CORE::ref( $self ) ) | ||
| 123 | { | ||
| 124 | foreach( CORE::keys( %$hash ) ) | ||
| 125 | { | ||
| 126 | $self->{ $_ } = CORE::delete( $hash->{ $_ } ); | ||
| 127 | } | ||
| 128 | $new = $self; | ||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | $new = CORE::bless( $hash => $class ); | ||
| 133 | } | ||
| 134 | CORE::return( $new ); | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | # NOTE: POD | ||
| 139 | __END__ | ||
| 140 | |||
| 141 | =encoding utf-8 | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | DB::Object::Constraint::Foreign - Table Foreign Key Constraint Class | ||
| 146 | |||
| 147 | =head1 SYNOPSIS | ||
| 148 | |||
| 149 | use DB::Object::Constraint::Foreign; | ||
| 150 | my $foreign = DB::Object::Constraint::Foreign->new( | ||
| 151 | expr => q{FOREIGN KEY (lang) REFERENCES language(lang) ON DELETE RESTRICT}, | ||
| 152 | fields => [qw( lang )], | ||
| 153 | match => 'simple', | ||
| 154 | on_delete => 'restrict', | ||
| 155 | on_update => 'nothing', | ||
| 156 | name => 'fk_user_info_lang', | ||
| 157 | table => 'language', | ||
| 158 | ) || die( DB::Object::Constraint::Foreign->error, "\n" ); | ||
| 159 | |||
| 160 | =head1 VERSION | ||
| 161 | |||
| 162 | v0.2.0 | ||
| 163 | |||
| 164 | =head1 DESCRIPTION | ||
| 165 | |||
| 166 | This class represents a table foreign key constraint. It is instantiated by the L<structure|DB::Object::Tables/structure> method when retrieving the table structure details. | ||
| 167 | |||
| 168 | =head1 CONSTRUCTOR | ||
| 169 | |||
| 170 | =head2 new | ||
| 171 | |||
| 172 | To instantiate new object, you can pass an hash or hash reference of properties matching the method names available below. | ||
| 173 | |||
| 174 | =head1 METHODS | ||
| 175 | |||
| 176 | =head2 expr | ||
| 177 | |||
| 178 | Sets or gets the foreign key constraint expression. | ||
| 179 | |||
| 180 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 181 | |||
| 182 | =head2 fields | ||
| 183 | |||
| 184 | Sets or gets an array reference of table field names associated with this constraint. | ||
| 185 | |||
| 186 | It returns a L<array object|Module::Generic::Array> | ||
| 187 | |||
| 188 | =head2 match | ||
| 189 | |||
| 190 | Sets or gets the method a foreign key constraint matches. | ||
| 191 | |||
| 192 | For example: C<full>, C<partial> and C<simple> | ||
| 193 | |||
| 194 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 195 | |||
| 196 | =head2 name | ||
| 197 | |||
| 198 | Sets or gets the foreign key constraint name. | ||
| 199 | |||
| 200 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 201 | |||
| 202 | =head2 on_delete | ||
| 203 | |||
| 204 | Sets or gets the action taken by the database upon deletion of this foreign key. | ||
| 205 | |||
| 206 | For example: C<nothing>, C<restrict>, C<cascade>, C<null> or C<default> | ||
| 207 | |||
| 208 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 209 | |||
| 210 | =head2 on_update | ||
| 211 | |||
| 212 | Sets or gets the action taken by the database upon update of this foreign key. | ||
| 213 | |||
| 214 | For example: C<nothing>, C<restrict>, C<cascade>, C<null> or C<default> | ||
| 215 | |||
| 216 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 217 | |||
| 218 | =head2 table | ||
| 219 | |||
| 220 | Sets or gets the table name for this foreign key. | ||
| 221 | |||
| 222 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 223 | |||
| 224 | =head1 AUTHOR | ||
| 225 | |||
| 226 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 227 | |||
| 228 | =head1 SEE ALSO | ||
| 229 | |||
| 230 | L<https://www.postgresql.org/docs/current/tutorial-fk.html> | ||
| 231 | |||
| 232 | =head1 COPYRIGHT & LICENSE | ||
| 233 | |||
| 234 | Copyright(c) 2023 DEGUEST Pte. Ltd. | ||
| 235 | |||
| 236 | All rights reserved | ||
| 237 | |||
| 238 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 239 | |||
| 240 | =cut |