[Bps-public-commit] dbix-searchbuilder branch, query-hinting, created. 1.66-1-gcb80dfe

Shawn Moore shawn at bestpractical.com
Thu Mar 10 13:09:36 EST 2016


The branch, query-hinting has been created
        at  cb80dfe54fbd95d2f441439443239244d0138cc8 (commit)

- 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;
     }

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list