DB.pm now keeps a Schema Instance
[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::Schema;
5 use DBIx::Class::Storage::DBI;
6 use DBIx::Class::ClassResolver::PassThrough;
7 use DBI;
8
9 __PACKAGE__->load_components(qw/ResultSetInstance/);
10
11 *dbi_commit = \&txn_commit;
12 *dbi_rollback = \&txn_rollback;
13
14 sub storage { shift->schema_instance(@_)->storage; }
15
16 sub resultset_instance {
17   my $class = shift;
18   my $source = $class->result_source;
19   if ($source->result_class ne $class) {
20     $source = $source->new($source);
21     $source->result_class($class);
22   }
23   return $source->resultset;
24 }
25
26 =head1 NAME 
27
28 DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance
29
30 =head1 SYNOPSIS
31
32   package MyDB;
33
34   use base qw/DBIx::Class/;
35   __PACKAGE__->load_components('DB');
36
37   __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
38
39   package MyDB::MyTable;
40
41   use base qw/MyDB/;
42   __PACKAGE__->load_components('Core'); # just load this in MyDB if it will always be there
43
44   ...
45
46 =head1 DESCRIPTION
47
48 This class provides a simple way of specifying a database connection.
49
50 =head1 METHODS
51
52 =head2 storage
53
54 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
55
56 =head2 class_resolver ****DEPRECATED****
57
58 Sets or gets the class to use for resolving a class. Defaults to 
59 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
60 it. See resolve_class below.
61
62 =cut
63
64 __PACKAGE__->mk_classdata('class_resolver' =>
65                             'DBIx::Class::ClassResolver::PassThrough');
66
67 =head2 connection
68
69   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
70
71 Specifies the arguments that will be passed to DBI->connect(...) to
72 instantiate the class dbh when required.
73
74 =cut
75
76 sub connection {
77   my ($class, @info) = @_;
78   my $storage = DBIx::Class::Storage::DBI->new;
79   $storage->connect_info(\@info);
80   my $schema = bless({ storage => $storage }, 'DBIx::Class::Schema');
81   $class->mk_classdata('schema_instance' => $schema);
82 }
83
84 =head2 txn_begin
85
86 Begins a transaction (does nothing if AutoCommit is off).
87
88 =cut
89
90 sub txn_begin { $_[0]->storage->txn_begin }
91
92 =head2 txn_commit
93
94 Commits the current transaction.
95
96 =cut
97
98 sub txn_commit { $_[0]->storage->txn_commit }
99
100 =head2 txn_rollback
101
102 Rolls back the current transaction.
103
104 =cut
105
106 sub txn_rollback { $_[0]->storage->txn_rollback }
107
108 sub resolve_class {
109   warn "resolve_class deprecated as of 0.04999_02";
110   return shift->class_resolver->class(@_);
111 }
112
113 1;
114
115 =head1 AUTHORS
116
117 Matt S. Trout <mst@shadowcatsystems.co.uk>
118
119 =head1 LICENSE
120
121 You may distribute this code under the same terms as Perl itself.
122
123 =cut
124