Remove stray $DB::single
[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;
12
13 unless ($INC{"DBIx/Class/CDBICompat.pm"}) {
14   warn "IMPORTANT: DBIx::Class::DB is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n";
15 }
16
17 __PACKAGE__->load_components(qw/ResultSetProxy/);
18
19 {
20     no warnings 'once';
21     *dbi_commit = \&txn_commit;
22     *dbi_rollback = \&txn_rollback;
23 }
24
25 sub storage { shift->schema_instance(@_)->storage; }
26
27 =head1 NAME
28
29 DBIx::Class::DB - (DEPRECATED) classdata schema component
30
31 =head1 DESCRIPTION
32
33 This class is designed to support the Class::DBI connection-as-classdata style
34 for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
35 instead; DBIx::Class::DB will not undergo new development and will be moved
36 to being a CDBICompat-only component before 1.0. In order to discourage further
37 use, documentation has been removed as of 0.08000
38
39 =begin HIDE_BECAUSE_THIS_CLASS_IS_DEPRECATED
40
41 =head1 METHODS
42
43 =head2 storage
44
45 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
46
47 =head2 class_resolver
48
49 ****DEPRECATED****
50
51 Sets or gets the class to use for resolving a class. Defaults to
52 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
53 it. See resolve_class below.
54
55 =cut
56
57 __PACKAGE__->mk_classdata('class_resolver' =>
58                           'DBIx::Class::ClassResolver::PassThrough');
59
60 =head2 connection
61
62   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
63
64 Specifies the arguments that will be passed to DBI->connect(...) to
65 instantiate the class dbh when required.
66
67 =cut
68
69 sub connection {
70   my ($class, @info) = @_;
71   $class->setup_schema_instance unless $class->can('schema_instance');
72   $class->schema_instance->connection(@info);
73 }
74
75 =head2 setup_schema_instance
76
77 Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
78 all class-method operations are proxies through to this object. If you don't
79 call ->connection in your DBIx::Class::DB subclass at load time you *must*
80 call ->setup_schema_instance in order for subclasses to find the schema and
81 register themselves with it.
82
83 =cut
84
85 sub setup_schema_instance {
86   my $class = shift;
87   my $schema = {};
88   bless $schema, 'DBIx::Class::Schema';
89   $class->mk_classdata('schema_instance' => $schema);
90 }
91
92 =head2 txn_begin
93
94 Begins a transaction (does nothing if AutoCommit is off).
95
96 =cut
97
98 sub txn_begin { shift->schema_instance->txn_begin(@_); }
99
100 =head2 txn_commit
101
102 Commits the current transaction.
103
104 =cut
105
106 sub txn_commit { shift->schema_instance->txn_commit(@_); }
107
108 =head2 txn_rollback
109
110 Rolls back the current transaction.
111
112 =cut
113
114 sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
115
116 =head2 txn_do
117
118 Executes a block of code transactionally. If this code reference
119 throws an exception, the transaction is rolled back and the exception
120 is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
121
122 =cut
123
124 sub txn_do { shift->schema_instance->txn_do(@_); }
125
126 {
127   my $warn;
128
129   sub resolve_class {
130     warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
131     return shift->class_resolver->class(@_);
132   }
133 }
134
135 =head2 resultset_instance
136
137 Returns an instance of a resultset for this class - effectively
138 mapping the L<Class::DBI> connection-as-classdata paradigm into the
139 native L<DBIx::Class::ResultSet> system.
140
141 =cut
142
143 sub resultset_instance {
144   $_[0]->result_source_instance->resultset
145 }
146
147 =head2 result_source_instance
148
149 Returns an instance of the result source for this class
150
151 =cut
152
153 sub result_source_instance {
154   my $class = shift;
155   $class = ref $class || $class;
156  
157   __PACKAGE__->mk_classdata(qw/_result_source_instance/)
158     unless __PACKAGE__->can('_result_source_instance');
159
160   
161   return $class->_result_source_instance(@_) if @_;
162
163   my $source = $class->_result_source_instance;
164   return unless Scalar::Util::blessed($source);
165
166   if ($source->result_class ne $class) {
167     # Remove old source instance so we dont get deep recursion
168     #$DB::single = 1;
169     # Need to set it to a non-undef value so that it doesn't just fallback to
170     # a parent class's _result_source_instance
171     #$class->_result_source_instance({});
172     #$class->table($class);
173     #$source = $class->_result_source_instance;
174
175     $source = $source->new({ 
176         %$source, 
177         source_name  => $class,
178         result_class => $class
179     } );
180     $class->_result_source_instance($source);
181     if (my $coderef = $class->can('schema_instance')) {
182         $coderef->($class)->register_class($class, $class);
183     }
184   }
185   return $source;
186 }
187
188 =head2 resolve_class
189
190 ****DEPRECATED****
191
192 See L<class_resolver>
193
194 =head2 dbi_commit
195
196 ****DEPRECATED****
197
198 Alias for L<txn_commit>
199
200 =head2 dbi_rollback
201
202 ****DEPRECATED****
203
204 Alias for L<txn_rollback>
205
206 =end HIDE_BECAUSE_THIS_CLASS_IS_DEPRECATED
207
208 =head1 AUTHORS
209
210 Matt S. Trout <mst@shadowcatsystems.co.uk>
211
212 =head1 LICENSE
213
214 You may distribute this code under the same terms as Perl itself.
215
216 =cut
217
218 1;