From 90e5222f20d2e77baa678e540540fcb0077f036e Mon Sep 17 00:00:00 2001 From: mark burdett Date: Mon, 25 Feb 2019 04:09:05 -0800 Subject: [PATCH] Use TempTable methods; add getCreateSql() method for debugging purposes. --- CRM/Contact/Form/Search/Custom/DateAdded.php | 62 ++++---------------- CRM/Contact/Form/Task.php | 10 ++-- CRM/Utils/SQL/TempTable.php | 11 ++++ 3 files changed, 27 insertions(+), 56 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom/DateAdded.php b/CRM/Contact/Form/Search/Custom/DateAdded.php index 55a599b966..fb6f378bf3 100644 --- a/CRM/Contact/Form/Search/Custom/DateAdded.php +++ b/CRM/Contact/Form/Search/Custom/DateAdded.php @@ -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:
";
-      print "$sql;";
-      print "
"; - } - 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:
";
-      print "$dateRange;";
-      print "
"; - } - 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:
";
-        print "$sql;";
-        print "
"; - } - - 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:
";
-        print "$includeGroup;";
-        print "
"; - } - 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:
";
-            print "$smartGroupQuery;";
-            print "
"; - } $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:
";
-            print "$insertGroupNameQuery;";
-            print "
"; - } } } } diff --git a/CRM/Contact/Form/Task.php b/CRM/Contact/Form/Task.php index 74a62c32a0..857d1b22b0 100644 --- a/CRM/Contact/Form/Task.php +++ b/CRM/Contact/Form/Task.php @@ -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 diff --git a/CRM/Utils/SQL/TempTable.php b/CRM/Utils/SQL/TempTable.php index 7f1955003c..444f1ff92b 100644 --- a/CRM/Utils/SQL/TempTable.php +++ b/CRM/Utils/SQL/TempTable.php @@ -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 */ -- 2.25.1