more dob updates
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
index afaf27b..469d051 100644 (file)
 package DBIx::Class::DB;
 
-use base qw/Class::Data::Inheritable/;
+use base qw/DBIx::Class/;
+use DBIx::Class::Storage::DBI;
+use DBIx::Class::ClassResolver::PassThrough;
 use DBI;
 
-__PACKAGE__->mk_classdata('_dbi_connect_info');
-__PACKAGE__->mk_classdata('_dbi_connect_package');
-__PACKAGE__->mk_classdata('_dbh');
+*dbi_commit = \&tx_commit;
+*dbi_rollback = \&tx_rollback;
 
 =head1 NAME 
 
-DBIx::Class::DB - DBIx::Class Database connection
+DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance
 
 =head1 SYNOPSIS
 
+  package MyDB;
+
+  use base qw/DBIx::Class/;
+  __PACKAGE__->load_components('DB');
+
+  __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
+
+  package MyDB::MyTable;
+
+  use base qw/MyDB/;
+  __PACKAGE__->load_components('Core'); # just load this in MyDB if it will always be there
+
+  ...
+
 =head1 DESCRIPTION
 
-This class represents the connection to the database
+This class provides a simple way of specifying a database connection.
 
 =head1 METHODS
 
-=over 4
+=head2 storage
+
+Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
+
+=head2 class_resolver
+
+Sets or gets the class to use for resolving a class. Defaults to 
+L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
+it. See resolve_class below.
 
 =cut
 
-sub _get_dbh {
-  my ($class) = @_;
-  my $dbh;
-  unless (($dbh = $class->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
-    $class->_populate_dbh;
-  }
-  return $class->_dbh;
-}
+__PACKAGE__->mk_classdata('storage');
+__PACKAGE__->mk_classdata('class_resolver' =>
+                            'DBIx::Class::ClassResolver::PassThrough');
 
-sub _populate_dbh {
-  my ($class) = @_;
-  my @info = @{$class->_dbi_connect_info || []};
-  my $pkg = $class->_dbi_connect_package || $class;
-  $pkg->_dbh($class->_dbi_connect(@info));
-}
+=head2 connection
 
-sub _dbi_connect {
-  my ($class, @info) = @_;
-  return DBI->connect_cached(@info);
-}
+  __PACKAGE__->connection($dsn, $user, $pass, $attrs);
+
+Specifies the arguments that will be passed to DBI->connect(...) to
+instantiate the class dbh when required.
+
+=cut
 
 sub connection {
   my ($class, @info) = @_;
-  $class->_dbi_connect_package($class);
-  $class->_dbi_connect_info(\@info);
+  my $storage = DBIx::Class::Storage::DBI->new;
+  $storage->connect_info(\@info);
+  $class->storage($storage);
 }
 
-sub dbi_commit { $_[0]->_get_dbh->commit; }
+=head2 tx_begin
 
-sub dbi_rollback { $_[0]->_get_dbh->rollback; }
+Begins a transaction (does nothing if AutoCommit is off).
 
-1;
+=cut
+
+sub tx_begin { $_[0]->storage->tx_begin }
+
+=head2 tx_commit
+
+Commits the current transaction.
+
+=cut
+
+sub tx_commit { $_[0]->storage->tx_commit }
 
-=back
+=head2 tx_rollback
+
+Rolls back the current transaction.
+
+=cut
+
+sub tx_rollback { $_[0]->storage->tx_rollback }
+
+sub resolve_class { return shift->class_resolver->class(@_); }
+
+1;
 
 =head1 AUTHORS
 
-Matt S. Trout <perl-stuff@trout.me.uk>
+Matt S. Trout <mst@shadowcatsystems.co.uk>
 
 =head1 LICENSE