[Bps-public-commit] dbix-searchbuilder branch bind-values-in-limit created. 1.71-2-gde1d15f
BPS Git Server
git at git.bestpractical.com
Tue Aug 30 16:39:12 UTC 2022
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "dbix-searchbuilder".
The branch, bind-values-in-limit has been created
at de1d15f3f20dd443dd0c12e6e39eebe013efca72 (commit)
- Log -----------------------------------------------------------------
commit de1d15f3f20dd443dd0c12e6e39eebe013efca72
Author: sunnavy <sunnavy at bestpractical.com>
Date: Wed Aug 31 00:38:06 2022 +0800
Test bind values support for LIMIT clauses
diff --git a/t/03searches_bind.t b/t/03searches_bind.t
index f83289b..40d5b98 100644
--- a/t/03searches_bind.t
+++ b/t/03searches_bind.t
@@ -4,7 +4,7 @@ use Test::More;
BEGIN { require "./t/utils.pl" }
our (@AvailableDrivers);
-use constant TESTS_PER_DRIVER => 39;
+use constant TESTS_PER_DRIVER => 45;
my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
plan tests => $total;
@@ -62,6 +62,20 @@ SKIP: {
[ "Baggins' Frodo", "Bilbo\\Baggins" ],
'2 Baggins',
);
+ $users_obj->OrderBy( FIELD => 'id', ORDER => 'ASC' );
+ $users_obj->RowsPerPage(1);
+
+ is( $users_obj->First->Login, "Bilbo\\Baggins", "Bilbo\\Baggins on the first page" );
+ is( $users_obj->Count, 1, "1 value on the first page" );
+ ok( $users_obj->BuildSelectQuery =~ /LIMIT\s*\?|rownum\s*<=\s*\?.*limitrownum\s*>=\s*\?/i,
+ 'found placeholders in limit in select query' );
+
+ $users_obj->GotoPage(1);
+ is( $users_obj->First->Login, "Baggins' Frodo", "Baggins' Frodo on the second page" );
+ is( $users_obj->Count, 1, "1 value on the second page" );
+ ok( $users_obj->BuildSelectQuery =~ /LIMIT\s*\?,\s*\?|rownum\s*<=\s*\?.*limitrownum\s*>=\s*\?/i,
+ 'found placeholders in limit in select query' );
+
$users_obj->CleanSlate;
for my $name ( "Shire's Bag End", 'The Fellowship of the Ring' ) {
commit e211f5a4f3c1752cfb53e128e73bdf58629da06f
Author: sunnavy <sunnavy at bestpractical.com>
Date: Wed Aug 31 00:35:26 2022 +0800
Add bind values support for LIMIT clauses
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 9d72e32..2337edc 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -325,7 +325,7 @@ enforcing an ordering on the rows (with C<OrderByCols>, say).
sub _ApplyLimits {
my $self = shift;
my $statementref = shift;
- $self->_Handle->ApplyLimits($statementref, $self->RowsPerPage, $self->FirstRow);
+ $self->_Handle->ApplyLimits($statementref, $self->RowsPerPage, $self->FirstRow, $self);
$$statementref =~ s/main\.\*/join(', ', @{$self->{columns}})/eg
if $self->{columns} and @{$self->{columns}};
}
@@ -1895,9 +1895,10 @@ sub _OptimizeQuery {
undef $self->{_bind_values};
if ( $args{PreferBind} ) {
( $$query, my @bind_values ) = $self->_Handle->_ExtractBindValues($$query);
- if (@bind_values) {
- $self->{_bind_values} = \@bind_values;
- }
+
+ # Set _bind_values even if no values are extracted, as we use it in
+ # ApplyLimits to determine if bind is enabled.
+ $self->{_bind_values} = \@bind_values;
}
}
diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index d08faa5..c4794e9 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -983,11 +983,18 @@ sub ApplyLimits {
my $statementref = shift;
my $per_page = shift;
my $first = shift;
+ my $sb = shift;
my $limit_clause = '';
if ( $per_page) {
$limit_clause = " LIMIT ";
+ if ( $sb->{_bind_values} ) {
+ push @{$sb->{_bind_values}}, $first || (), $per_page;
+ $first = '?' if $first;
+ $per_page = '?';
+ }
+
if ( $first ) {
$limit_clause .= $first . ", ";
}
diff --git a/lib/DBIx/SearchBuilder/Handle/Oracle.pm b/lib/DBIx/SearchBuilder/Handle/Oracle.pm
index 14bf2f1..8e5b02a 100755
--- a/lib/DBIx/SearchBuilder/Handle/Oracle.pm
+++ b/lib/DBIx/SearchBuilder/Handle/Oracle.pm
@@ -247,6 +247,7 @@ sub ApplyLimits {
my $statementref = shift;
my $per_page = shift;
my $first = shift;
+ my $sb = shift;
# Transform an SQL query from:
#
@@ -275,7 +276,12 @@ sub ApplyLimits {
# Oracle orders from 1 not zero
$first++;
# Make current query a sub select
- $$statementref = "SELECT * FROM ( SELECT limitquery.*,rownum limitrownum FROM ( $$statementref ) limitquery WHERE rownum <= " . ($first + $per_page - 1) . " ) WHERE limitrownum >= " . $first;
+ my $last = $first + $per_page - 1;
+ if ( $sb->{_bind_values} ) {
+ push @{ $sb->{_bind_values} }, $last, $first;
+ $first = $last = '?';
+ }
+ $$statementref = "SELECT * FROM ( SELECT limitquery.*,rownum limitrownum FROM ( $$statementref ) limitquery WHERE rownum <= " . $last . " ) WHERE limitrownum >= " . $first;
}
}
diff --git a/lib/DBIx/SearchBuilder/Handle/Pg.pm b/lib/DBIx/SearchBuilder/Handle/Pg.pm
index d7c48f2..f9adafb 100755
--- a/lib/DBIx/SearchBuilder/Handle/Pg.pm
+++ b/lib/DBIx/SearchBuilder/Handle/Pg.pm
@@ -172,11 +172,19 @@ sub ApplyLimits {
my $statementref = shift;
my $per_page = shift;
my $first = shift;
+ my $sb = shift;
my $limit_clause = '';
if ( $per_page) {
$limit_clause = " LIMIT ";
+
+ if ( $sb->{_bind_values} ) {
+ push @{ $sb->{_bind_values} }, $per_page, $first || ();
+ $first = '?' if $first;
+ $per_page = '?';
+ }
+
$limit_clause .= $per_page;
if ( $first && $first != 0 ) {
$limit_clause .= " OFFSET $first";
-----------------------------------------------------------------------
hooks/post-receive
--
dbix-searchbuilder
More information about the Bps-public-commit
mailing list