ktats****@users*****
ktats****@users*****
2011年 4月 29日 (金) 02:14:30 JST
Index: docs/modules/DBIx-Class-0.08127/lib/DBIx/Class/Manual/Cookbook.pod diff -u docs/modules/DBIx-Class-0.08127/lib/DBIx/Class/Manual/Cookbook.pod:1.1 docs/modules/DBIx-Class-0.08127/lib/DBIx/Class/Manual/Cookbook.pod:1.2 --- docs/modules/DBIx-Class-0.08127/lib/DBIx/Class/Manual/Cookbook.pod:1.1 Fri Apr 29 01:44:57 2011 +++ docs/modules/DBIx-Class-0.08127/lib/DBIx/Class/Manual/Cookbook.pod Fri Apr 29 02:14:29 2011 @@ -216,11 +216,15 @@ =head2 ç¹å®ã®ã«ã©ã ã使ã +=begin original + When you only want specific columns from a table, you can use C<columns> to specify which ones you need. This is useful to avoid loading columns with large amounts of data that you aren't about to use anyway: +=end original + ãã¼ãã«ããç¹å®ã®ã«ã©ã ã欲ããã ãã®ã¨ãã«ã¯ãC<columns>ã使ã£ã¦ã å¿ è¦ãªãã®ãæå®ã§ãã¾ããä½ã«ã使ããªã大éã®ãã¼ã¿ãåãè¾¼ãã®ã é¿ãããã¨ãã§ãã¾ãã @@ -235,20 +239,28 @@ # Equivalent SQL: # SELECT artist.name FROM artist +=begin original + This is a shortcut for C<select> and C<as>, see below. C<columns> cannot be used together with C<select> and C<as>. +=end original + å¾ã§ãè¦ã¾ãããããã¯ãC<select>ã¨C<as>ã®ã·ã§ã¼ãã«ããã§ãã C<columns>ã¯C<select>ã¨C<as>ã¨ä¸ç·ã«ã¯ä½¿ãã¾ããã =head2 ãã¼ã¿ãã¼ã¹ã®é¢æ°ãã¹ãã¢ãããã·ã¼ã¸ã£ã使ã +=begin original + The combination of C<select> and C<as> can be used to return the result of a database function or stored procedure as a column value. You use C<select> to specify the source for your column value (e.g. a column name, function, or stored procedure name). You then use C<as> to set the column name you will use to access the returned value: +=end original + C<select>ã¨C<as>ã®çµã¿åããã§ãã«ã©ã ã®å¤ã¨ãã¦ãã¼ã¿ãã¼ã¹é¢æ°ãã¹ãã¢ã ããã·ã¼ã¸ã£ã®çµæãè¿ãã®ã«ä½¿ããã¨ãåºæ¥ã¾ããC<select>ã使ã£ã¦ãã«ã©ã ã® å¤ã®ããã®ã½ã¼ã¹ãæå®ã§ãã¾ã(ä¾ãã°ãã«ã©ã åãé¢æ°ãã¹ãã¢ãããã·ã¼ã¸ã£å)ã @@ -289,11 +301,15 @@ # GROUP BY me.artistid, me.name, me.rank, me.charfield # ORDER BY amount_of_cds DESC +=begin original + If your alias exists as a column in your base class (i.e. it was added with L<add_columns|DBIx::Class::ResultSource/add_columns>), you just access it as normal. Our C<Artist> class has a C<name> column, so we just use the C<name> accessor: +=end original + C< as >å±æ§ã¯ãSQLã®ã·ã³ã¿ãã¯ã¹C< SELECT foo AS bar>ã¨ã¾ã£ããé¢ä¿ãªããã¨ã« æ°ãã¤ãã¦ãã ãã(L<DBIx::Class::ResultSet/ATTRIBUTES>ã®ããã¥ã¡ã³ããè¦ã¦ãã ãã)ã ãã¼ã¹ã¯ã©ã¹ã«ã«ã©ã ã¨ãã¦ã¨ã¤ãªã¢ã¹ããã(ããªãã¡ãC<add_columns>ã§è¿½å ããã¦ãã)ã®ãªãã @@ -303,17 +319,25 @@ my $artist = $rs->first(); my $name = $artist->name(); +=begin original + If on the other hand the alias does not correspond to an existing column, you have to fetch the value using the C<get_column> accessor: +=end original + ä¸æ¹ã§ãã¨ã¤ãªã¢ã¹ãæ¢åã®ã«ã©ã ã«ä¸è´ããªããªããC<get_column>ã¢ã¯ã»ãµã使ã£ã¦ã å¤ãåå¾ããå¿ è¦ãããã¾ã: my $name_length = $artist->get_column('name_length'); +=begin original + If you don't like using C<get_column>, you can always create an accessor for any of your aliases using either of these: +=end original + C<get_column>ãæ°ã«å ¥ããªããã°ããã¤ã§ããä¸è¨ã®ããããã使ã£ã¦ã©ã㪠ã¨ã¤ãªã¢ã¹ã«ãã¢ã¯ã»ãµãä½ãã¾ã: @@ -392,10 +416,14 @@ # LEFT JOIN cd ON artist.id = cd.artist # GROUP BY name +=begin original + Please see L<DBIx::Class::ResultSet/ATTRIBUTES> documentation if you are in any way unsure about the use of the attributes above (C< join >, C< select >, C< as > and C< group_by >). +=end original + ä¸è¨ã®(C< join >ã C< select >ã C< as >ã C< group_by>)å±æ§ã®ä½¿ãæ¹ãããããªããã°ã L<DBIx::Class::ResultSet/ATTRIBUTES>ããã¥ã¡ã³ããã¿ã¦ãã ããã @@ -449,9 +477,13 @@ =head2 å®ç¾©æ¸ã¿æ¤ç´¢ +=begin original + You can define frequently used searches as methods by subclassing L<DBIx::Class::ResultSet>: +=end original + L<DBIx::Class::ResultSet>ã¯ã©ã¹ãç¶æ¿ãã¦ãèªåèªèº«ã®ã¯ã©ã¹ãæ¸ãããã使ã æ¤ç´¢ãã¡ã½ããã¨ãã¦å®ç¾©ã§ãã¾ã: @@ -475,10 +507,14 @@ into the C<ResultSet> directory next to your C<Result> directory, and it will be automatically loaded. +=begin original + If however you are still using L<DBIx::Class::Schema/load_classes>, first tell DBIx::Class to create an instance of the ResultSet class for you, in your My::DBIC::Schema::CD class: +=end original + èªåã®çµæã»ãã使ãã«ã¯ãæåã«ãèªåã®My::DBIC::Schema::CDã¯ã©ã¹ã®ä¸ã§ã DBIx::Classã«ãã®ã¤ã³ã¹ã¿ã³ã¹ãä½ãããã«æãã¾ãã @@ -491,8 +527,12 @@ Note that C<resultset_class> must be called after C<load_components> and C<table>, or you will get errors about missing methods. +=begin original + Then call your new method in your code: +=end original + ãããããã³ã¼ãã®ä¸ã§ãæ°ããã¡ã½ãããå¼ã³ã¾ã: my $ordered_cds = $schema->resultset('CD')->search_cds_ordered(); @@ -535,9 +575,13 @@ =head2 joins 㨠prefetch ã使ã +=begin original + You can use the C<join> attribute to allow searching on, or sorting your results by, one or more columns in a related table. +=end original + C<join>å±æ§ã使ã£ã¦ãé¢é£ãããã¼ãã«ã®1ã¤ä»¥ä¸ã®ã«ã©ã ã使ã£ã¦ã æ¤ç´¢ããçµæã®ã½ã¼ããã§ãã¾ãã @@ -547,8 +591,12 @@ My::Schema::CD->has_many( artists => 'My::Schema::Artist', 'artist_id'); +=begin original + To return all CDs matching a particular artist name, you specify the name of the relationship ('artists'): +=end original + ç¹å®ã®ã¢ã¼ãã£ã¹ãåã®å ¨ã¦ã®CDãè¿ãããã«ã¯: @@ -569,9 +617,13 @@ In that example both the join, and the condition use the relationship name rather than the table name (see L<DBIx::Class::Manual::Joining> for more details on aliasing ). +=begin original + If required, you can now sort on any column in the related tables by including it in your C<order_by> attribute, (again using the aliased relation name rather than table name) : +=end original + å¿ è¦ãªããC<order_by>å±æ§ã«ãããå«ãã¦ãé¢é£ãããã¼ãã«ã®ããããã®ã«ã©ã 㧠ã½ã¼ããããã¨ãåºæ¥ã¾ã(ãã¼ãã«åã§ã¯ãªãã¨ã¤ãªã¢ã¹ã®ãªã¬ã¼ã·ã§ã³åãå度使ã£ã¦): @@ -591,18 +643,26 @@ # WHERE artist.name = 'Bob Marley' # ORDER BY artist.name +=begin original + Note that the C<join> attribute should only be used when you need to search or sort using columns in a related table. Joining related tables when you only need columns from the main table will make performance worse! +=end original + C<join>å±æ§ã¯é¢é£ãããã¼ãã«ã®ã«ã©ã ã使ã£ã¦æ¤ç´¢ãã½ã¼ããããå¿ è¦ãããã¨ãã«ã®ã¿ 使ãããã¹ãã ã¨ãããã¨ã«æ³¨æãã¦ãã ããã ã¡ã¤ã³ã®ãã¼ãã«ããã«ã©ã ãå¿ è¦ãªã¨ãã«ãé¢é£ãããã¼ãã«ãçµåããã®ã¯ã ããã©ã¼ãã³ã¹ãæªãã§ã! +=begin original + Now let's say you want to display a list of CDs, each with the name of the artist. The following will work fine: +=end original + ã§ããCDã®ãªã¹ãããããããã®ã¢ã¼ãã£ã¹ãã®ååã¨ä¸ç·ã«è¡¨ç¤ºãããã¨ãã¾ãããã ä¸è¨ã®ããããã§ãã¾ãããã¾ã: @@ -610,28 +670,40 @@ print "CD: " . $cd->title . ", Artist: " . $cd->artist->name; } +=begin original + There is a problem however. We have searched both the C<cd> and C<artist> tables in our main query, but we have only returned data from the C<cd> table. To get the artist name for any of the CD objects returned, L<DBIx::Class> will go back to the database: +=end original + ã§ãããä¸ã¤åé¡ãããã¾ãããã®ã¡ã¤ã³ã¯ã¨ãªã§ãC<cd>ã¨C<artist>ãã¼ãã«ã®ä¸¡æ¹ã æ¤ç´¢ãã¦ãã¾ãããC<cd>ããã®ã¿ãã¼ã¿ãè¿ããã¦ãã¾ããè¿ãããCDãªãã¸ã§ã¯ãã®ä¸é¨ã§ã ã¢ã¼ãã£ã¹ãåãå¾ãããã«ãL<DBIx::Class>ã¯ããã¼ã¿ãã¼ã¹ã«æ»ãã¾ã: SELECT artist.* FROM artist WHERE artist.id = ? +=begin original + A statement like the one above will run for each and every CD returned by our main query. Five CDs, five extra queries. A hundred CDs, one hundred extra queries! +=end original + ä¸è¨ã®ãããªã¹ãã¼ãã¡ã³ãããã¡ã¤ã³ã¯ã¨ãªã«ãã£ã¦è¿ããããããããã®ãå ¨ã¦ã® CDã§èµ°ãã¾ãã5ã¤ã®CDã§ããã°ã5ã¤ã®å¥ã®ã¯ã¨ãªã§ãã100ã®CDã§ããã°ã100ã®å¥ã® ã¯ã¨ãª! +=begin original + Thankfully, L<DBIx::Class> has a C<prefetch> attribute to solve this problem. This allows you to fetch results from related tables in advance: +=end original + ããããããã¨ã«ãL<DBIx::Class>ã¯ãC<prefetch>å±æ§ãããããã®åé¡ã解決ã§ãã¾ãã ãã®å±æ§ã使ãã¨ãå ã«é¢é£ãããã¼ãã«ããçµæãã¨ã£ã¦ããã¾ã: @@ -653,25 +725,37 @@ # WHERE artist.name = 'Bob Marley' # ORDER BY artist.name +=begin original + The code to print the CD list remains the same: +=end original + CDã®ãªã¹ãã表示ããã³ã¼ãã¯åãç©ã使ãã¾ã: while (my $cd = $rs->next) { print "CD: " . $cd->title . ", Artist: " . $cd->artist->name; } +=begin original + L<DBIx::Class> has now prefetched all matching data from the C<artist> table, so no additional SQL statements are executed. You now have a much more efficient query. +=end original + L<DBIx::Class>ã¯C<artist>ãã¼ãã«ãããã¹ã¦ã®ããããããã¼ã¿ãå ã«ã¨ã£ã¦ãã¦ãã¾ãã ãã®ãããä½åãªSQLã¹ãã¼ãã¡ã³ãã¯å®è¡ããã¾ãããããå¹ççãªã¯ã¨ãªã«ãªãã¾ããã +=begin original + Also note that C<prefetch> should only be used when you know you will definitely use data from a related table. Pre-fetching related tables when you only need columns from the main table will make performance worse! +=end original + ã¾ããC<prefetch>ã¯ãé¢é£ãããã¼ãã«ãããã¼ã¿ãå¿ ã使ãã¨ããã£ã¦ããã¨ãã®ã¿ã«ã 使ãã¹ãã§ããã¡ã¤ã³ãã¼ãã«ããã®ã«ã©ã ããå¿ è¦ã¨ããªããªãã é¢é£ãããã¼ãã«ããå ã«åå¾ããã®ã¯ãããã©ã¼ãã³ã¹ãæªããã¾ã! @@ -706,10 +790,14 @@ =head2 ãã«ãã¹ãããã®çµå(join) +=begin original + Sometimes you want to join more than one relationship deep. In this example, we want to find all C<Artist> objects who have C<CD>s whose C<LinerNotes> contain a specific string: +=end original + 2ã¤ä»¥ä¸ã®æ·±ããªã¬ã¼ã·ã§ã³ã·ããã§joinãããã¨ããããã§ãããã ãã®ä¾ã§ã¯ãC<LinerNotes>ã«ç¹å®ã®æåãå«ã¾ããC<CD>ãæã£ã¦ããã C<Artist>ãªãã¸ã§ã¯ããæ¢ãããã¨ãã¾ã: @@ -735,10 +823,14 @@ # LEFT JOIN liner_notes ON cd.id = liner_notes.cd # WHERE liner_notes.notes LIKE '%some text%' +=begin original + Joins can be nested to an arbitrary level. So if we decide later that we want to reduce the number of Artists returned based on who wrote the liner notes: +=end original + çµåã¯ä»»æã®ã¬ãã«ã§ãã¹ãã§ãã¾ããã§ãã®ã§ãå¾ãããã©ã¤ãã¼ãã¼ãã 誰ãæ¸ããããå ã«ãè¿ãããã¢ã¼ãã£ã¹ãã®æ°ãæ¸ããããã¨æ±ºããã¨ããã: @@ -796,9 +888,13 @@ =head2 Multi-step prefetch +=begin original + C<prefetch> can be nested more than one relationship deep using the same syntax as a multi-step join: +=end original + C<prefetch>ã¯ããã«ãã¹ãããã®çµåã¨åãã·ã³ã¿ãã¯ã¹ã§ã 2ã¤ä»¥ä¸ã®æ·±ããªã¬ã¼ã·ã§ã³ã·ããã§ãã¹ãã§ãã¾ãã: @@ -816,9 +912,13 @@ # JOIN cd ON tag.cd = cd.id # JOIN artist ON cd.artist = artist.id +=begin original + Now accessing our C<cd> and C<artist> relationships does not need additional SQL statements: +=end original + ããã§ãC<cd>ã¨C<artist>ã®ãªã¬ã¼ã·ã§ã³ã·ããã«ã¢ã¯ã»ã¹ããã®ã«ã 追å ã®SQLã¹ãã¼ãã¡ã³ãã¯å¿ è¦ããã¾ãã: @@ -830,56 +930,88 @@ =head2 åãªãã¸ã§ã¯ãã®ã¹ãã¼ããå¾ã +=begin original + It is possible to get a Schema object from a row object like so: +=end original + 次ã®ããã«ãã¦ãåã®ãªãã¸ã§ã¯ãããã¹ãã¼ããå¾ããã¨ãã§ãã¾ã: my $schema = $cd->result_source->schema; # use the schema as normal: my $artist_rs = $schema->resultset('Artist'); +=begin original + This can be useful when you don't want to pass around a Schema object to every method. +=end original + å ¨ã¦ã®ã¡ã½ããã§ãã¹ãã¼ããªãã¸ã§ã¯ããé ã«åããããªããã°ã便å©ã§ãããã =head2 æå¾ã«ãã¼ã¿ãã¼ã¹ã«ã¤ã³ãµã¼ããããã©ã¤ããªãã¼ã®å¤ãåããã +=begin original + AKA getting last_insert_id +=end original + last_insert_id ãåãã¨ãããã¾ãã +=begin original + Thanks to the core component PK::Auto, this is straightforward: +=end original + ã³ã¢ã³ã³ãã¼ãã³ãã®PK::Autoã«æè¬ãã¦ãç´æ¥: my $foo = $rs->create(\%blah); # do more stuff my $id = $foo->id; # foo->my_primary_key_field will also work. +=begin original + If you are not using autoincrementing primary keys, this will probably not work, but then you already know the value of the last primary key anyway. +=end original + ãªã¼ãã¤ã³ã¯ãªã¡ã³ãã®ãã©ã¤ããªãã¼ã使ã£ã¦ããªãã®ãªããããããåãã¾ããã ã§ããããã®å ´åã¯ããã§ã«ããã©ã¤ããªãã¼ã®å¤ãç¥ã£ã¦ãããã¨ã§ãããã =head2 Stringification +=begin original + Employ the standard stringification technique by using the L<overload> module. +=end original + C<overload> ã¢ã¸ã¥ã¼ã«ã§æ¨æºçãªæåååã®ãã¯ããã¯ã使ãã¾ãã +=begin original + To make an object stringify itself as a single column, use something like this (replace C<name> with the column/method of your choice): +=end original + ã²ã¨ã¤ã®ã«ã©ã ã«ã¤ãã¦ããªãã¸ã§ã¯ãèªèº«ãæåååããã«ã¯ã 次ã®ããã«ãã¾ãã(ã«ã©ã /ã¡ã½ããã§C<name>ãç½®ãæãã¦ãã ãã) use overload '""' => sub { shift->name}, fallback => 1; +=begin original + For more complex stringification, you can use an anonymous subroutine: +=end original + ããè¤éãªæåååã§ã¯ãç¡åãµãã«ã¼ãã³ã使ãã¾ã: use overload '""' => sub { $_[0]->name . ", " . @@ -887,32 +1019,52 @@ =head3 æåååã®ä¾ +=begin original + Suppose we have two tables: C<Product> and C<Category>. The table specifications are: +=end original + äºã¤ã®ãã¼ãã«ãããã¨ãã¾ã:C<Product>ã¨C<Cateogry>ã ãã¼ãã«ã®å®ç¾©ã¯æ¬¡ã®éã: Product(id, Description, category) Category(id, Description) +=begin original + C<category> is a foreign key into the Category table. +=end original + C<category>ã¯Categoryãã¼ãã«ã®å¤é¨ãã¼ã§ãã +=begin original + If you have a Product object C<$obj> and write something like +=end original + Productãªãã¸ã§ã¯ãC<$obj>ãããã次ã®ããã«æ¸ããã¨ããã¨ã print $obj->category +=begin original + things will not work as expected. +=end original + æå¾ ã©ããã«ã¯åãã¾ããã +=begin original + To obtain, for example, the category description, you should add this method to the class defining the Category table: +=end original + ã«ãã´ãªã®å 容ãå¾ãããªããä¾ãã°ãCategoryãã¼ãã«ã®ã¯ã©ã¹å®ç¾©ã«æ¬¡ã® ã¡ã½ããã追å ãã¹ãã§ã: @@ -924,8 +1076,12 @@ =head2 find_or_create ãè¦ä»ããã®ããåãä½ã£ãã®ããç¥ãããï¼ +=begin original + Just use C<find_or_new> instead, then check C<in_storage>: +=end original + C<find_or_new>ã代ããã«ä½¿ã£ã¦ãã ããããããããC<in_storage>ããã§ãã¯ãã¾ã: my $obj = $rs->find_or_new({ blah => 'blarg' }); @@ -978,10 +1134,16 @@ =head2 DBIx::Classã®ãããã·ã¯ã©ã¹ãåçã«ãµãã¯ã©ã¹åãã +=begin original + AKA multi-class object inflation from one table +=end original + AKA 1ã¤ã®ãã¼ãã«ãããã«ãã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã«å±éãã +=begin original + L<DBIx::Class> classes are proxy classes, therefore some different techniques need to be employed for more than basic subclassing. In this example we have a single user table that carries a boolean bit @@ -992,6 +1154,8 @@ methods into the Admin class. There is a cleaner way to accomplish this. +=end original + L<DBIx::Class>ã¯ã©ã¹ã¯ãããã·ã¯ã©ã¹ã§ãããã®ãããåºæ¬çãªãµãã¯ã©ã¹å以ä¸ã«ã ããã¤ãã®éã£ããã¯ããã¯ãå¿ è¦ã¨ããã¾ãã ãã®ä¾ã§ã¯ã管çè ç¨ã«çå½å¤ãæã£ã¦ããã¦ã¼ã¶ã¼ãã¼ãã«ãããã¾ãã @@ -1001,6 +1165,8 @@ çã«ããªãã¾ãããAdminã¯ã©ã¹ã«å ¨ã¦ã®ã¦ã¼ã¶ã¼ã¯ã©ã¹ã®ã¡ã½ãããã³ãã¼ ãããã¨ã«ãªãã¾ãããããããããã«ããããã£ããããæ¹æ³ãããã¾ãã +=begin original + Overriding the C<inflate_result> method within the User proxy-class gives us the effect we want. This method is called by L<DBIx::Class::ResultSet> when inflating a result from storage. So we @@ -1008,6 +1174,8 @@ bless it if it's an admin object, and then return it. See the example below: +=end original + ã¦ã¼ã¶ã¼ã®ãããã·ã¯ã©ã¹å ã§C<inflate_result>ã¡ã½ããããªã¼ãã¼ã©ã¤ããããã¨ã§ã æãã§ããå¹æãå¾ããã¾ãããã®ã¡ã½ããã¯ãã¹ãã¬ã¼ã¸ããçµæãå±éãããã¨ãã«ã L<DBIx::Class::ResultSet>ã«ãã£ã¦å¼ã°ãã¾ãã @@ -1126,10 +1294,14 @@ =head2 é«éã«çµæãå¾ãããã«ããªãã¸ã§ã¯ãã®ä½æãã¹ãããããã +=begin original + DBIx::Class is not built for speed, it's built for convenience and ease of use, but sometimes you just need to get the data, and skip the fancy objects. +=end original + DBIx::Class ã¯ã¹ãã¼ãã®ããã«ã¯ä½ããã¦ããããDBIx::Classã¯ã å©ä¾¿æ§ã¨ä½¿ãæãã®ããã«ä½ããã¾ãããã§ãããæã«ã¯ããã¼ã¿ããã åå¾ããªããã°ãããªãã ãã®æããããç´ æµãªãªãã¸ã§ã¯ãã¯ã¹ããã @@ -1181,9 +1353,13 @@ AKA Aggregating Data +=begin original + If you want to find the sum of a particular column there are several ways, the obvious one is to use search: +=end original + ç¹å®ã®ã«ã©ã ã®åè¨ãæ¢ããããã°ãããã¤ãã®æ¹æ³ãããã¾ããèªæã®ãã®ã¨ãã¦ã¯ã searchã使ããã®ã§ã: @@ -1196,10 +1372,14 @@ ); my $tc = $rs->first->get_column('total_cost'); +=begin original + Or, you can use the L<DBIx::Class::ResultSetColumn>, which gets returned when you ask the C<ResultSet> for a column using C<get_column>: +=end original + ãããã¯ãL<DBIx::Class::ResultSetColumn>ã使ããã¨ãåºæ¥ã¾ãã ããã¯ãC<ResultSet>ã§C<get_column>ã使ã£ã¦ã«ã©ã ãåãã¨ãã« è¿ããããã®ãåãã¾ãã @@ -1207,15 +1387,23 @@ my $cost = $schema->resultset('Items')->get_column('Cost'); my $tc = $cost->sum; +=begin original + With this you can also do: +=end original + ãããã次ã®ããã«ã§ãã¾ã: my $minvalue = $cost->min; my $maxvalue = $cost->max; +=begin original + Or just iterate through the values of this column only: +=end original + ã¾ãã¯ããã®ã«ã©ã ã®å¤ã®ã¿ãéãã¦ã¤ãã¬ã¼ããã¾ã: while ( my $c = $cost->next ) { @@ -1226,24 +1414,36 @@ print $c; } +=begin original + C<ResultSetColumn> only has a limited number of built-in functions. If you need one that it doesn't have, then you can use the C<func> method instead: +=end original + C<ResultSetColumn>ã¯å°ãã ããã«ãã¤ã³ã®é¢æ°ãããã¾ãã ããã«ãªããã®ãå¿ è¦ãªããC<func>ã¡ã½ããã代ããã«ä½¿ããã¨ãã§ãã¾ã: my $avg = $cost->func('AVERAGE'); +=begin original + This will cause the following SQL statement to be run: +=end original + ããããã¨ãä¸è¨ã®SQLã¹ãã¼ãã¡ã³ããèµ°ãã¾ã: SELECT AVERAGE(Cost) FROM Items me +=begin original + Which will of course only work if your database supports this function. See L<DBIx::Class::ResultSetColumn> for more documentation. +=end original + ãã¡ããã使ã£ã¦ãããã¼ã¿ãã¼ã¹ããã®é¢æ°ããµãã¼ããã¦ããªããã°ããã¾ããã ãã詳ããã¯ãL<DBIx::Class::ResultSetColumn>ãã¿ã¦ãã ããã @@ -1271,30 +1471,46 @@ =head2 é¢é£ãããã¼ãã«ãæ¤ç´¢ãã +=begin original + Only searches for books named 'Titanic' by the author in $author. +=end original + $autorã®èè ã§ã'Titanic'ã¨ããååã®æ¬ã ããæ¤ç´¢ãããã my $books_rs = $author->search_related('books', { name => 'Titanic' }); =head2 é¢é£ãããã¼ãã«ã®ãã¼ã¿ãåé¤ãã +=begin original + Deletes only the book named Titanic by the author in $author. +=end original + $autorã®èè ã§ãTitanicã¨ããååã®æ¬ã ããåé¤ãããã $author->delete_related('books', { name => 'Titanic' }); =head2 é¢ä¿ããçµæã»ããã®é åºä»ã +=begin original + If you always want a relation to be ordered, you can specify this when you create the relationship. +=end original + é åºä»ããããé¢ä¿ã常ã«ã»ãããªãããªã¬ã¼ã·ã§ã³ã·ãããä½ãã¨ãã«ã次ã®æå®ãã§ãã¾ãã +=begin original + To order C<< $book->pages >> by descending page_number, create the relation as follows: +=end original + page_numberãéé ã§ãC<< $book->pages >>ã並ã³å¤ããããªãã次ã®ããã« ãªã¬ã¼ã·ã§ã³ãä½ãã¾ã: @@ -1309,8 +1525,12 @@ =head2 Many-to-many ã®ãªã¬ã¼ã·ã§ã³ã·ãã +=begin original + This is straightforward using L<ManyToMany|DBIx::Class::Relationship/many_to_many>: +=end original + ããã¯ãåç´ã«L<ManyToMany|DBIx::Class::Relationship/many_to_many>ã使ãã¾ã: package My::User; @@ -1482,10 +1702,14 @@ You need to make sure that your coderef can be invoked multiple times without terrible side effects. +=begin original + Nested transactions will work as expected. That is, only the outermost transaction will actually issue a commit to the $dbh, and a rollback at any level of any transaction will cause the entire nested transaction to fail. + +=end original ãã¹ãããããã©ã³ã¶ã¯ã·ã§ã³ã¯æå¾ ã©ããã«åãã¾ãã ä¸çªå¤å´ã®ãã©ã³ã¶ã¯ã·ã§ã³ã ããå®éã«$dbhã«ã³ããããçºè¡ãã¾ãã @@ -1808,9 +2032,13 @@ =head2 ã¹ãã¼ãã®ãã¼ã¸ã§ãã³ã° +=begin original + The following example shows simplistically how you might use DBIx::Class to deploy versioned schemas to your customers. The basic process is as follows: +=end original + ä¸è¨ã®ä¾ã§ã¯ãDBIx::Classã使ã£ã¦ã顧客åãã«ãã¼ã¸ã§ã³ä»ãã®ã¹ãã¼ããã©ããã£ã¦ ãããã¤ãããããè¦ããã¾ããåºæ¬çãªããã»ã¹ã¯ä¸è¨ã®ããã«ãªãã¾ã: @@ -1818,59 +2046,99 @@ =item 1. +=begin original + Create a DBIx::Class schema +=end original + DBIx::Classã¹ãã¼ããä½ãã¾ã =item 2. +=begin original + Save the schema +=end original + ã¹ãã¼ããä¿åãã¾ã =item 3. +=begin original + Deploy to customers +=end original + 顧客ã«ãããã¤ãã¾ã =item 4. +=begin original + Modify schema to change functionality +=end original + ã¹ãã¼ããå¤æ´ãã¦ã functionality ãå¤æ´ãã¾ã =item 5. +=begin original + Deploy update to customers +=end original + 顧客ã«æ´æ°ããããã¤ãã¾ã =back +=begin original + B<Create a DBIx::Class schema> +=end original + B<DBIx::Calssã¹ãã¼ããä½ã> +=begin original + This can either be done manually, or generated from an existing database as described under L</Creating Schemas From An Existing Database> +=end original + ããã¯ãæã§è¡ããã¨ããC<æ¢åã®ãã¼ã¿ãã¼ã¹ããã¹ãã¼ããä½ã>ã§èª¬æãã¾ããã æ¢åã®ãã¼ã¿ãã¼ã¹ããçæãããã¨ãã§ãã¾ãã +=begin original + B<Save the schema> +=end original + B<ã¹ãã¼ããä¿åãã> Call L<DBIx::Class::Schema/create_ddl_dir> as above under L</Creating DDL SQL>. +=begin original + B<Deploy to customers> +=end original + B<顧客ã«ãããã¤ãã> +=begin original + There are several ways you could deploy your schema. These are probably beyond the scope of this recipe, but might include: +=end original + ã¹ãã¼ãããããã¤ããã®ã«ã¯è¤æ°ã®æ¹æ³ãããã¾ãã ãã®ã¬ã·ãã®ç¯çãè¶ãããã§ãããå«ãã¦ããã¾ã: @@ -1878,29 +2146,45 @@ =item 1. +=begin original + Require customer to apply manually using their RDBMS. +=end original + 顧客ã«RDBMSã使ã£ã¦ãæã§é©ç¨ããããã«æ±ãã =item 2. +=begin original + Package along with your app, making database dump/schema update/tests all part of your install. +=end original + ã¢ããªã±ã¼ã·ã§ã³ã¨ä¸ç·ã«ããã¼ã¿ãã¼ã¹ã®ãã³ããã¹ãã¼ãã®ã¢ãããã¼ãã ã¤ã³ã¹ãã¼ã«ã®å ¨ãã¼ãã®ãã¹ããä½ãããã«ãããã±ã¼ã¸ãã =back +=begin original + B<Modify the schema to change functionality> +=end original + B<æ©è½æ§ãå¤æ´ããããã«ãã¹ãã¼ããå¤æ´ãã> +=begin original + As your application evolves, it may be necessary to modify your schema to change functionality. Once the changes are made to your schema in DBIx::Class, export the modified schema and the conversion scripts as in L</Creating DDL SQL>. +=end original + ã¢ããªã±ã¼ã·ã§ã³ãé²åããã«ã¤ããæ©è½æ§ãå¤æ´ããããã«ãã¹ãã¼ããä¿®æ£ããå¿ è¦ãããã§ãããã DBIx::Classã§ã¹ãã¼ããå¤æ´ãããã以åã®ããã«ä¿®æ£ãããã¹ãã¼ããã¨ã¯ã¹ãã¼ããã L</Creating DDL SQL>ã«ãããããªå¤æã¹ã¯ãªããã使ãã¾ã @@ -1920,27 +2204,39 @@ =head2 çæãããSQLãã¯ã©ã¼ããã +=begin original + If the database contains column names with spaces and/or reserved words, they need to be quoted in the SQL queries. This is done using: +=end original + ãã¼ã¿ãã¼ã¹ã«ã¹ãã¼ã¹ããã³/ã¾ãã¯äºç´èªã®ã¤ããã«ã©ã åãããå ´åã SQLã¯ã¨ãªå ã§ãã¯ã©ã¼ããããå¿ è¦ãããã¾ãã次ã®ããã«ãã¾ã: $schema->storage->sql_maker->quote_char([ qw/[ ]/] ); $schema->storage->sql_maker->name_sep('.'); +=begin original + The first sets the quote characters. Either a pair of matching brackets, or a C<"> or C<'>: +=end original + 1è¡ç®ã¯ãã¯ã©ã¼ãæåãã»ãããã¦ãã¾ãããã©ã±ããã®ãã¢ããC<">, C<'>ã§ãã $schema->storage->sql_maker->quote_char('"'); +=begin original + Check the documentation of your database for the correct quote characters to use. C<name_sep> needs to be set to allow the SQL generator to put the quotes the correct place, and defaults to C<.> if not supplied. +=end original + æ£ããã¯ã©ã¼ãæåã使ãããã«ããã¼ã¿ãã¼ã¹ã®ããã¥ã¡ã³ãããã§ãã¯ãã¦ãã ããã C<name_sep>ã¯ãSQLã¸ã§ãã¬ã¼ã¿ã¼ãæ£ããå ´æã«ã¯ã©ã¼ããç½®ãããã«ã ã»ããããªããã°ããã¾ãããããã©ã«ãã§ã¯C<.>ã«ãªãã¾ãã @@ -2113,11 +2409,15 @@ =head2 ã¯ã©ã¹ãã¼ã¹ããã¹ãã¼ããã¼ã¹ã»ããã¢ããã¸ã®ç°¡åãªç§»è¡ +=begin original + You want to start using the schema-based approach to L<DBIx::Class> (see L<DBIx::Class::Manual::Intro/Setting it up manually>), but have an established class-based setup with lots of existing classes that you don't want to move by hand. Try this nifty script instead: +=end original + L<DBIx::Class>ã¸ã®ã¹ãã¼ããã¼ã¹ã®ã¢ããã¼ãã使ããã(L<SchemaIntro.pod>ãã¿ã¦ãã ãã)ã ã§ããæ¢åã®å¤§éã®ã¯ã©ã¹ã§ãå¾æ¥ã®ã¯ã©ã¹ãã¼ã¹ã®ã»ããã¢ãããããã æã§ããããåããããã¯ãªãã¨ãã¾ããæã§åããå¤ããã«ãä¸è¨ã®æ°ã®å©ããã¹ã¯ãªããã @@ -2151,20 +2451,28 @@ print $output; +=begin original + You could use L<Module::Find> to search for all subclasses in the MyDB::* namespace, which is currently left as an exercise for the reader. +=end original + L<Module::Find>ã使ã£ã¦ãMyDB::*åå空éã«ããå ¨ã¦ã®ãµãã¯ã©ã¹ãæ¢ããã¨ãåºæ¥ã¾ããã ããã¯ãä»ã®ã¨ãããèªè ã¸ã®èª²é¡ã¨ãã¦ããã¾ãã =head1 ã¡ã½ããã®ãªã¼ãã¼ãã¼ã +=begin original + L<DBIx::Class> uses the L<Class::C3> package, which provides for redispatch of method calls, useful for things like default values and triggers. You have to use calls to C<next::method> to overload methods. More information on using L<Class::C3> with L<DBIx::Class> can be found in L<DBIx::Class::Manual::Component>. +=end original + L<DBIx::Class>ã¯L<Class::C3>ããã±ã¼ã¸ã使ã£ã¦ãã¾ããL<Class::C3>ã¯ã¡ã½ããã³ã¼ã«ã ååå²ãããããã«ä½¿ããã¦ãã¾ããã¡ã½ããããªã¼ãã¼ãã¼ãããããã«ã C<next::method>ã®å¼ã³åºãã使ããªããã°ããã¾ããã @@ -2173,9 +2481,13 @@ =head2 åã®ããã©ã«ãã®å¤ãç¨æãã +=begin original + It's as simple as overriding the C<new> method. Note the use of C<next::method>. +=end original + åç´ã«ãC<new>ã¡ã½ããããªã¼ãã¼ã©ã¤ããã¾ãã C<next::method>ã®ä½¿ãããã«æ³¨æãã¦ãã ããã @@ -2189,27 +2501,39 @@ return $new; } +=begin original + For more information about C<next::method>, look in the L<Class::C3> documentation. See also L<DBIx::Class::Manual::Component> for more ways to write your own base classes to do this. +=end original + C<next::method>ã«ã¤ãã¦ãã詳ããã¯ãL<Class::C3>ã®ããã¥ã¡ã³ããåç §ãã¦ãã ããã ãããããããã®ãèªåèªèº«ã®ãã¼ã¹ã¯ã©ã¹ãæ¸ãããã®ãããå¤ãã®æ¹æ³ã«ã¤ãã¦ã¯ã L<DBIx::CLass::Manual::Component>ãè¦ã¦ãã ããã +=begin original + People looking for ways to do "triggers" with DBIx::Class are probably just looking for this. +=end original + DBIx::Classã§"triggers"ãããæ¹æ³ãæ¢ãã¦ãã人ãããããæ¢ãã¦ããã§ãããã =head3 ä»ãå¤æ´ãããããã¤ã§ããããã£ã¼ã«ããå¤æ´ãã +=begin original + For example, say that you have three columns, C<id>, C<number>, and C<squared>. You would like to make changes to C<number> and have C<squared> be automagically set to the value of C<number> squared. You can accomplish this by wrapping the C<number> accessor with L<Class::Method::Modifiers>: +=end original + ä¾ãã°ã3ã¤ã®ã«ã©ã ããã£ãã¨ãã¾ããC<id>ãC<number>ãC<squared>ã C<number>ã«å¤æ´ãå ããC<squared>ã¯èªåçã«ãC<number>ã®äºä¹ã®å¤ã ã»ãããããã¨ãã¾ããC<number>ã¢ã¯ã»ãµãL<Class::Method::Modifiers>㧠@@ -2226,9 +2550,13 @@ $self->next::method(@_); } +=begin original + Note that the hard work is done by the call to C<next::method>, which redispatches your call to store_column in the superclass(es). +=end original + C<next::method>ãå¼ã³åºããã¨ã§ã大å¤ãªä»äºãããã¦ãããã¨ã«æ³¨æãã¾ãããã å¼ã³åºããã(è¤æ°ã®)ã¹ã¼ãã¼ã¯ã©ã¹ã®store_columnã«ååå²ããã¦ã¾ã: @@ -2238,10 +2566,14 @@ =head2 é¢é£ãããªãã¸ã§ã¯ããèªåçã«ä½ã +=begin original + You might have a class C<Artist> which has many C<CD>s. Further, you want to create a C<CD> object every time you insert an C<Artist> object. You can accomplish this by overriding C<insert> on your objects: +=end original + å¤ãã®C<CD>ãæã£ãC<Artist>ã¯ã©ã¹ãããã¨ãã¾ãã ããã«ãC<Artist>ãªãã¸ã§ã¯ããã¤ã³ãµã¼ããã度æ¯ã«C<CD>ãªãã¸ã§ã¯ãã ä½ãããã¨ãã¾ããããã¯ããªãã¸ã§ã¯ãã®C<insert>ããªã¼ããã¼ãããã°ã§ãã¾ã: @@ -2276,11 +2608,15 @@ B<åé¡:> +=begin original + Say you have a table "Camera" and want to associate a description with each camera. For most cameras, you'll be able to generate the description from the other columns. However, in a few special cases you may want to associate a custom description with a camera. +=end original + "Camera"ãã¼ãã«ããã£ãã¨ãã¦ãããããã®ã«ã¡ã©ã«ã¤ãã¦ã 説æãé¢é£ä»ãããã¨ãã¾ããã»ã¨ãã©ã®ã«ã¡ã©ã§ã¯ãä»ã®ã«ã©ã ãã説æãçæã§ããã§ãããã ããããç¹å¥ãªæ°ã±ã¼ã¹ã§ã¯ãã«ã¡ã©ã®ã«ã¹ã¿ã ã®èª¬æãé¢é£ä»ãããã¨ãã¾ãã @@ -2289,28 +2625,44 @@ B<解:> +=begin original + In your database schema, define a description field in the "Camera" table that can contain text and null values. +=end original + ãã¼ã¿ãã¼ã¹ã¹ãã¼ãã§ã"Camera"ã«descriptionãã£ã¼ã«ããå®ç¾©ãã textã¨nullã®å¤ãå«ããã¨ãã§ããããã«ãã¾ãã +=begin original + In DBIC, we'll overload the column accessor to provide a sane default if no custom description is defined. The accessor will either return or generate the description, depending on whether the field is null or not. +=end original + DBICã¯ãã«ã¹ã¿ã ã® description ãå®ç¾©ããã¦ããªããã°ã æä¾ããã¦ããã¾ã¨ããªããã©ã«ãã®ã«ã©ã ã¢ã¯ã»ãµããªã¼ãã¼ãã¼ãã§ãã¾ãã ãã£ã¼ã«ããnullãnullã§ãªããã«ä¾åãã¦ãã¢ã¯ã»ãµã¯descriptionãè¿ããçæãã¾ãã +=begin original + First, in your "Camera" schema class, define the description field as follows: +=end original + ã¾ãã"Camera"ã¹ãã¼ãã¯ã©ã¹ã§ãä¸è¨ã®ããã«descriptionãã£ã¼ã«ããå®ç¾©ãã¾ã: __PACKAGE__->add_columns(description => { accessor => '_description' }); +=begin original + Next, we'll define the accessor-wrapper subroutine: +=end original + 次ã«ãã¢ã¯ã»ãµã©ããã¼ãµãã«ã¼ãã³ãå®ç¾©ãã¾ã: sub description { @@ -2334,10 +2686,14 @@ =head2 Data::Dumperã¨ãDBIx::Classãªãã¸ã§ã¯ã +=begin original + L<Data::Dumper> can be a very useful tool for debugging, but sometimes it can be hard to find the pertinent data in all the data it can generate. Specifically, if one naively tries to use it like so, +=end original + L<Data::Dumper> ã¯ãããã°ã«ã¨ã¦ã便å©ãªãã¼ã«ã§ããã§ããã çæãããå ¨ã¦ã®ãã¼ã¿ã®ä¸ã®ã該å½ã®ãã¼ã¿ãè¦ä»ããã®ãé£ããæãããã¾ãã ç¹ã«ã次ã®ããã«åç´ã«ä½¿ããã¨ãããã @@ -2347,19 +2703,27 @@ my $cd = $schema->resultset('CD')->find(1); print Dumper($cd); +=begin original + several pages worth of data from the CD object's schema and result source will be dumped to the screen. Since usually one is only interested in a few column values of the object, this is not very helpful. +=end original + è¤æ°ãã¼ã¸ã«ããããCDãªãã¸ã§ã¯ãã®ã¹ãã¼ãã¨çµæã®ã½ã¼ã¹ããè¤æ° ãã¼ã¸ã«ããããã¼ã¿ã¨ãªã£ã¦ã¹ã¯ãªã¼ã³ã«ãã³ãããã¾ããã§ããã æ®éã¯ãªãã¸ã§ã¯ãã®æ°ã«ã©ã ã®å¤ã®1ã¤ã®ã¿ã«èå³ãããã®ã§ãããã§ã¯ã ãã¾ã便å©ã§ã¯ããã¾ããã +=begin original + Luckily, it is possible to modify the data before L<Data::Dumper> outputs it. Simply define a hook that L<Data::Dumper> will call on the object before dumping it. For example, +=end original + 幸éã«ããL<Data::Dumper>ãåºåããåã«ãã¼ã¿ãå å·¥ãããã¨ãåºæ¥ã¾ãã ç°¡åã«ããã¯ãå®ç¾©ããã¨ãL<Data::Dumper>ããã³ãããåã«ããªãã¸ã§ã¯ã㧠ãããå¼ã³åºãã¾ãã @@ -2383,12 +2747,16 @@ print Dumper($cd); # dumps $cd without its ResultSource +=begin original + If the structure of your schema is such that there is a common base class for all your table classes, simply put a method similar to C<_dumper_hook> in the base class and set C<$Data::Dumper::Freezer> to its name and L<Data::Dumper> will automagically clean up your data before printing it. See L<Data::Dumper/EXAMPLES> for more information. +=end original + ã¹ãã¼ãã®æ§é ããå ¨ã¦ã®ãã¼ãã«ã¯ã©ã¹ã®ããã®å ±éã®ãã¼ã¹ã¯ã©ã¹ããããã㪠ãã®ã§ããã°ãåç´ã«ããã¼ã¹ã¯ã©ã¹ã«ãC<_dumper_hook>ã¨åããããªã¡ã½ãããä½ãã C<$Data::Dumper::Freezer>ã«ãã®ååãã»ãããã¾ãã @@ -2397,12 +2765,16 @@ =head2 ãããã¡ã¤ãªã³ã° +=begin original + When you enable L<DBIx::Class::Storage>'s debugging it prints the SQL executed as well as notifications of query completion and transaction begin/commit. If you'd like to profile the SQL you can subclass the L<DBIx::Class::Storage::Statistics> class and write your own profiling mechanism: +=end original + L<DBIx::Class::Storage>ã®ãããã®ã³ã°ãæå¹ã«ããã°ã å®è¡ãããSQLã ãã§ãªããã¯ã¨ãªã®å®äºãããã©ã³ã¶ã¯ã·ã§ã³ã®éå§/ã³ããã ããåºåãã¾ããSQLãåæããããã°ã L<DBIx::Class::Storage::Statistics> @@ -2462,10 +2834,14 @@ }); } +=begin original + You could then create average, high and low execution times for an SQL statement and dig down to see if certain parameters cause aberrant behavior. You might want to check out L<DBIx::Class::QueryLog> as well. +=end original + ãããããSQLã¹ãã¼ãã¡ã³ãã®å¹³åãæé·ãæçå®è¡æéãåãã¾ããããã ãã©ã¡ã¼ã¿ãç°å¸¸ãªæ¯ãèããå¼ãèµ·ããã¦ããã°ãæãä¸ãããã¨ãåºæ¥ãã§ãããã L<DBIx::Class::QueryLog>ããã§ãã¯ããããããããã¾ããã