| 1 | package DBIx::Class::LookupColumn; | ||
| 2 | { | ||
| 3 | 4 | $DBIx::Class::LookupColumn::VERSION = '0.10'; | |
| 4 | } | ||
| 5 | 4 | ▶ | use base DBIx::Class::LookupColumn::LookupColumnComponent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | DBIx::Class::LookupColumn - DBIx::Class components to help using Lookup tables. | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | L<DBIx::Class::LookupColumn::Auto> is probably what you need to apply this system on your schema. | ||
| 15 | |||
| 16 | # User table with columns user_id, name, and user_type_id (foreign_key to UserType Lookup table) | ||
| 17 | package MySchema::Result::User; | ||
| 18 | __PACKAGE__->table("user"); | ||
| 19 | __PACKAGE__->add_columns( "user_id",{}, "name", {}, "user_type_id", {} ); | ||
| 20 | __PACKAGE__->belongs_to( "user_type" => "Schema3::Result::UserType", {"user_type_id" => "self.user_type_id"} ); | ||
| 21 | |||
| 22 | # UserType Lookup table, with 2 columns (user_type_id, name) with rows: ( 1 => 'Administrator' , 2 => 'User' , 3 => 'Guest' ) | ||
| 23 | |||
| 24 | # $user is a DBIx::Class::Row instance, e.g. $user=$schema->resultset('User')->find( { name => 'Flash Gordon'} ) | ||
| 25 | |||
| 26 | print $user->type; # print 'Administrator', not very impressive, could be written as $user->user_type()->name() | ||
| 27 | print $user->type; # same thing, but we are sure that no database request is done thanks to the cache system | ||
| 28 | |||
| 29 | print $user->is_type('Administrator') ? 'Ok' : 'Access Restricted'; | ||
| 30 | # equivalent (but more efficient) to | ||
| 31 | my $type = $schema->resultset('UserType')->find( { name => 'Administrator' }) | ||
| 32 | or die "Bad name 'Administrator' for Lookup table UserType"; | ||
| 33 | print $user->user_type_id eq $type->id ? 'Ok' : 'Access Restricted'; | ||
| 34 | |||
| 35 | $user->set_type('User'); | ||
| 36 | # equivalent (but more efficient) to | ||
| 37 | my $type = $schema->resultset('UserType')->find( { name => 'User'} ) or die "Bad name 'User' for Lookup table UserType"; | ||
| 38 | $user->user_type_id( $type->id ); | ||
| 39 | |||
| 40 | my @users = $schema->resultset('User')->all; # suppose there are 1000 users | ||
| 41 | |||
| 42 | # how many database requests: at most one ! | ||
| 43 | # and if you mispelled 'Administrator' the code would die with a meaningful error message | ||
| 44 | foreach my $user (@users) { | ||
| 45 | print $user->name, " has admin rights\n" if $user->is_type('Administrator'); | ||
| 46 | } | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | The objective of this module is to bring efficient and convenient methods for dealing with B<Lookup Tables>. | ||
| 51 | We call Lookup tables tables that are actually catalogs of terms. | ||
| 52 | |||
| 53 | A good example is the table UserType in the L<Synopsis>, | ||
| 54 | that describles all the possible values for the user types. A normalized database has usually lots of such Lookup tables. | ||
| 55 | This is a good database design practice, basically avoiding using hard_coded strings in your database tables. | ||
| 56 | Unfortunately it tends to either moving the problem in your code by using hard-coded strings such as 'Administrator' | ||
| 57 | , or to complexifying your code. | ||
| 58 | |||
| 59 | This module tries to solve this problem by allowing the developer to use strings instead of IDs in his code | ||
| 60 | to increase readability (e.g. using 'Administrator' instead of the corresponding ID) without sacrificing performance | ||
| 61 | nor safety, thanks to the cache (see L<DBIx::Class::LookupColumn::Manager> ) and the constant value checking. | ||
| 62 | |||
| 63 | It works in a lazy slurpy way: the first time a Lookup table value is needed, the whole table is read and stored in | ||
| 64 | the cache (see L<DBIx::Class::LookupColumn::Manager>). | ||
| 65 | The L<DBIx::Class::LookupColumn::LookupColumnComponent> allows to generate accessors for convenience. | ||
| 66 | The L<DBIx::Class::LookupColumn::Auto> allows to quickly add those accessors for all your tables. | ||
| 67 | |||
| 68 | |||
| 69 | =head1 Lookup Tables | ||
| 70 | |||
| 71 | What we call B<Lookup Tables>, as stated above, are catalogs of terms, hard-coded values. | ||
| 72 | This a quite fuzzy definition, and in our understanding these kinds of tables are also usually I<small> | ||
| 73 | and I<stable>, making perfect candidates for caching. | ||
| 74 | |||
| 75 | The idea of this modules came when using a database schema with dozens of tables called PermissionType, UserType, DocumentType | ||
| 76 | each with less than 10 values. Using DBIx::Class objects, this leaded to very inefficient database queries. | ||
| 77 | Using this module solved this problem, and we hope it could be useful to others. | ||
| 78 | |||
| 79 | |||
| 80 | =head1 CAVEATS | ||
| 81 | |||
| 82 | Side effect : if one ResultSource class L<DBIx::Class::ResultSource> you would like to apply a lookup relation on has been set to immutable L<Class::MOP::Class> | ||
| 83 | it will turn it to mutable for adding the accessors. It will then be reset to immutable. | ||
| 84 | |||
| 85 | Do not use this module on I<big> tables, it could actually slow down your code and eat all your memory. | ||
| 86 | |||
| 87 | |||
| 88 | =head1 Example of use in a many-to-many relationship | ||
| 89 | |||
| 90 | Suppose we have a many-to-many relationship between the B<Actor> and B<RoleType> tables. Then a new table, let's call it | ||
| 91 | B<ActorRole>, has to be designed for playing the interface between them. | ||
| 92 | In this case the RoleType is the Lookup table and ActorRole the one the lookup relation is called on. However let's add two sugar functions for | ||
| 93 | applying the getter and checker accessor on the Actor objects directly. | ||
| 94 | |||
| 95 | # In the Actor ResultSource | ||
| 96 | # let's add both functions below as suggar | ||
| 97 | sub role_names { | ||
| 98 | my $self = shift; | ||
| 99 | # role() is generated by DBIx::Class::LookupColumn::LookupColumnComponent | ||
| 100 | return map { $_->role() } $self->actorroles; | ||
| 101 | }; | ||
| 102 | |||
| 103 | sub has_role { | ||
| 104 | my ( $self, $role_name ) = @_; | ||
| 105 | # is_role() is generated by DBIx::Class::LookupColumn::LookupColumnComponent | ||
| 106 | my $found = grep { $_->is_role( $role_name ) } $self->actorroles; | ||
| 107 | return $found; | ||
| 108 | } | ||
| 109 | |||
| 110 | # In the appropriated ResultSource | ||
| 111 | # let's define the relationships | ||
| 112 | SchemaMany2Many::Result::Actor->has_many('actorroles', 'SchemaMany2Many::Result::ActorRole', { 'foreign.actor_id' => 'self.actor_id' } ); | ||
| 113 | SchemaMany2Many::Result::Actor->many_to_many('roletypes', 'actorroles', 'roletype'); | ||
| 114 | |||
| 115 | SchemaMany2Many::Result::ActorRole->belongs_to('roletype', 'SchemaMany2Many::Result::RoleType', {"foreign.role_id" => "self.role_id" } ); | ||
| 116 | SchemaMany2Many::Result::ActorRole->belongs_to('actor', 'SchemaMany2Many::Result::Actor', {"foreign.actor_id" => "self.actor_id" }); | ||
| 117 | |||
| 118 | SchemaMany2Many::Result::RoleType->has_many('actorroles', 'SchemaMany2Many::Result::ActorRole', { 'foreign.role_id' => 'self.role_id' } ); | ||
| 119 | SchemaMany2Many::Result::RoleType->many_to_many('actors', 'actorroles', 'actor'); | ||
| 120 | |||
| 121 | # In the Schema Class | ||
| 122 | # let's automatize the lookup relation creation | ||
| 123 | __PACKAGE__->load_components( qw/LookupColumn::Auto/ ); | ||
| 124 | |||
| 125 | my @tables = __PACKAGE__->sources; | ||
| 126 | __PACKAGE__->add_lookups( | ||
| 127 | targets => [ grep{ !/Type$/ } @tables ], | ||
| 128 | lookups => [ grep{ /Type$/} @tables], | ||
| 129 | relation_name_builder => sub{ | ||
| 130 | my ( $class, %args) = @_; | ||
| 131 | $args{lookup} =~ /^(.+)Type$/; | ||
| 132 | lc( $1 ); | ||
| 133 | }, | ||
| 134 | lookup_field_name_builder => sub { 'name' } | ||
| 135 | ); | ||
| 136 | |||
| 137 | # In a script | ||
| 138 | my $actor = $schema->resultset('Actor')->find ( 1 ); | ||
| 139 | |||
| 140 | # with DBIx::Class::LookupColumn package | ||
| 141 | my @roles_played = $actor->role_names(); # ONE and only one DB request whose result is cached | ||
| 142 | my $played_warlock = $actor->has_role ('Warlock'); # assigns boolean | ||
| 143 | # my $played_warlock = $actor->has_role ('Warloc'); # throws an exception because does not exist in the DB | ||
| 144 | |||
| 145 | # inefficient classical way to do it | ||
| 146 | my @roles_played_classical = map { $_->roletype()->name } $actor->actorroles(); # does a DB request at EACH loop ! | ||
| 147 | my $played_warlock_classical = 'Warlock' ~~ @roles_played_classical; # assigns boolean | ||
| 148 | # my $played_warlock_classical = 'Warloc' ~~ @roles_played_classical; # assigns an empty string, NOT safe regarding TYPOS ! | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | 4 | 1; # End of DBIx::Class::LookupColumn | |