add ResultSetManager tests
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Schema.pm
index 787a018..d492dc1 100644 (file)
@@ -4,7 +4,6 @@ use strict;
 use warnings;
 
 use Carp::Clan qw/^DBIx::Class/;
-use UNIVERSAL::require;
 
 use base qw/DBIx::Class/;
 
@@ -230,7 +229,9 @@ you expect.
 sub compose_connection {
   my ($self, $target, @info) = @_;
   my $base = 'DBIx::Class::ResultSetProxy';
-  $base->require;
+  eval "require ${base};";
+  $self->throw_exception("No arguments to load_classes and couldn't load".
+      " ${base} ($@)") if $@;
 
   if ($self eq $target) {
     # Pathological case, largely caused by the docs on early C::M::DBIC::Plain
@@ -241,10 +242,16 @@ sub compose_connection {
       $class->mk_classdata(resultset_instance => $source->resultset);
       $class->mk_classdata(class_resolver => $self);
     }
+    $self->connection(@info);
     return $self;
   }
 
   my $schema = $self->compose_namespace($target, $base);
+  {
+    no strict 'refs';
+    *{"${target}::schema"} = sub { $schema };
+  }
+
   $schema->connection(@info);
   foreach my $moniker ($schema->sources) {
     my $source = $schema->source($moniker);
@@ -273,8 +280,6 @@ sub compose_namespace {
   }
   {
     no strict 'refs';
-    *{"${target}::schema"} =
-      sub { $schema };
     foreach my $meth (qw/class source resultset/) {
       *{"${target}::${meth}"} =
         sub { shift->schema->$meth(@_) };
@@ -310,7 +315,9 @@ sub connection {
   my $storage_class = $self->storage_type;
   $storage_class = 'DBIx::Class::Storage'.$storage_class
     if $storage_class =~ m/^::/;
-  $storage_class->require;
+  eval "require ${storage_class};";
+  $self->throw_exception("No arguments to load_classes and couldn't load".
+      " ${storage_class} ($@)") if $@;
   my $storage = $storage_class->new;
   $storage->connect_info(\@info);
   $self->storage($storage);
@@ -323,7 +330,31 @@ Conveneience method, equivalent to $schema->clone->connection(@info)
 
 =cut
 
-sub connect { shift->clone->connection(@_) };
+sub connect { shift->clone->connection(@_) }
+
+=head2 txn_begin
+
+Begins a transaction (does nothing if AutoCommit is off).
+
+=cut
+
+sub txn_begin { shift->storage->txn_begin }
+
+=head2 txn_commit
+
+Commits the current transaction.
+
+=cut
+
+sub txn_commit { shift->storage->txn_commit }
+
+=head2 txn_rollback
+
+Rolls back the current transaction.
+
+=cut
+
+sub txn_rollback { shift->storage->txn_rollback }
 
 =head2 clone
 
@@ -343,7 +374,33 @@ sub clone {
   return $clone;
 }
 
-=item throw_exception
+=head2 populate($moniker, \@data);
+
+Populates the source registered with the given moniker with the supplied data.
+@data should be a list of listrefs, the first containing column names, the
+second matching values - i.e.
+
+$schema->populate('Foo', [
+  [ qw/foo_id foo_string/ ],
+  [ 1, 'One' ],
+  [ 2, 'Two' ],
+  ...
+]);
+
+=cut
+
+sub populate {
+  my ($self, $name, $data) = @_;
+  my $rs = $self->resultset($name);
+  my @names = @{shift(@$data)};
+  foreach my $item (@$data) {
+    my %create;
+    @create{@names} = @$item;
+    $rs->create(\%create);
+  }
+}
+
+=head2 throw_exception
 
 Defaults to using Carp::Clan to report errors from user perspective.