7 # Always remember to do all digits for the version even if they're 0
8 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
9 # brain damage and presumably various other packaging systems too
11 # $VERSION declaration must stay up here, ahead of any other package
12 # declarations, as to not confuse various modules attempting to determine
13 # this ones version, whether that be s.c.o. or Module::Metadata, etc
14 $VERSION = '0.082700_06';
16 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
18 use DBIx::Class::_Util;
21 use DBIx::Class::Optional::Dependencies;
23 use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
24 use DBIx::Class::StartupCheck;
25 use DBIx::Class::Exception;
27 __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
28 __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve');
31 shift->mk_classaccessor(@_);
34 sub mk_classaccessor {
36 $self->mk_group_accessors('inherited', $_[0]);
37 $self->set_inherited(@_) if @_ > 1;
40 sub component_base_class { 'DBIx::Class' }
42 sub MODIFY_CODE_ATTRIBUTES {
43 my ($class,$code,@attrs) = @_;
44 $class->mk_classdata('__attr_cache' => {})
45 unless $class->can('__attr_cache');
46 $class->__attr_cache->{$code} = [@attrs];
52 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
56 %{ $self->maybe::next::method || {} },
60 # *DO NOT* change this URL nor the identically named =head1 below
61 # it is linked throughout the ecosystem
62 sub DBIx::Class::_ENV_::HELP_URL () {
63 'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT'
74 DBIx::Class - Extensible and flexible object <-> relational mapper.
76 =head1 WHERE TO START READING
78 See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
79 To get the most out of DBIx::Class with the least confusion it is strongly
80 recommended to read (at the very least) the
81 L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
85 =head1 GETTING HELP/SUPPORT
87 Due to the sheer size of its problem domain, DBIx::Class is a relatively
88 complex framework. After you start using DBIx::Class questions will inevitably
89 arise. If you are stuck with a problem or have doubts about a particular
90 approach do not hesitate to contact us via any of the following options (the
91 list is sorted by "fastest response time"):
95 =item * IRC: irc.perl.org#dbix-class
98 <a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
100 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
102 =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
104 =item * Twitter: L<https://www.twitter.com/dbix_class>
106 =item * Web Site: L<http://www.dbix-class.org/>
112 For the very impatient: L<DBIx::Class::Manual::QuickStart>
114 This code in the next step can be generated automatically from an existing
115 database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
117 =head2 Schema classes preparation
119 Create a schema class called F<MyApp/Schema.pm>:
121 package MyApp::Schema;
122 use base qw/DBIx::Class::Schema/;
124 __PACKAGE__->load_namespaces();
128 Create a result class to represent artists, who have many CDs, in
129 F<MyApp/Schema/Result/Artist.pm>:
131 See L<DBIx::Class::ResultSource> for docs on defining result classes.
133 package MyApp::Schema::Result::Artist;
134 use base qw/DBIx::Class::Core/;
136 __PACKAGE__->table('artist');
137 __PACKAGE__->add_columns(qw/ artistid name /);
138 __PACKAGE__->set_primary_key('artistid');
139 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
143 A result class to represent a CD, which belongs to an artist, in
144 F<MyApp/Schema/Result/CD.pm>:
146 package MyApp::Schema::Result::CD;
147 use base qw/DBIx::Class::Core/;
149 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
150 __PACKAGE__->table('cd');
151 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
152 __PACKAGE__->set_primary_key('cdid');
153 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
159 Then you can use these classes in your application's code:
161 # Connect to your database.
163 my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
165 # Query for all artists and put them in an array,
166 # or retrieve them as a result set object.
167 # $schema->resultset returns a DBIx::Class::ResultSet
168 my @all_artists = $schema->resultset('Artist')->all;
169 my $all_artists_rs = $schema->resultset('Artist');
171 # Output all artists names
172 # $artist here is a DBIx::Class::Row, which has accessors
173 # for all its columns. Rows are also subclasses of your Result class.
174 foreach $artist (@all_artists) {
175 print $artist->name, "\n";
178 # Create a result set to search for artists.
179 # This does not query the DB.
180 my $johns_rs = $schema->resultset('Artist')->search(
181 # Build your WHERE using an SQL::Abstract structure:
182 { name => { like => 'John%' } }
185 # Execute a joined query to get the cds.
186 my @all_john_cds = $johns_rs->search_related('cds')->all;
188 # Fetch the next available row.
189 my $first_john = $johns_rs->next;
191 # Specify ORDER BY on the query.
192 my $first_john_cds_by_title_rs = $first_john->cds(
194 { order_by => 'title' }
197 # Create a result set that will fetch the artist data
198 # at the same time as it fetches CDs, using only one query.
199 my $millennium_cds_rs = $schema->resultset('CD')->search(
201 { prefetch => 'artist' }
204 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
205 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
207 # new() makes a Result object but doesnt insert it into the DB.
208 # create() is the same as new() then insert().
209 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
210 $new_cd->artist($cd->artist);
211 $new_cd->insert; # Auto-increment primary key filled in after INSERT
212 $new_cd->title('Fork');
214 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
216 # change the year of all the millennium CDs at once
217 $millennium_cds_rs->update({ year => 2002 });
221 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
222 (with a compatibility layer as a springboard for porting) and a resultset API
223 that allows abstract encapsulation of database operations. It aims to make
224 representing queries in your code as perl-ish as possible while still
225 providing access to as many of the capabilities of the database as possible,
226 including retrieving related records from multiple tables in a single query,
227 C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
230 DBIx::Class can handle multi-column primary and foreign keys, complex
231 queries and database-level paging, and does its best to only query the
232 database in order to return something you've directly asked for. If a
233 resultset is used as an iterator it only fetches rows off the statement
234 handle as requested in order to minimise memory usage. It has auto-increment
235 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
236 known to be used in production on at least the first four, and is fork-
237 and thread-safe out of the box (although
238 L<your DBD may not be|DBI/Threads and Thread Safety>).
240 This project is still under rapid development, so large new features may be
241 marked B<experimental> - such APIs are still usable but may have edge bugs.
242 Failing test cases are I<always> welcome and point releases are put out rapidly
243 as bugs are found and fixed.
245 We do our best to maintain full backwards compatibility for published
246 APIs, since DBIx::Class is used in production in many organisations,
247 and even backwards incompatible changes to non-published APIs will be fixed
248 if they're reported and doing so doesn't cost the codebase anything.
250 The test suite is quite substantial, and several developer releases
251 are generally made to CPAN before the branch for the next release is
252 merged back to trunk for a major release.
254 =head1 HOW TO CONTRIBUTE
256 Contributions are always welcome, in all usable forms (we especially
257 welcome documentation improvements). The delivery methods include git-
258 or unified-diff formatted patches, GitHub pull requests, or plain bug
259 reports either via RT or the Mailing list. Contributors are generally
260 granted full access to the official repository after their first patch
261 passes successful review.
264 FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
266 This project is maintained in a git repository. The code and related tools are
267 accessible at the following locations:
271 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
273 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
275 =item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class>
277 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
279 =item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds>
282 ↪ Stable branch CI status: <img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img>
288 Even though a large portion of the source I<appears> to be written by just a
289 handful of people, this library continues to remain a collaborative effort -
290 perhaps one of the most successful such projects on L<CPAN|http://cpan.org>.
291 It is important to remember that ideas do not always result in a direct code
292 contribution, but deserve acknowledgement just the same. Time and time again
293 the seemingly most insignificant questions and suggestions have been shown
294 to catalyze monumental improvements in consistency, accuracy and performance.
296 =for comment this line is replaced with the author list at dist-building time
298 The canonical source of authors and their details is the F<AUTHORS> file at
299 the root of this distribution (or repository). The canonical source of
300 per-line authorship is the L<git repository|/HOW TO CONTRIBUTE> history
305 Copyright (c) 2005 the DBIx::Class L</AUTHORS> as listed above.
309 This library is free software and may be distributed under the same terms