| 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 | ## 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::Fields::Unknown; | ||
| 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 | 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__ |