described schema better, and added note about when you want to use it
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
CommitLineData
a02675cd 1package DBIx::Class::Schema;
2
3use strict;
4use warnings;
5
6use base qw/Class::Data::Inheritable/;
41a6f8c0 7use base qw/DBIx::Class/;
a02675cd 8
227d4dee 9__PACKAGE__->load_components(qw/Exception Componentised/);
74b92d9a 10__PACKAGE__->mk_classdata('class_registrations' => {});
a02675cd 11
c2da098a 12=head1 NAME
13
14DBIx::Class::Schema - composable schemas
15
16=head1 SYNOPSIS
17
18 in My/Schema.pm
19
20 package My::Schema;
21
22 use base qw/DBIx::Class::Schema/;
23
24 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
25
26 in My/Schema/Foo.pm
27
28 package My::Schema::Foo;
29
30 use base qw/DBIx::Class::Core/;
31
32 __PACKAGE__->table('foo');
33 ...
34
35 in My/DB.pm
36
37 use My::Schema;
38
39 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
40
41 then in app code
42
656796f2 43 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
c2da098a 44
45=head1 DESCRIPTION
46
429bd4f1 47Creates database classes based on a schema. This allows you to have more than
48one concurrent connection using the same database classes, by making
49subclasses under a new namespace for each connection. If you only need one
50class, you should probably use L<DBIx::Class::DB> directly instead.
51
c2da098a 52=head1 METHODS
53
54=over 4
55
076652e8 56=item register_class <component> <component_class>
57
58Registers the class in the schema's class_registrations. This is a hash
429bd4f1 59containing database classes, keyed by their monikers. It's used by
076652e8 60compose_connection to create/modify all the existing database classes.
61
c2da098a 62=cut
63
a02675cd 64sub register_class {
65 my ($class, $name, $to_register) = @_;
74b92d9a 66 my %reg = %{$class->class_registrations};
a02675cd 67 $reg{$name} = $to_register;
74b92d9a 68 $class->class_registrations(\%reg);
69}
70
076652e8 71=item registered_classes
72
73Simple read-only accessor for the schema's registered classes. See
74register_class above if you want to modify it.
75
76
77=cut
78
74b92d9a 79sub registered_classes {
80 return values %{shift->class_registrations};
a02675cd 81}
82
076652e8 83=item load_classes [<classes>}
84
429bd4f1 85Uses L<Module::Find> to find all classes under the database class' namespace,
86or uses the classes you select. Then it loads the component (using L<use>),
87and registers them (using B<register_class>);
076652e8 88
89=cut
90
a02675cd 91sub load_classes {
92 my $class = shift;
93 my @comp = grep { $_ !~ /^#/ } @_;
41a6f8c0 94 unless (@comp) {
95 eval "require Module::Find;";
96 $class->throw("No arguments to load_classes and couldn't load".
97 " Module::Find ($@)") if $@;
98 @comp = map { substr $_, length "${class}::" }
99 Module::Find::findallmod($class);
100 }
a02675cd 101 foreach my $comp (@comp) {
102 my $comp_class = "${class}::${comp}";
d3dec624 103 eval "use $comp_class"; # If it fails, assume the user fixed it
a02675cd 104 $class->register_class($comp => $comp_class);
105 }
106}
107
429bd4f1 108=item compose_connection <target> <@db_info>
109
110This is the most important method in this class. it takes a target namespace,
111as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
112well as subclasses for each of your database classes in this namespace, using
113this connection.
076652e8 114
115=cut
116
a02675cd 117sub compose_connection {
118 my ($class, $target, @info) = @_;
11b78bd6 119 my $conn_class = "${target}::_db";
120 $class->setup_connection_class($conn_class, @info);
74b92d9a 121 my %reg = %{ $class->class_registrations };
11b78bd6 122 my %target;
123 my %map;
a02675cd 124 while (my ($comp, $comp_class) = each %reg) {
125 my $target_class = "${target}::${comp}";
f753fd2e 126 $class->inject_base($target_class, $conn_class, $comp_class);
227d4dee 127 $target_class->table($comp_class->table);
11b78bd6 128 @map{$comp, $comp_class} = ($target_class, $target_class);
b7951443 129 }
11b78bd6 130 {
131 no strict 'refs';
132 *{"${target}::class"} =
133 sub {
134 my ($class, $to_map) = @_;
135 return $map{$to_map};
136 };
d3dec624 137 *{"${target}::classes"} = sub { return \%map; };
11b78bd6 138 }
aa698961 139 $conn_class->class_resolver($target);
b7951443 140}
141
076652e8 142=item setup_connection_class <$target> <@info>
143
429bd4f1 144Sets up a database connection class to inject between the schema
145and the subclasses the schema creates.
146
076652e8 147=cut
148
b7951443 149sub setup_connection_class {
150 my ($class, $target, @info) = @_;
151 $class->inject_base($target => 'DBIx::Class');
152 $target->load_components('DB');
153 $target->connection(@info);
154}
155
a02675cd 1561;
c2da098a 157
158=back
159
160=head1 AUTHORS
161
daec44b8 162Matt S. Trout <mst@shadowcatsystems.co.uk>
c2da098a 163
164=head1 LICENSE
165
166You may distribute this code under the same terms as Perl itself.
167
168=cut
169