Couple things forgotten during 399b9455/b46b8537
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
CommitLineData
ea2e61bf 1package DBIx::Class::DB;
2
bf5ecff9 3use strict;
4use warnings;
5
1edd1722 6use base qw/DBIx::Class/;
7fb16f1a 7use DBIx::Class::Schema;
8b445e33 8use DBIx::Class::Storage::DBI;
11b78bd6 9use DBIx::Class::ClassResolver::PassThrough;
604d9f38 10use DBI;
6298a324 11use Scalar::Util 'blessed';
12use namespace::clean;
ea2e61bf 13
c216324a 14unless ($INC{"DBIx/Class/CDBICompat.pm"}) {
15 warn "IMPORTANT: DBIx::Class::DB is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";
16}
17
80c90f5d 18__PACKAGE__->load_components(qw/ResultSetProxy/);
2d679367 19
7fb16f1a 20sub storage { shift->schema_instance(@_)->storage; }
9c1700e3 21sub dbi_commit { shift->txn_commit(@_) }
22sub dbi_rollback { shift->txn_rollback(@_) }
2d679367 23
75d07914 24=head1 NAME
34d52be2 25
1c81f831 26DBIx::Class::DB - (DEPRECATED) classdata schema component
34d52be2 27
34d52be2 28=head1 DESCRIPTION
29
66d9ef6b 30This class is designed to support the Class::DBI connection-as-classdata style
31for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
1c81f831 32instead; DBIx::Class::DB will not undergo new development and will be moved
c216324a 33to being a CDBICompat-only component before 1.0. In order to discourage further
34use, documentation has been removed as of 0.08000
35
34d52be2 36=head1 METHODS
37
a807d012 38Hidden.
39
43c7e15d 40=begin hidden
41
42=head2 storage
076652e8 43
8091aa91 44Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
076652e8 45
a807d012 46=end hidden
47
48=cut
49
43c7e15d 50=begin hidden
51
52=head2 class_resolver
87c4e602 53
54****DEPRECATED****
076652e8 55
75d07914 56Sets or gets the class to use for resolving a class. Defaults to
8091aa91 57L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
58it. See resolve_class below.
076652e8 59
a807d012 60=end hidden
61
34d52be2 62=cut
63
e5053694 64__PACKAGE__->mk_classaccessor('class_resolver' =>
181a28f4 65 'DBIx::Class::ClassResolver::PassThrough');
8fe001e1 66
43c7e15d 67=begin hidden
68
69=head2 connection
39fe0e65 70
71 __PACKAGE__->connection($dsn, $user, $pass, $attrs);
72
73Specifies the arguments that will be passed to DBI->connect(...) to
74instantiate the class dbh when required.
75
a807d012 76=end hidden
77
39fe0e65 78=cut
79
8fe001e1 80sub connection {
81 my ($class, @info) = @_;
66d9ef6b 82 $class->setup_schema_instance unless $class->can('schema_instance');
83 $class->schema_instance->connection(@info);
84}
85
43c7e15d 86=begin hidden
87
88=head2 setup_schema_instance
66d9ef6b 89
90Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
91all class-method operations are proxies through to this object. If you don't
92call ->connection in your DBIx::Class::DB subclass at load time you *must*
93call ->setup_schema_instance in order for subclasses to find the schema and
94register themselves with it.
95
a807d012 96=end hidden
97
66d9ef6b 98=cut
99
100sub setup_schema_instance {
101 my $class = shift;
04786a4c 102 my $schema = {};
103 bless $schema, 'DBIx::Class::Schema';
e5053694 104 $class->mk_classaccessor('schema_instance' => $schema);
ea2e61bf 105}
106
43c7e15d 107=begin hidden
108
109=head2 txn_begin
39fe0e65 110
8091aa91 111Begins a transaction (does nothing if AutoCommit is off).
39fe0e65 112
a807d012 113=end hidden
114
39fe0e65 115=cut
116
181a28f4 117sub txn_begin { shift->schema_instance->txn_begin(@_); }
a29644e1 118
43c7e15d 119=begin hidden
120
121=head2 txn_commit
39fe0e65 122
8091aa91 123Commits the current transaction.
39fe0e65 124
a807d012 125=end hidden
126
8091aa91 127=cut
128
181a28f4 129sub txn_commit { shift->schema_instance->txn_commit(@_); }
8091aa91 130
43c7e15d 131=begin hidden
132
133=head2 txn_rollback
8091aa91 134
135Rolls back the current transaction.
39fe0e65 136
a807d012 137=end hidden
138
39fe0e65 139=cut
140
181a28f4 141sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
142
43c7e15d 143=begin hidden
144
145=head2 txn_do
181a28f4 146
147Executes a block of code transactionally. If this code reference
148throws an exception, the transaction is rolled back and the exception
bc0c9800 149is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
181a28f4 150
a807d012 151=end hidden
152
181a28f4 153=cut
154
155sub txn_do { shift->schema_instance->txn_do(@_); }
8b445e33 156
8452e496 157{
158 my $warn;
159
160 sub resolve_class {
161 warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
162 return shift->class_resolver->class(@_);
163 }
7fb16f1a 164}
11b78bd6 165
43c7e15d 166=begin hidden
167
168=head2 resultset_instance
7eb4ecc8 169
170Returns an instance of a resultset for this class - effectively
171mapping the L<Class::DBI> connection-as-classdata paradigm into the
172native L<DBIx::Class::ResultSet> system.
173
a807d012 174=end hidden
175
7eb4ecc8 176=cut
177
178sub resultset_instance {
e87bedbe 179 $_[0]->result_source_instance->resultset
180}
181
43c7e15d 182=begin hidden
183
184=head2 result_source_instance
7137528d 185
186Returns an instance of the result source for this class
187
a807d012 188=end hidden
189
7137528d 190=cut
191
e5053694 192__PACKAGE__->mk_classaccessor('_result_source_instance' => []);
654f330d 193
0e6c5d58 194# Yep. this is horrific. Basically what's happening here is that
195# (with good reason) DBIx::Class::Schema copies the result source for
196# registration. Because we have a retarded setup order forced on us we need
197# to actually make our ->result_source_instance -be- the source used, and we
198# need to get the source name and schema into ourselves. So this makes it
199# happen.
200
201sub _maybe_attach_source_to_schema {
202 my ($class, $source) = @_;
203 if (my $meth = $class->can('schema_instance')) {
9381840d 204 if (my $schema = $class->$meth) {
205 $schema->register_class($class, $class);
206 my $new_source = $schema->source($class);
207 %$source = %$new_source;
208 $schema->source_registrations->{$class} = $source;
209 }
0e6c5d58 210 }
211}
212
e87bedbe 213sub result_source_instance {
214 my $class = shift;
215 $class = ref $class || $class;
d4daee7b 216
0e6c5d58 217 if (@_) {
218 my $source = $_[0];
219 $class->_result_source_instance([$source, $class]);
220 $class->_maybe_attach_source_to_schema($source);
221 return $source;
222 }
e87bedbe 223
654f330d 224 my($source, $result_class) = @{$class->_result_source_instance};
6298a324 225 return unless blessed $source;
e87bedbe 226
654f330d 227 if ($result_class ne $class) { # new class
faaba25f 228 # Give this new class its own source and register it.
8273e845 229 $source = $source->new({
230 %$source,
e87bedbe 231 source_name => $class,
232 result_class => $class
233 } );
654f330d 234 $class->_result_source_instance([$source, $class]);
0e6c5d58 235 $class->_maybe_attach_source_to_schema($source);
7eb4ecc8 236 }
e87bedbe 237 return $source;
7eb4ecc8 238}
239
43c7e15d 240=begin hidden
241
242=head2 resolve_class
7eb4ecc8 243
244****DEPRECATED****
245
f92a9d79 246See L</class_resolver>
7eb4ecc8 247
a807d012 248=end hidden
249
43c7e15d 250=begin hidden
251
252=head2 dbi_commit
7eb4ecc8 253
254****DEPRECATED****
255
f92a9d79 256Alias for L</txn_commit>
7eb4ecc8 257
a807d012 258=end hidden
259
43c7e15d 260=begin hidden
261
262=head2 dbi_rollback
7eb4ecc8 263
264****DEPRECATED****
265
f92a9d79 266Alias for L</txn_rollback>
7eb4ecc8 267
a807d012 268=end hidden
269
a2bd3796 270=head1 FURTHER QUESTIONS?
34d52be2 271
a2bd3796 272Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
34d52be2 273
a2bd3796 274=head1 COPYRIGHT AND LICENSE
34d52be2 275
a2bd3796 276This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
277by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
278redistribute it and/or modify it under the same terms as the
279L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
34d52be2 280
281=cut
282
76b2b77c 2831;