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