Merge 'load_namespaces' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
index 5c5b22b..ee4b936 100644 (file)
@@ -593,8 +593,10 @@ sub setup_connection_class {
 
 Instantiates a new Storage object of type
 L<DBIx::Class::Schema/"storage_type"> and passes the arguments to
-$storage->connect_info. Sets the connection in-place on the schema. See
-L<DBIx::Class::Storage::DBI/"connect_info"> for more information.
+$storage->connect_info. Sets the connection in-place on the schema.
+
+See L<DBIx::Class::Storage::DBI/"connect_info"> for DBI-specific syntax,
+or L<DBIx::Class::Storage> in general.
 
 =cut
 
@@ -632,138 +634,83 @@ information.
 
 sub connect { shift->clone->connection(@_) }
 
-=head2 txn_begin
-
-Begins a transaction (does nothing if AutoCommit is off). Equivalent to
-calling $schema->storage->txn_begin. See
-L<DBIx::Class::Storage::DBI/"txn_begin"> for more information.
+=head2 txn_do
 
-=cut
+=over 4
 
-sub txn_begin { shift->storage->txn_begin }
+=item Arguments: C<$coderef>, @coderef_args?
 
-=head2 txn_commit
+=item Return Value: The return value of $coderef
 
-Commits the current transaction. Equivalent to calling
-$schema->storage->txn_commit. See L<DBIx::Class::Storage::DBI/"txn_commit">
-for more information.
+=back
 
-=cut
+Executes C<$coderef> with (optional) arguments C<@coderef_args> atomically,
+returning its result (if any). Equivalent to calling $schema->storage->txn_do.
+See L<DBIx::Class::Storage/"txn_do"> for more information.
 
-sub txn_commit { shift->storage->txn_commit }
+This interface is preferred over using the individual methods L</txn_begin>,
+L</txn_commit>, and L</txn_rollback> below.
 
-=head2 txn_rollback
+=cut
 
-Rolls back the current transaction. Equivalent to calling
-$schema->storage->txn_rollback. See
-L<DBIx::Class::Storage::DBI/"txn_rollback"> for more information.
+sub txn_do {
+  my $self = shift;
 
-=cut
+  $self->storage or $self->throw_exception
+    ('txn_do called on $schema without storage');
 
-sub txn_rollback { shift->storage->txn_rollback }
+  $self->storage->txn_do(@_);
+}
 
-=head2 txn_do
+=head2 txn_begin
 
-=over 4
+Begins a transaction (does nothing if AutoCommit is off). Equivalent to
+calling $schema->storage->txn_begin. See
+L<DBIx::Class::Storage::DBI/"txn_begin"> for more information.
 
-=item Arguments: C<$coderef>, @coderef_args?
+=cut
 
-=item Return Value: The return value of $coderef
+sub txn_begin {
+  my $self = shift;
 
-=back
+  $self->storage or $self->throw_exception
+    ('txn_begin called on $schema without storage');
 
-Executes C<$coderef> with (optional) arguments C<@coderef_args> atomically,
-returning its result (if any). If an exception is caught, a rollback is issued
-and the exception is rethrown. If the rollback fails, (i.e. throws an
-exception) an exception is thrown that includes a "Rollback failed" message.
+  $self->storage->txn_begin;
+}
 
-For example,
+=head2 txn_commit
 
-  my $author_rs = $schema->resultset('Author')->find(1);
-  my @titles = qw/Night Day It/;
+Commits the current transaction. Equivalent to calling
+$schema->storage->txn_commit. See L<DBIx::Class::Storage::DBI/"txn_commit">
+for more information.
 
-  my $coderef = sub {
-    # If any one of these fails, the entire transaction fails
-    $author_rs->create_related('books', {
-      title => $_
-    }) foreach (@titles);
+=cut
 
-    return $author->books;
-  };
+sub txn_commit {
+  my $self = shift;
 
-  my $rs;
-  eval {
-    $rs = $schema->txn_do($coderef);
-  };
+  $self->storage or $self->throw_exception
+    ('txn_commit called on $schema without storage');
 
-  if ($@) {                                  # Transaction failed
-    die "something terrible has happened!"   #
-      if ($@ =~ /Rollback failed/);          # Rollback failed
+  $self->storage->txn_commit;
+}
 
-    deal_with_failed_transaction();
-  }
+=head2 txn_rollback
 
-In a nested transaction (calling txn_do() from within a txn_do() coderef) only
-the outermost transaction will issue a L<DBIx::Class::Schema/"txn_commit"> on
-the Schema's storage, and txn_do() can be called in void, scalar and list
-context and it will behave as expected.
+Rolls back the current transaction. Equivalent to calling
+$schema->storage->txn_rollback. See
+L<DBIx::Class::Storage::DBI/"txn_rollback"> for more information.
 
 =cut
 
-sub txn_do {
-  my ($self, $coderef, @args) = @_;
+sub txn_rollback {
+  my $self = shift;
 
   $self->storage or $self->throw_exception
-    ('txn_do called on $schema without storage');
-  ref $coderef eq 'CODE' or $self->throw_exception
-    ('$coderef must be a CODE reference');
-
-  my (@return_values, $return_value);
-
-  $self->txn_begin; # If this throws an exception, no rollback is needed
-
-  my $wantarray = wantarray; # Need to save this since the context
-                             # inside the eval{} block is independent
-                             # of the context that called txn_do()
-  eval {
-
-    # Need to differentiate between scalar/list context to allow for
-    # returning a list in scalar context to get the size of the list
-    if ($wantarray) {
-      # list context
-      @return_values = $coderef->(@args);
-    } elsif (defined $wantarray) {
-      # scalar context
-      $return_value = $coderef->(@args);
-    } else {
-      # void context
-      $coderef->(@args);
-    }
-    $self->txn_commit;
-  };
-
-  if ($@) {
-    my $error = $@;
+    ('txn_rollback called on $schema without storage');
 
-    eval {
-      $self->txn_rollback;
-    };
-
-    if ($@) {
-      my $rollback_error = $@;
-      my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION";
-      $self->throw_exception($error)  # propagate nested rollback
-        if $rollback_error =~ /$exception_class/;
-
-      $self->throw_exception(
-        "Transaction aborted: $error. Rollback failed: ${rollback_error}"
-      );
-    } else {
-      $self->throw_exception($error); # txn failed but rollback succeeded
-    }
-  }
-
-  return $wantarray ? @return_values : $return_value;
+  $self->storage->txn_rollback;
 }
 
 =head2 clone
@@ -781,7 +728,9 @@ copy.
 
 sub clone {
   my ($self) = @_;
-  my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self);
+  my $clone = { (ref $self ? %$self : ()) };
+  bless $clone, (ref $self || $self);
+
   foreach my $moniker ($self->sources) {
     my $source = $self->source($moniker);
     my $new = $source->new($source);
@@ -882,7 +831,7 @@ sub throw_exception {
 
 =over 4
 
-=item Arguments: $sqlt_args
+=item Arguments: $sqlt_args, $dir
 
 =back
 
@@ -898,9 +847,9 @@ produced include a DROP TABLE statement for each table created.
 =cut
 
 sub deploy {
-  my ($self, $sqltargs) = @_;
+  my ($self, $sqltargs, $dir) = @_;
   $self->throw_exception("Can't deploy without storage") unless $self->storage;
-  $self->storage->deploy($self, undef, $sqltargs);
+  $self->storage->deploy($self, undef, $sqltargs, $dir);
 }
 
 =head2 create_ddl_dir (EXPERIMENTAL)
@@ -919,8 +868,7 @@ across all databases, or fully handle complex relationships.
 
 =cut
 
-sub create_ddl_dir
-{
+sub create_ddl_dir {
   my $self = shift;
 
   $self->throw_exception("Can't create_ddl_dir without storage") unless $self->storage;
@@ -936,8 +884,7 @@ intended for direct end user use.
 
 =cut
 
-sub ddl_filename
-{
+sub ddl_filename {
     my ($self, $type, $dir, $version) = @_;
 
     my $filename = ref($self);
@@ -958,4 +905,3 @@ Matt S. Trout <mst@shadowcatsystems.co.uk>
 You may distribute this code under the same terms as Perl itself.
 
 =cut
-