Remove preemptive DESTROY guard from 87f4bab0 / d63c9e64
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
1 package DBIx::Class;
2
3 # important to load early
4 use DBIx::Class::_Util;
5
6 use strict;
7 use warnings;
8
9 our $VERSION;
10 # Always remember to do all digits for the version even if they're 0
11 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
12 # brain damage and presumably various other packaging systems too
13
14 # $VERSION declaration must stay up here, ahead of any other package
15 # declarations, as to not confuse various modules attempting to determine
16 # this ones version, whether that be s.c.o. or Module::Metadata, etc
17 $VERSION = '0.082899_15';
18
19 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
20
21 use mro 'c3';
22
23 use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
24 use DBIx::Class::Exception;
25
26 __PACKAGE__->mk_classaccessor( _skip_namespace_frames =>
27   '^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve|^Moose::Meta::'
28 );
29
30 sub component_base_class { 'DBIx::Class' }
31
32 # *DO NOT* change this URL nor the identically named =head1 below
33 # it is linked throughout the ecosystem
34 sub DBIx::Class::_ENV_::HELP_URL () {
35   'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT'
36 }
37
38 1;
39
40 __END__
41
42 =head1 NAME
43
44 DBIx::Class - Extensible and flexible object <-> relational mapper.
45
46 =head1 WHERE TO START READING
47
48 See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
49 To get the most out of DBIx::Class with the least confusion it is strongly
50 recommended to read (at the very least) the
51 L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
52
53 =cut
54
55 =head1 GETTING HELP/SUPPORT
56
57 Due to the sheer size of its problem domain, DBIx::Class is a relatively
58 complex framework. After you start using DBIx::Class questions will inevitably
59 arise. If you are stuck with a problem or have doubts about a particular
60 approach do not hesitate to contact us via any of the following options (the
61 list is sorted by "fastest response time"):
62
63 =over
64
65 =item * IRC: irc.perl.org#dbix-class
66
67 =for html
68 <a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
69
70 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
71
72 =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
73
74 =item * Twitter: L<https://www.twitter.com/dbix_class>
75
76 =item * Web Site: L<http://www.dbix-class.org/>
77
78 =back
79
80 =head1 SYNOPSIS
81
82 For the very impatient: L<DBIx::Class::Manual::QuickStart>
83
84 This code in the next step can be generated automatically from an existing
85 database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
86
87 =head2 Schema classes preparation
88
89 Create a schema class called F<MyApp/Schema.pm>:
90
91   package MyApp::Schema;
92   use base qw/DBIx::Class::Schema/;
93
94   __PACKAGE__->load_namespaces();
95
96   1;
97
98 Create a result class to represent artists, who have many CDs, in
99 F<MyApp/Schema/Result/Artist.pm>:
100
101 See L<DBIx::Class::ResultSource> for docs on defining result classes.
102
103   package MyApp::Schema::Result::Artist;
104   use base qw/DBIx::Class::Core/;
105
106   __PACKAGE__->table('artist');
107   __PACKAGE__->add_columns(qw/ artistid name /);
108   __PACKAGE__->set_primary_key('artistid');
109   __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
110
111   1;
112
113 A result class to represent a CD, which belongs to an artist, in
114 F<MyApp/Schema/Result/CD.pm>:
115
116   package MyApp::Schema::Result::CD;
117   use base qw/DBIx::Class::Core/;
118
119   __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
120   __PACKAGE__->table('cd');
121   __PACKAGE__->add_columns(qw/ cdid artistid title year /);
122   __PACKAGE__->set_primary_key('cdid');
123   __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
124
125   1;
126
127 =head2 API usage
128
129 Then you can use these classes in your application's code:
130
131   # Connect to your database.
132   use MyApp::Schema;
133   my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
134
135   # Query for all artists and put them in an array,
136   # or retrieve them as a result set object.
137   # $schema->resultset returns a DBIx::Class::ResultSet
138   my @all_artists = $schema->resultset('Artist')->all;
139   my $all_artists_rs = $schema->resultset('Artist');
140
141   # Output all artists names
142   # $artist here is a DBIx::Class::Row, which has accessors
143   # for all its columns. Rows are also subclasses of your Result class.
144   foreach $artist (@all_artists) {
145     print $artist->name, "\n";
146   }
147
148   # Create a result set to search for artists.
149   # This does not query the DB.
150   my $johns_rs = $schema->resultset('Artist')->search(
151     # Build your WHERE using an SQL::Abstract structure:
152     { name => { like => 'John%' } }
153   );
154
155   # Execute a joined query to get the cds.
156   my @all_john_cds = $johns_rs->search_related('cds')->all;
157
158   # Fetch the next available row.
159   my $first_john = $johns_rs->next;
160
161   # Specify ORDER BY on the query.
162   my $first_john_cds_by_title_rs = $first_john->cds(
163     undef,
164     { order_by => 'title' }
165   );
166
167   # Create a result set that will fetch the artist data
168   # at the same time as it fetches CDs, using only one query.
169   my $millennium_cds_rs = $schema->resultset('CD')->search(
170     { year => 2000 },
171     { prefetch => 'artist' }
172   );
173
174   my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
175   my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
176
177   # new() makes a Result object but doesn't insert it into the DB.
178   # create() is the same as new() then insert().
179   my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
180   $new_cd->artist($cd->artist);
181   $new_cd->insert; # Auto-increment primary key filled in after INSERT
182   $new_cd->title('Fork');
183
184   $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
185
186   # change the year of all the millennium CDs at once
187   $millennium_cds_rs->update({ year => 2002 });
188
189 =head1 DESCRIPTION
190
191 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
192 (with a compatibility layer as a springboard for porting) and a resultset API
193 that allows abstract encapsulation of database operations. It aims to make
194 representing queries in your code as perl-ish as possible while still
195 providing access to as many of the capabilities of the database as possible,
196 including retrieving related records from multiple tables in a single query,
197 C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
198 C<HAVING> support.
199
200 DBIx::Class can handle multi-column primary and foreign keys, complex
201 queries and database-level paging, and does its best to only query the
202 database in order to return something you've directly asked for. If a
203 resultset is used as an iterator it only fetches rows off the statement
204 handle as requested in order to minimise memory usage. It has auto-increment
205 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
206 known to be used in production on at least the first four, and is fork-
207 and thread-safe out of the box (although
208 L<your DBD may not be|DBI/Threads and Thread Safety>).
209
210 This project is still under rapid development, so large new features may be
211 marked B<experimental> - such APIs are still usable but may have edge bugs.
212 Failing test cases are I<always> welcome and point releases are put out rapidly
213 as bugs are found and fixed.
214
215 We do our best to maintain full backwards compatibility for published
216 APIs, since DBIx::Class is used in production in many organisations,
217 and even backwards incompatible changes to non-published APIs will be fixed
218 if they're reported and doing so doesn't cost the codebase anything.
219
220 The test suite is quite substantial, and several developer releases
221 are generally made to CPAN before the branch for the next release is
222 merged back to trunk for a major release.
223
224 =head1 HOW TO CONTRIBUTE
225
226 Contributions are always welcome, in all usable forms (we especially
227 welcome documentation improvements). The delivery methods include git-
228 or unified-diff formatted patches, GitHub pull requests, or plain bug
229 reports either via RT or the Mailing list. Contributors are generally
230 granted access to the official repository after their first several
231 patches pass successful review. Don't hesitate to
232 L<contact|/GETTING HELP/SUPPORT> either of the L</CAT HERDERS> with
233 any further questions you may have.
234
235 =for comment
236 FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
237
238 This project is maintained in a git repository. The code and related tools are
239 accessible at the following locations:
240
241 =over
242
243 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
244
245 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
246
247 =item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class>
248
249 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
250
251 =item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds>
252
253 =for html
254 &#x21AA; Bleeding edge dev CI status: <img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img>
255
256 =back
257
258 =head1 AUTHORS
259
260 Even though a large portion of the source I<appears> to be written by just a
261 handful of people, this library continues to remain a collaborative effort -
262 perhaps one of the most successful such projects on L<CPAN|http://cpan.org>.
263 It is important to remember that ideas do not always result in a direct code
264 contribution, but deserve acknowledgement just the same. Time and time again
265 the seemingly most insignificant questions and suggestions have been shown
266 to catalyze monumental improvements in consistency, accuracy and performance.
267
268 =for comment this line is replaced with the author list at dist-building time
269
270 The canonical source of authors and their details is the F<AUTHORS> file at
271 the root of this distribution (or repository). The canonical source of
272 per-line authorship is the L<git repository|/HOW TO CONTRIBUTE> history
273 itself.
274
275 =head1 CAT HERDERS
276
277 The fine folks nudging the project in a particular direction:
278
279 =over
280
281 B<ribasushi>: Peter Rabbitson <ribasushi@cpan.org>
282 (present day maintenance and controlled evolution)
283
284 B<castaway>: Jess Robinson <castaway@desert-island.me.uk>
285 (lions share of the reference documentation and manuals)
286
287 B<mst>: Matt S Trout <mst@shadowcat.co.uk> (project founder -
288 original idea, architecture and implementation)
289
290 =back
291
292 =head1 COPYRIGHT AND LICENSE
293
294 Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class
295 L</AUTHORS> as listed above and in F<AUTHORS>.
296
297 This library is free software and may be distributed under the same terms
298 as perl5 itself. See F<LICENSE> for the complete licensing terms.