Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class::DB; |
2 | |
bf5ecff9 |
3 | use strict; |
4 | use warnings; |
5 | |
1edd1722 |
6 | use base qw/DBIx::Class/; |
7fb16f1a |
7 | use DBIx::Class::Schema; |
8b445e33 |
8 | use DBIx::Class::Storage::DBI; |
11b78bd6 |
9 | use DBIx::Class::ClassResolver::PassThrough; |
604d9f38 |
10 | use DBI; |
e87bedbe |
11 | use Scalar::Util; |
ea2e61bf |
12 | |
c216324a |
13 | unless ($INC{"DBIx/Class/CDBICompat.pm"}) { |
14 | warn "IMPORTANT: DBIx::Class::DB is DEPRECATED AND *WILL* BE REMOVED. DO NOT USE.\n"; |
15 | } |
16 | |
80c90f5d |
17 | __PACKAGE__->load_components(qw/ResultSetProxy/); |
2d679367 |
18 | |
bf5ecff9 |
19 | { |
20 | no warnings 'once'; |
21 | *dbi_commit = \&txn_commit; |
22 | *dbi_rollback = \&txn_rollback; |
23 | } |
8091aa91 |
24 | |
7fb16f1a |
25 | sub storage { shift->schema_instance(@_)->storage; } |
2d679367 |
26 | |
75d07914 |
27 | =head1 NAME |
34d52be2 |
28 | |
1c81f831 |
29 | DBIx::Class::DB - (DEPRECATED) classdata schema component |
34d52be2 |
30 | |
34d52be2 |
31 | =head1 DESCRIPTION |
32 | |
66d9ef6b |
33 | This class is designed to support the Class::DBI connection-as-classdata style |
34 | for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema |
1c81f831 |
35 | instead; DBIx::Class::DB will not undergo new development and will be moved |
c216324a |
36 | to being a CDBICompat-only component before 1.0. In order to discourage further |
37 | use, documentation has been removed as of 0.08000 |
38 | |
39 | =begin HIDE_BECAUSE_THIS_CLASS_IS_DEPRECATED |
34d52be2 |
40 | |
41 | =head1 METHODS |
42 | |
8091aa91 |
43 | =head2 storage |
076652e8 |
44 | |
8091aa91 |
45 | Sets or gets the storage backend. Defaults to L<DBIx::Class::Storage::DBI>. |
076652e8 |
46 | |
87c4e602 |
47 | =head2 class_resolver |
48 | |
49 | ****DEPRECATED**** |
076652e8 |
50 | |
75d07914 |
51 | Sets or gets the class to use for resolving a class. Defaults to |
8091aa91 |
52 | L<DBIx::Class::ClassResolver::Passthrough>, which returns whatever you give |
53 | it. See resolve_class below. |
076652e8 |
54 | |
34d52be2 |
55 | =cut |
56 | |
11b78bd6 |
57 | __PACKAGE__->mk_classdata('class_resolver' => |
181a28f4 |
58 | 'DBIx::Class::ClassResolver::PassThrough'); |
8fe001e1 |
59 | |
8091aa91 |
60 | =head2 connection |
39fe0e65 |
61 | |
62 | __PACKAGE__->connection($dsn, $user, $pass, $attrs); |
63 | |
64 | Specifies the arguments that will be passed to DBI->connect(...) to |
65 | instantiate the class dbh when required. |
66 | |
67 | =cut |
68 | |
8fe001e1 |
69 | sub connection { |
70 | my ($class, @info) = @_; |
66d9ef6b |
71 | $class->setup_schema_instance unless $class->can('schema_instance'); |
72 | $class->schema_instance->connection(@info); |
73 | } |
74 | |
75 | =head2 setup_schema_instance |
76 | |
77 | Creates a class method ->schema_instance which contains a DBIx::Class::Schema; |
78 | all class-method operations are proxies through to this object. If you don't |
79 | call ->connection in your DBIx::Class::DB subclass at load time you *must* |
80 | call ->setup_schema_instance in order for subclasses to find the schema and |
81 | register themselves with it. |
82 | |
83 | =cut |
84 | |
85 | sub setup_schema_instance { |
86 | my $class = shift; |
04786a4c |
87 | my $schema = {}; |
88 | bless $schema, 'DBIx::Class::Schema'; |
7fb16f1a |
89 | $class->mk_classdata('schema_instance' => $schema); |
ea2e61bf |
90 | } |
91 | |
8091aa91 |
92 | =head2 txn_begin |
39fe0e65 |
93 | |
8091aa91 |
94 | Begins a transaction (does nothing if AutoCommit is off). |
39fe0e65 |
95 | |
96 | =cut |
97 | |
181a28f4 |
98 | sub txn_begin { shift->schema_instance->txn_begin(@_); } |
a29644e1 |
99 | |
8091aa91 |
100 | =head2 txn_commit |
39fe0e65 |
101 | |
8091aa91 |
102 | Commits the current transaction. |
39fe0e65 |
103 | |
8091aa91 |
104 | =cut |
105 | |
181a28f4 |
106 | sub txn_commit { shift->schema_instance->txn_commit(@_); } |
8091aa91 |
107 | |
108 | =head2 txn_rollback |
109 | |
110 | Rolls back the current transaction. |
39fe0e65 |
111 | |
112 | =cut |
113 | |
181a28f4 |
114 | sub txn_rollback { shift->schema_instance->txn_rollback(@_); } |
115 | |
116 | =head2 txn_do |
117 | |
118 | Executes a block of code transactionally. If this code reference |
119 | throws an exception, the transaction is rolled back and the exception |
bc0c9800 |
120 | is rethrown. See L<DBIx::Class::Schema/"txn_do"> for more details. |
181a28f4 |
121 | |
122 | =cut |
123 | |
124 | sub txn_do { shift->schema_instance->txn_do(@_); } |
8b445e33 |
125 | |
8452e496 |
126 | { |
127 | my $warn; |
128 | |
129 | sub resolve_class { |
130 | warn "resolve_class deprecated as of 0.04999_02" unless $warn++; |
131 | return shift->class_resolver->class(@_); |
132 | } |
7fb16f1a |
133 | } |
11b78bd6 |
134 | |
7eb4ecc8 |
135 | =head2 resultset_instance |
136 | |
137 | Returns an instance of a resultset for this class - effectively |
138 | mapping the L<Class::DBI> connection-as-classdata paradigm into the |
139 | native L<DBIx::Class::ResultSet> system. |
140 | |
141 | =cut |
142 | |
143 | sub resultset_instance { |
e87bedbe |
144 | $_[0]->result_source_instance->resultset |
145 | } |
146 | |
7137528d |
147 | =head2 result_source_instance |
148 | |
149 | Returns an instance of the result source for this class |
150 | |
151 | =cut |
152 | |
654f330d |
153 | __PACKAGE__->mk_classdata('_result_source_instance' => []); |
154 | |
0e6c5d58 |
155 | # Yep. this is horrific. Basically what's happening here is that |
156 | # (with good reason) DBIx::Class::Schema copies the result source for |
157 | # registration. Because we have a retarded setup order forced on us we need |
158 | # to actually make our ->result_source_instance -be- the source used, and we |
159 | # need to get the source name and schema into ourselves. So this makes it |
160 | # happen. |
161 | |
162 | sub _maybe_attach_source_to_schema { |
163 | my ($class, $source) = @_; |
164 | if (my $meth = $class->can('schema_instance')) { |
9381840d |
165 | if (my $schema = $class->$meth) { |
166 | $schema->register_class($class, $class); |
167 | my $new_source = $schema->source($class); |
168 | %$source = %$new_source; |
169 | $schema->source_registrations->{$class} = $source; |
170 | } |
0e6c5d58 |
171 | } |
172 | } |
173 | |
e87bedbe |
174 | sub result_source_instance { |
175 | my $class = shift; |
176 | $class = ref $class || $class; |
d4daee7b |
177 | |
0e6c5d58 |
178 | if (@_) { |
179 | my $source = $_[0]; |
180 | $class->_result_source_instance([$source, $class]); |
181 | $class->_maybe_attach_source_to_schema($source); |
182 | return $source; |
183 | } |
e87bedbe |
184 | |
654f330d |
185 | my($source, $result_class) = @{$class->_result_source_instance}; |
e60dc79f |
186 | return unless Scalar::Util::blessed($source); |
e87bedbe |
187 | |
654f330d |
188 | if ($result_class ne $class) { # new class |
faaba25f |
189 | # Give this new class its own source and register it. |
e87bedbe |
190 | $source = $source->new({ |
191 | %$source, |
192 | source_name => $class, |
193 | result_class => $class |
194 | } ); |
654f330d |
195 | $class->_result_source_instance([$source, $class]); |
0e6c5d58 |
196 | $class->_maybe_attach_source_to_schema($source); |
7eb4ecc8 |
197 | } |
e87bedbe |
198 | return $source; |
7eb4ecc8 |
199 | } |
200 | |
201 | =head2 resolve_class |
202 | |
203 | ****DEPRECATED**** |
204 | |
205 | See L<class_resolver> |
206 | |
207 | =head2 dbi_commit |
208 | |
209 | ****DEPRECATED**** |
210 | |
211 | Alias for L<txn_commit> |
212 | |
213 | =head2 dbi_rollback |
214 | |
215 | ****DEPRECATED**** |
216 | |
217 | Alias for L<txn_rollback> |
218 | |
c216324a |
219 | =end HIDE_BECAUSE_THIS_CLASS_IS_DEPRECATED |
220 | |
34d52be2 |
221 | =head1 AUTHORS |
222 | |
daec44b8 |
223 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
224 | |
225 | =head1 LICENSE |
226 | |
227 | You may distribute this code under the same terms as Perl itself. |
228 | |
229 | =cut |
230 | |
76b2b77c |
231 | 1; |