release 0.08123
[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
bf5ecff9 20{
21 no warnings 'once';
22 *dbi_commit = \&txn_commit;
23 *dbi_rollback = \&txn_rollback;
24}
8091aa91 25
7fb16f1a 26sub storage { shift->schema_instance(@_)->storage; }
2d679367 27
75d07914 28=head1 NAME
34d52be2 29
1c81f831 30DBIx::Class::DB - (DEPRECATED) classdata schema component
34d52be2 31
34d52be2 32=head1 DESCRIPTION
33
66d9ef6b 34This class is designed to support the Class::DBI connection-as-classdata style
35for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
1c81f831 36instead; DBIx::Class::DB will not undergo new development and will be moved
c216324a 37to being a CDBICompat-only component before 1.0. In order to discourage further
38use, documentation has been removed as of 0.08000
39
40=begin HIDE_BECAUSE_THIS_CLASS_IS_DEPRECATED
34d52be2 41
42=head1 METHODS
43
8091aa91 44=head2 storage
076652e8 45
8091aa91 46Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
076652e8 47
87c4e602 48=head2 class_resolver
49
50****DEPRECATED****
076652e8 51
75d07914 52Sets or gets the class to use for resolving a class. Defaults to
8091aa91 53L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
54it. See resolve_class below.
076652e8 55
34d52be2 56=cut
57
11b78bd6 58__PACKAGE__->mk_classdata('class_resolver' =>
181a28f4 59 'DBIx::Class::ClassResolver::PassThrough');
8fe001e1 60
8091aa91 61=head2 connection
39fe0e65 62
63 __PACKAGE__->connection($dsn, $user, $pass, $attrs);
64
65Specifies the arguments that will be passed to DBI->connect(...) to
66instantiate the class dbh when required.
67
68=cut
69
8fe001e1 70sub connection {
71 my ($class, @info) = @_;
66d9ef6b 72 $class->setup_schema_instance unless $class->can('schema_instance');
73 $class->schema_instance->connection(@info);
74}
75
76=head2 setup_schema_instance
77
78Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
79all class-method operations are proxies through to this object. If you don't
80call ->connection in your DBIx::Class::DB subclass at load time you *must*
81call ->setup_schema_instance in order for subclasses to find the schema and
82register themselves with it.
83
84=cut
85
86sub setup_schema_instance {
87 my $class = shift;
04786a4c 88 my $schema = {};
89 bless $schema, 'DBIx::Class::Schema';
7fb16f1a 90 $class->mk_classdata('schema_instance' => $schema);
ea2e61bf 91}
92
8091aa91 93=head2 txn_begin
39fe0e65 94
8091aa91 95Begins a transaction (does nothing if AutoCommit is off).
39fe0e65 96
97=cut
98
181a28f4 99sub txn_begin { shift->schema_instance->txn_begin(@_); }
a29644e1 100
8091aa91 101=head2 txn_commit
39fe0e65 102
8091aa91 103Commits the current transaction.
39fe0e65 104
8091aa91 105=cut
106
181a28f4 107sub txn_commit { shift->schema_instance->txn_commit(@_); }
8091aa91 108
109=head2 txn_rollback
110
111Rolls back the current transaction.
39fe0e65 112
113=cut
114
181a28f4 115sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
116
117=head2 txn_do
118
119Executes a block of code transactionally. If this code reference
120throws an exception, the transaction is rolled back and the exception
bc0c9800 121is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
181a28f4 122
123=cut
124
125sub txn_do { shift->schema_instance->txn_do(@_); }
8b445e33 126
8452e496 127{
128 my $warn;
129
130 sub resolve_class {
131 warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
132 return shift->class_resolver->class(@_);
133 }
7fb16f1a 134}
11b78bd6 135
7eb4ecc8 136=head2 resultset_instance
137
138Returns an instance of a resultset for this class - effectively
139mapping the L<Class::DBI> connection-as-classdata paradigm into the
140native L<DBIx::Class::ResultSet> system.
141
142=cut
143
144sub resultset_instance {
e87bedbe 145 $_[0]->result_source_instance->resultset
146}
147
7137528d 148=head2 result_source_instance
149
150Returns an instance of the result source for this class
151
152=cut
153
654f330d 154__PACKAGE__->mk_classdata('_result_source_instance' => []);
155
0e6c5d58 156# Yep. this is horrific. Basically what's happening here is that
157# (with good reason) DBIx::Class::Schema copies the result source for
158# registration. Because we have a retarded setup order forced on us we need
159# to actually make our ->result_source_instance -be- the source used, and we
160# need to get the source name and schema into ourselves. So this makes it
161# happen.
162
163sub _maybe_attach_source_to_schema {
164 my ($class, $source) = @_;
165 if (my $meth = $class->can('schema_instance')) {
9381840d 166 if (my $schema = $class->$meth) {
167 $schema->register_class($class, $class);
168 my $new_source = $schema->source($class);
169 %$source = %$new_source;
170 $schema->source_registrations->{$class} = $source;
171 }
0e6c5d58 172 }
173}
174
e87bedbe 175sub result_source_instance {
176 my $class = shift;
177 $class = ref $class || $class;
d4daee7b 178
0e6c5d58 179 if (@_) {
180 my $source = $_[0];
181 $class->_result_source_instance([$source, $class]);
182 $class->_maybe_attach_source_to_schema($source);
183 return $source;
184 }
e87bedbe 185
654f330d 186 my($source, $result_class) = @{$class->_result_source_instance};
6298a324 187 return unless blessed $source;
e87bedbe 188
654f330d 189 if ($result_class ne $class) { # new class
faaba25f 190 # Give this new class its own source and register it.
e87bedbe 191 $source = $source->new({
192 %$source,
193 source_name => $class,
194 result_class => $class
195 } );
654f330d 196 $class->_result_source_instance([$source, $class]);
0e6c5d58 197 $class->_maybe_attach_source_to_schema($source);
7eb4ecc8 198 }
e87bedbe 199 return $source;
7eb4ecc8 200}
201
202=head2 resolve_class
203
204****DEPRECATED****
205
206See L<class_resolver>
207
208=head2 dbi_commit
209
210****DEPRECATED****
211
212Alias for L<txn_commit>
213
214=head2 dbi_rollback
215
216****DEPRECATED****
217
218Alias for L<txn_rollback>
219
c216324a 220=end HIDE_BECAUSE_THIS_CLASS_IS_DEPRECATED
221
34d52be2 222=head1 AUTHORS
223
daec44b8 224Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 225
226=head1 LICENSE
227
228You may distribute this code under the same terms as Perl itself.
229
230=cut
231
76b2b77c 2321;