X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FCookbook.pod;h=c91208c0206ad2e882d3f66231606ed9c502769c;hb=9c510ba5d0481e77302d8689b4ae5cb63548b200;hp=83f7cee8907e770940129b4e22e6e19a7cef9a40;hpb=e66002835945589b7174d524ba5add64e714abd2;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index 83f7cee..c91208c 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -326,10 +326,10 @@ You can write subqueries relatively easily in DBIC. }); my $rs = $schema->resultset('CD')->search({ - artist_id => { 'IN' => $inside_rs->get_column('id')->as_query }, + artist_id => { -in => $inside_rs->get_column('id')->as_query }, }); -The usual operators ( =, !=, IN, NOT IN, etc.) are supported. +The usual operators ( '=', '!=', -in, -not_in, etc.) are supported. B: You have to explicitly use '=' when doing an equality comparison. The following will B work: @@ -413,44 +413,62 @@ 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. - -Your approach for doing so will depend on whether you have turned -quoting on via the C and C attributes. If you -explicitly defined C and C in your -C (see L) then -you are using quoting, otherwise not. - -If you do not have quoting on, simply include the function in your search -specification as you would any column: - - $rs->search({ 'YEAR(date_of_birth)' => 1979 }); - -With quoting on, or for a more portable solution, use literal SQL values with -placeholders: +it can be accomplished with C when necessary by resorting to +literal SQL: $rs->search(\[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ]); # Equivalent SQL: # SELECT * FROM employee WHERE YEAR(date_of_birth) = ? - $rs->search({ + $rs->search({ -and => [ name => 'Bob', - -nest => \[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ], - }); + \[ 'YEAR(date_of_birth) = ?', [ plain_value => 1979 ] ], + ]}); # Equivalent SQL: # SELECT * FROM employee WHERE name = ? AND YEAR(date_of_birth) = ? 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. +return value of the function is the same as the type of the column) or in the +case of a function it's currently treated as a dummy string (it is a good idea +to use C or something similar to convey intent). The value is +currently only significant when handling special column types (BLOBs, arrays, +etc.), but this may change in the future. See also L. +=head2 Software Limits + +When your RDBMS does not have a working SQL limit mechanism (e.g. Sybase ASE) +and L is either too slow or does +not work at all, you can try the +L +L attribute, which skips over records to simulate limits +in the Perl layer. + +For example: + + my $paged_rs = $rs->search({}, { + rows => 25, + page => 3, + order_by => [ 'me.last_name' ], + software_limit => 1, + }); + +You can set it as a default for your schema by placing the following in your +C: + + __PACKAGE__->default_resultset_attributes({ software_limit => 1 }); + +B If you are dealing with large resultsets and your L or +ODBC/ADO driver does not have proper cursor support (i.e. it loads the whole +resultset into memory) then this feature will be extremely slow and use huge +amounts of memory at best, and may cause your process to run out of memory and +cause instability on your server at worst, beware! + =head1 JOINS AND PREFETCHING =head2 Using joins and prefetch @@ -1089,7 +1107,7 @@ If you want to get a filtered result set, you can just add add to $attr as follo __PACKAGE__->has_many('pages' => 'Page', 'book', { where => { scrap => 0 } } ); -=head2 Many-to-many relationships +=head2 Many-to-many relationship bridges This is straightforward using L: @@ -1381,14 +1399,21 @@ retry if the server goes away mid-operations, unlike C. L will connect to a database and create a L and associated sources by examining the database. -The recommend way of achieving this is to use the +The recommend way of achieving this is to use the L utility or the +L helper, as described in +L. + +Alternatively, use the L method: perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib \ - -e 'make_schema_at("My::Schema", { debug => 1 }, [ "dbi:Pg:dbname=foo","postgres" ])' + -e 'make_schema_at("My::Schema", \ + { db_schema => 'myschema', components => ["InflateColumn::DateTime"] }, \ + [ "dbi:Pg:dbname=foo", "username", "password" ])' -This will create a tree of files rooted at C<./lib/My/Schema/> containing -source definitions for all the tables found in the C database. +This will create a tree of files rooted at C<./lib/My/Schema/> containing source +definitions for all the tables found in the C schema in the C +database. =head2 Creating DDL SQL @@ -1731,8 +1756,41 @@ See L and L for more explanation. Note that L sets L to C, so you must pass the bind values (the C<[1, 2, 3]> arrayref in the above example) wrapped in -arrayrefs together with the column name, like this: C<< [column_name => value] ->>. +arrayrefs together with the column name, like this: +C<< [column_name => value] >>. + +=head2 Formatting DateTime objects in queries + +To ensure C conditions containing L arguments are properly +formatted to be understood by your RDBMS, you must use the C +formatter returned by L to format +any L objects you pass to L +conditions. Any L object attached to your +L provides a correct C formatter, so +all you have to do is: + + my $dtf = $schema->storage->datetime_parser; + my $rs = $schema->resultset('users')->search( + { + signup_date => { + -between => [ + $dtf->format_datetime($dt_start), + $dtf->format_datetime($dt_end), + ], + } + }, + ); + +Without doing this the query will contain the simple stringification of the +C object, which almost never matches the RDBMS expectations. + +This kludge is necessary only for conditions passed to +L, whereas +L, +L, +L (but not L) are all +L-aware and will do the right thing when supplied +an inflated C object. =head2 Using Unicode @@ -1878,17 +1936,21 @@ just looking for this. For example, say that you have three columns, C, C, and C. You would like to make changes to C and have C be automagically set to the value of C squared. -You can accomplish this by overriding C in your L: +You can accomplish this by wrapping the C accessor with +L: + + around number => sub { + my ($orig, $self) = (shift, shift); - sub store_column { - my ( $self, $name, $value ) = @_; - if ($name eq 'number') { - $self->squared($value * $value); + if (@_) { + my $value = $_[0]; + $self->squared( $value * $value ); } - $self->next::method($name, $value); + + $self->$orig(@_); } -Note that the hard work is done by the call to C, which +Note that the hard work is done by the call to C<< $self->$orig >>, which redispatches your call to store_column in the superclass(es). Generally, if this is a calculation your database can easily do, try