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