Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / lib / CXGN / DB / DBICFactory.pm
blobecb4b68e9d036a3577bdae303fb8931abdb0711f
1 =head1 DEPRECATED
3 CXGN::DB::DBICFactory is deprecated, do not use in new code.
5 =cut
7 package CXGN::DB::DBICFactory;
8 use Moose;
10 use English;
11 use Carp;
13 use CXGN::DB::Connection;
15 # =head1 NAME
17 # CXGN::DB::DBICFactory - thin wrapper to instantiate L<DBIx::Class> schemas
18 # using connection params from L<CXGN::DB::Connection>
20 # =head1 SYNOPSIS
22 # my $s = CXGN::DB::DBICFactory
23 # ->open_schema( 'Bio::Chado::Schema',
24 # search_path => ['public','sgn'],
25 # );
28 # my $m = CXGN::DB::DBICFactory
29 # ->merge_schemas( schema_classes =>
30 # [ 'Bio::Chado::Schema',
31 # 'SGN::Schema',
32 # ],
33 # search_path => ['public','sgn'],
34 # );
36 # =head1 CLASS METHODS
38 # =head2 open_schema
40 # Status : public
41 # Usage : my $schema = CXGN::DB::DBICFactory
42 # ->open_schema( $schema_classname,
43 # %options
44 # );
45 # Returns : a DBIx::Class::Schema object
46 # Args : schema class name, hash-style list of options as:
48 # search_path => arrayref of schema names to set
49 # as the search path
50 # dbconn_args => hashref of args passed directly to
51 # CXGN::DB::Connection->new_no_connect
53 # Side Eff: dies on error
55 # =cut
57 sub open_schema {
58 my ($class, $package_name, %options) = @_;
60 my @params = CXGN::DB::Connection
61 ->new_no_connect( $options{dbconn_args} || () )
62 ->get_connection_parameters;
64 $params[-1]->{AutoCommit} = 1; #< override autocommit, which
65 # should be on by default for
66 # DBIC, which does its own
67 # transaction control
69 # make sure the schema class is loaded
70 { no strict 'refs';
71 unless( @{$package_name.'::ISA'} ) {
72 eval "require $package_name"; #< load the package name if we need to
73 die "could not require $package_name: $EVAL_ERROR" if $EVAL_ERROR;
77 return $package_name
78 ->connect( @params,
80 ( $options{search_path}
81 ? (on_connect_do => ['SET search_path TO '.join(',',@{$options{search_path}})])
82 : ()
90 # =head2 merge_schemas
92 # Usage : my $s = CXGN::DB::DBICFactory
93 # ->merge_schemas( schema_classes =>
94 # [ 'Foo', 'Bar', 'Baz' ],
95 # search_path => ...
96 # )
97 # Returns : a L<DBIx::Class::Schema>-based object,
98 # containing all the ResultSource objects from the
99 # listed schema classes
101 # Args : same as open_schema above, except
102 # schema_class is replaced with schema_classes:
104 # schema_classes => arrayref of schema class names,
106 # Merges multiple DBIC Schema namespaces into a single schema.
108 # =cut
110 my $merge_increment = 0;
111 sub merge_schemas {
112 # get args and validate
113 my $class = shift;
114 my %args = @_;
115 my $schema_classes = delete $args{schema_classes};
116 $schema_classes && ref $schema_classes eq 'ARRAY'
117 or croak 'must pass a schema_classes arrayref';
120 #### make a new schema class on the fly
122 # make a unique package name for the merged schema
123 my $merged_package = $class.'::auto_merged::'.++$merge_increment;
125 # create the merged schema package, with the proper base class
127 require DBIx::Class::Schema;
128 no strict 'refs';
129 @{$merged_package.'::ISA'} = qw( DBIx::Class::Schema );
132 # load the sources for each of the given schemas into the new
133 # package, checking for collisions in monikers or table names
134 my %used_monikers;
135 my %used_table_names;
136 for my $schema_class (@$schema_classes) {
137 for my $source_moniker ( $schema_class->sources ) {
138 my $source_obj = $schema_class->source( $source_moniker );
140 # croak for any collisions in source monikers
141 $used_monikers{$source_moniker}
142 and croak "both $schema_class and $used_monikers{$source_moniker} "
143 . "have moniker $source_moniker, cannot merge";
144 $used_monikers{$source_moniker} = $schema_class;
146 # warn about any collisions in table names
147 if( $source_obj->isa('DBIx::Class::ResultSource::Table') ) {
148 my $table_name = $source_obj->from;
150 my $result_class = $source_obj->result_class;
151 $used_table_names{$table_name}
152 and carp "WARNING: both $result_class and "
153 . "$used_table_names{$table_name} use table/view "
154 . "$table_name";
155 $used_table_names{$table_name} = $result_class;
158 # finally, register the resultsource with the target schema
159 $merged_package->register_source( $source_moniker, $source_obj );
163 ### and use open_schema to open it with the given connection
164 ### arguments
165 return $class->open_schema( $merged_package, %args );
169 # =head1 MAINTAINER
171 # Robert Buels
173 # =head1 AUTHOR
175 # Robert Buels, E<lt>rmb32@cornell.eduE<gt>
177 # =head1 COPYRIGHT & LICENSE
179 # Copyright 2009 Boyce Thompson Institute for Plant Research
181 # This program is free software; you can redistribute it and/or modify
182 # it under the same terms as Perl itself.
184 # =cut
186 ####