| 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 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use parent qw( Module::Generic ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use vars qw( $REGISTRY $VERSION $EXCEPTION_CLASS ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | use overload ( | ||||||||||||||||||||||||||||
| 23 | '""' => 'as_string', | ||||||||||||||||||||||||||||
| 24 | 160 | ▶ | 'bool' => sub{1}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | our $REGISTRY = {}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 4 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | |||||||||||||||||||||||||||
| 28 | 4 | our $VERSION = 'v0.13.0'; | |||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub init | ||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 40 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 40 | $self->{type} = undef; | |||||||||||||||||||||||||||
| 38 | 40 | $self->{value} = undef; | |||||||||||||||||||||||||||
| 39 | 40 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 40 | 40 | $self->{_exception_class} = $EXCEPTION_CLASS; | |||||||||||||||||||||||||||
| 41 | 40 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 40 | my $addr = Scalar::Util::refaddr( $self ); | |||||||||||||||||||||||||||
| 43 | 40 | $REGISTRY->{ $addr } = $self; | |||||||||||||||||||||||||||
| 44 | 40 | return( $self ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub as_string | ||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | 3 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 3 | my $addr = Scalar::Util::refaddr( $self ); | |||||||||||||||||||||||||||
| 51 | 3 | return( "__PLACEHOLDER__${addr}__" ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub has | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 37 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
has: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 37 | my $str = shift( @_ ); | |||||||||||||||||||||||||||
| 58 | 37 | ▶ | $str = Scalar::Util::reftype( $str ) eq 'SCALAR' ? $str : \$str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 37 | return( CORE::index( $$str, '__PLACEHOLDER__' ) != -1 ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub replace | ||||||||||||||||||||||||||||
| 63 | { | ||||||||||||||||||||||||||||
| 64 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
replace: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 2 | my $str = shift( @_ ); | |||||||||||||||||||||||||||
| 66 | 2 | ▶ | $str = Scalar::Util::reftype( $str ) eq 'SCALAR' ? $str : \$str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | return if( !defined( $$str ) || !length( $$str ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $$str and length $$str
MC/DC0%0/2
defined $$str and length $$str
defined $$str
length $$str
Truth table33%1/3
A and B
A= defined $$strB= length $$str
| |||||||||||||||||||||||||||||
| 68 | 2 | my $types = $self->new_array( [] ); | |||||||||||||||||||||||||||
| 69 | 2 | my $values = $self->new_array( [] ); | |||||||||||||||||||||||||||
| 70 | 2 | $$str =~ s | |||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | __PLACEHOLDER__(\d+)__ | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 3 | ▶ | if( exists( $REGISTRY->{ $1 } ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 3 | my $p = $REGISTRY->{ $1 }; | |||||||||||||||||||||||||||
| 78 | 3 | push( @$types, $p->type ); | |||||||||||||||||||||||||||
| 79 | 3 | push( @$values, $p->value ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 3 | "?"; | |||||||||||||||||||||||||||
| 82 | }gexm; | ||||||||||||||||||||||||||||
| 83 | 2 | ▶ | return( wantarray() ? ( $types, $$str ) : $types ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 5 | ▶ | sub type { return( shift->_set_get_scalar( 'type', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | sub value { return( shift->_set_get_scalar( 'value', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | 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 | ||||||||||||||||||||||||||||