Chucked out some unrequired column case stuff in CDBICompat
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
CommitLineData
a02675cd 1package DBIx::Class::Schema;
2
3use strict;
4use warnings;
63e9583a 5use DBIx::Class::DB;
a02675cd 6
41a6f8c0 7use base qw/DBIx::Class/;
a02675cd 8
42a1aaa1 9__PACKAGE__->load_components(qw/Exception/);
74b92d9a 10__PACKAGE__->mk_classdata('class_registrations' => {});
d7156e50 11__PACKAGE__->mk_classdata('storage_type' => 'DBI');
12__PACKAGE__->mk_classdata('storage');
a02675cd 13
c2da098a 14=head1 NAME
15
16DBIx::Class::Schema - composable schemas
17
18=head1 SYNOPSIS
19
03312470 20in My/Schema.pm
c2da098a 21
22 package My::Schema;
23
24 use base qw/DBIx::Class::Schema/;
25
26 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
27
03312470 28in My/Schema/Foo.pm
c2da098a 29
30 package My::Schema::Foo;
31
03312470 32 use base qw/DBIx::Class/;
c2da098a 33
54540863 34 __PACKAGE__->load_components(qw/PK::Auto::Pg Core/); # for example
c2da098a 35 __PACKAGE__->table('foo');
36 ...
37
03312470 38in My/DB.pm
c2da098a 39
40 use My::Schema;
41
42 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
43
03312470 44then in app code
c2da098a 45
656796f2 46 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
c2da098a 47
48=head1 DESCRIPTION
49
429bd4f1 50Creates database classes based on a schema. This allows you to have more than
51one concurrent connection using the same database classes, by making
52subclasses under a new namespace for each connection. If you only need one
53class, you should probably use L<DBIx::Class::DB> directly instead.
54
03312470 55NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
56carefully as DBIx::Class does things a little differently. Note in
57particular which module inherits off which.
58
c2da098a 59=head1 METHODS
60
130c6439 61=head2 register_class <component> <component_class>
076652e8 62
63Registers the class in the schema's class_registrations. This is a hash
429bd4f1 64containing database classes, keyed by their monikers. It's used by
076652e8 65compose_connection to create/modify all the existing database classes.
66
c2da098a 67=cut
68
a02675cd 69sub register_class {
7fb16f1a 70 my ($self, $name, $to_register) = @_;
71 my %reg = %{$self->class_registrations};
a02675cd 72 $reg{$name} = $to_register;
7fb16f1a 73 $self->class_registrations(\%reg);
74 $to_register->result_source->schema($self);
74b92d9a 75}
76
130c6439 77=head2 registered_classes
076652e8 78
79Simple read-only accessor for the schema's registered classes. See
80register_class above if you want to modify it.
81
82
83=cut
84
74b92d9a 85sub registered_classes {
86 return values %{shift->class_registrations};
a02675cd 87}
88
bfb2bd4f 89=head2 class
90
91 my $class = $schema->class('Foo');
92
93Shortcut to retrieve a single class by its registered name
94
95=cut
96
97sub class {
98 my ($self, $class) = @_;
99 return $self->class_registrations->{$class};
100}
101
ea20d0fd 102=head2 source
103
104 my $source = $schema->source('Foo');
105
106Returns the result source object for the registered name
107
108=cut
109
110sub source {
111 my ($self, $class) = @_;
112 return $self->class_registrations->{$class}->result_source;
113}
114
115=head2 resultset
116
117 my $rs = $schema->resultset('Foo');
118
119Returns the resultset for the registered name
120
121=cut
122
123sub resultset {
124 my ($self, $class) = @_;
125 return $self->class_registrations->{$class}->result_source->resultset;
126}
127
128
130c6439 129=head2 load_classes [<classes>, (<class>, <class>), {<namespace> => [<classes>]}]
076652e8 130
429bd4f1 131Uses L<Module::Find> to find all classes under the database class' namespace,
132or uses the classes you select. Then it loads the component (using L<use>),
133and registers them (using B<register_class>);
076652e8 134
5ce32fc1 135It is possible to comment out classes with a leading '#', but note that perl
136will think it's a mistake (trying to use a comment in a qw list) so you'll
137need to add "no warnings 'qw';" before your load_classes call.
138
076652e8 139=cut
140
a02675cd 141sub load_classes {
5ce32fc1 142 my ($class, @params) = @_;
143
144 my %comps_for;
145
146 if (@params) {
147 foreach my $param (@params) {
148 if (ref $param eq 'ARRAY') {
149 # filter out commented entries
150 my @modules = grep { $_ !~ /^#/ } @$param;
151
152 push (@{$comps_for{$class}}, @modules);
153 }
154 elsif (ref $param eq 'HASH') {
155 # more than one namespace possible
156 for my $comp ( keys %$param ) {
157 # filter out commented entries
158 my @modules = grep { $_ !~ /^#/ } @{$param->{$comp}};
159
160 push (@{$comps_for{$comp}}, @modules);
161 }
162 }
163 else {
164 # filter out commented entries
165 push (@{$comps_for{$class}}, $param) if $param !~ /^#/;
166 }
167 }
168 } else {
41a6f8c0 169 eval "require Module::Find;";
170 $class->throw("No arguments to load_classes and couldn't load".
171 " Module::Find ($@)") if $@;
5ce32fc1 172 my @comp = map { substr $_, length "${class}::" } Module::Find::findallmod($class);
173 $comps_for{$class} = \@comp;
41a6f8c0 174 }
5ce32fc1 175
176 foreach my $prefix (keys %comps_for) {
177 foreach my $comp (@{$comps_for{$prefix}||[]}) {
178 my $comp_class = "${prefix}::${comp}";
5ce32fc1 179 eval "use $comp_class"; # If it fails, assume the user fixed it
bfb2bd4f 180 if ($@) {
181 die $@ unless $@ =~ /Can't locate/;
182 }
5ce32fc1 183 $class->register_class($comp => $comp_class);
184 }
a02675cd 185 }
186}
187
130c6439 188=head2 compose_connection <target> <@db_info>
429bd4f1 189
190This is the most important method in this class. it takes a target namespace,
191as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
192well as subclasses for each of your database classes in this namespace, using
193this connection.
076652e8 194
54540863 195It will also setup a ->class method on the target class, which lets you
8c130052 196resolve database classes based on the schema component name, for example
197
54540863 198 MyApp::DB->class('Foo') # returns MyApp::DB::Foo,
8c130052 199 # which ISA MyApp::Schema::Foo
200
201This is the recommended API for accessing Schema generated classes, and
202using it might give you instant advantages with future versions of DBIC.
203
54540863 204WARNING: Loading components into Schema classes after compose_connection
205may not cause them to be seen by the classes in your target namespace due
206to the dispatch table approach used by Class::C3. If you do this you may find
207you need to call Class::C3->reinitialize() afterwards to get the behaviour
208you expect.
209
076652e8 210=cut
211
a02675cd 212sub compose_connection {
ea20d0fd 213 my ($self, $target, @info) = @_;
11b78bd6 214 my $conn_class = "${target}::_db";
ea20d0fd 215 $self->setup_connection_class($conn_class, @info);
216 my $schema = $self->compose_namespace($target, $conn_class);
bfb2bd4f 217 $schema->storage($conn_class->storage);
218 foreach my $class ($schema->registered_classes) {
219 my $source = $class->result_source;
220 $source = $source->new($source);
221 $source->schema($schema);
222 $source->result_class($class);
223 $class->mk_classdata(result_source => $source);
ea20d0fd 224 $class->mk_classdata(resultset_instance => $source->resultset);
bfb2bd4f 225 }
226 return $schema;
e678398e 227}
228
229sub compose_namespace {
230 my ($class, $target, $base) = @_;
74b92d9a 231 my %reg = %{ $class->class_registrations };
11b78bd6 232 my %target;
233 my %map;
bfb2bd4f 234 my $schema = bless({ }, $class);
a02675cd 235 while (my ($comp, $comp_class) = each %reg) {
236 my $target_class = "${target}::${comp}";
ea20d0fd 237 $class->inject_base($target_class, $comp_class, ($base ? $base : ()));
11b78bd6 238 @map{$comp, $comp_class} = ($target_class, $target_class);
b7951443 239 }
bfb2bd4f 240 $schema->class_registrations(\%map);
11b78bd6 241 {
242 no strict 'refs';
bfb2bd4f 243 *{"${target}::schema"} =
244 sub { $schema };
11b78bd6 245 *{"${target}::class"} =
bfb2bd4f 246 sub { shift->schema->class(@_) };
11b78bd6 247 }
e678398e 248 $base->class_resolver($target);
bfb2bd4f 249 return $schema;
b7951443 250}
251
130c6439 252=head2 setup_connection_class <$target> <@info>
076652e8 253
429bd4f1 254Sets up a database connection class to inject between the schema
255and the subclasses the schema creates.
256
076652e8 257=cut
258
b7951443 259sub setup_connection_class {
260 my ($class, $target, @info) = @_;
63e9583a 261 $class->inject_base($target => 'DBIx::Class::DB');
262 #$target->load_components('DB');
b7951443 263 $target->connection(@info);
264}
265
a02675cd 2661;
c2da098a 267
c2da098a 268=head1 AUTHORS
269
daec44b8 270Matt S. Trout <mst@shadowcatsystems.co.uk>
c2da098a 271
272=head1 LICENSE
273
274You may distribute this code under the same terms as Perl itself.
275
276=cut
277