| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Database Object Interface - ~/lib/DB/Object/Constraint/Check.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::Check; | ||
| 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->{name} = undef; | ||
| 34 | $self->{_init_strict_use_sub} = 1; | ||
| 35 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 36 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 37 | return( $self ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub expr { return( shift->_set_get_scalar_as_object( 'expr', @_ ) ); } | ||
| 41 | |||
| 42 | sub fields { return( shift->_set_get_array_as_object( 'fields', @_ ) ); } | ||
| 43 | |||
| 44 | sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); } | ||
| 45 | |||
| 46 | # NOTE: For CBOR and Sereal | ||
| 47 | sub FREEZE | ||
| 48 | { | ||
| 49 | my $self = CORE::shift( @_ ); | ||
| 50 | my $serialiser = CORE::shift( @_ ) // ''; | ||
| 51 | my $class = CORE::ref( $self ); | ||
| 52 | |||
| 53 | my @props = qw( | ||
| 54 | expr fields name | ||
| 55 | ); | ||
| 56 | |||
| 57 | my $hash = {}; | ||
| 58 | foreach my $prop ( @props ) | ||
| 59 | { | ||
| 60 | if( CORE::exists( $self->{ $prop } ) && | ||
| 61 | defined( $self->{ $prop } ) && | ||
| 62 | CORE::ref( $self->{ $prop } ) ne 'CODE' ) | ||
| 63 | { | ||
| 64 | $hash->{ $prop } = $self->{ $prop }; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | # Return an array reference rather than a list so this works with Sereal and CBOR. | ||
| 69 | # Before Sereal version 4.023, Sereal did not support multiple values returned. | ||
| 70 | if( $serialiser eq 'Sereal' ) | ||
| 71 | { | ||
| 72 | require Sereal::Encoder; | ||
| 73 | require version; | ||
| 74 | |||
| 75 | if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) ) | ||
| 76 | { | ||
| 77 | CORE::return( [$class, $hash] ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | # But Storable wants a list with the first element being the serialised element | ||
| 82 | CORE::return( $class, $hash ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); } | ||
| 86 | |||
| 87 | sub STORABLE_thaw { return( shift->THAW( @_ ) ); } | ||
| 88 | |||
| 89 | sub STORABLE_thaw_post_processing | ||
| 90 | { | ||
| 91 | my $obj = shift( @_ ); | ||
| 92 | my @keys = %$obj; | ||
| 93 | my $class = ref( $obj ); | ||
| 94 | my $hash = {}; | ||
| 95 | @$hash{ @keys } = @$obj{ @keys }; | ||
| 96 | my $self = bless( $hash => $class ); | ||
| 97 | return( $self ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub THAW | ||
| 101 | { | ||
| 102 | # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue | ||
| 103 | # 'CBOR' as the second value. | ||
| 104 | my( $self, undef, @args ) = @_; | ||
| 105 | my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args; | ||
| 106 | my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self ); | ||
| 107 | my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {}; | ||
| 108 | my $new; | ||
| 109 | # Storable pattern requires to modify the object it created rather than returning a new one | ||
| 110 | if( CORE::ref( $self ) ) | ||
| 111 | { | ||
| 112 | foreach( CORE::keys( %$hash ) ) | ||
| 113 | { | ||
| 114 | $self->{ $_ } = CORE::delete( $hash->{ $_ } ); | ||
| 115 | } | ||
| 116 | $new = $self; | ||
| 117 | } | ||
| 118 | else | ||
| 119 | { | ||
| 120 | $new = CORE::bless( $hash => $class ); | ||
| 121 | } | ||
| 122 | CORE::return( $new ); | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | # NOTE: POD | ||
| 127 | __END__ | ||
| 128 | |||
| 129 | =encoding utf-8 | ||
| 130 | |||
| 131 | =head1 NAME | ||
| 132 | |||
| 133 | DB::Object::Constraint::Check - Table Check Constraint Class | ||
| 134 | |||
| 135 | =head1 SYNOPSIS | ||
| 136 | |||
| 137 | use DB::Object::Constraint::Check; | ||
| 138 | my $check = DB::Object::Constraint::Check->new( | ||
| 139 | expr => q{CHECK (status::text ~* '^(active|inactive|locked|pending|protected|removed|suspended)$'::text)}, | ||
| 140 | fields => [qw( status )], | ||
| 141 | name => 'chk_users_status', | ||
| 142 | ) || die( DB::Object::Constraint::Check->error, "\n" ); | ||
| 143 | |||
| 144 | =head1 VERSION | ||
| 145 | |||
| 146 | v0.2.0 | ||
| 147 | |||
| 148 | =head1 DESCRIPTION | ||
| 149 | |||
| 150 | This class represents a table check constraint. It is instantiated by the L<structure|DB::Object::Tables/structure> method when retrieving the table structure details. | ||
| 151 | |||
| 152 | =head1 CONSTRUCTOR | ||
| 153 | |||
| 154 | =head2 new | ||
| 155 | |||
| 156 | To instantiate new object, you can pass an hash or hash reference of properties matching the method names available below. | ||
| 157 | |||
| 158 | =head1 METHODS | ||
| 159 | |||
| 160 | =head2 expr | ||
| 161 | |||
| 162 | Sets or gets a check constraint expression. | ||
| 163 | |||
| 164 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 165 | |||
| 166 | =head2 fields | ||
| 167 | |||
| 168 | Sets or gets an array reference of table field names associated with this constraint. | ||
| 169 | |||
| 170 | It returns a L<array object|Module::Generic::Array> | ||
| 171 | |||
| 172 | =head2 name | ||
| 173 | |||
| 174 | Sets or gets the check constraint name. | ||
| 175 | |||
| 176 | It returns a L<scalar object|Module::Generic::Scalar> | ||
| 177 | |||
| 178 | =head1 AUTHOR | ||
| 179 | |||
| 180 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 181 | |||
| 182 | =head1 SEE ALSO | ||
| 183 | |||
| 184 | L<perl> | ||
| 185 | |||
| 186 | =head1 COPYRIGHT & LICENSE | ||
| 187 | |||
| 188 | Copyright(c) 2023 DEGUEST Pte. Ltd. | ||
| 189 | |||
| 190 | All rights reserved | ||
| 191 | |||
| 192 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 193 | |||
| 194 | =cut |