3 # important to load early
4 use DBIx::Class::_Util;
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
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';
19 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
23 use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
24 use DBIx::Class::Exception;
26 use DBIx::Class::_Util qw( uniq refdesc visit_namespaces );
27 use Scalar::Util qw( weaken refaddr );
30 __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
31 __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve|^Moose::Meta::');
33 # FIXME - this is not really necessary, and is in
34 # fact going to slow things down a bit
35 # However it is the right thing to do in order to get
36 # various install bases to highlight their brokenness
37 # Remove at some unknown point in the future
39 # The oddball BEGIN is there for... reason unknown
40 # It does make non-segfaulty difference on pre-5.8.5 perls, so shrug
42 sub DESTROY { &DBIx::Class::_Util::detected_reinvoked_destructor };
45 sub component_base_class { 'DBIx::Class' }
49 sub DBIx::Class::__Attr_iThreads_handler__::CLONE {
51 # this is disgusting, but the best we can do without even more surgery
52 visit_namespaces( action => sub {
55 # skip dangerous namespaces
56 return 1 if $pkg =~ /^ (?:
57 DB | next | B | .+? ::::ISA (?: ::CACHE ) | Class::C3
63 exists ${"${pkg}::"}{__cag___attr_cache}
65 ref( my $attr_stash = ${"${pkg}::__cag___attr_cache"} ) eq 'HASH'
67 $attr_stash->{ $cref_registry->{$_} } = delete $attr_stash->{$_}
68 for keys %$attr_stash;
75 sub MODIFY_CODE_ATTRIBUTES {
76 my ($class,$code,@attrs) = @_;
77 $class->mk_classaccessor('__attr_cache' => {})
78 unless $class->can('__attr_cache');
81 defined $cref_registry->{$_} or delete $cref_registry->{$_}
82 for keys %$cref_registry;
84 # The original API used stringification instead of refaddr - can't change that now
85 if( $cref_registry->{$code} ) {
86 Carp::confess( sprintf
87 "Coderefs '%s' and '%s' stringify to the same value '%s': nothing will work",
89 refdesc($cref_registry->{$code}),
91 ) if refaddr($cref_registry->{$code}) != refaddr($code);
94 weaken( $cref_registry->{$code} = $code )
97 $class->__attr_cache->{$code} = [ sort( uniq(
98 @{ $class->__attr_cache->{$code} || [] },
102 # FIXME - DBIC essentially gobbles up any attribute it can lay its hands on:
105 # There should be some sort of warning on unrecognized attributes or
106 # somesuch... OTOH people do use things in the wild hence the plan of action
107 # is anything but clear :/
109 # https://metacpan.org/source/ZIGOROU/DBIx-Class-Service-0.02/lib/DBIx/Class/Service.pm#L93-110
110 # https://metacpan.org/source/ZIGOROU/DBIx-Class-Service-0.02/t/lib/DBIC/Test/Service/User.pm#L29
111 # https://metacpan.org/source/ZIGOROU/DBIx-Class-Service-0.02/t/lib/DBIC/Test/Service/User.pm#L36
116 sub FETCH_CODE_ATTRIBUTES {
117 my ($class,$code) = @_;
118 @{ $class->_attr_cache->{$code} || [] }
124 %{ $self->can('__attr_cache') ? $self->__attr_cache : {} },
125 %{ $self->maybe::next::method || {} },
129 # *DO NOT* change this URL nor the identically named =head1 below
130 # it is linked throughout the ecosystem
131 sub DBIx::Class::_ENV_::HELP_URL () {
132 'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT'
141 DBIx::Class - Extensible and flexible object <-> relational mapper.
143 =head1 WHERE TO START READING
145 See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
146 To get the most out of DBIx::Class with the least confusion it is strongly
147 recommended to read (at the very least) the
148 L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
152 =head1 GETTING HELP/SUPPORT
154 Due to the sheer size of its problem domain, DBIx::Class is a relatively
155 complex framework. After you start using DBIx::Class questions will inevitably
156 arise. If you are stuck with a problem or have doubts about a particular
157 approach do not hesitate to contact us via any of the following options (the
158 list is sorted by "fastest response time"):
162 =item * IRC: irc.perl.org#dbix-class
165 <a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
167 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
169 =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
171 =item * Twitter: L<https://www.twitter.com/dbix_class>
173 =item * Web Site: L<http://www.dbix-class.org/>
179 For the very impatient: L<DBIx::Class::Manual::QuickStart>
181 This code in the next step can be generated automatically from an existing
182 database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
184 =head2 Schema classes preparation
186 Create a schema class called F<MyApp/Schema.pm>:
188 package MyApp::Schema;
189 use base qw/DBIx::Class::Schema/;
191 __PACKAGE__->load_namespaces();
195 Create a result class to represent artists, who have many CDs, in
196 F<MyApp/Schema/Result/Artist.pm>:
198 See L<DBIx::Class::ResultSource> for docs on defining result classes.
200 package MyApp::Schema::Result::Artist;
201 use base qw/DBIx::Class::Core/;
203 __PACKAGE__->table('artist');
204 __PACKAGE__->add_columns(qw/ artistid name /);
205 __PACKAGE__->set_primary_key('artistid');
206 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
210 A result class to represent a CD, which belongs to an artist, in
211 F<MyApp/Schema/Result/CD.pm>:
213 package MyApp::Schema::Result::CD;
214 use base qw/DBIx::Class::Core/;
216 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
217 __PACKAGE__->table('cd');
218 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
219 __PACKAGE__->set_primary_key('cdid');
220 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
226 Then you can use these classes in your application's code:
228 # Connect to your database.
230 my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
232 # Query for all artists and put them in an array,
233 # or retrieve them as a result set object.
234 # $schema->resultset returns a DBIx::Class::ResultSet
235 my @all_artists = $schema->resultset('Artist')->all;
236 my $all_artists_rs = $schema->resultset('Artist');
238 # Output all artists names
239 # $artist here is a DBIx::Class::Row, which has accessors
240 # for all its columns. Rows are also subclasses of your Result class.
241 foreach $artist (@all_artists) {
242 print $artist->name, "\n";
245 # Create a result set to search for artists.
246 # This does not query the DB.
247 my $johns_rs = $schema->resultset('Artist')->search(
248 # Build your WHERE using an SQL::Abstract structure:
249 { name => { like => 'John%' } }
252 # Execute a joined query to get the cds.
253 my @all_john_cds = $johns_rs->search_related('cds')->all;
255 # Fetch the next available row.
256 my $first_john = $johns_rs->next;
258 # Specify ORDER BY on the query.
259 my $first_john_cds_by_title_rs = $first_john->cds(
261 { order_by => 'title' }
264 # Create a result set that will fetch the artist data
265 # at the same time as it fetches CDs, using only one query.
266 my $millennium_cds_rs = $schema->resultset('CD')->search(
268 { prefetch => 'artist' }
271 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
272 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
274 # new() makes a Result object but doesn't insert it into the DB.
275 # create() is the same as new() then insert().
276 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
277 $new_cd->artist($cd->artist);
278 $new_cd->insert; # Auto-increment primary key filled in after INSERT
279 $new_cd->title('Fork');
281 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
283 # change the year of all the millennium CDs at once
284 $millennium_cds_rs->update({ year => 2002 });
288 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
289 (with a compatibility layer as a springboard for porting) and a resultset API
290 that allows abstract encapsulation of database operations. It aims to make
291 representing queries in your code as perl-ish as possible while still
292 providing access to as many of the capabilities of the database as possible,
293 including retrieving related records from multiple tables in a single query,
294 C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
297 DBIx::Class can handle multi-column primary and foreign keys, complex
298 queries and database-level paging, and does its best to only query the
299 database in order to return something you've directly asked for. If a
300 resultset is used as an iterator it only fetches rows off the statement
301 handle as requested in order to minimise memory usage. It has auto-increment
302 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
303 known to be used in production on at least the first four, and is fork-
304 and thread-safe out of the box (although
305 L<your DBD may not be|DBI/Threads and Thread Safety>).
307 This project is still under rapid development, so large new features may be
308 marked B<experimental> - such APIs are still usable but may have edge bugs.
309 Failing test cases are I<always> welcome and point releases are put out rapidly
310 as bugs are found and fixed.
312 We do our best to maintain full backwards compatibility for published
313 APIs, since DBIx::Class is used in production in many organisations,
314 and even backwards incompatible changes to non-published APIs will be fixed
315 if they're reported and doing so doesn't cost the codebase anything.
317 The test suite is quite substantial, and several developer releases
318 are generally made to CPAN before the branch for the next release is
319 merged back to trunk for a major release.
321 =head1 HOW TO CONTRIBUTE
323 Contributions are always welcome, in all usable forms (we especially
324 welcome documentation improvements). The delivery methods include git-
325 or unified-diff formatted patches, GitHub pull requests, or plain bug
326 reports either via RT or the Mailing list. Contributors are generally
327 granted access to the official repository after their first several
328 patches pass successful review. Don't hesitate to
329 L<contact|/GETTING HELP/SUPPORT> either of the L</CAT HERDERS> with
330 any further questions you may have.
333 FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
335 This project is maintained in a git repository. The code and related tools are
336 accessible at the following locations:
340 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
342 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
344 =item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class>
346 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
348 =item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds>
351 ↪ Bleeding edge dev CI status: <img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img>
357 Even though a large portion of the source I<appears> to be written by just a
358 handful of people, this library continues to remain a collaborative effort -
359 perhaps one of the most successful such projects on L<CPAN|http://cpan.org>.
360 It is important to remember that ideas do not always result in a direct code
361 contribution, but deserve acknowledgement just the same. Time and time again
362 the seemingly most insignificant questions and suggestions have been shown
363 to catalyze monumental improvements in consistency, accuracy and performance.
365 =for comment this line is replaced with the author list at dist-building time
367 The canonical source of authors and their details is the F<AUTHORS> file at
368 the root of this distribution (or repository). The canonical source of
369 per-line authorship is the L<git repository|/HOW TO CONTRIBUTE> history
374 The fine folks nudging the project in a particular direction:
378 B<ribasushi>: Peter Rabbitson <ribasushi@cpan.org>
379 (present day maintenance and controlled evolution)
381 B<castaway>: Jess Robinson <castaway@desert-island.me.uk>
382 (lions share of the reference documentation and manuals)
384 B<mst>: Matt S Trout <mst@shadowcat.co.uk> (project founder -
385 original idea, architecture and implementation)
389 =head1 COPYRIGHT AND LICENSE
391 Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class
392 L</AUTHORS> as listed above and in F<AUTHORS>.
394 This library is free software and may be distributed under the same terms
395 as perl5 itself. See F<LICENSE> for the complete licensing terms.