reshuffling the division of labor between Storage and Storage::DBI (not complete)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage.pm
CommitLineData
4012acd8 1package DBIx::Class::Storage;
a62cf8d4 2
3use strict;
4use warnings;
5
046ad905 6use base qw/DBIx::Class/;
7
8use Scalar::Util qw/weaken/;
9use Carp::Clan qw/DBIx::Class/;
10
11__PACKAGE__->load_components(qw/AccessorGroup/);
12__PACKAGE__->mk_group_accessors('simple' => qw/debug debugobj schema/);
13
4012acd8 14package # Hide from PAUSE
15 DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION;
16
17use overload '"' => sub {
18 'DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION'
19};
20
21sub new {
22 my $class = shift;
23 my $self = {};
24 return bless $self, $class;
25}
26
27package DBIx::Class::Storage;
28
046ad905 29=head1 NAME
30
31DBIx::Class::Storage - Generic Storage Handler
32
33=head1 DESCRIPTION
34
35A base implementation of common Storage methods. For specific
36information about L<DBI>-based storage, see L<DBIx::Class::Storage::DBI>.
37
38=head1 METHODS
39
40=head2 new
41
42Arguments: $schema
43
44Instantiates the Storage object.
45
46=cut
47
48sub new {
49 my ($self, $schema) = @_;
50
51 $self = ref $self if ref $self;
52
53 my $new = {};
54 bless $new, $self;
55
56 $new->set_schema($schema);
57 $new->debugobj(new DBIx::Class::Storage::Statistics());
58
59 my $fh;
60
61 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}
62 || $ENV{DBIC_TRACE};
63
64 if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) {
65 $fh = IO::File->new($1, 'w')
66 or $new->throw_exception("Cannot open trace file $1");
67 } else {
68 $fh = IO::File->new('>&STDERR');
69 }
70
71 $new->debugfh($fh);
72 $new->debug(1) if $debug_env;
73
74 $new;
75}
76
77=head2 set_schema
78
79Used to reset the schema class or object which owns this
80storage object, such as during L<DBIx::Class::Schema/clone>.
81
82=cut
83
84sub set_schema {
85 my ($self, $schema) = @_;
86 $self->schema($schema);
87 weaken($self->{schema}) if ref $self->{schema};
88}
89
90=head2 connected
91
92Returns true if we have an open storage connection, false
93if it is not (yet) open.
94
95=cut
96
a62cf8d4 97sub connected { die "Virtual method!" }
046ad905 98
99=head2 disconnect
100
101Closes any open storage connection unconditionally.
102
103=cut
104
105sub disconnect { die "Virtual method!" }
106
107=head2 ensure_connected
108
109Initiate a connection to the storage if one isn't already open.
110
111=cut
112
a62cf8d4 113sub ensure_connected { die "Virtual method!" }
046ad905 114
115=head2 throw_exception
116
117Throws an exception - croaks.
118
119=cut
120
121sub throw_exception {
122 my $self = shift;
123
124 $self->schema->throw_exception(@_) if $self->schema;
125 croak @_;
126}
a62cf8d4 127
4012acd8 128=head2 txn_do
a62cf8d4 129
4012acd8 130=over 4
a62cf8d4 131
4012acd8 132=item Arguments: C<$coderef>, @coderef_args?
a62cf8d4 133
4012acd8 134=item Return Value: The return value of $coderef
135
136=back
137
138Executes C<$coderef> with (optional) arguments C<@coderef_args> atomically,
139returning its result (if any). If an exception is caught, a rollback is issued
140and the exception is rethrown. If the rollback fails, (i.e. throws an
141exception) an exception is thrown that includes a "Rollback failed" message.
142
143For example,
144
145 my $author_rs = $schema->resultset('Author')->find(1);
146 my @titles = qw/Night Day It/;
147
148 my $coderef = sub {
149 # If any one of these fails, the entire transaction fails
150 $author_rs->create_related('books', {
151 title => $_
152 }) foreach (@titles);
153
154 return $author->books;
155 };
156
157 my $rs;
158 eval {
159 $rs = $schema->txn_do($coderef);
160 };
161
162 if ($@) { # Transaction failed
163 die "something terrible has happened!" #
164 if ($@ =~ /Rollback failed/); # Rollback failed
165
166 deal_with_failed_transaction();
167 }
168
169In a nested transaction (calling txn_do() from within a txn_do() coderef) only
170the outermost transaction will issue a L</txn_commit>, and txn_do() can be
171called in void, scalar and list context and it will behave as expected.
172
173=cut
174
175sub txn_do {
176 my ($self, $coderef, @args) = @_;
177
178 ref $coderef eq 'CODE' or $self->throw_exception
179 ('$coderef must be a CODE reference');
180
181 my (@return_values, $return_value);
182
183 $self->txn_begin; # If this throws an exception, no rollback is needed
184
185 my $wantarray = wantarray; # Need to save this since the context
186 # inside the eval{} block is independent
187 # of the context that called txn_do()
188 eval {
189
190 # Need to differentiate between scalar/list context to allow for
191 # returning a list in scalar context to get the size of the list
192 if ($wantarray) {
193 # list context
194 @return_values = $coderef->(@args);
195 } elsif (defined $wantarray) {
196 # scalar context
197 $return_value = $coderef->(@args);
198 } else {
199 # void context
200 $coderef->(@args);
201 }
202 $self->txn_commit;
203 };
204
205 if ($@) {
206 my $error = $@;
207
208 eval {
209 $self->txn_rollback;
210 };
211
212 if ($@) {
213 my $rollback_error = $@;
214 my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
215 $self->throw_exception($error) # propagate nested rollback
216 if $rollback_error =~ /$exception_class/;
217
218 $self->throw_exception(
219 "Transaction aborted: $error. Rollback failed: ${rollback_error}"
220 );
221 } else {
222 $self->throw_exception($error); # txn failed but rollback succeeded
223 }
224 }
225
226 return $wantarray ? @return_values : $return_value;
a62cf8d4 227}
228
046ad905 229=head2 txn_begin
230
231Starts a transaction.
232
233See the preferred L</txn_do> method, which allows for
234an entire code block to be executed transactionally.
235
236=cut
237
238sub txn_begin { die "Virtual method!" }
239
240=head2 txn_commit
241
242Issues a commit of the current transaction.
243
244=cut
245
246sub txn_commit { die "Virtual method!" }
247
248=head2 txn_rollback
249
250Issues a rollback of the current transaction. A nested rollback will
251throw a L<DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION> exception,
252which allows the rollback to propagate to the outermost transaction.
253
254=cut
255
256sub txn_rollback { die "Virtual method!" }
257
258=head2 sql_maker
259
260Returns a C<sql_maker> object - normally an object of class
261C<DBIC::SQL::Abstract>.
262
263=cut
264
265sub sql_maker { die "Virtual method!" }
266
267=head2 debug
268
269Causes trace information to be emitted on the C<debugobj> object.
270(or C<STDERR> if C<debugobj> has not specifically been set).
271
272This is the equivalent to setting L</DBIC_TRACE> in your
273shell environment.
274
275=head2 debugfh
276
277Set or retrieve the filehandle used for trace/debug output. This should be
278an IO::Handle compatible ojbect (only the C<print> method is used. Initially
279set to be STDERR - although see information on the
280L<DBIC_TRACE> environment variable.
281
282=cut
283
284sub debugfh {
285 my $self = shift;
286
287 if ($self->debugobj->can('debugfh')) {
288 return $self->debugobj->debugfh(@_);
289 }
290}
291
292=head2 debugobj
293
294Sets or retrieves the object used for metric collection. Defaults to an instance
295of L<DBIx::Class::Storage::Statistics> that is compatible with the original
296method of using a coderef as a callback. See the aforementioned Statistics
297class for more information.
298
299=head2 debugcb
300
301Sets a callback to be executed each time a statement is run; takes a sub
302reference. Callback is executed as $sub->($op, $info) where $op is
303SELECT/INSERT/UPDATE/DELETE and $info is what would normally be printed.
304
305See L<debugobj> for a better way.
306
307=cut
308
309sub debugcb {
310 my $self = shift;
311
312 if ($self->debugobj->can('callback')) {
313 return $self->debugobj->callback(@_);
314 }
315}
316
317=head2 cursor
318
319The cursor class for this Storage object.
320
321=cut
322
323sub cursor { die "Virtual method!" }
324
325=head2 deploy
326
327Deploy the tables to storage (CREATE TABLE and friends in a SQL-based
328Storage class). This would normally be called through
329L<DBIx::Class::Schema/deploy>.
330
331=cut
332
333sub deploy { die "Virtual method!" }
334
335sub on_connect_do { die "Virtual method!" }
336sub connect_info { die "Virtual method!" }
337sub insert { die "Virtual method!" }
338sub update { die "Virtual method!" }
339sub delete { die "Virtual method!" }
340sub select { die "Virtual method!" }
341sub select_single { die "Virtual method!" }
342sub columns_info_for { die "Virtual method!" }
343
344=head1 ENVIRONMENT VARIABLES
345
346=head2 DBIC_TRACE
347
348If C<DBIC_TRACE> is set then trace information
349is produced (as when the L<debug> method is set).
350
351If the value is of the form C<1=/path/name> then the trace output is
352written to the file C</path/name>.
353
354This environment variable is checked when the storage object is first
355created (when you call connect on your schema). So, run-time changes
356to this environment variable will not take effect unless you also
357re-connect on your schema.
358
359=head2 DBIX_CLASS_STORAGE_DBI_DEBUG
360
361Old name for DBIC_TRACE
362
363=head1 AUTHORS
364
365Matt S. Trout <mst@shadowcatsystems.co.uk>
366
367Andy Grundman <andy@hybridized.org>
368
369=head1 LICENSE
370
371You may distribute this code under the same terms as Perl itself.
372
373=cut
374
a62cf8d4 3751;