| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Database Object Interface - ~/lib/DB/Object/Placeholder.pm | ||
| 3 | ## Version v0.13.0 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2023/07/08 | ||
| 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::Placeholder; | ||
| 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( $REGISTRY $VERSION $EXCEPTION_CLASS ); | ||
| 21 | use Scalar::Util (); | ||
| 22 | use overload ( | ||
| 23 | '""' => 'as_string', | ||
| 24 | 'bool' => sub{1}, | ||
| 25 | ); | ||
| 26 | our $REGISTRY = {}; | ||
| 27 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | ||
| 28 | our $VERSION = 'v0.13.0'; | ||
| 29 | }; | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | |||
| 34 | sub init | ||
| 35 | { | ||
| 36 | my $self = shift( @_ ); | ||
| 37 | $self->{type} = undef; | ||
| 38 | $self->{value} = undef; | ||
| 39 | $self->{_init_strict_use_sub} = 1; | ||
| 40 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 41 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 42 | my $addr = Scalar::Util::refaddr( $self ); | ||
| 43 | $REGISTRY->{ $addr } = $self; | ||
| 44 | return( $self ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub as_string | ||
| 48 | { | ||
| 49 | my $self = shift( @_ ); | ||
| 50 | my $addr = Scalar::Util::refaddr( $self ); | ||
| 51 | return( "__PLACEHOLDER__${addr}__" ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub has | ||
| 55 | { | ||
| 56 | my $self = shift( @_ ); | ||
| 57 | my $str = shift( @_ ); | ||
| 58 | $str = Scalar::Util::reftype( $str ) eq 'SCALAR' ? $str : \$str; | ||
| 59 | return( CORE::index( $$str, '__PLACEHOLDER__' ) != -1 ); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub replace | ||
| 63 | { | ||
| 64 | my $self = shift( @_ ); | ||
| 65 | my $str = shift( @_ ); | ||
| 66 | $str = Scalar::Util::reftype( $str ) eq 'SCALAR' ? $str : \$str; | ||
| 67 | return if( !defined( $$str ) || !length( $$str ) ); | ||
| 68 | my $types = $self->new_array( [] ); | ||
| 69 | my $values = $self->new_array( [] ); | ||
| 70 | $$str =~ s | ||
| 71 | { | ||
| 72 | __PLACEHOLDER__(\d+)__ | ||
| 73 | } | ||
| 74 | { | ||
| 75 | if( exists( $REGISTRY->{ $1 } ) ) | ||
| 76 | { | ||
| 77 | my $p = $REGISTRY->{ $1 }; | ||
| 78 | push( @$types, $p->type ); | ||
| 79 | push( @$values, $p->value ); | ||
| 80 | } | ||
| 81 | "?"; | ||
| 82 | }gexm; | ||
| 83 | return( wantarray() ? ( $types, $$str ) : $types ); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub type { return( shift->_set_get_scalar( 'type', @_ ) ); } | ||
| 87 | |||
| 88 | sub value { return( shift->_set_get_scalar( 'value', @_ ) ); } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | # NOTE: POD | ||
| 92 | __END__ |