From: Daniel Westermann-Clark Date: Tue, 10 Jan 2006 22:29:01 +0000 (+0000) Subject: - Update indentation (again) to be consistent with code X-Git-Tag: v0.05005~117^2~72 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=35d4fe78ded60b3ae56daf65f9ae885abbbe3513;hp=a3c5e7e3f7db38cda83d5cf8e75eb24f9347bbea;p=dbsrgits%2FDBIx-Class.git - Update indentation (again) to be consistent with code - Change $iter to $rs in Manual::Intro - Provide more examples of ResultSet usage in Manual::Intro --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index bc9d12c..bd0e6b8 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -8,31 +8,31 @@ DBIx::Class::Manual::Cookbook - Miscellaneous recipes Sometimes you need to formulate a query using specific operators: - my @albums = MyApp::DB::Album->search({ - artist => { 'like', '%Lamb%' }, - title => { 'like', '%Fear of Fours%' }, - }); + my @albums = MyApp::DB::Album->search({ + artist => { 'like', '%Lamb%' }, + title => { 'like', '%Fear of Fours%' }, + }); This results in something like the following C clause: - WHERE artist LIKE '%Lamb%' AND title LIKE '%Fear of Fours%' + WHERE artist LIKE '%Lamb%' AND title LIKE '%Fear of Fours%' Other queries might require slightly more complex logic: - my @albums = MyApp::DB::Album->search({ - -or => [ - -and => [ - artist => { 'like', '%Smashing Pumpkins%' }, - title => 'Siamese Dream', - ], - artist => 'Starchildren', - ], - }); + my @albums = MyApp::DB::Album->search({ + -or => [ + -and => [ + artist => { 'like', '%Smashing Pumpkins%' }, + title => 'Siamese Dream', + ], + artist => 'Starchildren', + ], + }); This results in the following C clause: - WHERE ( artist LIKE '%Smashing Pumpkins%' AND title = 'Siamese Dream' ) - OR artist = 'Starchildren' + WHERE ( artist LIKE '%Smashing Pumpkins%' AND title = 'Siamese Dream' ) + OR artist = 'Starchildren' For more information on generating complex queries, see L. @@ -43,9 +43,9 @@ If you find yourself quitting an app with Control-C a lot during development, you might like to put the following signal handler in your main database class to make sure it disconnects cleanly: - $SIG{INT} = sub { - __PACKAGE__->storage->dbh->disconnect; - }; + $SIG{INT} = sub { + __PACKAGE__->storage->dbh->disconnect; + }; =head2 Using joins and prefetch @@ -54,117 +54,117 @@ See L. =head2 Transactions As of version 0.04001, there is improved transaction support in -L. Here is an example of the recommended +L. Here is an example of the recommended way to use it: - my $genus = Genus->find(12); + my $genus = Genus->find(12); + eval { + MyDB->txn_begin; + $genus->add_to_species({ name => 'troglodyte' }); + $genus->wings(2); + $genus->update; + cromulate($genus); # Can have a nested transation + MyDB->txn_commit; + }; + if ($@) { + # Rollback might fail, too eval { - MyDB->txn_begin; - $genus->add_to_species({ name => 'troglodyte' }); - $genus->wings(2); - $genus->update; - cromulate($genus); # Can have a nested transation - MyDB->txn_commit; + MyDB->txn_rollback }; - if ($@) { - # Rollback might fail, too - eval { - MyDB->txn_rollback - }; - } + } Currently, a nested commit will do nothing and a nested rollback will -die. The code at each level must be sure to call rollback in the case +die. The code at each level must be sure to call rollback in the case of an error, to ensure that the rollback will propagate to the top -level and be issued. Support for savepoints and for true nested +level and be issued. Support for savepoints and for true nested transactions (for databases that support them) will hopefully be added in the future. =head2 Many-to-many relationships -This is not as easy as it could be, but it's possible. Here's an +This is not as easy as it could be, but it's possible. Here's an example to illustrate: - # Set up inherited connection information - package MyApp::DBIC; - use base qw/DBIx::Class/; - - __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); - __PACKAGE__->connection(...); - - # Set up a class for the 'authors' table - package MyApp::DBIC::Author; - use base qw/MyApp::DBIC/; - - __PACKAGE__->table('authors'); - __PACKAGE__->add_columns(qw/authID first_name last_name/); - __PACKAGE__->set_primary_key(qw/authID/); - - # Define relationship to the link table - __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'authID'); - - # Create the accessor for books from the Author class - sub books { - my ($self) = @_; - return MyApp::DBIC::Book->search( - { 'b2a.authID' => $self->authID }, # WHERE clause - { join => 'b2a' } # join condition (part of search attrs) - # 'b2a' refers to the relationship named earlier in the Author class. - # 'b2a.authID' refers to the authID column of the b2a relationship, - # which becomes accessible in the search by being joined. - ); - } - - # Define the link table class - package MyApp::DBIC::Book2Author; - use base qw/MyApp::DBIC/; - - __PACKAGE__->table('book2author'); - __PACKAGE__->add_columns(qw/bookID authID/); - __PACKAGE__->set_primary_key(qw/bookID authID/); - - __PACKAGE__->belongs_to('authID' => 'MyApp::DBIC::Author'); - __PACKAGE__->belongs_to('bookID' => 'MyApp::DBIC::Book'); - - package MyApp::DBIC::Book; - use base qw/MyApp::DBIC/; - - __PACKAGE__->table('books'); - __PACKAGE__->add_columns(qw/bookID title edition isbn publisher year/); - __PACKAGE__->set_primary_key(qw/bookID/); - - __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'bookID'); - - - # Returns an author record where the bookID field of the - # book2author table equals the bookID of the books (using the - # bookID relationship table) - sub authors { - my ($self) = @_; - return MyApp::DBIC::Author->search( - { 'b2a.bookID' => $self->bookID }, # WHERE clause - { join => 'b2a' } # JOIN condition - ); - } + # Set up inherited connection information + package MyApp::DBIC; + use base qw/DBIx::Class/; + + __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); + __PACKAGE__->connection(...); + + # Set up a class for the 'authors' table + package MyApp::DBIC::Author; + use base qw/MyApp::DBIC/; + + __PACKAGE__->table('authors'); + __PACKAGE__->add_columns(qw/authID first_name last_name/); + __PACKAGE__->set_primary_key(qw/authID/); + + # Define relationship to the link table + __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'authID'); + + # Create the accessor for books from the Author class + sub books { + my ($self) = @_; + return MyApp::DBIC::Book->search( + { 'b2a.authID' => $self->authID }, # WHERE clause + { join => 'b2a' } # join condition (part of search attrs) + # 'b2a' refers to the relationship named earlier in the Author class. + # 'b2a.authID' refers to the authID column of the b2a relationship, + # which becomes accessible in the search by being joined. + ); + } + + # Define the link table class + package MyApp::DBIC::Book2Author; + use base qw/MyApp::DBIC/; + + __PACKAGE__->table('book2author'); + __PACKAGE__->add_columns(qw/bookID authID/); + __PACKAGE__->set_primary_key(qw/bookID authID/); + + __PACKAGE__->belongs_to('authID' => 'MyApp::DBIC::Author'); + __PACKAGE__->belongs_to('bookID' => 'MyApp::DBIC::Book'); + + package MyApp::DBIC::Book; + use base qw/MyApp::DBIC/; + + __PACKAGE__->table('books'); + __PACKAGE__->add_columns(qw/bookID title edition isbn publisher year/); + __PACKAGE__->set_primary_key(qw/bookID/); + + __PACKAGE__->has_many('b2a' => 'MyApp::DBIC::Book2Author', 'bookID'); + + + # Returns an author record where the bookID field of the + # book2author table equals the bookID of the books (using the + # bookID relationship table) + sub authors { + my ($self) = @_; + return MyApp::DBIC::Author->search( + { 'b2a.bookID' => $self->bookID }, # WHERE clause + { join => 'b2a' } # JOIN condition + ); + } =head2 Setting default values -It's as simple as overriding the C method. Note the use of +It's as simple as overriding the C method. Note the use of C. - sub new { - my ( $class, $attrs ) = @_; + sub new { + my ( $class, $attrs ) = @_; - $attrs->{foo} = 'bar' unless defined $attrs->{foo}; + $attrs->{foo} = 'bar' unless defined $attrs->{foo}; - $class->next::method($attrs); - } + $class->next::method($attrs); + } =head2 Stringification Employ the standard stringification technique by using the C -module. Replace C with the column/method of your choice. +module. Replace C with the column/method of your choice. - use overload '""' => 'foo', fallback => 1; + use overload '""' => 'foo', fallback => 1; =cut diff --git a/lib/DBIx/Class/Manual/FAQ.pod b/lib/DBIx/Class/Manual/FAQ.pod index 131f4a0..ff49a3b 100644 --- a/lib/DBIx/Class/Manual/FAQ.pod +++ b/lib/DBIx/Class/Manual/FAQ.pod @@ -4,7 +4,7 @@ DBIx::Class::Manual::FAQ - Frequently asked questions =head1 GENERAL -=head2 What is the point of this module? Is it a fork of Class::DBI? +=head2 What is the point of this module? Is it a fork of Class::DBI? This is an alternative to L, intended to provide greater functionality and simplicity. @@ -29,13 +29,13 @@ release for people to test against. =head2 Where can I go for support? - Mailing list: http://lists.rawmode.org/mailman/listinfo/dbix-class/ + Mailing list: http://lists.rawmode.org/mailman/listinfo/dbix-class/ - SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/ + SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/ - Wiki: http://dbix-class.shadowcatsystems.co.uk/ + Wiki: http://dbix-class.shadowcatsystems.co.uk/ - IRC: irc.perl.org#dbix-class + IRC: irc.perl.org#dbix-class =head1 POSTGRESQL-SPECIFIC PROBLEMS @@ -45,7 +45,7 @@ Older L and L versions do not handle C correctly, causing code that uses auto-incrementing primary key columns to fail with a message such as: - Can't get last insert id at /.../DBIx/Class/Row.pm line 95 + Can't get last insert id at /.../DBIx/Class/Row.pm line 95 In particular the RHEL 4 and FC3 Linux distributions both ship with combinations of L and L modules that do not work diff --git a/lib/DBIx/Class/Manual/Intro.pod b/lib/DBIx/Class/Manual/Intro.pod index 446592f..f288db6 100644 --- a/lib/DBIx/Class/Manual/Intro.pod +++ b/lib/DBIx/Class/Manual/Intro.pod @@ -6,67 +6,72 @@ DBIx::Class::Manual::Intro - Introduction to DBIx::Class So, you are bored with SQL, and want a native Perl interface for your database? Or you've been doing this for a while with L, -and think there's a better way? You've come to the right place. Let's -look at how you can set and use your first native L tree. +and think there's a better way? You've come to the right place. +Let's look at how you can set and use your first native L +tree. -First we'll see how you can set up your classes yourself. If you want +First we'll see how you can set up your classes yourself. If you want them to be auto-discovered, just skip to the next section, which shows you how to use L. =head2 Setting it up manually -First, you'll need a base class. It should inherit from DBIx::Class -like this: +First, you'll need a base class. It should inherit from +L like this: - package MyApp::DB; - use base qw/DBIx::Class/; + package MyApp::DB; + use base qw/DBIx::Class/; -You will also want to load some of L's components. -L provides a good basic set. In addition you'll -have to use either L or L We'll -use C in this introduction, since it involves less magic. -L is mostly useful if you want to use multiple database +You will also want to load some of the L components. +L provides a good starter set. In addition you'll +have to use either L or L. +We'll use C in this introduction, since it involves less magic. +C is mostly useful if you want to use multiple database connections. - __PACKAGE__->load_components(qw/Core DB/); + __PACKAGE__->load_components(qw/Core DB/); -If you want serial/auto-incremental primary keys, you'll need to add -the apropriate component for your db as well, for example. The -L modules help L keep up with -newly generated keys in auto increment database fields. +If you want serial/auto-incrementing primary keys, you should use the +L component for your database. For example, if +you're using SQLite add C to the list: - __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); + __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); -Once you've loaded the components, it's time to set up your connection: +C classes exist for many databases; see +L for more information. - __PACKAGE__->connection('dbi:SQLite:/home/me/myapp/my.db'); +Once you've loaded the components, it's time to set up your +connection: -This method is similar to the normal L, and can take username, -password, and L attribute hash as well as the DSN. + __PACKAGE__->connection('dbi:SQLite:/home/me/myapp/my.db'); + +This method is similar to the normal L C method, and can +take username, password, and L attribute hash as well as the DSN. With that out of the way, we can define our first table class: - package MyApp::DB::Album; - use base qw/MyApp::DB/; + package MyApp::DB::Album; + use base qw/MyApp::DB/; Then we specify which table it uses, - __PACKAGE__->table('album'); + __PACKAGE__->table('album'); and specify which columns it has. - __PACKAGE__->add_columns(qw/albumID artist title label year/); + __PACKAGE__->add_columns(qw/albumid artist title label year/); This will automatically create accessors for each of the columns, so that you can read/update the values in rows you've retrieved. Also, you need to tell it which column is the primary key: - __PACKAGE__->set_primary_key('albumID'); + __PACKAGE__->set_primary_key('albumid'); -If you have multiple primary keys, just pass a list instead. +If you have a primary key composed of multiple columns, just pass a +list instead. -That's pretty much all you need for a basic setup. If you have more +That's pretty much all you need for a basic setup. If you have more advanced needs like using more than one database connection for the same class, see L. @@ -74,52 +79,52 @@ same class, see L. This is an additional class, and not part of the L distribution. Like L, it inspects your database, -and automatically creates classes for all the tables in your -database. Here's a simple setup: +and automatically creates classes for all the tables in your database. +Here's a simple setup: - package MyApp::DB; - use DBIx::Class::Loader; + package MyApp::DB; + use DBIx::Class::Loader; - my $loader = DBIx::Class::Loader->new( - dsn => 'dbi:SQLite:/home/me/myapp/my.db', - namespace => 'MyApp::DB' - ); + my $loader = DBIx::Class::Loader->new( + dsn => 'dbi:SQLite:/home/me/myapp/my.db', + namespace => 'MyApp::DB' + ); - 1; + 1; -This should be equivalent to the manual in the section above. -L takes lots of other options. For more +This should be equivalent to the manual setup in the section above. +L takes lots of other options. For more information, consult its documentation. =head2 Basic usage -Once you've defined the basic classes, you can start interacting with -your database. The simplest way to get a column is by primary key: +Once you've defined the basic classes, either manually or using +L, you can start interacting with your database. +The simplest way to get a record is by primary key: - my $albumID = 14; - my $album = MyApp::DB::Album->find($albumID); + my $album = MyApp::DB::Album->find(14); -This will run a select with C in the C clause, -and return an instance of C that represents this -row. Once you have that row, you can access and update columns: +This will run a C