PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/perl/site/lib/DBIx/Class.pm

http://github.com/dwimperl/perl-5.14.2.1-32bit-windows
Perl | 521 lines | 327 code | 181 blank | 13 comment | 17 complexity | 8275ed6f1fa43ad5e2d4f5fba172d748 MD5 | raw file
Possible License(s): LGPL-3.0, Unlicense, GPL-2.0, LGPL-2.0, BSD-3-Clause, LGPL-2.1, AGPL-1.0, GPL-3.0
  1. package DBIx::Class;
  2. use strict;
  3. use warnings;
  4. our $VERSION;
  5. # Always remember to do all digits for the version even if they're 0
  6. # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
  7. # brain damage and presumably various other packaging systems too
  8. # $VERSION declaration must stay up here, ahead of any other package
  9. # declarations, as to not confuse various modules attempting to determine
  10. # this ones version, whether that be s.c.o. or Module::Metadata, etc
  11. $VERSION = '0.08196';
  12. $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
  13. BEGIN {
  14. package # hide from pause
  15. DBIx::Class::_ENV_;
  16. if ($] < 5.009_005) {
  17. require MRO::Compat;
  18. *OLD_MRO = sub () { 1 };
  19. }
  20. else {
  21. require mro;
  22. *OLD_MRO = sub () { 0 };
  23. }
  24. # ::Runmode would only be loaded by DBICTest, which in turn implies t/
  25. *DBICTEST = eval { DBICTest::RunMode->is_author }
  26. ? sub () { 1 }
  27. : sub () { 0 }
  28. ;
  29. # There was a brief period of p5p insanity when $@ was invisible in a DESTROY
  30. *INVISIBLE_DOLLAR_AT = ($] >= 5.013001 and $] <= 5.013007)
  31. ? sub () { 1 }
  32. : sub () { 0 }
  33. ;
  34. # During 5.13 dev cycle HELEMs started to leak on copy
  35. *PEEPEENESS = (defined $ENV{DBICTEST_ALL_LEAKS}
  36. # request for all tests would force "non-leaky" illusion and vice-versa
  37. ? ! $ENV{DBICTEST_ALL_LEAKS}
  38. # otherwise confess that this perl is busted ONLY on smokers
  39. : do {
  40. if (eval { DBICTest::RunMode->is_smoker }) {
  41. # leaky 5.13.6 (fixed in blead/cefd5c7c)
  42. if ($] == '5.013006') { 1 }
  43. # not sure why this one leaks, but disable anyway - ANDK seems to make it weep
  44. elsif ($] == '5.013005') { 1 }
  45. else { 0 }
  46. }
  47. else { 0 }
  48. }
  49. ) ? sub () { 1 } : sub () { 0 };
  50. }
  51. use mro 'c3';
  52. use DBIx::Class::Optional::Dependencies;
  53. use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
  54. use DBIx::Class::StartupCheck;
  55. __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
  56. __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped$');
  57. sub mk_classdata {
  58. shift->mk_classaccessor(@_);
  59. }
  60. sub mk_classaccessor {
  61. my $self = shift;
  62. $self->mk_group_accessors('inherited', $_[0]);
  63. $self->set_inherited(@_) if @_ > 1;
  64. }
  65. sub component_base_class { 'DBIx::Class' }
  66. sub MODIFY_CODE_ATTRIBUTES {
  67. my ($class,$code,@attrs) = @_;
  68. $class->mk_classdata('__attr_cache' => {})
  69. unless $class->can('__attr_cache');
  70. $class->__attr_cache->{$code} = [@attrs];
  71. return ();
  72. }
  73. sub _attr_cache {
  74. my $self = shift;
  75. my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
  76. return {
  77. %$cache,
  78. %{ $self->maybe::next::method || {} },
  79. };
  80. }
  81. 1;
  82. =head1 NAME
  83. DBIx::Class - Extensible and flexible object <-> relational mapper.
  84. =head1 GETTING HELP/SUPPORT
  85. The community can be found via:
  86. =over
  87. =item * Web Site: L<http://www.dbix-class.org/>
  88. =item * IRC: irc.perl.org#dbix-class
  89. =for html
  90. <a href="http://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
  91. =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
  92. =item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class>
  93. =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
  94. =item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
  95. =item * twitter L<http://www.twitter.com/dbix_class>
  96. =back
  97. =head1 SYNOPSIS
  98. Create a schema class called MyApp/Schema.pm:
  99. package MyApp::Schema;
  100. use base qw/DBIx::Class::Schema/;
  101. __PACKAGE__->load_namespaces();
  102. 1;
  103. Create a result class to represent artists, who have many CDs, in
  104. MyApp/Schema/Result/Artist.pm:
  105. See L<DBIx::Class::ResultSource> for docs on defining result classes.
  106. package MyApp::Schema::Result::Artist;
  107. use base qw/DBIx::Class::Core/;
  108. __PACKAGE__->table('artist');
  109. __PACKAGE__->add_columns(qw/ artistid name /);
  110. __PACKAGE__->set_primary_key('artistid');
  111. __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
  112. 1;
  113. A result class to represent a CD, which belongs to an artist, in
  114. MyApp/Schema/Result/CD.pm:
  115. package MyApp::Schema::Result::CD;
  116. use base qw/DBIx::Class::Core/;
  117. __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
  118. __PACKAGE__->table('cd');
  119. __PACKAGE__->add_columns(qw/ cdid artistid title year /);
  120. __PACKAGE__->set_primary_key('cdid');
  121. __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
  122. 1;
  123. Then you can use these classes in your application's code:
  124. # Connect to your database.
  125. use MyApp::Schema;
  126. my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
  127. # Query for all artists and put them in an array,
  128. # or retrieve them as a result set object.
  129. # $schema->resultset returns a DBIx::Class::ResultSet
  130. my @all_artists = $schema->resultset('Artist')->all;
  131. my $all_artists_rs = $schema->resultset('Artist');
  132. # Output all artists names
  133. # $artist here is a DBIx::Class::Row, which has accessors
  134. # for all its columns. Rows are also subclasses of your Result class.
  135. foreach $artist (@all_artists) {
  136. print $artist->name, "\n";
  137. }
  138. # Create a result set to search for artists.
  139. # This does not query the DB.
  140. my $johns_rs = $schema->resultset('Artist')->search(
  141. # Build your WHERE using an SQL::Abstract structure:
  142. { name => { like => 'John%' } }
  143. );
  144. # Execute a joined query to get the cds.
  145. my @all_john_cds = $johns_rs->search_related('cds')->all;
  146. # Fetch the next available row.
  147. my $first_john = $johns_rs->next;
  148. # Specify ORDER BY on the query.
  149. my $first_john_cds_by_title_rs = $first_john->cds(
  150. undef,
  151. { order_by => 'title' }
  152. );
  153. # Create a result set that will fetch the artist data
  154. # at the same time as it fetches CDs, using only one query.
  155. my $millennium_cds_rs = $schema->resultset('CD')->search(
  156. { year => 2000 },
  157. { prefetch => 'artist' }
  158. );
  159. my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
  160. my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
  161. # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
  162. # create() is the same as new() then insert().
  163. my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
  164. $new_cd->artist($cd->artist);
  165. $new_cd->insert; # Auto-increment primary key filled in after INSERT
  166. $new_cd->title('Fork');
  167. $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
  168. # change the year of all the millennium CDs at once
  169. $millennium_cds_rs->update({ year => 2002 });
  170. =head1 DESCRIPTION
  171. This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
  172. (with a compatibility layer as a springboard for porting) and a resultset API
  173. that allows abstract encapsulation of database operations. It aims to make
  174. representing queries in your code as perl-ish as possible while still
  175. providing access to as many of the capabilities of the database as possible,
  176. including retrieving related records from multiple tables in a single query,
  177. JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
  178. DBIx::Class can handle multi-column primary and foreign keys, complex
  179. queries and database-level paging, and does its best to only query the
  180. database in order to return something you've directly asked for. If a
  181. resultset is used as an iterator it only fetches rows off the statement
  182. handle as requested in order to minimise memory usage. It has auto-increment
  183. support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
  184. known to be used in production on at least the first four, and is fork-
  185. and thread-safe out of the box (although
  186. L<your DBD may not be|DBI/Threads_and_Thread_Safety>).
  187. This project is still under rapid development, so large new features may be
  188. marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
  189. Failing test cases are *always* welcome and point releases are put out rapidly
  190. as bugs are found and fixed.
  191. We do our best to maintain full backwards compatibility for published
  192. APIs, since DBIx::Class is used in production in many organisations,
  193. and even backwards incompatible changes to non-published APIs will be fixed
  194. if they're reported and doing so doesn't cost the codebase anything.
  195. The test suite is quite substantial, and several developer releases
  196. are generally made to CPAN before the branch for the next release is
  197. merged back to trunk for a major release.
  198. =head1 WHERE TO GO NEXT
  199. L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
  200. the modules where you will find documentation.
  201. =head1 AUTHOR
  202. mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
  203. (I mostly consider myself "project founder" these days but the AUTHOR heading
  204. is traditional :)
  205. =head1 CONTRIBUTORS
  206. abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
  207. acca: Alexander Kuznetsov <acca@cpan.org>
  208. aherzog: Adam Herzog <adam@herzogdesigns.com>
  209. Alexander Keusch <cpan@keusch.at>
  210. alnewkirk: Al Newkirk <we@ana.im>
  211. amiri: Amiri Barksdale <amiri@metalabel.com>
  212. amoore: Andrew Moore <amoore@cpan.org>
  213. andyg: Andy Grundman <andy@hybridized.org>
  214. ank: Andres Kievsky
  215. arc: Aaron Crane <arc@cpan.org>
  216. arcanez: Justin Hunter <justin.d.hunter@gmail.com>
  217. ash: Ash Berlin <ash@cpan.org>
  218. bert: Norbert Csongradi <bert@cpan.org>
  219. blblack: Brandon L. Black <blblack@gmail.com>
  220. bluefeet: Aran Deltac <bluefeet@cpan.org>
  221. bphillips: Brian Phillips <bphillips@cpan.org>
  222. boghead: Bryan Beeley <cpan@beeley.org>
  223. bricas: Brian Cassidy <bricas@cpan.org>
  224. brunov: Bruno Vecchi <vecchi.b@gmail.com>
  225. caelum: Rafael Kitover <rkitover@cpan.org>
  226. caldrin: Maik Hentsche <maik.hentsche@amd.com>
  227. castaway: Jess Robinson
  228. claco: Christopher H. Laco
  229. clkao: CL Kao
  230. da5id: David Jack Olrik <djo@cpan.org>
  231. debolaz: Anders Nor Berle <berle@cpan.org>
  232. dew: Dan Thomas <dan@godders.org>
  233. dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
  234. dnm: Justin Wheeler <jwheeler@datademons.com>
  235. dpetrov: Dimitar Petrov <mitakaa@gmail.com>
  236. dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
  237. dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
  238. felliott: Fitz Elliott <fitz.elliott@gmail.com>
  239. freetime: Bill Moseley <moseley@hank.org>
  240. frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
  241. goraxe: Gordon Irving <goraxe@cpan.org>
  242. gphat: Cory G Watson <gphat@cpan.org>
  243. Grant Street Group L<http://www.grantstreet.com/>
  244. groditi: Guillermo Roditi <groditi@cpan.org>
  245. Haarg: Graham Knop <haarg@haarg.org>
  246. hobbs: Andrew Rodland <arodland@cpan.org>
  247. ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
  248. initself: Mike Baas <mike@initselftech.com>
  249. ironcamel: Naveed Massjouni <naveedm9@gmail.com>
  250. jawnsy: Jonathan Yu <jawnsy@cpan.org>
  251. jasonmay: Jason May <jason.a.may@gmail.com>
  252. jesper: Jesper Krogh
  253. jgoulah: John Goulah <jgoulah@cpan.org>
  254. jguenther: Justin Guenther <jguenther@cpan.org>
  255. jhannah: Jay Hannah <jay@jays.net>
  256. jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
  257. jon: Jon Schutz <jjschutz@cpan.org>
  258. jshirley: J. Shirley <jshirley@gmail.com>
  259. kaare: Kaare Rasmussen
  260. konobi: Scott McWhirter
  261. littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
  262. lukes: Luke Saunders <luke.saunders@gmail.com>
  263. marcus: Marcus Ramberg <mramberg@cpan.org>
  264. mattlaw: Matt Lawrence
  265. mattp: Matt Phillips <mattp@cpan.org>
  266. michaelr: Michael Reddick <michael.reddick@gmail.com>
  267. milki: Jonathan Chu <milki@rescomp.berkeley.edu>
  268. mstratman: Mark A. Stratman <stratman@gmail.com>
  269. ned: Neil de Carteret
  270. nigel: Nigel Metheringham <nigelm@cpan.org>
  271. ningu: David Kamholz <dkamholz@cpan.org>
  272. Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
  273. norbi: Norbert Buchmuller <norbi@nix.hu>
  274. nuba: Nuba Princigalli <nuba@cpan.org>
  275. Numa: Dan Sully <daniel@cpan.org>
  276. ovid: Curtis "Ovid" Poe <ovid@cpan.org>
  277. oyse: E<Oslash>ystein Torget <oystein.torget@dnv.com>
  278. paulm: Paul Makepeace
  279. penguin: K J Cheetham
  280. perigrin: Chris Prather <chris@prather.org>
  281. peter: Peter Collingbourne <peter@pcc.me.uk>
  282. Peter Valdemar ME<oslash>rch <peter@morch.com>
  283. phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
  284. plu: Johannes Plunien <plu@cpan.org>
  285. Possum: Daniel LeWarne <possum@cpan.org>
  286. quicksilver: Jules Bean
  287. rafl: Florian Ragwitz <rafl@debian.org>
  288. rainboxx: Matthias Dietrich <perl@rb.ly>
  289. rbo: Robert Bohne <rbo@cpan.org>
  290. rbuels: Robert Buels <rmb32@cornell.edu>
  291. rdj: Ryan D Johnson <ryan@innerfence.com>
  292. ribasushi: Peter Rabbitson <ribasushi@cpan.org>
  293. rjbs: Ricardo Signes <rjbs@cpan.org>
  294. robkinyon: Rob Kinyon <rkinyon@cpan.org>
  295. Robert Olson <bob@rdolson.org>
  296. Roman: Roman Filippov <romanf@cpan.org>
  297. Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
  298. sc_: Just Another Perl Hacker
  299. scotty: Scotty Allen <scotty@scottyallen.com>
  300. semifor: Marc Mims <marc@questright.com>
  301. solomon: Jared Johnson <jaredj@nmgi.com>
  302. spb: Stephen Bennett <stephen@freenode.net>
  303. Squeeks <squeek@cpan.org>
  304. sszabo: Stephan Szabo <sszabo@bigpanda.com>
  305. talexb: Alex Beamish <talexb@gmail.com>
  306. tamias: Ronald J Kimball <rjk@tamias.net>
  307. teejay : Aaron Trevena <teejay@cpan.org>
  308. Todd Lipcon
  309. Tom Hukins
  310. tonvoon: Ton Voon <tonvoon@cpan.org>
  311. triode: Pete Gamache <gamache@cpan.org>
  312. typester: Daisuke Murase <typester@cpan.org>
  313. victori: Victor Igumnov <victori@cpan.org>
  314. wdh: Will Hawes
  315. willert: Sebastian Willert <willert@cpan.org>
  316. wreis: Wallace Reis <wreis@cpan.org>
  317. yrlnry: Mark Jason Dominus <mjd@plover.com>
  318. zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
  319. =head1 COPYRIGHT
  320. Copyright (c) 2005 - 2011 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
  321. as listed above.
  322. =head1 LICENSE
  323. This library is free software and may be distributed under the same terms
  324. as perl itself.
  325. =cut