blib/lib/DB/Object/Placeholder.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 91 CC 5 SCAR 18.3
CC
5
Cov
0%
replace
20.0
has
6.0
BEGIN
2.0
CRAP 6.2
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 ##
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::Placeholder;
15 BEGIN
16 {
17 use strict;
18 use warnings;
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__
93  
94 =encoding utf-8
95  
96 =head1 NAME
97  
98 DB::Object::Placeholder - Placeholder Object
99  
100 =head1 SYNOPSIS
101  
102 my $p = $dbh->P( type => 'inet', value => '127.0.0.1' );
103 my $q = "SELECT * FROM ip_registry WHERE ip_addr = inet($p) OR inet($p) << ip_addr";
104 my $types = $p->replace( \$q );
105 # or, since the object here is just an accessor for this method
106 # my $types = DB::Object::Placeholder->replace( \$q );
107 # Got a Module::Generic::Array in response
108 # $types->first -> inet
109 # $types->second -> inet
110  
111 For example:
112  
113 my $P = $dbh->placeholder( type => 'inet' );
114 $orders_tbl->where( $dbh->OR( $orders_tbl->fo->ip_addr == "inet $P", "inet $P" << $orders_tbl->fo->ip_addr ) );
115 my $order_ip_sth = $orders_tbl->select( 'id' ) || fail( "An error has occurred while trying to create a select by ip query for table orders: " . $orders_tbl->error );
116 # SELECT id FROM orders WHERE ip_addr = inet ? OR inet ? << ip_addr
117  
118 =head1 DESCRIPTION
119  
120 This is a placeholder representation class, because sometime, putting a placeholder in complex or even simple SQL expression makes it impossible for this API to detect it.
121  
122 Using this class, you can place placeholder in your query, specify what data type they represent and allow this api to recognise them and benefit from them even.
123  
124 =head1 METHODS
125  
126 =head2 new
127  
128 Takes a list of below options-value pairs and return a new instance of this class.
129  
130 =over 5
131  
132 =item * C<type>
133  
134 The placeholder SQL data type
135  
136 =item * C<value>
137  
138 The placeholder value to bind, if any.
139  
140 =back
141  
142 =head2 as_string
143  
144 Returns the placeholder object as a string, which would look something like C<__PLACEHOLDER_1234567__>
145  
146 =head2 has
147  
148 Provided with a query as a string or as a scalar reference and this will check if it contains any placeholder objects. It returns true if it does or false otherwise.
149  
150 =head2 replace
151  
152 Provided with a scalar (string) or scalar reference and this will replace any placeholder objects with actual SQL placeholders, i.e. C<?>, and return an array object of those placeholder datatypes, which may be blank. This is ok, it will be passed to the database driver upon binding and let it guess the best type. In list context, it also returns the modified query. This is useful if you only passed a string and not a scalar reference.
153  
154 my $types = $p->replace( \$query );
155 # or
156 my( $types, $query ) = $p->replace( $query );
157  
158 =head2 type
159  
160 Sets or gets the SQL data type for this placeholder. It is not the constant, but the data type string itself. For example, for C<PG_JSONB> in PostgreSQL, it would simply be C<jsonb>
161  
162 =head2 value
163  
164 Sets or gets the value of the placeholder, if any. This method is actually not used for now. It is reserved here for the future.
165  
166 =head1 SEE ALSO
167  
168 L<DB::Object::DB::Element>, L<DB::Object::DB::Elements>
169  
170 L<DBI>, L<Apache::DBI>
171  
172 =head1 AUTHOR
173  
174 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
175  
176 =head1 COPYRIGHT & LICENSE
177  
178 Copyright(c) 2021-2023 DEGUEST Pte. Ltd.
179  
180 All rights reserved
181  
182 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
183  
184 =cut