File Coverage

blib/lib/GenOO/Data/DB/DBIC/Species/Schema.pm
Criterion Covered Total %
statement 34 35 97.1
branch 2 4 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 1 0.0
total 46 52 88.4


line stmt bran cond sub pod time code
1             # POD documentation - main docs before the code
2              
3             =head1 NAME
4              
5             GenOO::Data::DB::DBIC::Species::Schema - Schema object
6              
7             =head1 SYNOPSIS
8              
9             # All the database manipulation with DBIx::Class is done via one central Schema object
10             # which maintains the connection to the database. This class inherits from DBIx::Class::Schema
11             # and loads the tables with sequencing reads automatically.
12            
13             # To create a schema object, call connect on GenOO::Data::DB::DBIC::Species::Schema, passing it a Data Source Name.
14             GenOO::Data::DB::DBIC::Species::Schema->connect("$connection_string");
15              
16             =head1 DESCRIPTION
17              
18             -- Requesting a resultset with "sample_resultset"
19             In High Troughput Sequencing analysis we usually have many db tables with similar
20             structure and columns. Unfortunalely, DBIx::Class requires each Result class to specify
21             the table name explicitly which means that we would have to explicitly create a Result class
22             for every db table. To avoid this, upon request we dynamically create (meta-programming) a new Result class for the provided table name. The new Result class inherits the table structure from
23             a base class which is also provided.
24            
25             One can also hard code a Result class under the namespace GenOO::Data::DB::DBIC::Species::Schema::Result and it will also be registered under the schema.
26            
27             The implementation follows draegtun suggestion in
28             L<http://stackoverflow.com/questions/14515153/use-dbixclass-with-a-single-result-class-definition-to-handle-several-tables-w>
29              
30             =head1 EXAMPLES
31              
32             my $schema = GenOO::Data::DB::DBIC::Species::Schema->connect("dbi:mysql:dbname=$database;host=$host;", $user, $pass);
33             my $result_set = $schema->resultset('sample_table_name');
34            
35             =cut
36              
37             # Let the code begin...
38              
39             package GenOO::Data::DB::DBIC::Species::Schema;
40             $GenOO::Data::DB::DBIC::Species::Schema::VERSION = '1.5.1';
41              
42             #######################################################################
43             ####################### Load External modules #####################
44             #######################################################################
45 1     1   6 use Modern::Perl;
  1         2  
  1         7  
46 1     1   139 use Moose;
  1         1  
  1         6  
47 1     1   5549 use namespace::autoclean;
  1         1  
  1         6  
48 1     1   525 use MooseX::MarkAsMethods autoclean => 1;
  1         5809  
  1         3  
49              
50              
51             #######################################################################
52             ############################ Inheritance ##########################
53             #######################################################################
54             extends 'DBIx::Class::Schema';
55              
56              
57             #######################################################################
58             ######################## Interface Methods ########################
59             #######################################################################
60             sub sample_resultset {
61 17     17 0 36 my ($self, $records_class, @args) = @_;
62            
63 17         26 my $table_name = $args[0];
64 17   33     47 my $class = ref($self) || $self;
65            
66 17 50       42 if (not $self->_source_exists($table_name)) {
67 17         57 $self->_create_and_register_result_class_for($table_name, $records_class);
68             }
69            
70 17         4515 return $self->resultset(@args);
71             }
72              
73              
74             #######################################################################
75             ######################### Private Methods #########################
76             #######################################################################
77             sub _source_exists {
78 17     17   20 my ($self, $table_name) = @_;
79            
80 17 50       59 return 1 if (grep {$_ eq $table_name} $self->sources);
  0         0  
81 17         518 return 0;
82             }
83              
84             sub _create_and_register_result_class_for {
85 17     17   29 my ($self, $table_name, $records_class) = @_;
86            
87 17         43 $self->_create_sample_result_class_for($table_name, $records_class);
88 17         6001 $self->register_class($table_name, 'GenOO::Data::DB::DBIC::Species::Schema::Result::'.$table_name);
89             }
90              
91             =head2 _create_sample_result_class_for
92             Arg [1] : The database table name for a sample table.
93             Description: A result class is created using the provided table name
94             The new class inherits from $records_class
95             Returntype : DBIx::Class Result class
96             =cut
97             sub _create_sample_result_class_for {
98 17     17   22 my ($self, $table_name, $records_class) = @_;
99            
100 17         1200 eval "require $records_class";
101            
102 17         94 my $table_class = "GenOO::Data::DB::DBIC::Species::Schema::Result::$table_name";
103             {
104 1     1   7465 no strict 'refs';
  1         3  
  1         142  
  17         20  
105 17         19 @{$table_class . '::ISA'} = ($records_class);
  17         808  
106             }
107 17         604 $table_class->table($table_name);
108             }
109              
110              
111             #######################################################################
112             ######################### Package Methods #########################
113             #######################################################################
114             __PACKAGE__->load_namespaces; # Load classes from GenOO::Data::DB::DBIC::Species::Schema::Result/ResultSet
115              
116              
117             #######################################################################
118             ############################ Finalize #############################
119             #######################################################################
120             __PACKAGE__->meta->make_immutable(inline_constructor => 0);
121              
122             1;