[Bps-public-commit] dbix-searchbuilder branch, master, updated. 1.66-4-g0710df2
Shawn Moore
shawn at bestpractical.com
Mon Jul 18 15:07:10 EDT 2016
The branch, master has been updated
via 0710df230675e60a52f089437540ac9d391e980e (commit)
via 4ad0badd26a544022201365392cd4f51d221f128 (commit)
via f3e6fff5a3cfc4807f3b2216b7298fa9e3a69662 (commit)
via cb80dfe54fbd95d2f441439443239244d0138cc8 (commit)
from 8ecaf64ea6dfef87405df6e9104717dcb6174080 (commit)
Summary of changes:
Changes | 3 ++
lib/DBIx/SearchBuilder.pm | 69 ++++++++++++++++++++++++++-------
lib/DBIx/SearchBuilder/Handle.pm | 11 +++++-
lib/DBIx/SearchBuilder/Handle/Oracle.pm | 6 ++-
lib/DBIx/SearchBuilder/Handle/SQLite.pm | 1 +
5 files changed, 71 insertions(+), 19 deletions(-)
- Log -----------------------------------------------------------------
commit cb80dfe54fbd95d2f441439443239244d0138cc8
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Thu Mar 10 18:07:12 2016 +0000
Add ->QueryHint for Oracle
diff --git a/Changes b/Changes
index 7876ce6..b7d02a8 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
Revision history for Perl extension DBIx::SearchBuilder.
+NEXT
+ - Add ->QueryHint to collection API for Oracle
+
1.66
- No changes since 1.65_02
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 2ee3dff..f755e14 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -140,16 +140,17 @@ sub CleanSlate {
@{ $self->{'aliases'} } = ();
delete $self->{$_} for qw(
- items
- left_joins
- raw_rows
- count_all
- subclauses
- restrictions
- _open_parens
- _close_parens
- group_by
- columns
+ items
+ left_joins
+ raw_rows
+ count_all
+ subclauses
+ restrictions
+ _open_parens
+ _close_parens
+ group_by
+ columns
+ query_hint
);
#we have no limit statements. DoSearch won't work.
@@ -200,6 +201,7 @@ sub _ClonedAttributes
order_by
group_by
columns
+ query_hint
);
}
@@ -432,12 +434,15 @@ sub BuildSelectQuery {
$QueryString .= $self->_WhereClause . " "
if ( $self->_isLimited > 0 );
+ my $QueryHint = $self->QueryHint;
+ $QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
+
# DISTINCT query only required for multi-table selects
# when we have group by clause then the result set is distinct as
# it must contain only columns we group by or results of aggregate
# functions which give one result per group, so we can skip DISTINCTing
if ( my $clause = $self->_GroupClause ) {
- $QueryString = "SELECT main.* FROM $QueryString";
+ $QueryString = "SELECT" . $QueryHint . "main.* FROM $QueryString";
$QueryString .= $clause;
$QueryString .= $self->_OrderClause;
}
@@ -445,7 +450,7 @@ sub BuildSelectQuery {
$self->_DistinctQuery(\$QueryString);
}
else {
- $QueryString = "SELECT main.* FROM $QueryString";
+ $QueryString = "SELECT" . $QueryHint . "main.* FROM $QueryString";
$QueryString .= $self->_OrderClause;
}
@@ -479,7 +484,10 @@ sub BuildSelectCountQuery {
if ($self->_isJoined) {
$QueryString = $self->_Handle->DistinctCount(\$QueryString);
} else {
- $QueryString = "SELECT count(main.id) FROM " . $QueryString;
+ my $QueryHint = $self->QueryHint;
+ $QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
+
+ $QueryString = "SELECT" . $QueryHint . "count(main.id) FROM " . $QueryString;
}
return ($QueryString);
@@ -623,8 +631,11 @@ sub DistinctFieldValues {
$query_string .= ' '. $self->_WhereClause
if $self->_isLimited > 0;
+ my $query_hint = $self->QueryHint;
+ $query_hint = $query_hint ? " /* $query_hint */ " : " ";
+
my $column = 'main.'. $args{'Field'};
- $query_string = 'SELECT DISTINCT '. $column .' FROM '. $query_string;
+ $query_string = "SELECT" . $query_hint . "DISTINCT $column FROM $query_string";
if ( $args{'Order'} ) {
$query_string .= ' ORDER BY '. $column
@@ -1830,6 +1841,20 @@ sub Table {
return $self->{table};
}
+=head2 QueryHint [Hint]
+
+If called with an argument, sets a query hint for this collection.
+
+Always returns the query hint.
+
+=cut
+
+sub QueryHint {
+ my $self = shift;
+ $self->{query_hint} = shift if (@_);
+ return $self->{query_hint};
+}
+
=head1 DEPRECATED METHODS
=head2 GroupBy
diff --git a/lib/DBIx/SearchBuilder/Handle/Oracle.pm b/lib/DBIx/SearchBuilder/Handle/Oracle.pm
index add1ac6..14bf2f1 100755
--- a/lib/DBIx/SearchBuilder/Handle/Oracle.pm
+++ b/lib/DBIx/SearchBuilder/Handle/Oracle.pm
@@ -293,6 +293,8 @@ sub DistinctQuery {
my $sb = shift;
my $table = $sb->Table;
+ my $hint = $sb->QueryHint;
+ $hint = $hint ? " /* $hint */ " : " ";
if ($sb->_OrderClause =~ /(?<!main)\./) {
# If we are ordering by something not in 'main', we need to GROUP
@@ -308,11 +310,11 @@ sub DistinctQuery {
];
my $group = $sb->_GroupClause;
my $order = $sb->_OrderClause;
- $$statementref = "SELECT main.* FROM ( SELECT main.id, row_number() over( $order ) sortorder FROM $$statementref $group ) distinctquery, $table main WHERE (main.id = distinctquery.id) ORDER BY distinctquery.sortorder";
+ $$statementref = "SELECT" . $hint . "main.* FROM ( SELECT main.id, row_number() over( $order ) sortorder FROM $$statementref $group ) distinctquery, $table main WHERE (main.id = distinctquery.id) ORDER BY distinctquery.sortorder";
} else {
# Wrapp select query in a subselect as Oracle doesn't allow
# DISTINCT against CLOB/BLOB column types.
- $$statementref = "SELECT main.* FROM ( SELECT DISTINCT main.id FROM $$statementref ) distinctquery, $table main WHERE (main.id = distinctquery.id) ";
+ $$statementref = "SELECT" . $hint . " main.* FROM ( SELECT DISTINCT main.id FROM $$statementref ) distinctquery, $table main WHERE (main.id = distinctquery.id) ";
$$statementref .= $sb->_GroupClause;
$$statementref .= $sb->_OrderClause;
}
commit f3e6fff5a3cfc4807f3b2216b7298fa9e3a69662
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Thu Mar 10 18:19:49 2016 +0000
Add query hint to DistinctQuery and DistinctCount
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index f755e14..3a9abf2 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -482,7 +482,7 @@ sub BuildSelectCountQuery {
# DISTINCT query only required for multi-table selects
if ($self->_isJoined) {
- $QueryString = $self->_Handle->DistinctCount(\$QueryString);
+ $QueryString = $self->_Handle->DistinctCount(\$QueryString, $self);
} else {
my $QueryHint = $self->QueryHint;
$QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index 6ed2cc8..f4e3d6c 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -1398,8 +1398,11 @@ sub DistinctQuery {
my $statementref = shift;
my $sb = shift;
+ my $QueryHint = $sb->QueryHint;
+ $QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
+
# Prepend select query for DBs which allow DISTINCT on all column types.
- $$statementref = "SELECT DISTINCT main.* FROM $$statementref";
+ $$statementref = "SELECT" . $QueryHint . "DISTINCT main.* FROM $$statementref";
$$statementref .= $sb->_GroupClause;
$$statementref .= $sb->_OrderClause;
}
@@ -1417,9 +1420,13 @@ takes an incomplete SQL SELECT statement and massages it to return a DISTINCT re
sub DistinctCount {
my $self = shift;
my $statementref = shift;
+ my $sb = shift;
+
+ my $QueryHint = $sb->QueryHint;
+ $QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
# Prepend select query for DBs which allow DISTINCT on all column types.
- $$statementref = "SELECT COUNT(DISTINCT main.id) FROM $$statementref";
+ $$statementref = "SELECT" . $QueryHint . "COUNT(DISTINCT main.id) FROM $$statementref";
}
diff --git a/lib/DBIx/SearchBuilder/Handle/SQLite.pm b/lib/DBIx/SearchBuilder/Handle/SQLite.pm
index a9376fd..c172974 100644
--- a/lib/DBIx/SearchBuilder/Handle/SQLite.pm
+++ b/lib/DBIx/SearchBuilder/Handle/SQLite.pm
@@ -128,6 +128,7 @@ takes an incomplete SQL SELECT statement and massages it to return a DISTINCT re
sub DistinctCount {
my $self = shift;
my $statementref = shift;
+ my $sb = shift;
$$statementref = "SELECT count(*) FROM (SELECT DISTINCT main.id FROM $$statementref )";
}
commit 4ad0badd26a544022201365392cd4f51d221f128
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Wed Jul 13 17:17:57 2016 +0000
QueryHintFormatted and usage example
diff --git a/lib/DBIx/SearchBuilder.pm b/lib/DBIx/SearchBuilder.pm
index 3a9abf2..1ba3efd 100755
--- a/lib/DBIx/SearchBuilder.pm
+++ b/lib/DBIx/SearchBuilder.pm
@@ -434,8 +434,7 @@ sub BuildSelectQuery {
$QueryString .= $self->_WhereClause . " "
if ( $self->_isLimited > 0 );
- my $QueryHint = $self->QueryHint;
- $QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
+ my $QueryHint = $self->QueryHintFormatted;
# DISTINCT query only required for multi-table selects
# when we have group by clause then the result set is distinct as
@@ -484,8 +483,7 @@ sub BuildSelectCountQuery {
if ($self->_isJoined) {
$QueryString = $self->_Handle->DistinctCount(\$QueryString, $self);
} else {
- my $QueryHint = $self->QueryHint;
- $QueryHint = $QueryHint ? " /* $QueryHint */ " : " ";
+ my $QueryHint = $self->QueryHintFormatted;
$QueryString = "SELECT" . $QueryHint . "count(main.id) FROM " . $QueryString;
}
@@ -631,8 +629,7 @@ sub DistinctFieldValues {
$query_string .= ' '. $self->_WhereClause
if $self->_isLimited > 0;
- my $query_hint = $self->QueryHint;
- $query_hint = $query_hint ? " /* $query_hint */ " : " ";
+ my $query_hint = $self->QueryHintFormatted;
my $column = 'main.'. $args{'Field'};
$query_string = "SELECT" . $query_hint . "DISTINCT $column FROM $query_string";
@@ -1847,6 +1844,11 @@ If called with an argument, sets a query hint for this collection.
Always returns the query hint.
+When the query hint is included in the SQL query, the C</* ... */> will be
+included for you. Here's an example query hint for Oracle:
+
+ $sb->QueryHint("+CURSOR_SHARING_EXACT");
+
=cut
sub QueryHint {
@@ -1855,6 +1857,18 @@ sub QueryHint {
return $self->{query_hint};
}
+=head2 QueryHintFormatted
+
+Returns the query hint formatted appropriately for inclusion in SQL queries.
+
+=cut
+
+sub QueryHintFormatted {
+ my $self = shift;
+ my $QueryHint = $self->QueryHint;
+ return $QueryHint ? " /* $QueryHint */ " : " ";
+}
+
=head1 DEPRECATED METHODS
=head2 GroupBy
commit 0710df230675e60a52f089437540ac9d391e980e
Merge: 8ecaf64 4ad0bad
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Mon Jul 18 19:06:53 2016 +0000
Merge branch 'query-hinting'
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list