From: Jess Robinson Date: Fri, 6 Jan 2012 16:24:48 +0000 (+0000) Subject: Updated according to ribasushi's review. X-Git-Tag: v1.0~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=abc3212083b590690a61ea618a7817104119450c;p=dbsrgits%2FDBIx-Class-Manual-SQLHackers.git Updated according to ribasushi's review. Some minor parts still to finish --- diff --git a/lib/DBIx/Class/Manual/SQLHackers/Introduction.pod b/lib/DBIx/Class/Manual/SQLHackers/Introduction.pod index 54e4ded..4edbdf7 100644 --- a/lib/DBIx/Class/Manual/SQLHackers/Introduction.pod +++ b/lib/DBIx/Class/Manual/SQLHackers/Introduction.pod @@ -22,9 +22,7 @@ The part we can't do much about is the SQL in the code. We can move it around, p Why would you not want SQL in your Perl code? For a start, it's just a string to pass to the database interpreter, there is no syntax checking at the Perl compilation level. Thus it fails late, not early. Your editor will also not syntax check what it just sees as strings of text. -Modern Perl should also leverage OO where it can. DBI is a low level library that gets things right, but returns plain hashes and arrays, not objects. - -Perl ORMs still use DBI underneath, extending it to catch coding errors early (names of columns, SQL clauses), and to return the results as objects containing the database values, which work just like any other Perl object. +Modern Perl should also leverage code reuse and OO where it can. DBIx::Class promotes code reuse by allowing you to add methods for common queries, fetch related data in one query and cache data, also without refetching. DBIC uses the DBI library underneath, which gets things right, but returns plain hashes and arrays, not objects. DBIx::Class solves these issues, you write your SQL in perl instead of plain text. The syntax will be checked for you, existance of columns, catching typos and so on. It uses objects so that you can write re-usable queries, and string methods together to create complex queries. You define the database layout once, or you export it from your actual database (with ability to re-export on update). diff --git a/lib/DBIx/Class/Manual/SQLHackers/SELECT.pod b/lib/DBIx/Class/Manual/SQLHackers/SELECT.pod index 58cbd25..0571795 100644 --- a/lib/DBIx/Class/Manual/SQLHackers/SELECT.pod +++ b/lib/DBIx/Class/Manual/SQLHackers/SELECT.pod @@ -37,7 +37,7 @@ You can either fetch all the data at once, or iterate over the results: my $schema = MyDatabase::Schema->connect('dbi:SQLite:my.db'); -=item 2. The B method returns a ResultSet representing a query with no conditions on the given B: +=item 2. The B method returns a ResultSet representing a query with no conditions on the given B: my $user_resultset = $schema->resultset('User'); @@ -58,7 +58,7 @@ The Row object represents the results from a single data source table in the que print $user->username; -See the [DBIx::Class::Row]() documentation for more things you can do +See the L documentation for more things you can do with Row objects. =head2 Simple SELECT, one row via the primary key @@ -91,7 +91,7 @@ B<$fred_user> is a now Row object. FROM users WHERE username = 'fredbloggs'; -B also works well on unique constraints, for example the username of our user. Unique constraints can be defined on Result classes using B (See L. +B also works well on unique constraints, for example the username of our user. Unique constraints can be defined on Result classes using B (See L). =over @@ -132,7 +132,7 @@ To select all users born on the date '1910-02-01', we can use the B meth =back -To run the query, use the B or B methods show at the beginning of this page. +To run the query, use the B or B methods shown at the beginning of this page. =head2 SELECT with different WHERE conditions @@ -206,6 +206,8 @@ To run the query, use the B or B methods show at the beginning of thi There's usually little reason to do this sort of query, as fetching all the data in a row doesn't cost any more time than fetching some of it. Unless of course your source is a View with calculations, or has huge blobs, or.. Okay, you might well want to do this occasionally. +# this is completely false, is there a doc that states this that we need to fix? +# find() takes all search() attributes, including things like prefetch B will always pull all the columns for the found row, so use the *search* method for this. =over @@ -223,7 +225,7 @@ B will always pull all the columns for the found row, so use the *search* =back -Note that accessors for other columns not fetched will return B. To discover whether a columns data has been loaded or not, use the B method. +Note that accessors for other columns not fetched will return B, which is also the perl equivalent of the SQL C value. To discover whether a columns data has been loaded or not, use L. =head2 SELECT with aggregates @@ -279,8 +281,7 @@ The alternate way uses the B method and is easier to build further refin my $sum_prices_rs = $schema->resultset('Price')->search( { }, - { select => [ { SUM => 'amount'} ], - as => [ 'sum_amount' ] } + { columns => { sum_amount => { SUM => 'amount'} } }, ); =back @@ -307,8 +308,7 @@ To select data from other tables, use the B attribute to name the table re my $posts_count_per_user = $schema->resultset('User')->search( { }, - { select => [ qw/id username posts.id posts.title/ ], - as => [ qw/id username posts.id posts.title/ ], + { columns => [ qw/id username posts.id posts.title/ ], join => 'posts', } ); @@ -333,7 +333,7 @@ To retrieve the extra data, call the usual relationship accessor: JOIN posts posts ON posts.used_id = users.id GROUP BY users.id, username; -To group your results, use the B attribute on a B method. We also use the B