X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FCookbook.pod;h=87ffd968ea3fd96db5e2c30b386cecf76cec7cda;hb=ddabf34f6bef467bc213d2263f5ab2eb4d463b24;hp=e31245fa50e69f026477f3e7b669400fc0e7825b;hpb=336feb8ef85d16d22ae0b131b0c7a85abc9e9435;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index e31245f..87ffd96 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -151,7 +151,7 @@ Note that you cannot have bind parameters unless is_virtual is set to true. =item * NOTE If you're using the old deprecated C<< $rsrc_instance->name(\'( SELECT ...') >> -method for custom SQL execution, you are highly encouraged to update your code +method for custom SQL execution, you are highly encouraged to update your code to use a virtual view as above. If you do not want to change your code, and just want to suppress the deprecation warning when you call L, add this line to your source definition, so that @@ -202,7 +202,7 @@ to access the returned value: # FROM artist Note that the C attribute B with the SQL -syntax C< SELECT foo AS bar > (see the documentation in +syntax C< SELECT foo AS bar > (see the documentation in L). You can control the C part of the generated SQL via the C<-as> field attribute as follows: @@ -218,10 +218,10 @@ generated SQL via the C<-as> field attribute as follows: ); # Equivalent SQL - # SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds - # FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid - # GROUP BY me.artistid, me.name, me.rank, me.charfield - # ORDER BY amount_of_cds DESC + # SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds + # FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid + # GROUP BY me.artistid, me.name, me.rank, me.charfield + # ORDER BY amount_of_cds DESC If your alias exists as a column in your base class (i.e. it was added with @@ -1244,17 +1244,17 @@ example of the recommended way to use it: return $genus->species; }; + use Try::Tiny; my $rs; - eval { + try { $rs = $schema->txn_do($coderef1); - }; - - if ($@) { # Transaction failed + } catch { + # Transaction failed die "the sky is falling!" # - if ($@ =~ /Rollback failed/); # Rollback failed + if ($_ =~ /Rollback failed/); # Rollback failed deal_with_failed_transaction(); - } + }; Note: by default C will re-run the coderef one more time if an error occurs due to client disconnection (e.g. the server is bounced). @@ -1280,9 +1280,11 @@ row. my $schema = MySchema->connect("dbi:Pg:dbname=my_db"); - # Start a transaction. Every database change from here on will only be - # committed into the database if the eval block succeeds. - eval { + # Start a transaction. Every database change from here on will only be + # committed into the database if the try block succeeds. + use Try::Tiny; + my $exception; + try { $schema->txn_do(sub { # SQL: BEGIN WORK; @@ -1292,7 +1294,7 @@ row. for (1..10) { # Start a nested transaction, which in fact sets a savepoint. - eval { + try { $schema->txn_do(sub { # SQL: SAVEPOINT savepoint_0; @@ -1307,8 +1309,7 @@ row. # WHERE ( id = 42 ); } }); - }; - if ($@) { + } catch { # SQL: ROLLBACK TO SAVEPOINT savepoint_0; # There was an error while creating a $thing. Depending on the error @@ -1316,23 +1317,26 @@ row. # changes related to the creation of this $thing # Abort the whole job - if ($@ =~ /horrible_problem/) { + if ($_ =~ /horrible_problem/) { print "something horrible happend, aborting job!"; - die $@; # rethrow error + die $_; # rethrow error } # Ignore this $thing, report the error, and continue with the # next $thing - print "Cannot create thing: $@"; + print "Cannot create thing: $_"; } - # There was no error, so save all changes since the last + # There was no error, so save all changes since the last # savepoint. # SQL: RELEASE SAVEPOINT savepoint_0; } }); - }; - if ($@) { + } catch { + $exception = $_; + } + + if ($caught) { # There was an error while handling the $job. Rollback all changes # since the transaction started, including the already committed # ('released') savepoints. There will be neither a new $job nor any @@ -1340,7 +1344,7 @@ row. # SQL: ROLLBACK; - print "ERROR: $@\n"; + print "ERROR: $exception\n"; } else { # There was no error while handling the $job. Commit all changes. @@ -1354,7 +1358,7 @@ row. In this example it might be hard to see where the rollbacks, releases and commits are happening, but it works just the same as for plain L<>: If -the C-block around C fails, a rollback is issued. If the C +the C-block around C fails, a rollback is issued. If the C succeeds, the transaction is committed (or the savepoint released). While you can get more fine-grained control using C, C @@ -1691,24 +1695,6 @@ this, you can also overload the C method for your schema class: return $rv; } -=head2 Setting limit dialect for SQL::Abstract::Limit - -In some cases, SQL::Abstract::Limit cannot determine the dialect of -the remote SQL server by looking at the database handle. This is a -common problem when using the DBD::JDBC, since the DBD-driver only -know that in has a Java-driver available, not which JDBC driver the -Java component has loaded. This specifically sets the limit_dialect -to Microsoft SQL-server (See more names in SQL::Abstract::Limit --documentation. - - __PACKAGE__->storage->sql_maker->limit_dialect('mssql'); - -The JDBC bridge is one way of getting access to a MSSQL server from a platform -that Microsoft doesn't deliver native client libraries for. (e.g. Linux) - -The limit dialect can also be set at connect time by specifying a -C key in the final hash as shown above. - =head2 Working with PostgreSQL array types You can also assign values to PostgreSQL array columns by passing array @@ -1777,7 +1763,7 @@ database if the C is set in the connect options. my $schema = My::Schema->connection('dbi:mysql:dbname=test', $user, $pass, { mysql_enable_utf8 => 1} ); - + When set, a data retrieved from a textual column type (char, varchar, etc) will have the UTF-8 flag turned on if necessary. This @@ -1821,9 +1807,9 @@ C). =head2 Easy migration from class-based to schema-based setup You want to start using the schema-based approach to L -(see L), 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: +(see L), 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: use MyDB; use SQL::Translator; @@ -1891,7 +1877,7 @@ 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: +You can accomplish this by overriding C in your L: sub store_column { my ( $self, $name, $value ) = @_; @@ -1904,6 +1890,10 @@ You can accomplish this by overriding C: Note that the hard work is done by the call to C, which redispatches your call to store_column in the superclass(es). +Generally, if this is a calculation your database can easily do, try +and avoid storing the calculated value, it is safer to calculate when +needed, than rely on the data being in sync. + =head2 Automatically creating related objects You might have a class C which has many Cs. Further, you @@ -2042,7 +2032,7 @@ mechanism: sub query_start { my $self = shift(); my $sql = shift(); - my $params = @_; + my @params = @_; $self->print("Executing $sql: ".join(', ', @params)."\n"); $start = time(); @@ -2112,7 +2102,7 @@ time, but not so large that the table is locked for an unacceptably long time. If using L instead, use a transaction and commit every C rows; where C gives you the best performance without -locking the table for too long. +locking the table for too long. =item *