| 1 | package DBIx::Class::LookupColumn::Manager; | ||||||||||||
| 2 | { | ||||||||||||
| 3 | 18 | $DBIx::Class::LookupColumn::Manager::VERSION = '0.10'; | |||||||||||
| 4 | } | ||||||||||||
| 5 | |||||||||||||
| 6 | 18 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 18 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | DBIx::Class::LookupColumn::Manager - a lazy cache system for storing Lookup tables. | ||||||||||||
| 12 | |||||||||||||
| 13 | =cut | ||||||||||||
| 14 | |||||||||||||
| 15 | 18 | ▶ | use Carp qw(confess); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 18 | ▶ | use Smart::Comments -ENV; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 18 | my %CACHE; # main class variable containing all cached objects | |||||||||||
| 19 | |||||||||||||
| 20 | |||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||
| 22 | |||||||||||||
| 23 | use DBIx::Class::LookupColumn::Manager; | ||||||||||||
| 24 | DBIx::Class::LookupColumn::Manager->FETCH_ID_BY_NAME( $schema, 'PermissionType', 'name', 'Administrator' ); | ||||||||||||
| 25 | DBIx::Class::LookupColumn::Manager->FETCH_NAME_BY_ID( $schema, 'PermissionType', 'name', 1 ); | ||||||||||||
| 26 | |||||||||||||
| 27 | |||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||
| 29 | |||||||||||||
| 30 | This class is not intended to be used directly. It is the backbone of the L<DBIx::Class::LookupColumn> module. | ||||||||||||
| 31 | |||||||||||||
| 32 | It stores B<Lookup tables> ( (id, name) ) in a structure and indexes ids and names both ways, so that it is fast to test if a given | ||||||||||||
| 33 | name is defined by a Lookup table. | ||||||||||||
| 34 | |||||||||||||
| 35 | This module only supports tables having only one single primary key. | ||||||||||||
| 36 | |||||||||||||
| 37 | |||||||||||||
| 38 | =head1 STATIC METHODS | ||||||||||||
| 39 | |||||||||||||
| 40 | =cut | ||||||||||||
| 41 | |||||||||||||
| 42 | =head2 FETCH_ID_BY_NAME | ||||||||||||
| 43 | |||||||||||||
| 44 | $id = DBIx::Class::LookupColumn::Manager->FETCH_ID_BY_NAME($schema, $lookup_table, $field_name, $name) | ||||||||||||
| 45 | |||||||||||||
| 46 | Returns the id associated with the value C<$name> in the column C<$field_name> of the Lookup table named C<$lookup_table>. | ||||||||||||
| 47 | |||||||||||||
| 48 | As a side-effect, it will lazy-load the table C<$lookup_table> in the cache. | ||||||||||||
| 49 | |||||||||||||
| 50 | B<Arguments>: | ||||||||||||
| 51 | |||||||||||||
| 52 | =over 4 | ||||||||||||
| 53 | |||||||||||||
| 54 | =item $schema | ||||||||||||
| 55 | |||||||||||||
| 56 | The L<DBIx::Class::Schema> schema instance. | ||||||||||||
| 57 | |||||||||||||
| 58 | =item $lookup_table | ||||||||||||
| 59 | |||||||||||||
| 60 | The name of the Lookup table. | ||||||||||||
| 61 | |||||||||||||
| 62 | =item $field_name | ||||||||||||
| 63 | |||||||||||||
| 64 | The name of the column of the Lookup table that contains the value of the terms/definitions (e.g. 'name'). | ||||||||||||
| 65 | |||||||||||||
| 66 | =item $name | ||||||||||||
| 67 | |||||||||||||
| 68 | The value in the column C<$field_name> we want to fetch the primary key for. | ||||||||||||
| 69 | |||||||||||||
| 70 | =back | ||||||||||||
| 71 | |||||||||||||
| 72 | B<Example>: | ||||||||||||
| 73 | |||||||||||||
| 74 | my $admin_id = DBIx::Class::LookupColumn::Manager->FETCH_ID_BY_NAME( $schema, 'UserType', 'name', 'Administrator' ). | ||||||||||||
| 75 | |||||||||||||
| 76 | =head2 FETCH_NAME_BY_ID | ||||||||||||
| 77 | |||||||||||||
| 78 | $name = DBIx::Class::LookupColumn::Manager->FETCH_ID_BY_NAME($schema, $lookup_table, $field_name, $id) | ||||||||||||
| 79 | |||||||||||||
| 80 | Returns the name associated with the id C<$id> in the column C<$field_name> of the Lookup table named C<$lookup_table>. | ||||||||||||
| 81 | |||||||||||||
| 82 | As a side-effect, it will lazy-load the table C<$lookup_table> in the cache. | ||||||||||||
| 83 | |||||||||||||
| 84 | B<Arguments>: | ||||||||||||
| 85 | |||||||||||||
| 86 | =over 4 | ||||||||||||
| 87 | |||||||||||||
| 88 | =item $schema | ||||||||||||
| 89 | |||||||||||||
| 90 | The L<DBIx::Class::Schema> schema instance. | ||||||||||||
| 91 | |||||||||||||
| 92 | =item $lookup_table | ||||||||||||
| 93 | |||||||||||||
| 94 | The name of the Lookup table. | ||||||||||||
| 95 | |||||||||||||
| 96 | =item $field_name | ||||||||||||
| 97 | |||||||||||||
| 98 | The name of the column of the Lookup table that contains the value of the terms/definitions (e.g. 'name'). | ||||||||||||
| 99 | |||||||||||||
| 100 | =item $id | ||||||||||||
| 101 | |||||||||||||
| 102 | The id C<$id> in the column C<$field_name> we want to fetch the value for. | ||||||||||||
| 103 | |||||||||||||
| 104 | =back | ||||||||||||
| 105 | |||||||||||||
| 106 | B<Example>: | ||||||||||||
| 107 | |||||||||||||
| 108 | my $type_name = DBIx::Class::LookupColumn::Manager->FETCH_NAME_BY_ID( $schema, 'UserType', 'name', 1 ). | ||||||||||||
| 109 | |||||||||||||
| 110 | |||||||||||||
| 111 | |||||||||||||
| 112 | |||||||||||||
| 113 | =cut | ||||||||||||
| 114 | |||||||||||||
| 115 | 40 | ▶ | sub FETCH_ID_BY_NAME { | ||||||||||
|
Subroutine
FETCH_ID_BY_NAME: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 116 | 40 | my ( $class, $schema, $lookup_table, $field_name, $name ) = @_; | |||||||||||
| 117 | 40 | ▶ | confess "Bad args" unless defined $name; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 40 | my $cache = $class->_ENSURE_LOOKUP_IS_CACHED( $schema, $lookup_table, $field_name ); | |||||||||||
| 119 | 38 | ▶ | my $id = $cache->{name2id}{$name} or confess "name [$name] does not exist in (cached) Lookup table [$lookup_table]"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 33 | return $id; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | |||||||||||||
| 124 | |||||||||||||
| 125 | |||||||||||||
| 126 | 26 | ▶ | sub FETCH_NAME_BY_ID { | ||||||||||
|
Subroutine
FETCH_NAME_BY_ID: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 127 | 26 | my ( $class, $schema, $lookup_table, $field_name, $id ) = @_; | |||||||||||
| 128 | 26 | ▶ | confess "Bad args" unless defined $id; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 26 | my $cache = $class->_ENSURE_LOOKUP_IS_CACHED( $schema, $lookup_table, $field_name ); | |||||||||||
| 130 | 23 | ▶ | my $name = $cache->{id2name}{$id} or confess "Bad type_name [$id] in Lookup table [$lookup_table]"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 21 | return $name; | |||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | |||||||||||||
| 135 | |||||||||||||
| 136 | |||||||||||||
| 137 | 66 | ▶ | sub _ENSURE_LOOKUP_IS_CACHED { | ||||||||||
|
Subroutine
_ENSURE_LOOKUP_IS_CACHED: called CC=9 SCAR=22.3
| |||||||||||||
| 138 | 66 | my ( $class, $schema, $lookup_table, $field_name ) = @_; | |||||||||||
| 139 | |||||||||||||
| 140 | # check the table and field names | ||||||||||||
| 141 | 66 | ▶ | my $source_table = $schema->source( $lookup_table ) or confess "unknown table called $lookup_table"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 66 | ▶ | confess "the $field_name as field name does not exist in the $lookup_table lookup table" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | unless $source_table->has_column( $field_name ); | ||||||||||||
| 144 | |||||||||||||
| 145 | #### _ENSURE_LOOKUP_IS_CACHED: $lookup_table, $field_name | ||||||||||||
| 146 | |||||||||||||
| 147 | 61 | ▶ | unless ( $CACHE{$lookup_table} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 17 | $CACHE{$lookup_table} = {}; | |||||||||||
| 149 | |||||||||||||
| 150 | # get primary key name | ||||||||||||
| 151 | 17 | my @primary_columns = $schema->source( $lookup_table )->primary_columns; | |||||||||||
| 152 | 17 | ▶ | confess "Error, no primary defined in lookup table $lookup_table" unless @primary_columns; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 153 | 17 | ▶ | confess "we only support lookup table with ONE primary key for table $lookup_table" if @primary_columns > 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | 17 | my $primary_key = shift @primary_columns; | |||||||||||
| 155 | |||||||||||||
| 156 | # query for feching all (id, name) rows from lookup table | ||||||||||||
| 157 | 17 | my $rs = $schema->resultset($lookup_table)->search( undef, { select=>[$primary_key, $field_name] }); | |||||||||||
| 158 | |||||||||||||
| 159 | 17 | my ($id, $name); | |||||||||||
| 160 | 17 | ▶ | my $id2name = $CACHE{$lookup_table}{id2name} ||= {}; | ||||||||||
|
Condition50%1/2
$CACHE{$lookup_table}{"id2name"} ||= {}
MC/DC0%0/1
$CACHE{$lookup_table}{"id2name"} ||= {}
$CACHE{$lookup_table}{"id2name"}
Truth table50%1/2
A
A= $CACHE{$lookup_table}{"id2name"}
| |||||||||||||
| 161 | 17 | ▶ | my $name2id = $CACHE{$lookup_table}{name2id} ||= {}; | ||||||||||
|
Condition50%1/2
$CACHE{$lookup_table}{"name2id"} ||= {}
MC/DC0%0/1
$CACHE{$lookup_table}{"name2id"} ||= {}
$CACHE{$lookup_table}{"name2id"}
Truth table50%1/2
A
A= $CACHE{$lookup_table}{"name2id"}
| |||||||||||||
| 162 | 17 | my $cursor = $rs->cursor; | |||||||||||
| 163 | # fetch all and fill the cache | ||||||||||||
| 164 | 17 | ▶ | while ( ($id, $name) = $cursor->next ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 165 | 54 | $id2name->{$id} = $name; | |||||||||||
| 166 | 54 | $name2id->{$name} = $id; | |||||||||||
| 167 | } | ||||||||||||
| 168 | } | ||||||||||||
| 169 | 61 | return $CACHE{$lookup_table}; | |||||||||||
| 170 | } | ||||||||||||
| 171 | |||||||||||||
| 172 | |||||||||||||
| 173 | |||||||||||||
| 174 | |||||||||||||
| 175 | 0 | ▶ | sub RESET_CACHE { | ||||||||||
|
Subroutine
RESET_CACHE: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 176 | 0 | my ( $class ) = @_; | |||||||||||
| 177 | 0 | %CACHE = (); | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | |||||||||||||
| 181 | |||||||||||||
| 182 | |||||||||||||
| 183 | 0 | ▶ | sub RESET_CACHE_LOOKUP_TABLE { | ||||||||||
|
Subroutine
RESET_CACHE_LOOKUP_TABLE: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 184 | 0 | my ( $class, $lookup_table ) = @_; | |||||||||||
| 185 | 0 | delete $CACHE{$lookup_table}; | |||||||||||
| 186 | } | ||||||||||||
| 187 | |||||||||||||
| 188 | |||||||||||||
| 189 | 0 | ▶ | sub _GET_CACHE{ | ||||||||||
|
Subroutine
_GET_CACHE: not called CC=1 SCAR=6.9
| |||||||||||||
| 190 | 0 | my ( $class ) = @_; | |||||||||||
| 191 | 0 | return \%CACHE; | |||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | |||||||||||||
| 195 | |||||||||||||
| 196 | =head1 AUTHORS | ||||||||||||
| 197 | |||||||||||||
| 198 | Karl Forner <karl.forner@gmail.com> | ||||||||||||
| 199 | |||||||||||||
| 200 | Thomas Rubattel <rubattel@cpan.org> | ||||||||||||
| 201 | |||||||||||||
| 202 | |||||||||||||
| 203 | =head1 BUGS | ||||||||||||
| 204 | |||||||||||||
| 205 | Please report any bugs or feature requests to C<bug-dbix-class-lookupcolumn-manager at rt.cpan.org>, or through | ||||||||||||
| 206 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-LookupColumn-Manager>. I will be notified, and then you'll | ||||||||||||
| 207 | automatically be notified of progress on your bug as I make changes. | ||||||||||||
| 208 | |||||||||||||
| 209 | |||||||||||||
| 210 | |||||||||||||
| 211 | |||||||||||||
| 212 | =head1 SUPPORT | ||||||||||||
| 213 | |||||||||||||
| 214 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 215 | |||||||||||||
| 216 | perldoc DBIx::Class::LookupColumn::Manager | ||||||||||||
| 217 | |||||||||||||
| 218 | |||||||||||||
| 219 | You can also look for information at: | ||||||||||||
| 220 | |||||||||||||
| 221 | =over 4 | ||||||||||||
| 222 | |||||||||||||
| 223 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||
| 224 | |||||||||||||
| 225 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-LookupColumn-Manager> | ||||||||||||
| 226 | |||||||||||||
| 227 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 228 | |||||||||||||
| 229 | L<http://annocpan.org/dist/DBIx-Class-LookupColumn-Manager> | ||||||||||||
| 230 | |||||||||||||
| 231 | =item * CPAN Ratings | ||||||||||||
| 232 | |||||||||||||
| 233 | L<http://cpanratings.perl.org/d/DBIx-Class-LookupColumn-Manager> | ||||||||||||
| 234 | |||||||||||||
| 235 | =item * Search CPAN | ||||||||||||
| 236 | |||||||||||||
| 237 | L<http://search.cpan.org/dist/DBIx-Class-LookupColumn-Manager/> | ||||||||||||
| 238 | |||||||||||||
| 239 | =back | ||||||||||||
| 240 | |||||||||||||
| 241 | |||||||||||||
| 242 | |||||||||||||
| 243 | =head1 LICENCE AND COPYRIGHT | ||||||||||||
| 244 | |||||||||||||
| 245 | Copyright 2012 Karl Forner and Thomas Rubattel, All Rights Reserved. | ||||||||||||
| 246 | |||||||||||||
| 247 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 248 | under the terms as Perl itself. | ||||||||||||
| 249 | |||||||||||||
| 250 | =cut | ||||||||||||
| 251 | |||||||||||||
| 252 | 18 | 1; # End of DBIx::Class::LookupColumn::Manager | |||||||||||