Back out constructor/prefetch rewrite introduced mainly by 43245ada4a
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
1 package DBIx::Class::DB;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class/;
7 use DBIx::Class::Schema;
8 use DBIx::Class::Storage::DBI;
9 use DBIx::Class::ClassResolver::PassThrough;
10 use DBI;
11 use Scalar::Util 'blessed';
12 use namespace::clean;
13
14 unless ($INC{"DBIx/Class/CDBICompat.pm"}) {
15   warn "IMPORTANT: DBIx::Class::DB is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";
16 }
17
18 __PACKAGE__->load_components(qw/ResultSetProxy/);
19
20 sub storage { shift->schema_instance(@_)->storage; }
21 sub dbi_commit { shift->txn_commit(@_) }
22 sub dbi_rollback { shift->txn_rollback(@_) }
23
24 =head1 NAME
25
26 DBIx::Class::DB - (DEPRECATED) classdata schema component
27
28 =head1 DESCRIPTION
29
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
35
36 =head1 METHODS
37
38 Hidden.
39
40 =begin hidden head2 storage
41
42 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
43
44 =end hidden
45
46 =cut
47
48 =begin hidden head2 class_resolver
49
50 ****DEPRECATED****
51
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.
55
56 =end hidden
57
58 =cut
59
60 __PACKAGE__->mk_classdata('class_resolver' =>
61                           'DBIx::Class::ClassResolver::PassThrough');
62
63 =begin hidden head2 connection
64
65   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
66
67 Specifies the arguments that will be passed to DBI->connect(...) to
68 instantiate the class dbh when required.
69
70 =end hidden
71
72 =cut
73
74 sub connection {
75   my ($class, @info) = @_;
76   $class->setup_schema_instance unless $class->can('schema_instance');
77   $class->schema_instance->connection(@info);
78 }
79
80 =begin hidden head2 setup_schema_instance
81
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.
87
88 =end hidden
89
90 =cut
91
92 sub setup_schema_instance {
93   my $class = shift;
94   my $schema = {};
95   bless $schema, 'DBIx::Class::Schema';
96   $class->mk_classdata('schema_instance' => $schema);
97 }
98
99 =begin hidden head2 txn_begin
100
101 Begins a transaction (does nothing if AutoCommit is off).
102
103 =end hidden
104
105 =cut
106
107 sub txn_begin { shift->schema_instance->txn_begin(@_); }
108
109 =begin hidden head2 txn_commit
110
111 Commits the current transaction.
112
113 =end hidden
114
115 =cut
116
117 sub txn_commit { shift->schema_instance->txn_commit(@_); }
118
119 =begin hidden head2 txn_rollback
120
121 Rolls back the current transaction.
122
123 =end hidden
124
125 =cut
126
127 sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
128
129 =begin hidden head2 txn_do
130
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.
134
135 =end hidden
136
137 =cut
138
139 sub txn_do { shift->schema_instance->txn_do(@_); }
140
141 {
142   my $warn;
143
144   sub resolve_class {
145     warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
146     return shift->class_resolver->class(@_);
147   }
148 }
149
150 =begin hidden head2 resultset_instance
151
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.
155
156 =end hidden
157
158 =cut
159
160 sub resultset_instance {
161   $_[0]->result_source_instance->resultset
162 }
163
164 =begin hidden head2 result_source_instance
165
166 Returns an instance of the result source for this class
167
168 =end hidden
169
170 =cut
171
172 __PACKAGE__->mk_classdata('_result_source_instance' => []);
173
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
179 # happen.
180
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;
189     }
190   }
191 }
192
193 sub result_source_instance {
194   my $class = shift;
195   $class = ref $class || $class;
196
197   if (@_) {
198     my $source = $_[0];
199     $class->_result_source_instance([$source, $class]);
200     $class->_maybe_attach_source_to_schema($source);
201     return $source;
202   }
203
204   my($source, $result_class) = @{$class->_result_source_instance};
205   return unless blessed $source;
206
207   if ($result_class ne $class) {  # new class
208     # Give this new class its own source and register it.
209     $source = $source->new({
210         %$source,
211         source_name  => $class,
212         result_class => $class
213     } );
214     $class->_result_source_instance([$source, $class]);
215     $class->_maybe_attach_source_to_schema($source);
216   }
217   return $source;
218 }
219
220 =begin hidden head2 resolve_class
221
222 ****DEPRECATED****
223
224 See L</class_resolver>
225
226 =end hidden
227
228 =begin hidden head2 dbi_commit
229
230 ****DEPRECATED****
231
232 Alias for L</txn_commit>
233
234 =end hidden
235
236 =begin hidden head2 dbi_rollback
237
238 ****DEPRECATED****
239
240 Alias for L</txn_rollback>
241
242 =end hidden
243
244 =head1 AUTHORS
245
246 Matt S. Trout <mst@shadowcatsystems.co.uk>
247
248 =head1 LICENSE
249
250 You may distribute this code under the same terms as Perl itself.
251
252 =cut
253
254 1;