X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FCookbook.pod;h=2769ded281bc401b43597ad08356694636bbc699;hb=26283ee38f220f6c6bae720ea5a189c9c0f47f6f;hp=51a8a3334956fa357b9d43c10140aa35b33c0dfe;hpb=496846a46dcfae058a2fa48224316978171434ae;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 51a8a33..2769ded 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -37,7 +37,10 @@ Sometimes you need to formulate a query using specific operators: This results in something like the following C clause: - WHERE artist LIKE '%Lamb%' AND title LIKE '%Fear of Fours%' + WHERE artist LIKE ? AND title LIKE ? + +And the following bind values for the placeholders: C<'%Lamb%'>, C<'%Fear of +Fours%'>. Other queries might require slightly more complex logic: @@ -244,6 +247,8 @@ any of your aliases using either of these: # Or use DBIx::Class::AccessorGroup: __PACKAGE__->mk_group_accessors('column' => 'name_length'); +See also L. + =head2 SELECT DISTINCT with multiple columns my $rs = $schema->resultset('Artist')->search( @@ -331,7 +336,7 @@ B: You have to explicitly use '=' when doing an equality comparison. The following will B work: my $rs = $schema->resultset('CD')->search({ - artist_id => $inside_rs->get_column('id')->as_query, + artist_id => $inside_rs->get_column('id')->as_query, # does NOT work }); =head3 Support @@ -404,8 +409,10 @@ Then call your new method in your code: =head2 Using SQL functions on the left hand side of a comparison -Using SQL functions on the left hand side of a comparison is generally -not a good idea since it requires a scan of the entire table. However, +Using SQL functions on the left hand side of a comparison is generally not a +good idea since it requires a scan of the entire table. (Unless your RDBMS +supports indexes on expressions - including return values of functions -, and +you create an index on the return value of the function in question.) However, it can be accomplished with C when necessary. If you do not have quoting on, simply include the function in your search @@ -413,25 +420,30 @@ specification as you would any column: $rs->search({ 'YEAR(date_of_birth)' => 1979 }); -With quoting on, or for a more portable solution, use the C -attribute: +With quoting on, or for a more portable solution, use literal SQL values with +placeholders: - $rs->search({}, { where => \'YEAR(date_of_birth) = 1979' }); + $rs->search(\[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ]); -=begin hidden + # Equivalent SQL: + # SELECT * FROM employee WHERE YEAR(date_of_birth) = ? -(When the bind args ordering bug is fixed, this technique will be better -and can replace the one above.) + $rs->search({ + name => 'Bob', + -nest => \[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ], + }); -With quoting on, or for a more portable solution, use the C and -C attributes: + # Equivalent SQL: + # SELECT * FROM employee WHERE name = ? AND YEAR(date_of_birth) = ? - $rs->search({}, { - where => \'YEAR(date_of_birth) = ?', - bind => [ 1979 ] - }); +Note: the C string in the C<< [ plain_value => 1979 ] >> part +should be either the same as the name of the column (do this if the type of the +return value of the function is the same as the type of the column) or +otherwise it's essentially a dummy string currently (use C as a +habit). It is used by L to handle special column types. -=end hidden +See also L. =head1 JOINS AND PREFETCHING @@ -922,6 +934,9 @@ B test.pl ### The statement below will print print "I can do admin stuff\n" if $admin->can('do_admin_stuff'); +Alternatively you can use L that implements +exactly the above functionality. + =head2 Skip row object creation for faster results DBIx::Class is not built for speed, it's built for convenience and @@ -1062,7 +1077,7 @@ create the relationship. To order C<< $book->pages >> by descending page_number, create the relation as follows: - __PACKAGE__->has_many('pages' => 'Page', 'book', { order_by => \'page_number DESC'} ); + __PACKAGE__->has_many('pages' => 'Page', 'book', { order_by => { -desc => 'page_number'} } ); =head2 Filtering a relationship result set @@ -1104,6 +1119,16 @@ This is straightforward using Laddresses(); # get all addresses for a user $rs = $address->users(); # get all users for an address + my $address = $user->add_to_addresses( # returns a My::Address instance, + # NOT a My::UserAddress instance! + { + country => 'United Kingdom', + area_code => 'XYZ', + town => 'London', + street => 'Sesame', + } + ); + =head2 Relationships across DB schemas Mapping relationships across L @@ -1517,18 +1542,18 @@ database thinks it has. Alternatively, you can send the conversion sql scripts to your customers as above. -=head2 Setting quoting for the generated SQL. +=head2 Setting quoting for the generated SQL 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: - __PACKAGE__->storage->sql_maker->quote_char([ qw/[ ]/] ); - __PACKAGE__->storage->sql_maker->name_sep('.'); + $schema->storage->sql_maker->quote_char([ qw/[ ]/] ); + $schema->storage->sql_maker->name_sep('.'); The first sets the quote characters. Either a pair of matching brackets, or a C<"> or C<'>: - __PACKAGE__->storage->sql_maker->quote_char('"'); + $schema->storage->sql_maker->quote_char('"'); Check the documentation of your database for the correct quote characters to use. C needs to be set to allow the SQL @@ -1547,6 +1572,17 @@ L: } ) +In some cases, quoting will be required for all users of a schema. To enforce +this, you can also overload the C method for your schema class: + + sub connection { + my $self = shift; + my $rv = $self->next::method( @_ ); + $rv->storage->sql_maker->quote_char([ qw/[ ]/ ]); + $rv->storage->sql_maker->name_sep('.'); + return $rv; + } + =head2 Setting limit dialect for SQL::Abstract::Limit In some cases, SQL::Abstract::Limit cannot determine the dialect of