1 package DBIx::Class::Storage;
6 use base qw/DBIx::Class/;
8 use Scalar::Util qw/weaken/;
9 use Carp::Clan qw/DBIx::Class/;
11 __PACKAGE__->load_components(qw/AccessorGroup/);
12 __PACKAGE__->mk_group_accessors('simple' => qw/debug debugobj schema/);
14 package # Hide from PAUSE
15 DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION;
17 use overload '"' => sub {
18 'DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION'
24 return bless $self, $class;
27 package DBIx::Class::Storage;
31 DBIx::Class::Storage - Generic Storage Handler
35 A base implementation of common Storage methods. For specific
36 information about L<DBI>-based storage, see L<DBIx::Class::Storage::DBI>.
44 Instantiates the Storage object.
49 my ($self, $schema) = @_;
51 $self = ref $self if ref $self;
56 $new->set_schema($schema);
57 $new->debugobj(new DBIx::Class::Storage::Statistics());
61 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}
64 if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) {
65 $fh = IO::File->new($1, 'w')
66 or $new->throw_exception("Cannot open trace file $1");
68 $fh = IO::File->new('>&STDERR');
72 $new->debug(1) if $debug_env;
79 Used to reset the schema class or object which owns this
80 storage object, such as during L<DBIx::Class::Schema/clone>.
85 my ($self, $schema) = @_;
86 $self->schema($schema);
87 weaken($self->{schema}) if ref $self->{schema};
92 Returns true if we have an open storage connection, false
93 if it is not (yet) open.
97 sub connected { die "Virtual method!" }
101 Closes any open storage connection unconditionally.
105 sub disconnect { die "Virtual method!" }
107 =head2 ensure_connected
109 Initiate a connection to the storage if one isn't already open.
113 sub ensure_connected { die "Virtual method!" }
115 =head2 throw_exception
117 Throws an exception - croaks.
121 sub throw_exception {
124 $self->schema->throw_exception(@_) if $self->schema;
132 =item Arguments: C<$coderef>, @coderef_args?
134 =item Return Value: The return value of $coderef
138 Executes C<$coderef> with (optional) arguments C<@coderef_args> atomically,
139 returning its result (if any). If an exception is caught, a rollback is issued
140 and the exception is rethrown. If the rollback fails, (i.e. throws an
141 exception) an exception is thrown that includes a "Rollback failed" message.
145 my $author_rs = $schema->resultset('Author')->find(1);
146 my @titles = qw/Night Day It/;
149 # If any one of these fails, the entire transaction fails
150 $author_rs->create_related('books', {
152 }) foreach (@titles);
154 return $author->books;
159 $rs = $schema->txn_do($coderef);
162 if ($@) { # Transaction failed
163 die "something terrible has happened!" #
164 if ($@ =~ /Rollback failed/); # Rollback failed
166 deal_with_failed_transaction();
169 In a nested transaction (calling txn_do() from within a txn_do() coderef) only
170 the outermost transaction will issue a L</txn_commit>, and txn_do() can be
171 called in void, scalar and list context and it will behave as expected.
176 my ($self, $coderef, @args) = @_;
178 ref $coderef eq 'CODE' or $self->throw_exception
179 ('$coderef must be a CODE reference');
181 my (@return_values, $return_value);
183 $self->txn_begin; # If this throws an exception, no rollback is needed
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()
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
194 @return_values = $coderef->(@args);
195 } elsif (defined $wantarray) {
197 $return_value = $coderef->(@args);
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/;
218 $self->throw_exception(
219 "Transaction aborted: $error. Rollback failed: ${rollback_error}"
222 $self->throw_exception($error); # txn failed but rollback succeeded
226 return $wantarray ? @return_values : $return_value;
231 Starts a transaction.
233 See the preferred L</txn_do> method, which allows for
234 an entire code block to be executed transactionally.
238 sub txn_begin { die "Virtual method!" }
242 Issues a commit of the current transaction.
246 sub txn_commit { die "Virtual method!" }
250 Issues a rollback of the current transaction. A nested rollback will
251 throw a L<DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION> exception,
252 which allows the rollback to propagate to the outermost transaction.
256 sub txn_rollback { die "Virtual method!" }
260 Returns a C<sql_maker> object - normally an object of class
261 C<DBIC::SQL::Abstract>.
265 sub sql_maker { die "Virtual method!" }
269 Causes trace information to be emitted on the C<debugobj> object.
270 (or C<STDERR> if C<debugobj> has not specifically been set).
272 This is the equivalent to setting L</DBIC_TRACE> in your
277 Set or retrieve the filehandle used for trace/debug output. This should be
278 an IO::Handle compatible ojbect (only the C<print> method is used. Initially
279 set to be STDERR - although see information on the
280 L<DBIC_TRACE> environment variable.
287 if ($self->debugobj->can('debugfh')) {
288 return $self->debugobj->debugfh(@_);
294 Sets or retrieves the object used for metric collection. Defaults to an instance
295 of L<DBIx::Class::Storage::Statistics> that is compatible with the original
296 method of using a coderef as a callback. See the aforementioned Statistics
297 class for more information.
301 Sets a callback to be executed each time a statement is run; takes a sub
302 reference. Callback is executed as $sub->($op, $info) where $op is
303 SELECT/INSERT/UPDATE/DELETE and $info is what would normally be printed.
305 See L<debugobj> for a better way.
312 if ($self->debugobj->can('callback')) {
313 return $self->debugobj->callback(@_);
319 The cursor class for this Storage object.
323 sub cursor { die "Virtual method!" }
327 Deploy the tables to storage (CREATE TABLE and friends in a SQL-based
328 Storage class). This would normally be called through
329 L<DBIx::Class::Schema/deploy>.
333 sub deploy { die "Virtual method!" }
337 The arguments of C<connect_info> are always a single array reference,
338 and are Storage-handler specific.
340 This is normally accessed via L<DBIx::Class::Schema/connection>, which
341 encapsulates its argument list in an arrayref before calling
342 C<connect_info> here.
346 sub connect_info { die "Virtual method!" }
350 Handle a select statement.
354 sub select { die "Virtual method!" }
358 Handle an insert statement.
362 sub insert { die "Virtual method!" }
366 Handle an update statement.
370 sub update { die "Virtual method!" }
374 Handle a delete statement.
378 sub delete { die "Virtual method!" }
382 Performs a select, fetch and return of data - handles a single row
387 sub select_single { die "Virtual method!" }
389 =head2 columns_info_for
391 Returns database type info for the given table's columns.
395 sub columns_info_for { die "Virtual method!" }
397 =head1 ENVIRONMENT VARIABLES
401 If C<DBIC_TRACE> is set then trace information
402 is produced (as when the L<debug> method is set).
404 If the value is of the form C<1=/path/name> then the trace output is
405 written to the file C</path/name>.
407 This environment variable is checked when the storage object is first
408 created (when you call connect on your schema). So, run-time changes
409 to this environment variable will not take effect unless you also
410 re-connect on your schema.
412 =head2 DBIX_CLASS_STORAGE_DBI_DEBUG
414 Old name for DBIC_TRACE
418 Matt S. Trout <mst@shadowcatsystems.co.uk>
420 Andy Grundman <andy@hybridized.org>
424 You may distribute this code under the same terms as Perl itself.