DB::Object::Fields - Tables Fields Object Accessor
my $dbh = DB::Object->connect({ driver => 'Pg', conf_file => $conf, database => 'my_shop', host => 'localhost', login => 'super_admin', schema => 'auth', # debug => 3, }) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error ); my $tbl_object = $dbh->customers || die( "Unable to get the customers table object: ", $dbh->error, "\n" ); my $fields = $tbl_object->fields; print( "Fields for table \"", $tbl_object->name, "\": ", Dumper( $fields ), "\n" ); my $c = $tbl_object->fo->currency; print( "Got field object for currency: \"", ref( $c ), "\": '$c'\n" ); printf( "Name: %s\n", $c->name ); printf( "Type: %s\n", $c->type ); printf( "Default: %s\n", $c->default ); printf( "Position: %s\n", $c->pos ); printf( "Table: %s\n", $c->table ); printf( "Database: %s\n", $c->database ); printf( "Schema: %s\n", $c->schema ); printf( "Next field: %s (%s)\n", $c->next, ref( $c->next ) ); print( "Showing name fully qualified: ", $c->prefixed( 3 )->name, "\n" ); ## would print: my_shop.public.customers.currency print( "Trying again (should keep prefix): ", $c->name, "\n" ); ## would print again: my_shop.public.customers.currency print( "Now cancel prefixing at the table fields level.\n" ); $tbl_object->fo->prefixed( 0 ); print( "Showing name fully qualified again (should not be prefixed): ", $c->name, "\n" ); ## would print currency print( "First element is: ", $c->first, "\n" ); print( "Last element is: ", $c->last, "\n" );
0.1
The purpose of this module is to enable access to the table fields as DB::Object::Fields::Field objects.
DB::Object::Fields::Field
The way this works is by having DB::Object::Tables fields_object or fo for short, dynamically create a class based on the database name and table name. For example if the database driver were PostgreSQL, the database were my_shop and the table customers, the dynamically created package would become DB::Object::Postgres::Tables::MyShop::Customers. This class would inherit from this package DB::Object::Fields.
DB::Object::Tables
PostgreSQL
my_shop
customers
DB::Object::Postgres::Tables::MyShop::Customers
DB::Object::Fields
Field objects can than be dynamically instantiated by accessing them, such as (assuming the table object $tbl_object here represent the table customers) $tbl_object-fo->last_name>. This will return a DB::Object::Fields::Field object.
$tbl_object
$tbl_object-
A note on the design: there had to be a separate this separate package DB::Object::Fields, because access to table fields is done through the AUTOLOAD and the methods within the package DB::Object::Tables and its inheriting packages would clash with the tables fields. This package has very few methods, so the risk of a sql table field clashing with a method name is very limited. In any case, if you have in your table a field with the same name as one of those methods here (see below for the list), then you can instantiate a field object with:
AUTOLOAD
$tbl_object->_initiate_field_object( 'last_name' );
Creates a new DB::Object::Fields objects. It may also take an hash like arguments, that also are method of the same name.
Toggles debug mode on/off
Jacques Deguest <jack@deguest.jp>
perl
Copyright (c) 2020 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.
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
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.