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