| 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.4.9'; | ||
| 41 | |||
| 42 | ####################################################################### | ||
| 43 | ####################### Load External modules ##################### | ||
| 44 | ####################################################################### | ||
| 45 | use Modern::Perl; | ||
| 46 | use Moose; | ||
| 47 | use namespace::autoclean; | ||
| 48 | use MooseX::MarkAsMethods autoclean => 1; | ||
| 49 | |||
| 50 | |||
| 51 | ####################################################################### | ||
| 52 | ############################ Inheritance ########################## | ||
| 53 | ####################################################################### | ||
| 54 | extends 'DBIx::Class::Schema'; | ||
| 55 | |||
| 56 | |||
| 57 | ####################################################################### | ||
| 58 | ######################## Interface Methods ######################## | ||
| 59 | ####################################################################### | ||
| 60 | sub sample_resultset { | ||
| 61 | my ($self, $records_class, @args) = @_; | ||
| 62 | |||
| 63 | my $table_name = $args[0]; | ||
| 64 | my $class = ref($self) || $self; | ||
| 65 | |||
| 66 | if (not $self->_source_exists($table_name)) { | ||
| 67 | $self->_create_and_register_result_class_for($table_name, $records_class); | ||
| 68 | } | ||
| 69 | |||
| 70 | return $self->resultset(@args); | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | ####################################################################### | ||
| 75 | ######################### Private Methods ######################### | ||
| 76 | ####################################################################### | ||
| 77 | sub _source_exists { | ||
| 78 | my ($self, $table_name) = @_; | ||
| 79 | |||
| 80 | return 1 if (grep {$_ eq $table_name} $self->sources); | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _create_and_register_result_class_for { | ||
| 85 | my ($self, $table_name, $records_class) = @_; | ||
| 86 | |||
| 87 | $self->_create_sample_result_class_for($table_name, $records_class); | ||
| 88 | $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 | my ($self, $table_name, $records_class) = @_; | ||
| 99 | |||
| 100 | eval "require $records_class"; | ||
| 101 | |||
| 102 | my $table_class = "GenOO::Data::DB::DBIC::Species::Schema::Result::$table_name"; | ||
| 103 | { | ||
| 104 | no strict 'refs'; | ||
| 105 | @{$table_class . '::ISA'} = ($records_class); | ||
| 106 | } | ||
| 107 | $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; |