1 package DBIx::Class::DB;
6 use base qw/DBIx::Class/;
7 use DBIx::Class::Schema;
8 use DBIx::Class::Storage::DBI;
9 use DBIx::Class::ClassResolver::PassThrough;
11 use Scalar::Util 'blessed';
14 unless ($INC{"DBIx/Class/CDBICompat.pm"}) {
15 warn "IMPORTANT: DBIx::Class::DB is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";
18 __PACKAGE__->load_components(qw/ResultSetProxy/);
20 sub storage { shift->schema_instance(@_)->storage; }
21 sub dbi_commit { shift->txn_commit(@_) }
22 sub dbi_rollback { shift->txn_rollback(@_) }
26 DBIx::Class::DB - (DEPRECATED) classdata schema component
30 This class is designed to support the Class::DBI connection-as-classdata style
31 for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
32 instead; DBIx::Class::DB will not undergo new development and will be moved
33 to being a CDBICompat-only component before 1.0. In order to discourage further
34 use, documentation has been removed as of 0.08000
40 =begin hidden head2 storage
42 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
48 =begin hidden head2 class_resolver
52 Sets or gets the class to use for resolving a class. Defaults to
53 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
54 it. See resolve_class below.
60 __PACKAGE__->mk_classdata('class_resolver' =>
61 'DBIx::Class::ClassResolver::PassThrough');
63 =begin hidden head2 connection
65 __PACKAGE__->connection($dsn, $user, $pass, $attrs);
67 Specifies the arguments that will be passed to DBI->connect(...) to
68 instantiate the class dbh when required.
75 my ($class, @info) = @_;
76 $class->setup_schema_instance unless $class->can('schema_instance');
77 $class->schema_instance->connection(@info);
80 =begin hidden head2 setup_schema_instance
82 Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
83 all class-method operations are proxies through to this object. If you don't
84 call ->connection in your DBIx::Class::DB subclass at load time you *must*
85 call ->setup_schema_instance in order for subclasses to find the schema and
86 register themselves with it.
92 sub setup_schema_instance {
95 bless $schema, 'DBIx::Class::Schema';
96 $class->mk_classdata('schema_instance' => $schema);
99 =begin hidden head2 txn_begin
101 Begins a transaction (does nothing if AutoCommit is off).
107 sub txn_begin { shift->schema_instance->txn_begin(@_); }
109 =begin hidden head2 txn_commit
111 Commits the current transaction.
117 sub txn_commit { shift->schema_instance->txn_commit(@_); }
119 =begin hidden head2 txn_rollback
121 Rolls back the current transaction.
127 sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
129 =begin hidden head2 txn_do
131 Executes a block of code transactionally. If this code reference
132 throws an exception, the transaction is rolled back and the exception
133 is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
139 sub txn_do { shift->schema_instance->txn_do(@_); }
145 warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
146 return shift->class_resolver->class(@_);
150 =begin hidden head2 resultset_instance
152 Returns an instance of a resultset for this class - effectively
153 mapping the L<Class::DBI> connection-as-classdata paradigm into the
154 native L<DBIx::Class::ResultSet> system.
160 sub resultset_instance {
161 $_[0]->result_source_instance->resultset
164 =begin hidden head2 result_source_instance
166 Returns an instance of the result source for this class
172 __PACKAGE__->mk_classdata('_result_source_instance' => []);
174 # Yep. this is horrific. Basically what's happening here is that
175 # (with good reason) DBIx::Class::Schema copies the result source for
176 # registration. Because we have a retarded setup order forced on us we need
177 # to actually make our ->result_source_instance -be- the source used, and we
178 # need to get the source name and schema into ourselves. So this makes it
181 sub _maybe_attach_source_to_schema {
182 my ($class, $source) = @_;
183 if (my $meth = $class->can('schema_instance')) {
184 if (my $schema = $class->$meth) {
185 $schema->register_class($class, $class);
186 my $new_source = $schema->source($class);
187 %$source = %$new_source;
188 $schema->source_registrations->{$class} = $source;
193 sub result_source_instance {
195 $class = ref $class || $class;
199 $class->_result_source_instance([$source, $class]);
200 $class->_maybe_attach_source_to_schema($source);
204 my($source, $result_class) = @{$class->_result_source_instance};
205 return unless blessed $source;
207 if ($result_class ne $class) { # new class
208 # Give this new class its own source and register it.
209 $source = $source->new({
211 source_name => $class,
212 result_class => $class
214 $class->_result_source_instance([$source, $class]);
215 $class->_maybe_attach_source_to_schema($source);
220 =begin hidden head2 resolve_class
224 See L</class_resolver>
228 =begin hidden head2 dbi_commit
232 Alias for L</txn_commit>
236 =begin hidden head2 dbi_rollback
240 Alias for L</txn_rollback>
246 Matt S. Trout <mst@shadowcatsystems.co.uk>
250 You may distribute this code under the same terms as Perl itself.