From: Andy Grundman Date: Fri, 12 Aug 2005 16:40:02 +0000 (+0000) Subject: Added initial SQL::Abstract::Limit support and tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aeaf3ce2ab5d86cbdaca6857f5ee3c9cea3f1fd6;p=dbsrgits%2FDBIx-Class-Historic.git Added initial SQL::Abstract::Limit support and tests --- diff --git a/Build.PL b/Build.PL index a665523..b54876b 100644 --- a/Build.PL +++ b/Build.PL @@ -9,7 +9,7 @@ my %arguments = ( 'DBI' => 0, 'UNIVERSAL::require' => 0, 'NEXT' => 0, - 'SQL::Abstract' => 1.19, + 'SQL::Abstract::Limit' => 0.033, 'DBD::SQLite' => 1.08, 'Tie::IxHash' => 0, }, diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 453d51c..a30de81 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -3,7 +3,7 @@ package DBIx::Class::Storage::DBI; use strict; use warnings; use DBI; -use SQL::Abstract; +use SQL::Abstract::Limit; use DBIx::Class::Storage::DBI::Cursor; use base qw/DBIx::Class/; @@ -15,7 +15,6 @@ __PACKAGE__->mk_group_accessors('simple' => sub new { my $new = bless({}, ref $_[0] || $_[0]); - $new->sql_maker(new SQL::Abstract); $new->cursor("DBIx::Class::Storage::DBI::Cursor"); $new->debug(1) if $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}; return $new; @@ -60,6 +59,7 @@ sub _populate_dbh { my ($self) = @_; my @info = @{$self->connect_info || []}; $self->_dbh($self->_connect(@info)); + $self->sql_maker(new SQL::Abstract::Limit( limit_dialect => $self->_dbh )); } sub _connect { @@ -119,7 +119,7 @@ sub select { if (ref $condition eq 'SCALAR') { $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; } - my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order); + my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order, $attrs->{rows}, $attrs->{offset}); return $self->cursor->new($sth, \@bind, $attrs); } @@ -129,7 +129,7 @@ sub select_single { if (ref $condition eq 'SCALAR') { $order = $1 if $$condition =~ s/ORDER BY (.*)$//i; } - my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order); + my ($rv, $sth, @bind) = $self->_execute('select', $attrs->{bind}, $ident, $select, $condition, $order, 1, $attrs->{offset}); return $sth->fetchrow_array; } diff --git a/lib/DBIx/Class/Storage/DBI/Cursor.pm b/lib/DBIx/Class/Storage/DBI/Cursor.pm index 3ccaef9..143425b 100644 --- a/lib/DBIx/Class/Storage/DBI/Cursor.pm +++ b/lib/DBIx/Class/Storage/DBI/Cursor.pm @@ -24,9 +24,6 @@ sub next { my $sth = $self->{sth}; unless ($self->{live_sth}) { $sth->execute(@{$self->{args} || []}); - if (my $offset = $self->{attrs}{offset}) { - $sth->fetch for 1 .. $offset; - } $self->{live_sth} = 1; } my @row = $sth->fetchrow_array; diff --git a/t/15limit.t b/t/15limit.t new file mode 100644 index 0000000..7eb548b --- /dev/null +++ b/t/15limit.t @@ -0,0 +1,30 @@ +use strict; +use Test::More; + +BEGIN { + eval "use DBD::SQLite"; + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 5); +} + +use lib qw(t/lib); + +use_ok('DBICTest'); + +# test LIMIT +my $it = DBICTest::CD->search( {}, + { rows => 3, + order_by => 'title' } +); +is( $it->count, 3, "count ok" ); +is( $it->next->title, "Caterwaulin' Blues", "iterator->next ok" ); +$it->next; +$it->next; +is( $it->next, undef, "next past end of resultset ok" ); + +# test OFFSET +my @cds = DBICTest::CD->search( {}, + { rows => 2, + offset => 2, + order_by => 'year' } +); +is( $cds[0]->title, "Spoonful of bees", "offset ok" );