1 package DBIx::Class::Schema;
7 use base qw/DBIx::Class/;
9 __PACKAGE__->load_components(qw/Exception/);
10 __PACKAGE__->mk_classdata('class_registrations' => {});
14 DBIx::Class::Schema - composable schemas
22 use base qw/DBIx::Class::Schema/;
24 __PACKAGE__->load_classes(qw/Foo Bar Baz/);
28 package My::Schema::Foo;
30 use base qw/DBIx::Class/;
32 __PACKAGE__->load_components(qw/Core PK::Auto::Pg/); # for example
33 __PACKAGE__->table('foo');
40 My::Schema->compose_connection('My::DB', $dsn, $user, $pass, $attrs);
44 my @obj = My::DB::Foo->search({}); # My::DB::Foo isa My::Schema::Foo My::DB
48 Creates database classes based on a schema. This allows you to have more than
49 one concurrent connection using the same database classes, by making
50 subclasses under a new namespace for each connection. If you only need one
51 class, you should probably use L<DBIx::Class::DB> directly instead.
53 NB: If you're used to L<Class::DBI> it's worth reading the L</SYNOPSIS>
54 carefully as DBIx::Class does things a little differently. Note in
55 particular which module inherits off which.
61 =item register_class <component> <component_class>
63 Registers the class in the schema's class_registrations. This is a hash
64 containing database classes, keyed by their monikers. It's used by
65 compose_connection to create/modify all the existing database classes.
70 my ($class, $name, $to_register) = @_;
71 my %reg = %{$class->class_registrations};
72 $reg{$name} = $to_register;
73 $class->class_registrations(\%reg);
76 =item registered_classes
78 Simple read-only accessor for the schema's registered classes. See
79 register_class above if you want to modify it.
84 sub registered_classes {
85 return values %{shift->class_registrations};
88 =item load_classes [<classes>}
90 Uses L<Module::Find> to find all classes under the database class' namespace,
91 or uses the classes you select. Then it loads the component (using L<use>),
92 and registers them (using B<register_class>);
98 my @comp = grep { $_ !~ /^#/ } @_;
100 eval "require Module::Find;";
101 $class->throw("No arguments to load_classes and couldn't load".
102 " Module::Find ($@)") if $@;
103 @comp = map { substr $_, length "${class}::" }
104 Module::Find::findallmod($class);
106 foreach my $comp (@comp) {
107 my $comp_class = "${class}::${comp}";
108 eval "use $comp_class"; # If it fails, assume the user fixed it
109 $class->register_class($comp => $comp_class);
113 =item compose_connection <target> <@db_info>
115 This is the most important method in this class. it takes a target namespace,
116 as well as dbh connection info, and creates a L<DBIx::Class::DB> class as
117 well as subclasses for each of your database classes in this namespace, using
120 It will also setup a ->table method on the target class, which lets you
121 resolve database classes based on the schema component name, for example
123 MyApp::DB->table('Foo') # returns MyApp::DB::Foo,
124 # which ISA MyApp::Schema::Foo
126 This is the recommended API for accessing Schema generated classes, and
127 using it might give you instant advantages with future versions of DBIC.
131 sub compose_connection {
132 my ($class, $target, @info) = @_;
133 my $conn_class = "${target}::_db";
134 $class->setup_connection_class($conn_class, @info);
135 my %reg = %{ $class->class_registrations };
138 while (my ($comp, $comp_class) = each %reg) {
139 my $target_class = "${target}::${comp}";
140 $class->inject_base($target_class, $comp_class, $conn_class);
141 $target_class->table($comp_class->table);
142 @map{$comp, $comp_class} = ($target_class, $target_class);
146 *{"${target}::class"} =
148 my ($class, $to_map) = @_;
149 return $map{$to_map};
151 *{"${target}::classes"} = sub { return \%map; };
153 $conn_class->class_resolver($target);
156 =item setup_connection_class <$target> <@info>
158 Sets up a database connection class to inject between the schema
159 and the subclasses the schema creates.
163 sub setup_connection_class {
164 my ($class, $target, @info) = @_;
165 $class->inject_base($target => 'DBIx::Class::DB');
166 #$target->load_components('DB');
167 $target->connection(@info);
176 Matt S. Trout <mst@shadowcatsystems.co.uk>
180 You may distribute this code under the same terms as Perl itself.