From 208fcc31f9d57ef8a034d3ff7e602f75acfa3e60 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 28 Aug 2015 10:46:06 -0500
Subject: [PATCH] orm: Add options() method to influence compiler options

Disable sorting on ThreadEntry::email_info__mid lookups for email processing
which results in an on-disk temporary table.
---
 include/class.orm.php    | 10 ++++++++++
 include/class.thread.php |  4 +++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index cac0eb39e..a92d47668 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -919,6 +919,7 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
     var $distinct = array();
     var $lock = false;
     var $chain = array();
+    var $options = array();
 
     const LOCK_EXCLUSIVE = 1;
     const LOCK_SHARED = 2;
@@ -986,6 +987,9 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
         return $this;
     }
     function order_by($order, $direction=false) {
+        if ($order === false)
+            return $this->options(array('nosort' => true));
+
         $args = func_get_args();
         if (in_array($direction, array(self::ASC, self::DESC))) {
             $args = array($args[0]);
@@ -1184,6 +1188,11 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
         return $this;
     }
 
+    function options($options) {
+        $this->options = array_merge($this->options, $options);
+        return $this;
+    }
+
     function countSelectFields() {
         $count = count($this->values) + count($this->annotations);
         if (isset($this->extra['select']))
@@ -1259,6 +1268,7 @@ class QuerySet implements IteratorAggregate, ArrayAccess, Serializable, Countabl
         // Load defaults from model
         $model = $this->model;
         $query = clone $this;
+        $options += $this->options;
         if ($options['nosort'])
             $query->ordering = array();
         elseif (!$query->ordering && $model::getMeta('ordering'))
diff --git a/include/class.thread.php b/include/class.thread.php
index 8ac7534bf..ffffe918d 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1128,6 +1128,7 @@ implements TemplateVariable {
         // in-reply-to header
         if ($entry = ThreadEntry::objects()
             ->filter(array('email_info__mid' => $mailinfo['mid']))
+            ->order_by(false)
             ->first()
         ) {
             $seen = true;
@@ -1201,7 +1202,8 @@ implements TemplateVariable {
                 $mid = "$left@$right";
             }
             $entries = ThreadEntry::objects()
-                ->filter(array('email_info__mid' => $mid));
+                ->filter(array('email_info__mid' => $mid))
+                ->order_by(false);
             foreach ($entries as $t) {
                 // Capture the first match thread item
                 if (!$thread)
-- 
GitLab