marked DBIx::Class::DB as deprecated and due to be removed by 1.0
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / DB.pm
1 package DBIx::Class::DB;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class/;
7 use DBIx::Class::Schema;
8 use DBIx::Class::Storage::DBI;
9 use DBIx::Class::ClassResolver::PassThrough;
10 use DBI;
11
12 __PACKAGE__->load_components(qw/ResultSetProxy/);
13
14 {
15     no warnings 'once';
16     *dbi_commit = \&txn_commit;
17     *dbi_rollback = \&txn_rollback;
18 }
19
20 sub storage { shift->schema_instance(@_)->storage; }
21
22 sub resultset_instance {
23   my $class = ref $_[0] || $_[0];
24   my $source = $class->result_source_instance;
25   if ($source->result_class ne $class) {
26     $source = $source->new($source);
27     $source->result_class($class);
28   }
29   return $source->resultset;
30 }
31
32 =head1 NAME
33
34 DBIx::Class::DB - (DEPRECATED) classdata schema component
35
36 =head1 SYNOPSIS
37
38   package MyDB;
39
40   use base qw/DBIx::Class/;
41   __PACKAGE__->load_components('DB');
42
43   __PACKAGE__->connection('dbi:...', 'user', 'pass', \%attrs);
44
45   package MyDB::MyTable;
46
47   use base qw/MyDB/;
48   __PACKAGE__->load_components('Core'); # just load this in MyDB if it will
49                                         # always be there
50
51   ...
52
53 =head1 DESCRIPTION
54
55 This class is designed to support the Class::DBI connection-as-classdata style
56 for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema
57 instead; DBIx::Class::DB will not undergo new development and will be moved
58 to being a CDBICompat-only component before 1.0.
59
60 =head1 METHODS
61
62 =head2 storage
63
64 Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>.
65
66 =head2 class_resolver
67
68 ****DEPRECATED****
69
70 Sets or gets the class to use for resolving a class. Defaults to
71 L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give
72 it. See resolve_class below.
73
74 =cut
75
76 __PACKAGE__->mk_classdata('class_resolver' =>
77                           'DBIx::Class::ClassResolver::PassThrough');
78
79 =head2 connection
80
81   __PACKAGE__->connection($dsn, $user, $pass, $attrs);
82
83 Specifies the arguments that will be passed to DBI->connect(...) to
84 instantiate the class dbh when required.
85
86 =cut
87
88 sub connection {
89   my ($class, @info) = @_;
90   $class->setup_schema_instance unless $class->can('schema_instance');
91   $class->schema_instance->connection(@info);
92 }
93
94 =head2 setup_schema_instance
95
96 Creates a class method ->schema_instance which contains a DBIx::Class::Schema;
97 all class-method operations are proxies through to this object. If you don't
98 call ->connection in your DBIx::Class::DB subclass at load time you *must*
99 call ->setup_schema_instance in order for subclasses to find the schema and
100 register themselves with it.
101
102 =cut
103
104 sub setup_schema_instance {
105   my $class = shift;
106   my $schema = bless({}, 'DBIx::Class::Schema');
107   $class->mk_classdata('schema_instance' => $schema);
108 }
109
110 =head2 txn_begin
111
112 Begins a transaction (does nothing if AutoCommit is off).
113
114 =cut
115
116 sub txn_begin { shift->schema_instance->txn_begin(@_); }
117
118 =head2 txn_commit
119
120 Commits the current transaction.
121
122 =cut
123
124 sub txn_commit { shift->schema_instance->txn_commit(@_); }
125
126 =head2 txn_rollback
127
128 Rolls back the current transaction.
129
130 =cut
131
132 sub txn_rollback { shift->schema_instance->txn_rollback(@_); }
133
134 =head2 txn_do
135
136 Executes a block of code transactionally. If this code reference
137 throws an exception, the transaction is rolled back and the exception
138 is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details.
139
140 =cut
141
142 sub txn_do { shift->schema_instance->txn_do(@_); }
143
144 {
145   my $warn;
146
147   sub resolve_class {
148     warn "resolve_class deprecated as of 0.04999_02" unless $warn++;
149     return shift->class_resolver->class(@_);
150   }
151 }
152
153 1;
154
155 =head1 AUTHORS
156
157 Matt S. Trout <mst@shadowcatsystems.co.uk>
158
159 =head1 LICENSE
160
161 You may distribute this code under the same terms as Perl itself.
162
163 =cut
164