blib/lib/DB/Object/Constraint/Index.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 153 CC 8 SCAR 23.2
CC
8
Cov
0%
THAW
30.0
FREEZE
20.0
BEGIN
2.0
CRAP 10.2
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 ## 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::Index;
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->{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__