Merge 'trunk' into 'DBIx-Class-current'
[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/ResultSetProxy/);
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 = ref $_[0] || $_[0];
18   my $source = $class->result_source_instance;
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 - Non-recommended classdata schema component
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 is designed to support the Class::DBI connection-as-classdata style
49 for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
50 instead; DBIx::Class::DB will continue to be supported but new development
51 will be focused on Schema-based DBIx::Class setups.
52
53 =head1 METHODS
54
55 =head2 storage
56
57 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
58
59 =head2 class_resolver
60
61 ****DEPRECATED****
62
63 Sets or gets the class to use for resolving a class. Defaults to 
64 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
65 it. See resolve_class below.
66
67 =cut
68
69 __PACKAGE__->mk_classdata('class_resolver' =>
70                             'DBIx::Class::ClassResolver::PassThrough');
71
72 =head2 connection
73
74   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
75
76 Specifies the arguments that will be passed to DBI->connect(...) to
77 instantiate the class dbh when required.
78
79 =cut
80
81 sub connection {
82   my ($class, @info) = @_;
83   $class->setup_schema_instance unless $class->can('schema_instance');
84   $class->schema_instance->connection(@info);
85 }
86
87 =head2 setup_schema_instance
88
89 Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
90 all class-method operations are proxies through to this object. If you don't
91 call ->connection in your DBIx::Class::DB subclass at load time you *must*
92 call ->setup_schema_instance in order for subclasses to find the schema and
93 register themselves with it.
94
95 =cut
96
97 sub setup_schema_instance {
98   my $class = shift;
99   my $schema = bless({}, 'DBIx::Class::Schema');
100   $class->mk_classdata('schema_instance' => $schema);
101 }
102
103 =head2 txn_begin
104
105 Begins a transaction (does nothing if AutoCommit is off).
106
107 =cut
108
109 sub txn_begin { $_[0]->schema_instance->txn_begin }
110
111 =head2 txn_commit
112
113 Commits the current transaction.
114
115 =cut
116
117 sub txn_commit { $_[0]->schema_instance->txn_commit }
118
119 =head2 txn_rollback
120
121 Rolls back the current transaction.
122
123 =cut
124
125 sub txn_rollback { $_[0]->schema_instance->txn_rollback }
126
127 {
128   my $warn;
129
130   sub resolve_class {
131     warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
132     return shift->class_resolver->class(@_);
133   }
134 }
135
136 1;
137
138 =head1 AUTHORS
139
140 Matt S. Trout <mst@shadowcatsystems.co.uk>
141
142 =head1 LICENSE
143
144 You may distribute this code under the same terms as Perl itself.
145
146 =cut
147