clean up transaction handling
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
1 package DBIx::Class::DB;
2
3 use base qw/DBIx::Class/;
4 use DBIx::Class::Storage::DBI;
5 use DBIx::Class::ClassResolver::PassThrough;
6 use DBI;
7
8 *dbi_commit = \&tx_commit;
9 *dbi_rollback = \&tx_rollback;
10
11 =head1 NAME 
12
13 DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance
14
15 =head1 SYNOPSIS
16
17   package MyDB;
18
19   use base qw/DBIx::Class/;
20   __PACKAGE__->load_components('DB');
21
22   __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
23
24   package MyDB::MyTable;
25
26   use base qw/MyDB/;
27   __PACKAGE__->load_components('Core');
28
29   ...
30
31 =head1 DESCRIPTION
32
33 This class provides a simple way of specifying a database connection.
34
35 =head1 METHODS
36
37 =head2 storage
38
39 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
40
41 =head2 class_resolver
42
43 Sets or gets the class to use for resolving a class. Defaults to 
44 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
45 it. See resolve_class below.
46
47 =cut
48
49 __PACKAGE__->mk_classdata('storage');
50 __PACKAGE__->mk_classdata('class_resolver' =>
51                             'DBIx::Class::ClassResolver::PassThrough');
52
53 =head2 connection
54
55   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
56
57 Specifies the arguments that will be passed to DBI->connect(...) to
58 instantiate the class dbh when required.
59
60 =cut
61
62 sub connection {
63   my ($class, @info) = @_;
64   my $storage = DBIx::Class::Storage::DBI->new;
65   $storage->connect_info(\@info);
66   $class->storage($storage);
67 }
68
69 =head2 tx_begin
70
71 Begins a transaction (does nothing if AutoCommit is off).
72
73 =cut
74
75 sub tx_commit { $_[0]->storage->tx_begin }
76
77 =head2 tx_commit
78
79 Commits the current transaction.
80
81 =cut
82
83 sub tx_commit { $_[0]->storage->tx_commit }
84
85 =head2 tx_rollback
86
87 Rolls back the current transaction.
88
89 =cut
90
91 sub tx_rollback { $_[0]->storage->tx_rollback }
92
93 sub resolve_class { return shift->class_resolver->class(@_); }
94
95 1;
96
97 =head1 AUTHORS
98
99 Matt S. Trout <mst@shadowcatsystems.co.uk>
100
101 =head1 LICENSE
102
103 You may distribute this code under the same terms as Perl itself.
104
105 =cut
106