.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "DBIx::Class 3pm" .TH DBIx::Class 3pm "2022-05-21" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" DBIx::Class \- Extensible and flexible object <\-> relational mapper. .SH "WHERE TO START READING" .IX Header "WHERE TO START READING" See DBIx::Class::Manual::DocMap for an overview of the exhaustive documentation. To get the most out of DBIx::Class with the least confusion it is strongly recommended to read (at the very least) the Manuals in the order presented there. .SH "GETTING HELP/SUPPORT" .IX Header "GETTING HELP/SUPPORT" Due to the sheer size of its problem domain, DBIx::Class is a relatively complex framework. After you start using DBIx::Class questions will inevitably arise. If you are stuck with a problem or have doubts about a particular approach do not hesitate to contact us via any of the following options (the list is sorted by \*(L"fastest response time\*(R"): .IP "\(bu" 4 \&\s-1RT\s0 Bug Tracker: .IP "\(bu" 4 Email: .IP "\(bu" 4 Twitter: .SH "SYNOPSIS" .IX Header "SYNOPSIS" For the very impatient: DBIx::Class::Manual::QuickStart .PP This code in the next step can be generated automatically from an existing database, see dbicdump from the distribution \f(CW\*(C`DBIx\-Class\-Schema\-Loader\*(C'\fR. .SS "Schema classes preparation" .IX Subsection "Schema classes preparation" Create a schema class called \fIMyApp/Schema.pm\fR: .PP .Vb 2 \& package MyApp::Schema; \& use base qw/DBIx::Class::Schema/; \& \& _\|_PACKAGE_\|_\->load_namespaces(); \& \& 1; .Ve .PP Create a result class to represent artists, who have many CDs, in \&\fIMyApp/Schema/Result/Artist.pm\fR: .PP See DBIx::Class::ResultSource for docs on defining result classes. .PP .Vb 2 \& package MyApp::Schema::Result::Artist; \& use base qw/DBIx::Class::Core/; \& \& _\|_PACKAGE_\|_\->table(\*(Aqartist\*(Aq); \& _\|_PACKAGE_\|_\->add_columns(qw/ artistid name /); \& _\|_PACKAGE_\|_\->set_primary_key(\*(Aqartistid\*(Aq); \& _\|_PACKAGE_\|_\->has_many(cds => \*(AqMyApp::Schema::Result::CD\*(Aq, \*(Aqartistid\*(Aq); \& \& 1; .Ve .PP A result class to represent a \s-1CD,\s0 which belongs to an artist, in \&\fIMyApp/Schema/Result/CD.pm\fR: .PP .Vb 2 \& package MyApp::Schema::Result::CD; \& use base qw/DBIx::Class::Core/; \& \& _\|_PACKAGE_\|_\->load_components(qw/InflateColumn::DateTime/); \& _\|_PACKAGE_\|_\->table(\*(Aqcd\*(Aq); \& _\|_PACKAGE_\|_\->add_columns(qw/ cdid artistid title year /); \& _\|_PACKAGE_\|_\->set_primary_key(\*(Aqcdid\*(Aq); \& _\|_PACKAGE_\|_\->belongs_to(artist => \*(AqMyApp::Schema::Result::Artist\*(Aq, \*(Aqartistid\*(Aq); \& \& 1; .Ve .SS "\s-1API\s0 usage" .IX Subsection "API usage" Then you can use these classes in your application's code: .PP .Vb 3 \& # Connect to your database. \& use MyApp::Schema; \& my $schema = MyApp::Schema\->connect($dbi_dsn, $user, $pass, \e%dbi_params); \& \& # Query for all artists and put them in an array, \& # or retrieve them as a result set object. \& # $schema\->resultset returns a DBIx::Class::ResultSet \& my @all_artists = $schema\->resultset(\*(AqArtist\*(Aq)\->all; \& my $all_artists_rs = $schema\->resultset(\*(AqArtist\*(Aq); \& \& # Output all artists names \& # $artist here is a DBIx::Class::Row, which has accessors \& # for all its columns. Rows are also subclasses of your Result class. \& foreach $artist (@all_artists) { \& print $artist\->name, "\en"; \& } \& \& # Create a result set to search for artists. \& # This does not query the DB. \& my $johns_rs = $schema\->resultset(\*(AqArtist\*(Aq)\->search( \& # Build your WHERE using an SQL::Abstract::Classic\-compatible structure: \& { name => { like => \*(AqJohn%\*(Aq } } \& ); \& \& # Execute a joined query to get the cds. \& my @all_john_cds = $johns_rs\->search_related(\*(Aqcds\*(Aq)\->all; \& \& # Fetch the next available row. \& my $first_john = $johns_rs\->next; \& \& # Specify ORDER BY on the query. \& my $first_john_cds_by_title_rs = $first_john\->cds( \& undef, \& { order_by => \*(Aqtitle\*(Aq } \& ); \& \& # Create a result set that will fetch the artist data \& # at the same time as it fetches CDs, using only one query. \& my $millennium_cds_rs = $schema\->resultset(\*(AqCD\*(Aq)\->search( \& { year => 2000 }, \& { prefetch => \*(Aqartist\*(Aq } \& ); \& \& my $cd = $millennium_cds_rs\->next; # SELECT ... FROM cds JOIN artists ... \& my $cd_artist_name = $cd\->artist\->name; # Already has the data so no 2nd query \& \& # new() makes a Result object but doesn\*(Aqt insert it into the DB. \& # create() is the same as new() then insert(). \& my $new_cd = $schema\->resultset(\*(AqCD\*(Aq)\->new({ title => \*(AqSpoon\*(Aq }); \& $new_cd\->artist($cd\->artist); \& $new_cd\->insert; # Auto\-increment primary key filled in after INSERT \& $new_cd\->title(\*(AqFork\*(Aq); \& \& $schema\->txn_do(sub { $new_cd\->update }); # Runs the update in a transaction \& \& # change the year of all the millennium CDs at once \& $millennium_cds_rs\->update({ year => 2002 }); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is an \s-1SQL\s0 to \s-1OO\s0 mapper with an object \s-1API\s0 inspired by Class::DBI (with a compatibility layer as a springboard for porting) and a resultset \s-1API\s0 that allows abstract encapsulation of database operations. It aims to make representing queries in your code as perl-ish as possible while still providing access to as many of the capabilities of the database as possible, including retrieving related records from multiple tables in a single query, \&\f(CW\*(C`JOIN\*(C'\fR, \f(CW\*(C`LEFT JOIN\*(C'\fR, \f(CW\*(C`COUNT\*(C'\fR, \f(CW\*(C`DISTINCT\*(C'\fR, \f(CW\*(C`GROUP BY\*(C'\fR, \f(CW\*(C`ORDER BY\*(C'\fR and \&\f(CW\*(C`HAVING\*(C'\fR support. .PP DBIx::Class can handle multi-column primary and foreign keys, complex queries and database-level paging, and does its best to only query the database in order to return something you've directly asked for. If a resultset is used as an iterator it only fetches rows off the statement handle as requested in order to minimise memory usage. It has auto-increment support for SQLite, MySQL, PostgreSQL, Oracle, \s-1SQL\s0 Server and \s-1DB2\s0 and is known to be used in production on at least the first four, and is fork\- and thread-safe out of the box (although your \s-1DBD\s0 may not be). .PP This project is still under rapid development, so large new features may be marked \fBexperimental\fR \- such APIs are still usable but may have edge bugs. Failing test cases are \fIalways\fR welcome and point releases are put out rapidly as bugs are found and fixed. .PP We do our best to maintain full backwards compatibility for published APIs, since DBIx::Class is used in production in many organisations, and even backwards incompatible changes to non-published APIs will be fixed if they're reported and doing so doesn't cost the codebase anything. .PP The test suite is quite substantial, and several developer releases are generally made to \s-1CPAN\s0 before the branch for the next release is merged back to trunk for a major release. .SH "HOW TO CONTRIBUTE" .IX Header "HOW TO CONTRIBUTE" Contributions are always welcome, in all usable forms (we especially welcome documentation improvements). The delivery methods include git\- or unified-diff formatted patches, GitHub pull requests, or plain bug reports either via \s-1RT\s0 or the Mailing list. Do not hesitate to get in touch with any further questions you may have. .PP This project is maintained in a git repository. The code and related tools are accessible at the following locations: .IP "\(bu" 4 Current git repository: .IP "\(bu" 4 Travis-CI log: .SH "AUTHORS" .IX Header "AUTHORS" Even though a large portion of the source \fIappears\fR to be written by just a handful of people, this library continues to remain a collaborative effort \- perhaps one of the most successful such projects on \s-1CPAN\s0 . It is important to remember that ideas do not always result in a direct code contribution, but deserve acknowledgement just the same. Time and time again the seemingly most insignificant questions and suggestions have been shown to catalyze monumental improvements in consistency, accuracy and performance. .PP The canonical source of authors and their details is the \fI\s-1AUTHORS\s0\fR file at the root of this distribution (or repository). The canonical source of per-line authorship is the git repository history itself. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class \&\*(L"\s-1AUTHORS\*(R"\s0 as listed above and in \fI\s-1AUTHORS\s0\fR. .PP This library is free software and may be distributed under the same terms as perl5 itself. See \fI\s-1LICENSE\s0\fR for the complete licensing terms.