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