[Dbix-class] FW: DBIx::Class::LookupColumn

Alexander Hartmaier alexander.hartmaier at t-systems.at
Thu Oct 18 09:24:12 GMT 2012


On 2012-10-18 10:35, uro shima wrote:
Dear DBIx-Class mailing list

Hereby we'd like to inform you about a new package related to DBIx::Class.

Its objective is to bring efficient and convenient methods for dealing with=
 Lookup Tables. We call Lookup tables tables that are actually catalogs of =
terms, e.g. the table UserType with such rows: ( 1 =3D> 'Administrator' , 2=
 =3D> 'User' , 3 =3D> 'Guest' ).

Let's have an example of use.

package MySchema::Result::User;
__PACKAGE__->table("user");
__PACKAGE__->add_columns( "user_id",{}, "name", {}, "user_type_id", {} );
__PACKAGE__->belongs_to( "UserType" =3D> "Schema3::Result::UserType", {"use=
r_type_id" =3D> "self.user_type_id"} );

# UserType Lookup table, with 2 columns (user_type_id, name) with rows: ( 1=
 =3D> 'Administrator' , 2 =3D> 'User' , 3 =3D> 'Guest' )

# $user is a DBIx::Class::Row instance, e.g. $user=3D$schema->resultset('Us=
er')->find( name =3D> 'Flash Gordon' )
print $user->type; # print 'Administrator', not very impressive, could be w=
ritten as $user->user_type()->name()
print $user->type; # same thing, but we are sure that no database request i=
s done thanks to the cache system

print $user->is_type('Administrator') ? 'Ok' : 'Access Restricted';
# equivalent (but more efficient) to
my $type =3D $schema->resultset('UserType')->find( name =3D> 'Administrator=
') or die "Bad name 'Administrator' for Lookup table UserType";
print $user->user_type_id eq $type->id ? 'Ok' : 'Access Restricted';

The features it provides are the following :
- syntactic sugar to accessors, for example by allowing to deal with hard-c=
oded value directly on the client table, e.g. $user->is_type('Administrator=
').
- efficiency by means of a cache system for avoiding many repeated identica=
l individual DBMS requests related to one entire Lookup table.
- accessors generation automation to a whole schema.

Further informations are of course available on the CPAN at the following U=
RL : http://search.cpan.org/~rubattel/DBIx-Class-LookupColumn-0.01/lib/DBIx=
/Class/LookupColumn.pm

Questions or comments about it are very welcome.
Karl and Thomas
Thanks for announcing this useful component to the list!


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*
T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*
Notice: This e-mail contains information that is confidential and may be pr=
ivileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20121018/69b=
47e41/attachment.htm


More information about the DBIx-Class mailing list