From: Peter Rabbitson Date: Fri, 14 Nov 2008 09:46:00 +0000 (+0000) Subject: Document the new from usage X-Git-Tag: v0.08240~241 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ce106a7a3c66bf59b48aa393131eca02a70a46dd;p=dbsrgits%2FDBIx-Class.git Document the new from usage --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index ed08ef1..17d05f5 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2798,6 +2798,58 @@ with a father in the person table, we could explicitly use C: # SELECT child.* FROM person child # INNER JOIN person father ON child.father_id = father.id +If you need to express really complex joins or you need a subselect, you +can supply literal SQL to C via a scalar reference. In this case +the contents of the scalar will replace the table name asscoiated with the +resultsource. + +WARNING: This technique might very well not work as expected on chained +searches - you have been warned. + + # Assuming the Event resultsource is defined as: + + MySchema::Event->add_columns ( + sequence => { + data_type => 'INT', + is_auto_increment => 1, + }, + location => { + data_type => 'INT', + }, + type => { + data_type => 'INT', + }, + ); + MySchema::Event->set_primary_key ('sequence'); + + # This will get back the latest event for every location. The column + # selector is still provided by DBIC, all we do is add a JOIN/WHERE + # combo to limit the resultset + + $rs = $schema->resultset('Event'); + $table = $rs->result_source->name; + $latest = $rs->search ( + undef, + { from => \ " + (SELECT e1.* FROM $table e1 + JOIN $table e2 + ON e1.location = e2.location + AND e1.sequence < e2.sequence + WHERE e2.sequence is NULL + ) me", + }, + ); + + # Equivalent SQL (with the DBIC chunks added): + + SELECT me.sequence, me.location, me.type FROM + (SELECT e1.* FROM events e1 + JOIN events e2 + ON e1.location = e2.location + AND e1.sequence < e2.sequence + WHERE e2.sequence is NULL + ) me; + =head2 for =over 4