3 CXGN::DB::DBICFactory is deprecated, do not use in new code.
7 package CXGN
::DB
::DBICFactory
;
13 use CXGN
::DB
::Connection
;
17 # CXGN::DB::DBICFactory - thin wrapper to instantiate L<DBIx::Class> schemas
18 # using connection params from L<CXGN::DB::Connection>
22 # my $s = CXGN::DB::DBICFactory
23 # ->open_schema( 'Bio::Chado::Schema',
24 # search_path => ['public','sgn'],
28 # my $m = CXGN::DB::DBICFactory
29 # ->merge_schemas( schema_classes =>
30 # [ 'Bio::Chado::Schema',
33 # search_path => ['public','sgn'],
36 # =head1 CLASS METHODS
41 # Usage : my $schema = CXGN::DB::DBICFactory
42 # ->open_schema( $schema_classname,
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
50 # dbconn_args => hashref of args passed directly to
51 # CXGN::DB::Connection->new_no_connect
53 # Side Eff: dies on error
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
69 # make sure the schema class is loaded
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;
80 ( $options{search_path
}
81 ?
(on_connect_do
=> ['SET search_path TO '.join(',',@
{$options{search_path
}})])
90 # =head2 merge_schemas
92 # Usage : my $s = CXGN::DB::DBICFactory
93 # ->merge_schemas( schema_classes =>
94 # [ 'Foo', 'Bar', 'Baz' ],
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.
110 my $merge_increment = 0;
112 # get args and validate
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
;
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
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 "
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
165 return $class->open_schema( $merged_package, %args );
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.