116ac2e4cbaeccc3602f38602eb4df2fc5c92753
[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 It will also setup a ->table method on the target class, which lets you
116 resolve database classes based on the schema component name, for example
117
118   MyApp::DB->table('Foo') # returns MyApp::DB::Foo, 
119                           # which ISA MyApp::Schema::Foo
120
121 This is the recommended API for accessing Schema generated classes, and 
122 using it might give you instant advantages with future versions of DBIC.
123
124 =cut
125
126 sub compose_connection {
127   my ($class, $target, @info) = @_;
128   my $conn_class = "${target}::_db";
129   $class->setup_connection_class($conn_class, @info);
130   my %reg = %{ $class->class_registrations };
131   my %target;
132   my %map;
133   while (my ($comp, $comp_class) = each %reg) {
134     my $target_class = "${target}::${comp}";
135     $class->inject_base($target_class, $conn_class, $comp_class);
136     $target_class->table($comp_class->table);
137     @map{$comp, $comp_class} = ($target_class, $target_class);
138   }
139   {
140     no strict 'refs';
141     *{"${target}::class"} =
142       sub {
143         my ($class, $to_map) = @_;
144         return $map{$to_map};
145       };
146     *{"${target}::classes"} = sub { return \%map; };
147   }
148   $conn_class->class_resolver($target);
149 }
150
151 =item setup_connection_class <$target> <@info>
152
153 Sets up a database connection class to inject between the schema
154 and the subclasses the schema creates.
155
156 =cut
157
158 sub setup_connection_class {
159   my ($class, $target, @info) = @_;
160   $class->inject_base($target => 'DBIx::Class');
161   $target->load_components('DB');
162   $target->connection(@info);
163 }
164
165 1;
166
167 =back
168
169 =head1 AUTHORS
170
171 Matt S. Trout <mst@shadowcatsystems.co.uk>
172
173 =head1 LICENSE
174
175 You may distribute this code under the same terms as Perl itself.
176
177 =cut
178