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