Commit | Line | Data |
a02675cd |
1 | package DBIx::Class::Schema; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use base qw/Class::Data::Inheritable/; |
41a6f8c0 |
7 | use 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 | |
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 | |
656796f2 |
43 | my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB |
c2da098a |
44 | |
45 | =head1 DESCRIPTION |
46 | |
429bd4f1 |
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 | |
c2da098a |
52 | =head1 METHODS |
53 | |
54 | =over 4 |
55 | |
076652e8 |
56 | =item register_class <component> <component_class> |
57 | |
58 | Registers the class in the schema's class_registrations. This is a hash |
429bd4f1 |
59 | containing database classes, keyed by their monikers. It's used by |
076652e8 |
60 | compose_connection to create/modify all the existing database classes. |
61 | |
c2da098a |
62 | =cut |
63 | |
a02675cd |
64 | sub 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 | |
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 | |
74b92d9a |
79 | sub registered_classes { |
80 | return values %{shift->class_registrations}; |
a02675cd |
81 | } |
82 | |
076652e8 |
83 | =item load_classes [<classes>} |
84 | |
429bd4f1 |
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>); |
076652e8 |
88 | |
89 | =cut |
90 | |
a02675cd |
91 | sub 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 | |
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. |
076652e8 |
114 | |
8c130052 |
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 | |
076652e8 |
124 | =cut |
125 | |
a02675cd |
126 | sub compose_connection { |
127 | my ($class, $target, @info) = @_; |
11b78bd6 |
128 | my $conn_class = "${target}::_db"; |
129 | $class->setup_connection_class($conn_class, @info); |
74b92d9a |
130 | my %reg = %{ $class->class_registrations }; |
11b78bd6 |
131 | my %target; |
132 | my %map; |
a02675cd |
133 | while (my ($comp, $comp_class) = each %reg) { |
134 | my $target_class = "${target}::${comp}"; |
f753fd2e |
135 | $class->inject_base($target_class, $conn_class, $comp_class); |
227d4dee |
136 | $target_class->table($comp_class->table); |
11b78bd6 |
137 | @map{$comp, $comp_class} = ($target_class, $target_class); |
b7951443 |
138 | } |
11b78bd6 |
139 | { |
140 | no strict 'refs'; |
141 | *{"${target}::class"} = |
142 | sub { |
143 | my ($class, $to_map) = @_; |
144 | return $map{$to_map}; |
145 | }; |
d3dec624 |
146 | *{"${target}::classes"} = sub { return \%map; }; |
11b78bd6 |
147 | } |
aa698961 |
148 | $conn_class->class_resolver($target); |
b7951443 |
149 | } |
150 | |
076652e8 |
151 | =item setup_connection_class <$target> <@info> |
152 | |
429bd4f1 |
153 | Sets up a database connection class to inject between the schema |
154 | and the subclasses the schema creates. |
155 | |
076652e8 |
156 | =cut |
157 | |
b7951443 |
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 | |
a02675cd |
165 | 1; |
c2da098a |
166 | |
167 | =back |
168 | |
169 | =head1 AUTHORS |
170 | |
daec44b8 |
171 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
c2da098a |
172 | |
173 | =head1 LICENSE |
174 | |
175 | You may distribute this code under the same terms as Perl itself. |
176 | |
177 | =cut |
178 | |