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