Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class; |
2 | |
5d283305 |
3 | use strict; |
4 | use warnings; |
5 | |
329d7385 |
6 | use MRO::Compat; |
d38cd95c |
7 | use mro 'c3'; |
329d7385 |
8 | |
2527233b |
9 | use DBIx::Class::Optional::Dependencies; |
10 | |
5d283305 |
11 | use vars qw($VERSION); |
d38cd95c |
12 | use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/; |
11736b4c |
13 | use DBIx::Class::StartupCheck; |
3e110410 |
14 | |
ade0fe3b |
15 | sub mk_classdata { |
77d518d1 |
16 | shift->mk_classaccessor(@_); |
17 | } |
18 | |
19 | sub mk_classaccessor { |
20 | my $self = shift; |
ade0fe3b |
21 | $self->mk_group_accessors('inherited', $_[0]); |
77d518d1 |
22 | $self->set_inherited(@_) if @_ > 1; |
3e110410 |
23 | } |
3c0068c1 |
24 | |
7411204b |
25 | sub component_base_class { 'DBIx::Class' } |
227d4dee |
26 | |
95da6f35 |
27 | # Always remember to do all digits for the version even if they're 0 |
28 | # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports |
29 | # brain damage and presumably various other packaging systems too |
90d753b9 |
30 | $VERSION = '0.08122_01'; |
748ab0dc |
31 | |
033955f9 |
32 | $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases |
b8777a0d |
33 | |
f0750722 |
34 | sub MODIFY_CODE_ATTRIBUTES { |
b5d2c57f |
35 | my ($class,$code,@attrs) = @_; |
36 | $class->mk_classdata('__attr_cache' => {}) |
37 | unless $class->can('__attr_cache'); |
38 | $class->__attr_cache->{$code} = [@attrs]; |
39 | return (); |
f0750722 |
40 | } |
41 | |
da95b45f |
42 | sub _attr_cache { |
b5d2c57f |
43 | my $self = shift; |
44 | my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {}; |
9780718f |
45 | |
46 | return { |
47 | %$cache, |
48 | %{ $self->maybe::next::method || {} }, |
20674fcd |
49 | }; |
da95b45f |
50 | } |
51 | |
ea2e61bf |
52 | 1; |
34d52be2 |
53 | |
75d07914 |
54 | =head1 NAME |
34d52be2 |
55 | |
7e4b2f59 |
56 | DBIx::Class - Extensible and flexible object <-> relational mapper. |
34d52be2 |
57 | |
3b1c2bbd |
58 | =head1 GETTING HELP/SUPPORT |
59 | |
60 | The community can be found via: |
61 | |
a06e1181 |
62 | =over |
3b1c2bbd |
63 | |
c6fdaf2a |
64 | =item * IRC: irc.perl.org#dbix-class |
65 | |
66 | =for html |
67 | <a href="http://mibbit.com/chat/#dbix-class@irc.perl.org">(click for instant chatroom login)</a> |
3b1c2bbd |
68 | |
a06e1181 |
69 | =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class> |
3b1c2bbd |
70 | |
a06e1181 |
71 | =item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class> |
72 | |
aeb669b8 |
73 | =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git> |
a06e1181 |
74 | |
aeb669b8 |
75 | =item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git> |
a06e1181 |
76 | |
77 | =back |
3b1c2bbd |
78 | |
34d52be2 |
79 | =head1 SYNOPSIS |
80 | |
bd077b47 |
81 | Create a schema class called MyDB/Schema.pm: |
34d52be2 |
82 | |
bd077b47 |
83 | package MyDB::Schema; |
a0638a7b |
84 | use base qw/DBIx::Class::Schema/; |
34d52be2 |
85 | |
f0bb26f3 |
86 | __PACKAGE__->load_namespaces(); |
daec44b8 |
87 | |
a0638a7b |
88 | 1; |
daec44b8 |
89 | |
30e1753a |
90 | Create a result class to represent artists, who have many CDs, in |
f0bb26f3 |
91 | MyDB/Schema/Result/Artist.pm: |
daec44b8 |
92 | |
30e1753a |
93 | See L<DBIx::Class::ResultSource> for docs on defining result classes. |
94 | |
f0bb26f3 |
95 | package MyDB::Schema::Result::Artist; |
d88ecca6 |
96 | use base qw/DBIx::Class::Core/; |
daec44b8 |
97 | |
a0638a7b |
98 | __PACKAGE__->table('artist'); |
99 | __PACKAGE__->add_columns(qw/ artistid name /); |
100 | __PACKAGE__->set_primary_key('artistid'); |
f0bb26f3 |
101 | __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD'); |
daec44b8 |
102 | |
a0638a7b |
103 | 1; |
daec44b8 |
104 | |
30e1753a |
105 | A result class to represent a CD, which belongs to an artist, in |
f0bb26f3 |
106 | MyDB/Schema/Result/CD.pm: |
39fe0e65 |
107 | |
f0bb26f3 |
108 | package MyDB::Schema::Result::CD; |
d88ecca6 |
109 | use base qw/DBIx::Class::Core/; |
39fe0e65 |
110 | |
d88ecca6 |
111 | __PACKAGE__->load_components(qw/InflateColumn::DateTime/); |
a0638a7b |
112 | __PACKAGE__->table('cd'); |
bd077b47 |
113 | __PACKAGE__->add_columns(qw/ cdid artistid title year /); |
a0638a7b |
114 | __PACKAGE__->set_primary_key('cdid'); |
bd077b47 |
115 | __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Artist', 'artistid'); |
39fe0e65 |
116 | |
a0638a7b |
117 | 1; |
39fe0e65 |
118 | |
a0638a7b |
119 | Then you can use these classes in your application's code: |
39fe0e65 |
120 | |
a0638a7b |
121 | # Connect to your database. |
bd077b47 |
122 | use MyDB::Schema; |
123 | my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params); |
a0638a7b |
124 | |
125 | # Query for all artists and put them in an array, |
126 | # or retrieve them as a result set object. |
30e1753a |
127 | # $schema->resultset returns a DBIx::Class::ResultSet |
2053ab2a |
128 | my @all_artists = $schema->resultset('Artist')->all; |
129 | my $all_artists_rs = $schema->resultset('Artist'); |
126042ee |
130 | |
30e1753a |
131 | # Output all artists names |
4e8ffded |
132 | # $artist here is a DBIx::Class::Row, which has accessors |
16ccb4fe |
133 | # for all its columns. Rows are also subclasses of your Result class. |
85067746 |
134 | foreach $artist (@all_artists) { |
30e1753a |
135 | print $artist->name, "\n"; |
136 | } |
137 | |
a0638a7b |
138 | # Create a result set to search for artists. |
86beca1d |
139 | # This does not query the DB. |
2053ab2a |
140 | my $johns_rs = $schema->resultset('Artist')->search( |
6576ef54 |
141 | # Build your WHERE using an SQL::Abstract structure: |
2053ab2a |
142 | { name => { like => 'John%' } } |
a0638a7b |
143 | ); |
39fe0e65 |
144 | |
2053ab2a |
145 | # Execute a joined query to get the cds. |
a0638a7b |
146 | my @all_john_cds = $johns_rs->search_related('cds')->all; |
448c8424 |
147 | |
f0bb26f3 |
148 | # Fetch the next available row. |
a0638a7b |
149 | my $first_john = $johns_rs->next; |
448c8424 |
150 | |
2053ab2a |
151 | # Specify ORDER BY on the query. |
a0638a7b |
152 | my $first_john_cds_by_title_rs = $first_john->cds( |
153 | undef, |
154 | { order_by => 'title' } |
155 | ); |
448c8424 |
156 | |
bd077b47 |
157 | # Create a result set that will fetch the artist data |
2053ab2a |
158 | # at the same time as it fetches CDs, using only one query. |
884559b1 |
159 | my $millennium_cds_rs = $schema->resultset('CD')->search( |
a0638a7b |
160 | { year => 2000 }, |
161 | { prefetch => 'artist' } |
162 | ); |
448c8424 |
163 | |
880a1a0c |
164 | my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ... |
bd077b47 |
165 | my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query |
076652e8 |
166 | |
264f1571 |
167 | # new() makes a DBIx::Class::Row object but doesnt insert it into the DB. |
168 | # create() is the same as new() then insert(). |
884559b1 |
169 | my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' }); |
f183eccd |
170 | $new_cd->artist($cd->artist); |
f183eccd |
171 | $new_cd->insert; # Auto-increment primary key filled in after INSERT |
f183eccd |
172 | $new_cd->title('Fork'); |
173 | |
884559b1 |
174 | $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction |
f183eccd |
175 | |
bd077b47 |
176 | # change the year of all the millennium CDs at once |
177 | $millennium_cds_rs->update({ year => 2002 }); |
f183eccd |
178 | |
179 | =head1 DESCRIPTION |
180 | |
181 | This is an SQL to OO mapper with an object API inspired by L<Class::DBI> |
bd077b47 |
182 | (with a compatibility layer as a springboard for porting) and a resultset API |
f183eccd |
183 | that allows abstract encapsulation of database operations. It aims to make |
184 | representing queries in your code as perl-ish as possible while still |
a0638a7b |
185 | providing access to as many of the capabilities of the database as possible, |
f183eccd |
186 | including retrieving related records from multiple tables in a single query, |
bd077b47 |
187 | JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support. |
f183eccd |
188 | |
189 | DBIx::Class can handle multi-column primary and foreign keys, complex |
190 | queries and database-level paging, and does its best to only query the |
75d07914 |
191 | database in order to return something you've directly asked for. If a |
192 | resultset is used as an iterator it only fetches rows off the statement |
193 | handle as requested in order to minimise memory usage. It has auto-increment |
2053ab2a |
194 | support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is |
195 | known to be used in production on at least the first four, and is fork- |
75d07914 |
196 | and thread-safe out of the box (although your DBD may not be). |
f183eccd |
197 | |
dfccde48 |
198 | This project is still under rapid development, so large new features may be |
199 | marked EXPERIMENTAL - such APIs are still usable but may have edge bugs. |
200 | Failing test cases are *always* welcome and point releases are put out rapidly |
201 | as bugs are found and fixed. |
202 | |
203 | We do our best to maintain full backwards compatibility for published |
204 | APIs, since DBIx::Class is used in production in many organisations, |
205 | and even backwards incompatible changes to non-published APIs will be fixed |
206 | if they're reported and doing so doesn't cost the codebase anything. |
207 | |
264f1571 |
208 | The test suite is quite substantial, and several developer releases |
209 | are generally made to CPAN before the branch for the next release is |
210 | merged back to trunk for a major release. |
f183eccd |
211 | |
f183eccd |
212 | =head1 WHERE TO GO NEXT |
213 | |
2ca930b4 |
214 | L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and |
215 | the modules where you will find documentation. |
076652e8 |
216 | |
3942ab4d |
217 | =head1 AUTHOR |
34d52be2 |
218 | |
266bdcc3 |
219 | mst: Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
220 | |
dfccde48 |
221 | (I mostly consider myself "project founder" these days but the AUTHOR heading |
222 | is traditional :) |
223 | |
3942ab4d |
224 | =head1 CONTRIBUTORS |
225 | |
907ed671 |
226 | abraxxa: Alexander Hartmaier <abraxxa@cpan.org> |
84e3c114 |
227 | |
6d84db2c |
228 | aherzog: Adam Herzog <adam@herzogdesigns.com> |
229 | |
daeb1865 |
230 | Alexander Keusch <cpan@keusch.at> |
231 | |
b703fec7 |
232 | amoore: Andrew Moore <amoore@cpan.org> |
233 | |
266bdcc3 |
234 | andyg: Andy Grundman <andy@hybridized.org> |
3942ab4d |
235 | |
266bdcc3 |
236 | ank: Andres Kievsky |
3942ab4d |
237 | |
624764ae |
238 | arcanez: Justin Hunter <justin.d.hunter@gmail.com> |
239 | |
ce4c07df |
240 | ash: Ash Berlin <ash@cpan.org> |
241 | |
3d5bd2af |
242 | bert: Norbert Csongradi <bert@cpan.org> |
243 | |
967a4c40 |
244 | blblack: Brandon L. Black <blblack@gmail.com> |
3942ab4d |
245 | |
62eb8fe8 |
246 | bluefeet: Aran Deltac <bluefeet@cpan.org> |
247 | |
f856fe01 |
248 | boghead: Bryan Beeley <cpan@beeley.org> |
249 | |
bcb8f3ed |
250 | bricas: Brian Cassidy <bricas@cpan.org> |
251 | |
3d7e3e05 |
252 | brunov: Bruno Vecchi <vecchi.b@gmail.com> |
253 | |
281719d2 |
254 | caelum: Rafael Kitover <rkitover@cpan.org> |
255 | |
d3b0e369 |
256 | castaway: Jess Robinson |
3942ab4d |
257 | |
266bdcc3 |
258 | claco: Christopher H. Laco |
ccb9c9b1 |
259 | |
266bdcc3 |
260 | clkao: CL Kao |
3942ab4d |
261 | |
e21dfd6a |
262 | da5id: David Jack Olrik <djo@cpan.org> |
18360aed |
263 | |
13de943d |
264 | debolaz: Anders Nor Berle <berle@cpan.org> |
265 | |
d1f542db |
266 | dew: Dan Thomas <dan@godders.org> |
267 | |
266bdcc3 |
268 | dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com> |
ccb9c9b1 |
269 | |
9382ad07 |
270 | dnm: Justin Wheeler <jwheeler@datademons.com> |
271 | |
0818c9a7 |
272 | dpetrov: Dimitar Petrov <mitakaa@gmail.com> |
273 | |
266bdcc3 |
274 | dwc: Daniel Westermann-Clark <danieltwc@cpan.org> |
4685e006 |
275 | |
5cffe785 |
276 | dyfrgi: Michael Leuchtenburg <michael@slashhome.org> |
8fe164b9 |
277 | |
ade0fe3b |
278 | frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com> |
279 | |
b4987ed0 |
280 | goraxe: Gordon Irving <goraxe@cpan.org> |
281 | |
d3b0e369 |
282 | gphat: Cory G Watson <gphat@cpan.org> |
ad3d2d7c |
283 | |
e758ffe6 |
284 | groditi: Guillermo Roditi <groditi@cpan.org> |
285 | |
6dad89f5 |
286 | Haarg: Graham Knop <haarg@haarg.org> |
287 | |
157ce0cf |
288 | hobbs: Andrew Rodland <arodland@cpan.org> |
289 | |
5d779578 |
290 | ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org> |
291 | |
bee21976 |
292 | jasonmay: Jason May <jason.a.may@gmail.com> |
293 | |
d3b0e369 |
294 | jesper: Jesper Krogh |
5fb0c64c |
295 | |
4a743a00 |
296 | jgoulah: John Goulah <jgoulah@cpan.org> |
297 | |
102a2984 |
298 | jguenther: Justin Guenther <jguenther@cpan.org> |
d7c4c15c |
299 | |
a14a46e2 |
300 | jhannah: Jay Hannah <jay@jays.net> |
301 | |
8b93a938 |
302 | jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com> |
303 | |
11736b4c |
304 | jon: Jon Schutz <jjschutz@cpan.org> |
305 | |
1aec4bac |
306 | jshirley: J. Shirley <jshirley@gmail.com> |
307 | |
d3b0e369 |
308 | konobi: Scott McWhirter |
535fc2ee |
309 | |
4367679a |
310 | lukes: Luke Saunders <luke.saunders@gmail.com> |
311 | |
709ea492 |
312 | marcus: Marcus Ramberg <mramberg@cpan.org> |
313 | |
114780ee |
314 | mattlaw: Matt Lawrence |
315 | |
58755bba |
316 | michaelr: Michael Reddick <michael.reddick@gmail.com> |
317 | |
77e7e47d |
318 | ned: Neil de Carteret |
319 | |
266bdcc3 |
320 | nigel: Nigel Metheringham <nigelm@cpan.org> |
6565b410 |
321 | |
d3b0e369 |
322 | ningu: David Kamholz <dkamholz@cpan.org> |
323 | |
66cf3a84 |
324 | Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org> |
325 | |
20b4c148 |
326 | norbi: Norbert Buchmuller <norbi@nix.hu> |
327 | |
48580715 |
328 | nuba: Nuba Princigalli <nuba@cpan.org> |
329 | |
d3b0e369 |
330 | Numa: Dan Sully <daniel@cpan.org> |
331 | |
dc571b76 |
332 | ovid: Curtis "Ovid" Poe <ovid@cpan.org> |
333 | |
bf356c54 |
334 | oyse: Øystein Torget <oystein.torget@dnv.com> |
335 | |
266bdcc3 |
336 | paulm: Paul Makepeace |
4763f4b7 |
337 | |
d3b0e369 |
338 | penguin: K J Cheetham |
339 | |
8cfef6f5 |
340 | perigrin: Chris Prather <chris@prather.org> |
341 | |
14899528 |
342 | peter: Peter Collingbourne <peter@pcc.me.uk> |
caac1708 |
343 | |
266bdcc3 |
344 | phaylon: Robert Sedlacek <phaylon@dunkelheit.at> |
a53b95f1 |
345 | |
56fadd8f |
346 | plu: Johannes Plunien <plu@cpan.org> |
347 | |
d3b0e369 |
348 | quicksilver: Jules Bean |
022e0893 |
349 | |
4ed01b34 |
350 | rafl: Florian Ragwitz <rafl@debian.org> |
351 | |
868a7b26 |
352 | rbo: Robert Bohne <rbo@cpan.org> |
353 | |
7ff0dace |
354 | rbuels: Robert Buels <rmb32@cornell.edu> |
355 | |
0da8b7da |
356 | rdj: Ryan D Johnson <ryan@innerfence.com> |
357 | |
66b1e361 |
358 | ribasushi: Peter Rabbitson <ribasushi@cpan.org> |
d76e282a |
359 | |
b487918c |
360 | rjbs: Ricardo Signes <rjbs@cpan.org> |
361 | |
6ffb5be5 |
362 | robkinyon: Rob Kinyon <rkinyon@cpan.org> |
363 | |
e4c9f3f0 |
364 | Roman: Roman Filippov <romanf@cpan.org> |
365 | |
d3b0e369 |
366 | sc_: Just Another Perl Hacker |
ba606e58 |
367 | |
266bdcc3 |
368 | scotty: Scotty Allen <scotty@scottyallen.com> |
181a28f4 |
369 | |
1c133e22 |
370 | semifor: Marc Mims <marc@questright.com> |
371 | |
20b4c148 |
372 | solomon: Jared Johnson <jaredj@nmgi.com> |
373 | |
88f937fb |
374 | spb: Stephen Bennett <stephen@freenode.net> |
375 | |
637ca936 |
376 | sszabo: Stephan Szabo <sszabo@bigpanda.com> |
377 | |
386c2272 |
378 | teejay : Aaron Trevena <teejay@cpan.org> |
379 | |
84e3c114 |
380 | Todd Lipcon |
e063fe2c |
381 | |
6eb6264e |
382 | Tom Hukins |
383 | |
2e4b6d78 |
384 | tonvoon: Ton Voon <tonvoon@cpan.org> |
385 | |
d15e3fc5 |
386 | triode: Pete Gamache <gamache@cpan.org> |
387 | |
d3b0e369 |
388 | typester: Daisuke Murase <typester@cpan.org> |
c0e7b4e5 |
389 | |
4740bdb7 |
390 | victori: Victor Igumnov <victori@cpan.org> |
391 | |
d3b0e369 |
392 | wdh: Will Hawes |
4c248161 |
393 | |
00c03ced |
394 | willert: Sebastian Willert <willert@cpan.org> |
395 | |
66d2a14e |
396 | wreis: Wallace Reis <wreis@cpan.org> |
397 | |
d3b0e369 |
398 | zamolxes: Bogdan Lucaciu <bogdan@wiz.ro> |
78060df8 |
399 | |
c1807ed5 |
400 | Possum: Daniel LeWarne <possum@cpan.org> |
401 | |
b38e10bd |
402 | =head1 COPYRIGHT |
403 | |
48580715 |
404 | Copyright (c) 2005 - 2010 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS> |
b38e10bd |
405 | as listed above. |
406 | |
96154ef7 |
407 | =head1 LICENSE |
408 | |
409 | This library is free software and may be distributed under the same terms |
410 | as perl itself. |
411 | |
34d52be2 |
412 | =cut |