Updated Limit Patch for C::DBI & C::DBI::MySQL

[prev] [thread] [next] [Date index for 2004/07/28]

From: jason scott gessner
Subject: Updated Limit Patch for C::DBI & C::DBI::MySQL
Date: 16:25 on 28 Jul 2004
--Apple-Mail-3-1055570914
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

Taking a cue from mark addison, i have added a new method to C::DBI and 
greatly simplified the implementation in C::DBI::mysql.

Basically, there is a new method added to C::DBI called 
_make_limit_frag, which returns ''

Then, in C::DBI::mysql, i removed everything except the limit fragment 
logic and renamed it _make_limit_frag.

My new tests pass, as well as the C::DBI tests.  I am going to try this 
out on my mac, as well as my linux box, but feedback is needed from 
everyone else on the list!

Thanks!!!



--Apple-Mail-3-1055570914
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="class_dbi_mysql_limit.patch"
Content-Disposition: attachment;
	filename=class_dbi_mysql_limit.patch

Only in Class-DBI-mysql-0.23_jason/: blib
diff -r -u Class-DBI-mysql-0.23/lib/Class/DBI/mysql.pm Class-DBI-mysql-0.23_jason/lib/Class/DBI/mysql.pm
--- Class-DBI-mysql-0.23/lib/Class/DBI/mysql.pm	2004-02-28 04:19:47.000000000 -0600
+++ Class-DBI-mysql-0.23_jason/lib/Class/DBI/mysql.pm	2004-07-28 09:23:56.000000000 -0500
@@ -35,6 +35,7 @@
 
 =head1 METHODS
 
+        
 =head2 set_up_table
 
 	__PACKAGE__->set_up_table("table_name");
@@ -211,6 +212,50 @@
 	return $class->count_all(@_);
 }
 
+=head2 _make_limit_frag
+
+Provides Class::DBI::_do_search() method with a method to provide for MySQL specific LIMIT clauses.
+
+This function should NOT be called directly.  It is automatically called when
+you do $obj->search() or $obj->search_like().
+
+=cut
+
+sub _make_limit_frag {
+    my ($proto, $limit_args ) = @_;
+    my $class = ref $proto || $proto;
+    
+    my $frag = '';
+
+    if ( defined $limit_args ) {
+        if ( $limit_args =~ m/^(\d+\s?,\s?)?\d+$/ ) { 
+            $frag .= " LIMIT $limit_args" 
+        } else {
+            $class->_croak("LIMIT can only be a number (10), or 2 numbers separated by commas ( 1, 2 ), not '$limit_args'.");
+        }
+    }
+
+    return $frag;
+}
+
+=head2 search and search_like
+
+These functions are the same as they are Class::DBI proper, except they will
+also accept a limit clause in the search params, like so:
+
+        # This will return only the first 5 rows:
+        __PACKAGE__->search( artist => "The Beatles", { limit => 5 } );
+        
+        # This will return only 5 rows, starting from the 3rd row:
+        __PACKAGE__->search( artist => "The Beatles", { limit => "2, 5" } );
+
+        # can also be combined with order_by:
+        __PACKAGE__->search( artist => "The Beatles", 
+            { order_by => "album DESC", limit => "2, 5" } );
+
+=cut
+
+
 =head1 COPYRIGHT
 
 Copyright (C) 2001-2004 Tony Bowden. All rights reserved.
Only in Class-DBI-mysql-0.23_jason/: Makefile
Only in Class-DBI-mysql-0.23_jason/: Makefile.old
Only in Class-DBI-mysql-0.23_jason/: pm_to_blib
diff -r -u Class-DBI-mysql-0.23/t/mysql.t Class-DBI-mysql-0.23_jason/t/mysql.t
--- Class-DBI-mysql-0.23/t/mysql.t	2004-02-28 04:16:09.000000000 -0600
+++ Class-DBI-mysql-0.23_jason/t/mysql.t	2004-07-27 16:43:32.000000000 -0500
@@ -5,7 +5,7 @@
 
 my $HAVE_TP = eval { require Time::Piece::MySQL };
 
-plan tests => 11;
+plan tests => 21;
 
 use_ok "Class::DBI::mysql";
 
@@ -37,7 +37,7 @@
 __PACKAGE__->set_up_table;
 __PACKAGE__->autoinflate(dates => 'Time::Piece') if $HAVE_TP;
 
-END { __PACKAGE__->drop_table }
+# END { __PACKAGE__->drop_table }
 
 #-------------------------------------------------------------------------
 
@@ -80,4 +80,60 @@
 eval { Foo->enum_vals('mydate') };
 ok $@, $@;
 
+# Test overriden search methods
+my @searched_recs1 = Foo->search( name => "Full-text indexes" );
+is @searched_recs1, 1, "Overridden normal search works.";
+
+my @searched_recs2 = Foo->search_like( name => "support collections" );
+is @searched_recs2, 1, "Overridden normal search_like works.";
+
+my @limit_1         = Foo->search_like( name => "%", 
+    { order_by => "name ASC", limit => 1} );
+is @limit_1, 1, "Limit 1 returns proper number of rows";
+# should return 
+# +-------------------------------+
+# | name                          |
+# +-------------------------------+
+# | are called collections        |
+# | for full-text search          |
+# | Full-text indexes             |
+# | Full-text search in MySQL     |
+# | Function MATCH ... AGAINST()  |
+# | implements vector space model |
+# | is used to do a search        |
+# | MySQL has now support         |
+# | Only MyISAM tables            |
+# | support collections           |
+# +-------------------------------+
+
+
+my @limit_2         = Foo->search_like( name => "%", 
+    { order_by => "name ASC", limit => 2} );
+is @limit_2, 2, "Limit 2 returns proper number of rows";
+
+my @limit_3         = Foo->search_like( name => "%", 
+    { order_by => "name ASC", limit => "1, 3"} );
+is @limit_3, 3, "Limit 1, 3 returns proper number of rows";
+
+# should return 
+# +-------------------------------+
+# | name                          |
+# +-------------------------------+
+# | for full-text search          |
+# | Full-text indexes             |
+# | Full-text search in MySQL     |
+# +-------------------------------+
+
+is $limit_3[0]->Name, "for full-text search", "First row is correct";
+is $limit_3[1]->Name, "Full-text indexes", "Second row is correct";
+is $limit_3[2]->Name, "Full-text search in MySQL", "Third row is correct";
+
+my @limit_0         = Foo->search_like( name => "%", 
+    { order_by => "name ASC", limit => "0"} );
+is @limit_0, 0, "Limit 0 returns empty set";
+
+
+eval { Foo->search_like( name => "%", 
+    { order_by => "name ASC", limit => "z"} ); };
+ok $@, "Invalid Limit param croaks: $@";
 

--Apple-Mail-3-1055570914
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="class_dbi_make_limit_frag.patch"
Content-Disposition: attachment;
	filename=class_dbi_make_limit_frag.patch

Only in Class-DBI-0.96_jason/: blib
diff -r -u Class-DBI-0.96/lib/Class/DBI.pm Class-DBI-0.96_jason/lib/Class/DBI.pm
--- Class-DBI-0.96/lib/Class/DBI.pm	2004-04-30 02:22:12.000000000 -0500
+++ Class-DBI-0.96_jason/lib/Class/DBI.pm	2004-07-28 11:19:56.000000000 -0500
@@ -1090,11 +1090,25 @@
 		0 .. $#cols;
 	$frag .= " ORDER BY $search_opts->{order_by}"
 		if $search_opts->{order_by};
-	return $class->sth_to_objects($class->sql_Retrieve($frag),
+	
+        # added _make_limit_frag, which is designed to be subclassed 
+        $frag .= $class->_make_limit_frag( $search_opts->{limit} ) 
+            if defined $search_opts->{limit};
+                
+        return $class->sth_to_objects($class->sql_Retrieve($frag),
 		[ grep defined, @vals ]);
 
 }
 
+# _make_limit_frag
+
+# Simple sub returns empty string for now.  Subclasses of Class::DBI should
+# override this to provide for database specific limit functionality.   For an
+# example implementation, see L<Class::DBI::mysql>
+sub _make_limit_frag {
+    return '';
+}
+
 #----------------------------------------------------------------------
 # CONSTRUCTORS
 #----------------------------------------------------------------------
Only in Class-DBI-0.96_jason/: Makefile
Only in Class-DBI-0.96_jason/: pm_to_blib

--Apple-Mail-3-1055570914
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed


-jason scott gessner
     jason@xxxxxxxx.xxx
--Apple-Mail-3-1055570914--

Updated Limit Patch for C::DBI & C::DBI::MySQL
jason scott gessner 16:25 on 28 Jul 2004

Re: Updated Limit Patch for C::DBI & C::DBI::MySQL
jason scott gessner 16:51 on 28 Jul 2004

Re: Updated Limit Patch for C::DBI & C::DBI::MySQL
Todd Holbrook 16:53 on 28 Jul 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52