| 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 | ## This program is free software; you can redistribute it and/or modify it | ||
| 11 | ## under the same terms as Perl itself. | ||
| 12 | ##---------------------------------------------------------------------------- | ||
| 13 | package DB::Object::Constraint::Foreign; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | warnings::register_categories( 'DB::Object' ); | ||
| 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__ |