described schema better, and added note about when you want to use it
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
1 package DBIx::Class::Schema;
2
3 use strict;
4 use warnings;
5
6 use base qw/Class::Data::Inheritable/;
7 use base qw/DBIx::Class/;
8
9 __PACKAGE__->load_components(qw/Exception Componentised/);
10 __PACKAGE__->mk_classdata('class_registrations' => {});
11
12 =head1 NAME
13
14 DBIx::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
43   my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
44
45 =head1 DESCRIPTION
46
47 Creates database classes based on a schema. This allows you to have more than
48 one concurrent connection using the same database classes, by making 
49 subclasses under a new namespace for each connection. If you only need one 
50 class, you should probably use L<DBIx::Class::DB> directly instead.
51
52 =head1 METHODS
53
54 =over 4
55
56 =item register_class <component> <component_class>
57
58 Registers the class in the schema's class_registrations. This is a hash
59 containing database classes, keyed by their monikers. It's used by
60 compose_connection to create/modify all the existing database classes.
61
62 =cut
63
64 sub register_class {
65   my ($class, $name, $to_register) = @_;
66   my %reg = %{$class->class_registrations};
67   $reg{$name} = $to_register;
68   $class->class_registrations(\%reg);
69 }
70
71 =item registered_classes
72
73 Simple read-only accessor for the schema's registered classes. See 
74 register_class above if you want to modify it.
75
76
77 =cut
78
79 sub registered_classes {
80   return values %{shift->class_registrations};
81 }
82
83 =item  load_classes [<classes>}
84
85 Uses L<Module::Find> to find all classes under the database class' namespace,
86 or uses the classes you select.  Then it loads the component (using L<use>), 
87 and registers them (using B<register_class>);
88
89 =cut
90
91 sub load_classes {
92   my $class = shift;
93   my @comp = grep { $_ !~ /^#/ } @_;
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   }
101   foreach my $comp (@comp) {
102     my $comp_class = "${class}::${comp}";
103     eval "use $comp_class"; # If it fails, assume the user fixed it
104     $class->register_class($comp => $comp_class);
105   }
106 }
107
108 =item compose_connection <target> <@db_info>
109
110 This is the most important method in this class. it takes a target namespace,
111 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
112 well as subclasses for each of your database classes in this namespace, using
113 this connection.
114
115 =cut
116
117 sub compose_connection {
118   my ($class, $target, @info) = @_;
119   my $conn_class = "${target}::_db";
120   $class->setup_connection_class($conn_class, @info);
121   my %reg = %{ $class->class_registrations };
122   my %target;
123   my %map;
124   while (my ($comp, $comp_class) = each %reg) {
125     my $target_class = "${target}::${comp}";
126     $class->inject_base($target_class, $conn_class, $comp_class);
127     $target_class->table($comp_class->table);
128     @map{$comp, $comp_class} = ($target_class, $target_class);
129   }
130   {
131     no strict 'refs';
132     *{"${target}::class"} =
133       sub {
134         my ($class, $to_map) = @_;
135         return $map{$to_map};
136       };
137     *{"${target}::classes"} = sub { return \%map; };
138   }
139   $conn_class->class_resolver($target);
140 }
141
142 =item setup_connection_class <$target> <@info>
143
144 Sets up a database connection class to inject between the schema
145 and the subclasses the schema creates.
146
147 =cut
148
149 sub 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
156 1;
157
158 =back
159
160 =head1 AUTHORS
161
162 Matt S. Trout <mst@shadowcatsystems.co.uk>
163
164 =head1 LICENSE
165
166 You may distribute this code under the same terms as Perl itself.
167
168 =cut
169