DB::Object::Placeholder - Placeholder Object
my $p = $dbh->P( type => 'inet', value => '127.0.0.1' ); my $q = "SELECT * FROM ip_registry WHERE ip_addr = inet($p) OR inet($p) << ip_addr"; my $types = $p->replace( \$q ); # or, since the object here is just an accessor for this method # my $types = DB::Object::Placeholder->replace( \$q ); # Got a Module::Generic::Array in response # $types->first -> inet # $types->second -> inet
For example:
my $P = $dbh->placeholder( type => 'inet' ); $orders_tbl->where( $dbh->OR( $orders_tbl->fo->ip_addr == "inet $P", "inet $P" << $orders_tbl->fo->ip_addr ) ); 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 ); # SELECT id FROM orders WHERE ip_addr = inet ? OR inet ? << ip_addr
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.
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.
Takes a list of below options-value pairs and return a new instance of this class.
type
The placeholder SQL data type
value
The placeholder value to bind, if any.
Returns the placeholder object as a string, which would look something like __PLACEHOLDER_1234567__
__PLACEHOLDER_1234567__
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.
Provided with a scalar (string) or scalar reference and this will replace any placeholder objects with actual SQL placeholders, i.e. ?, 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.
?
my $types = $p->replace( \$query ); # or my( $types, $query ) = $p->replace( $query );
Sets or gets the SQL data type for this placeholder. It is not the constant, but the data type string itself. For example, for PG_JSONB in PostgreSQL, it would simply be jsonb
PG_JSONB
jsonb
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.
DB::Object::DB::Element, DB::Object::DB::Elements
DBI, Apache::DBI
Jacques Deguest <jack@deguest.jp>
Copyright(c) 2021-2023 DEGUEST Pte. Ltd.
All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DB::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DB::Object
CPAN shell
perl -MCPAN -e shell install DB::Object
For more information on module installation, please visit the detailed CPAN module installation guide.