Use TempTable methods; add getCreateSql() method for debugging purposes.
authormark burdett <mfburdett@gmail.com>
Mon, 25 Feb 2019 12:09:05 +0000 (04:09 -0800)
committermark burdett <mfburdett@gmail.com>
Sat, 9 Mar 2019 02:10:46 +0000 (18:10 -0800)
CRM/Contact/Form/Search/Custom/DateAdded.php
CRM/Contact/Form/Task.php
CRM/Utils/SQL/TempTable.php

index 55a599b9665ee0d90fde6e8640e6df3acaa39714..fb6f378bf3082885144402750c99647621ddf9fa 100644 (file)
@@ -32,7 +32,6 @@
  */
 class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_Custom_Base implements CRM_Contact_Form_Search_Interface {
 
-  protected $_debug = 0;
   protected $_aclFrom = NULL;
   protected $_aclWhere = NULL;
 
@@ -179,18 +178,15 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
    */
   public function from() {
     //define table name
-    $this->_datesTable = CRM_Utils_SQL_TempTable::build()->setCategory('dates')->getName();
-    $this->_xgTable = CRM_Utils_SQL_TempTable::build()->setCategory('xg')->getName();
-    $this->_igTable = CRM_Utils_SQL_TempTable::build()->setCategory('ig')->getName();
+    $datesTable = CRM_Utils_SQL_TempTable::build()->setCategory('dates')->setMemory();
+    $this->_datesTable = $datesTable->getName();
+    $xgTable = CRM_Utils_SQL_TempTable::build()->setCategory('xg')->setMemory();
+    $this->_xgTable = $xgTable->getName();
+    $igTable = CRM_Utils_SQL_TempTable::build()->setCategory('ig')->setMemory();
+    $this->_igTable = $igTable->getName();
 
     //grab the contacts added in the date range first
-    $sql = "CREATE TEMPORARY TABLE {$this->_datesTable} ( id int primary key, date_added date ) ENGINE=HEAP";
-    if ($this->_debug > 0) {
-      print "-- Date range query: <pre>";
-      print "$sql;";
-      print "</pre>";
-    }
-    CRM_Core_DAO::executeQuery($sql);
+    $datesTable->createWithColumns('id int primary key, date_added date');
 
     $startDate = !empty($this->_formValues['start_date']) ? $this->_formValues['start_date'] : date('Y-m-d');
     $endDateFix = NULL;
@@ -213,12 +209,6 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
               date_added >= '$startDate 00:00:00'
               $endDateFix";
 
-    if ($this->_debug > 0) {
-      print "-- Date range query: <pre>";
-      print "$dateRange;";
-      print "</pre>";
-    }
-
     CRM_Core_DAO::executeQuery($dateRange, CRM_Core_DAO::$_nullArray);
 
     // Only include groups in the search query of one or more Include OR Exclude groups has been selected.
@@ -251,10 +241,8 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
         $xGroups = 0;
       }
 
-      $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_xgTable}";
-      CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
-      $sql = "CREATE TEMPORARY TABLE {$this->_xgTable} ( contact_id int primary key) ENGINE=HEAP";
-      CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
+      $xgTable->drop();
+      $xgTable->createWithColumns('contact_id int primary key');
 
       //used only when exclude group is selected
       if ($xGroups != 0) {
@@ -286,20 +274,10 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
         }
       }
 
-      $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_igTable}";
-      CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
-      $sql = "CREATE TEMPORARY TABLE {$this->_igTable}
-                ( id int PRIMARY KEY AUTO_INCREMENT,
+      $igTable->drop();
+      $igTable->createWithColumns('id int PRIMARY KEY AUTO_INCREMENT,
                   contact_id int,
-                  group_names varchar(64)) ENGINE=HEAP";
-
-      if ($this->_debug > 0) {
-        print "-- Include groups query: <pre>";
-        print "$sql;";
-        print "</pre>";
-      }
-
-      CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
+                  group_names varchar(64)');
 
       $includeGroup = "INSERT INTO {$this->_igTable} (contact_id, group_names)
                  SELECT      d.id as contact_id, civicrm_group.name as group_name
@@ -323,12 +301,6 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
         $includeGroup .= " AND  {$this->_xgTable}.contact_id IS null";
       }
 
-      if ($this->_debug > 0) {
-        print "-- Include groups query: <pre>";
-        print "$includeGroup;";
-        print "</pre>";
-      }
-
       CRM_Core_DAO::executeQuery($includeGroup, CRM_Core_DAO::$_nullArray);
 
       //search for smart group contacts
@@ -357,22 +329,12 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
                         $smartSql";
 
           CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray);
-          if ($this->_debug > 0) {
-            print "-- Smart group query: <pre>";
-            print "$smartGroupQuery;";
-            print "</pre>";
-          }
           $insertGroupNameQuery = "UPDATE IGNORE {$this->_igTable}
                         SET group_names = (SELECT title FROM civicrm_group
                             WHERE civicrm_group.id = $values)
                         WHERE {$this->_igTable}.contact_id IS NOT NULL
                             AND {$this->_igTable}.group_names IS NULL";
           CRM_Core_DAO::executeQuery($insertGroupNameQuery, CRM_Core_DAO::$_nullArray);
-          if ($this->_debug > 0) {
-            print "-- Smart group query: <pre>";
-            print "$insertGroupNameQuery;";
-            print "</pre>";
-          }
         }
       }
     }
index 74a62c32a00fe059bcdcaee0c57e6216e941a573..857d1b22b00ab97d38476724039ba9f67f34e94e 100644 (file)
@@ -153,12 +153,10 @@ class CRM_Contact_Form_Task extends CRM_Core_Form_Task {
     $form->assign('taskName', CRM_Utils_Array::value($form->_task, $crmContactTaskTasks));
 
     if ($useTable) {
-      $form->_componentTable = CRM_Utils_SQL_TempTable::build()->setCategory('tskact')->setDurable()->setId($qfKey)->getName();
-      $sql = " DROP TABLE IF EXISTS {$form->_componentTable}";
-      CRM_Core_DAO::executeQuery($sql);
-
-      $sql = "CREATE TABLE {$form->_componentTable} ( contact_id int primary key) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
-      CRM_Core_DAO::executeQuery($sql);
+      $tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('tskact')->setDurable()->setId($qfKey)->setUtf8();
+      $form->_componentTable = $tempTable->getName();
+      $tempTable->drop();
+      $tempTable->createWithColumns('contact_id int primary key');
     }
 
     // all contacts or action = save a search
index 7f1955003c7e6734287c7e7cda69e634afa6b008..444f1ff92b5c33c085e196a53ab9b07926fe44be 100644 (file)
@@ -88,6 +88,8 @@ class CRM_Utils_SQL_TempTable {
 
   protected $memory;
 
+  protected $createSql;
+
   /**
    * @return CRM_Utils_SQL_TempTable
    */
@@ -138,6 +140,7 @@ class CRM_Utils_SQL_TempTable {
       ($selectQuery instanceof CRM_Utils_SQL_Select ? $selectQuery->toSQL() : $selectQuery)
     );
     CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, TRUE, FALSE);
+    $this->createSql = $sql;
     return $this;
   }
 
@@ -157,6 +160,7 @@ class CRM_Utils_SQL_TempTable {
       $this->utf8 ? self::UTF8 : ''
     );
     CRM_Core_DAO::executeQuery($sql, array(), TRUE, NULL, TRUE, FALSE);
+    $this->createSql = $sql;
     return $this;
   }
 
@@ -208,6 +212,13 @@ class CRM_Utils_SQL_TempTable {
     return $this->id;
   }
 
+  /**
+   * @return string|NULL
+   */
+  public function getCreateSql() {
+    return $this->createSql;
+  }
+
   /**
    * @return bool
    */