update proxy/cascade_update doc in Relationship::Base
[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
40=begin hidden head2 storage
076652e8 41
8091aa91 42Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
076652e8 43
a807d012 44=end hidden
45
46=cut
47
48=begin hidden head2 class_resolver
87c4e602 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
a807d012 56=end hidden
57
34d52be2 58=cut
59
11b78bd6 60__PACKAGE__->mk_classdata('class_resolver' =>
181a28f4 61 'DBIx::Class::ClassResolver::PassThrough');
8fe001e1 62
a807d012 63=begin hidden head2 connection
39fe0e65 64
65 __PACKAGE__->connection($dsn, $user, $pass, $attrs);
66
67Specifies the arguments that will be passed to DBI->connect(...) to
68instantiate the class dbh when required.
69
a807d012 70=end hidden
71
39fe0e65 72=cut
73
8fe001e1 74sub connection {
75 my ($class, @info) = @_;
66d9ef6b 76 $class->setup_schema_instance unless $class->can('schema_instance');
77 $class->schema_instance->connection(@info);
78}
79
a807d012 80=begin hidden head2 setup_schema_instance
66d9ef6b 81
82Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
83all class-method operations are proxies through to this object. If you don't
84call ->connection in your DBIx::Class::DB subclass at load time you *must*
85call ->setup_schema_instance in order for subclasses to find the schema and
86register themselves with it.
87
a807d012 88=end hidden
89
66d9ef6b 90=cut
91
92sub setup_schema_instance {
93 my $class = shift;
04786a4c 94 my $schema = {};
95 bless $schema, 'DBIx::Class::Schema';
7fb16f1a 96 $class->mk_classdata('schema_instance' => $schema);
ea2e61bf 97}
98
a807d012 99=begin hidden head2 txn_begin
39fe0e65 100
8091aa91 101Begins a transaction (does nothing if AutoCommit is off).
39fe0e65 102
a807d012 103=end hidden
104
39fe0e65 105=cut
106
181a28f4 107sub txn_begin { shift->schema_instance->txn_begin(@_); }
a29644e1 108
a807d012 109=begin hidden head2 txn_commit
39fe0e65 110
8091aa91 111Commits the current transaction.
39fe0e65 112
a807d012 113=end hidden
114
8091aa91 115=cut
116
181a28f4 117sub txn_commit { shift->schema_instance->txn_commit(@_); }
8091aa91 118
a807d012 119=begin hidden head2 txn_rollback
8091aa91 120
121Rolls back the current transaction.
39fe0e65 122
a807d012 123=end hidden
124
39fe0e65 125=cut
126
181a28f4 127sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
128
a807d012 129=begin hidden head2 txn_do
181a28f4 130
131Executes a block of code transactionally. If this code reference
132throws an exception, the transaction is rolled back and the exception
bc0c9800 133is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
181a28f4 134
a807d012 135=end hidden
136
181a28f4 137=cut
138
139sub txn_do { shift->schema_instance->txn_do(@_); }
8b445e33 140
8452e496 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 }
7fb16f1a 148}
11b78bd6 149
a807d012 150=begin hidden head2 resultset_instance
7eb4ecc8 151
152Returns an instance of a resultset for this class - effectively
153mapping the L<Class::DBI> connection-as-classdata paradigm into the
154native L<DBIx::Class::ResultSet> system.
155
a807d012 156=end hidden
157
7eb4ecc8 158=cut
159
160sub resultset_instance {
e87bedbe 161 $_[0]->result_source_instance->resultset
162}
163
a807d012 164=begin hidden head2 result_source_instance
7137528d 165
166Returns an instance of the result source for this class
167
a807d012 168=end hidden
169
7137528d 170=cut
171
654f330d 172__PACKAGE__->mk_classdata('_result_source_instance' => []);
173
0e6c5d58 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
181sub _maybe_attach_source_to_schema {
182 my ($class, $source) = @_;
183 if (my $meth = $class->can('schema_instance')) {
9381840d 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 }
0e6c5d58 190 }
191}
192
e87bedbe 193sub result_source_instance {
194 my $class = shift;
195 $class = ref $class || $class;
d4daee7b 196
0e6c5d58 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 }
e87bedbe 203
654f330d 204 my($source, $result_class) = @{$class->_result_source_instance};
6298a324 205 return unless blessed $source;
e87bedbe 206
654f330d 207 if ($result_class ne $class) { # new class
faaba25f 208 # Give this new class its own source and register it.
8273e845 209 $source = $source->new({
210 %$source,
e87bedbe 211 source_name => $class,
212 result_class => $class
213 } );
654f330d 214 $class->_result_source_instance([$source, $class]);
0e6c5d58 215 $class->_maybe_attach_source_to_schema($source);
7eb4ecc8 216 }
e87bedbe 217 return $source;
7eb4ecc8 218}
219
a807d012 220=begin hidden head2 resolve_class
7eb4ecc8 221
222****DEPRECATED****
223
f92a9d79 224See L</class_resolver>
7eb4ecc8 225
a807d012 226=end hidden
227
228=begin hidden head2 dbi_commit
7eb4ecc8 229
230****DEPRECATED****
231
f92a9d79 232Alias for L</txn_commit>
7eb4ecc8 233
a807d012 234=end hidden
235
236=begin hidden head2 dbi_rollback
7eb4ecc8 237
238****DEPRECATED****
239
f92a9d79 240Alias for L</txn_rollback>
7eb4ecc8 241
a807d012 242=end hidden
243
34d52be2 244=head1 AUTHORS
245
daec44b8 246Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 247
248=head1 LICENSE
249
250You may distribute this code under the same terms as Perl itself.
251
252=cut
253
76b2b77c 2541;