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