[Bast-commits] r3803 - in DBIx-Class: . 0.08/trunk/lib/DBIx 0.08/trunk/lib/DBIx/Class 0.08/trunk/lib/DBIx/Class/Manual 0.08/trunk/t

semifor at dev.catalyst.perl.org semifor at dev.catalyst.perl.org
Thu Oct 4 17:29:15 GMT 2007


Author: semifor
Date: 2007-10-04 17:29:15 +0100 (Thu, 04 Oct 2007)
New Revision: 3803

Added:
   DBIx-Class/0.08/trunk/t/47bind_attribute.t
Modified:
   DBIx-Class/
   DBIx-Class/0.08/trunk/lib/DBIx/Class.pm
   DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/Cookbook.pod
   DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/FAQ.pod
   DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
   DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm
Log:
 r1357 at titanic:  mjm | 2007-10-04 08:48:06 -0700
 - Doc patch for using an SQL function on the left side of a comparison.
 - Which lead to the discovery of a bind args ordering bug.  TODO tests added
   for that.
 - Fixed =for hidden with =begin hidden / =end hidden elsewhere.
 - Added myself to the contributors list.



Property changes on: DBIx-Class
___________________________________________________________________
Name: svk:merge
   - d61e1e54-d1c0-42ab-944f-1b6a4b221ec0:/local/dbix-class:1191
   + d61e1e54-d1c0-42ab-944f-1b6a4b221ec0:/local/dbix-class:1357

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/Cookbook.pod
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/Cookbook.pod	2007-10-04 14:04:27 UTC (rev 3802)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/Cookbook.pod	2007-10-04 16:29:15 UTC (rev 3803)
@@ -263,6 +263,37 @@
 
    my $ordered_cds = $schema->resultset('CD')->search_cds_ordered();
 
+=head3 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,
+it can be accomplished with C<DBIx::Class> when necessary.
+
+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 the C<where>
+attribute:
+
+  $rs->search({}, { where => \'YEAR(date_of_birth) = 1979' });
+
+=begin hidden
+
+(When the bind args ordering bug is fixed, this technique will be better
+and can replace the one above.)
+
+With quoting on, or for a more portable solution, use the C<where> and
+C<bind> attributes:
+
+  $rs->search({}, {
+      where => \'YEAR(date_of_birth) = ?',
+      bind  => [ 1979 ]
+  });
+
+=end hidden
+
 =head2 Using joins and prefetch
 
 You can use the C<join> attribute to allow searching on, or sorting your

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/FAQ.pod
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/FAQ.pod	2007-10-04 14:04:27 UTC (rev 3802)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Manual/FAQ.pod	2007-10-04 16:29:15 UTC (rev 3803)
@@ -232,6 +232,25 @@
  my $interval = "now() - interval '12 hours'";
  ->search({last_attempt => { '<' => \$interval } })
 
+=item .. search with an SQL function on the left hand side?
+
+To use an SQL function on the left hand side of a comparison:
+
+ ->search({}, { where => \'YEAR(date_of_birth)=1979' });
+
+=begin hidden
+
+(When the bind arg ordering bug is fixed, the previous example can be
+replaced with the following.)
+
+ ->search({}, { where => \'YEAR(date_of_birth)=?', bind => [ 1979 ] });
+
+=end hidden
+
+Or, if you have quoting off:
+
+ ->search({ 'YEAR(date_of_birth' => 1979 });
+
 =item .. find more help on constructing searches?
 
 Behind the scenes, DBIx::Class uses L<SQL::Abstract> to help construct

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm	2007-10-04 14:04:27 UTC (rev 3802)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/ResultSet.pm	2007-10-04 16:29:15 UTC (rev 3803)
@@ -264,6 +264,13 @@
 Pass a literal chunk of SQL to be added to the conditional part of the
 resultset query.
 
+CAVEAT: C<search_literal> is provided for Class::DBI compatibility and should
+only be used in that context. There are known problems using C<search_literal>
+in chained queries; it can result in bind values in the wrong order.  See
+L<DBIx::Class::Manual::Cookbook/Searching> and
+L<DBIx::Class::Manual::FAQ/Searching> for seaching techniques that do not
+require C<search_literal>.
+
 =cut
 
 sub search_literal {

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm	2007-10-04 14:04:27 UTC (rev 3802)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/Schema.pm	2007-10-04 16:29:15 UTC (rev 3803)
@@ -484,8 +484,10 @@
 
 Actually, you probably just wanted to call connect.
 
-=for hidden due to deprecation
+=begin hidden
 
+(hidden due to deprecation)
+
 Calls L<DBIx::Class::Schema/"compose_namespace"> to the target namespace,
 calls L<DBIx::Class::Schema/connection> with @db_info on the new schema,
 then injects the L<DBix::Class::ResultSetProxy> component and a
@@ -498,6 +500,8 @@
 on L<DBIx::Class::ResultSet> objects with L<DBIx::Class::Schema/resultset> for
 more information.
 
+=end hidden
+
 =cut
 
 {

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class.pm	2007-10-04 14:04:27 UTC (rev 3802)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class.pm	2007-10-04 16:29:15 UTC (rev 3803)
@@ -261,6 +261,8 @@
 
 scotty: Scotty Allen <scotty at scottyallen.com>
 
+semifor: Marc Mims <marc at questright.com>
+
 sszabo: Stephan Szabo <sszabo at bigpanda.com>
 
 Todd Lipcon

Added: DBIx-Class/0.08/trunk/t/47bind_attribute.t
===================================================================
--- DBIx-Class/0.08/trunk/t/47bind_attribute.t	                        (rev 0)
+++ DBIx-Class/0.08/trunk/t/47bind_attribute.t	2007-10-04 16:29:15 UTC (rev 3803)
@@ -0,0 +1,82 @@
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema;
+
+BEGIN {
+    eval "use DBD::SQLite";
+    plan $@
+        ? ( skip_all => 'needs DBD::SQLite for testing' )
+        : ( tests => 7 );
+}
+
+### $schema->storage->debug(1);
+
+my $where_bind = {
+    where => \'name like ?',
+    bind  => [ 'Cat%' ],
+};
+
+my $rs;
+
+TODO: {
+    local $TODO = 'bind args order needs fixing (semifor)';
+
+    # First, the simple cases...
+    $rs = $schema->resultset('Artist')->search(
+            { artistid => 1 },
+            $where_bind,
+    );
+
+    is ( $rs->count, 1, 'where/bind combined' );
+
+    $rs= $schema->resultset('Artist')->search({}, $where_bind)
+        ->search({ artistid => 1});
+
+    is ( $rs->count, 1, 'where/bind first' );
+            
+    $rs = $schema->resultset('Artist')->search({ artistid => 1})
+        ->search({}, $where_bind);
+
+    is ( $rs->count, 1, 'where/bind last' );
+}
+
+# More complex cases, based primarily on the Cookbook
+# "Arbitrary SQL through a custom ResultSource" technique,
+# which seems to be the only place the bind attribute is
+# documented.  Breaking this technique probably breaks existing
+# application code.
+my $source = DBICTest::Artist->result_source_instance;
+my $new_source = $source->new($source);
+$new_source->source_name('Complex');
+
+$new_source->name(\<<'');
+( select a.*, cd.cdid as cdid, cd.title as title, cd.year as year 
+  from artist a
+  join cd on cd.artist=a.artistid
+  where cd.year=?)
+
+$schema->register_source('Complex' => $new_source);
+
+$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
+is ( $rs->count, 1, 'cookbook arbitrary sql example' );
+
+$rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
+is ( $rs->count, 1, '...coobook + search condition' );
+
+$rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
+    ->search({ 'artistid' => 1 });
+is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
+
+TODO: {
+    local $TODO = 'bind args order needs fixing (semifor)';
+    $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
+        ->search({ 'artistid' => 1 }, {
+            where => \'title like ?',
+            bind => [ 'Spoon%' ] });
+    is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
+}




More information about the Bast-commits mailing list