blib/lib/DB/Object/Fields/Unknown.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 160 CC 8 SCAR 22.9
CC
8
Cov
0%
THAW
30.0
FREEZE
20.0
BEGIN
2.0
CRAP 9.9
1 ##----------------------------------------------------------------------------
2 ## Database Object Interface - ~/lib/DB/Object/Fields/Unknown.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/12/21
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::Fields::Unknown;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 use parent qw( Module::Generic );
20 use vars qw( $VERSION $EXCEPTION_CLASS );
21 use overload (
22 # '""' => \&as_string,
23 'bool' => sub{$_[0]},
24 '+' => sub{ $_[0] },
25 '-' => sub{ $_[0] },
26 '*' => sub{ $_[0] },
27 '/' => sub{ $_[0] },
28 '%' => sub{ $_[0] },
29 '<' => sub{ $_[0] },
30 '>' => sub{ $_[0] },
31 '<=' => sub{ $_[0] },
32 '>=' => sub{ $_[0] },
33 '!=' => sub{ $_[0] },
34 '<<' => sub{ $_[0] },
35 '>>' => sub{ $_[0] },
36 '&' => sub{ $_[0] },
37 '^' => sub{ $_[0] },
38 '|' => sub{ $_[0] },
39 '==' => sub{ $_[0] },
40 fallback => 1,
41 );
42 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
43 our $VERSION = 'v0.2.0';
44 };
45  
46 use strict;
47 use warnings;
48  
49 sub init
50 {
51 my $self = shift( @_ );
52 $self->{error} = undef;
53 $self->{field} = undef;
54 $self->{table} = undef;
55 $self->{_init_strict_use_sub} = 1;
56 $self->{_exception_class} = $EXCEPTION_CLASS;
57 $self->SUPER::init( @_ ) || return( $self->pass_error );
58 return( $self );
59 }
60  
61 sub as_string { return( shift->error->scalar ); }
62  
63 sub error { return( shift->_set_get_scalar_as_object( 'error', @_ ) ); }
64  
65 sub field { return( shift->_set_get_scalar_as_object( 'field', @_ ) ); }
66  
67 sub table { return( shift->_set_get_scalar_as_object( 'table', @_ ) ); }
68  
69 # NOTE: For CBOR and Sereal
70 sub FREEZE
71 {
72 my $self = CORE::shift( @_ );
73 my $serialiser = CORE::shift( @_ ) // '';
74 my $class = CORE::ref( $self );
75  
76 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
77 # process-local state.
78 # 2026-01-29: I removed 'query_object' in an effort to reduce memory consumption. Let's see...
79 my @props = qw(
80 field table
81 );
82  
83 my $hash = {};
84 foreach my $prop ( @props )
85 {
86 if( CORE::exists( $self->{ $prop } ) &&
87 defined( $self->{ $prop } ) &&
88 CORE::ref( $self->{ $prop } ) ne 'CODE' )
89 {
90 $hash->{ $prop } = $self->{ $prop };
91 }
92 }
93  
94 # Return an array reference rather than a list so this works with Sereal and CBOR.
95 # Before Sereal version 4.023, Sereal did not support multiple values returned.
96 if( $serialiser eq 'Sereal' )
97 {
98 require Sereal::Encoder;
99 require version;
100  
101 if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
102 {
103 CORE::return( [$class, $hash] );
104 }
105 }
106  
107 # But Storable wants a list with the first element being the serialised element
108 CORE::return( $class, $hash );
109 }
110  
111 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
112  
113 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
114  
115 sub STORABLE_thaw_post_processing
116 {
117 my $obj = shift( @_ );
118 my @keys = %$obj;
119 my $class = ref( $obj );
120 my $hash = {};
121 @$hash{ @keys } = @$obj{ @keys };
122 my $self = bless( $hash => $class );
123 return( $self );
124 }
125  
126 sub THAW
127 {
128 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
129 # 'CBOR' as the second value.
130 my( $self, undef, @args ) = @_;
131 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
132 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
133 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
134 my $new;
135 # Storable pattern requires to modify the object it created rather than returning a new one
136 if( CORE::ref( $self ) )
137 {
138 foreach( CORE::keys( %$hash ) )
139 {
140 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
141 }
142 $new = $self;
143 }
144 else
145 {
146 $new = CORE::bless( $hash => $class );
147 }
148 CORE::return( $new );
149 }
150  
151 1;
152 # NOTE: POD
153 __END__
154  
155 =encoding utf-8
156  
157 =head1 NAME
158  
159 DB::Object::Fields::Unknown - Unknown Field Class
160  
161 =head1 SYNOPSIS
162  
163 use DB::Object::Fields::Unknown;
164 my $f = DB::Object::Fields::Unknown->new(
165 table => 'some_table',
166 error => 'Table some_table has no such field \"some_field\".',
167 ) || die( DB::Object::Fields::Unknown->error, "\n" );
168  
169 =head1 VERSION
170  
171 v0.2.0
172  
173 =head1 DESCRIPTION
174  
175 This class represents an unknown field. This happens when L<DB::Object::Fields> cannot find a given field used in a SQL query. Instead of returning an error (undef), it returns this object, which is then ignored when he query is formulated.
176  
177 A warning is issued by L<DB::Object::Fields> when a field is unknown, so make sure to check your error output or your error log.
178  
179 =head1 METHODS
180  
181 =head2 as_string
182  
183 Returns the error message as a regular string.
184  
185 =head2 error
186  
187 Sets or gets the error that triggered this new object.
188  
189 This returns the error as a L<string object|Module::Generic::Scalar>
190  
191 =head2 field
192  
193 Sets or gets the name of the unknown field.
194  
195 This returns the field name as a L<string object|Module::Generic::Scalar>
196  
197 =head2 table
198  
199 Sets or gets the name of the table associated with this unknown field
200  
201 This returns the table name as a L<string object|Module::Generic::Scalar>
202  
203 =head1 AUTHOR
204  
205 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
206  
207 =head1 SEE ALSO
208  
209 L<DB::Object::Fields>, L<DB::Object::Fields::Field>, L<DB::Object::Query>
210  
211 =head1 COPYRIGHT & LICENSE
212  
213 Copyright(c) 2022 DEGUEST Pte. Ltd.
214  
215 All rights reserved.
216  
217 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
218  
219 =cut