From: Tim Otten Date: Tue, 5 Jul 2016 21:06:58 +0000 (-0700) Subject: Move AbstractPartialQuery::matchText() to QueryFormatter::formatSql() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2395ef816f5d62aca6731b696bc7fcf99e14d305;p=civicrm-core.git Move AbstractPartialQuery::matchText() to QueryFormatter::formatSql() --- diff --git a/CRM/Contact/Form/Search/Custom/FullText/AbstractPartialQuery.php b/CRM/Contact/Form/Search/Custom/FullText/AbstractPartialQuery.php index 7bbb325cef..6d154437f3 100644 --- a/CRM/Contact/Form/Search/Custom/FullText/AbstractPartialQuery.php +++ b/CRM/Contact/Form/Search/Custom/FullText/AbstractPartialQuery.php @@ -260,51 +260,7 @@ GROUP BY {$tableValues['id']} * SQL, eg "MATCH (col1) AGAINST (queryText)" or "col1 LIKE '%queryText%'" */ public function matchText($table, $fullTextFields, $queryText) { - if ($queryText === '*' || $queryText === '%' || empty($queryText)) { - return '(1)'; - } - - $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; - - if (strpos($table, ' ') === FALSE) { - $tableName = $tableAlias = $table; - } - else { - list ($tableName, $tableAlias) = explode(' ', $table); - } - if (is_scalar($fullTextFields)) { - $fullTextFields = array($fullTextFields); - } - - $clauses = array(); - if (CRM_Core_InnoDBIndexer::singleton()->hasDeclaredIndex($tableName, $fullTextFields)) { - $formattedQuery = CRM_Utils_QueryFormatter::singleton() - ->format($queryText, CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL); - - $prefixedFieldNames = array(); - foreach ($fullTextFields as $fieldName) { - $prefixedFieldNames[] = "$tableAlias.$fieldName"; - } - - $clauses[] = sprintf("MATCH (%s) AGAINST ('%s' IN BOOLEAN MODE)", - implode(',', $prefixedFieldNames), - $strtolower(CRM_Core_DAO::escapeString($formattedQuery)) - ); - } - else { - //CRM_Core_Session::setStatus(ts('Cannot use FTS for %1 (%2)', array( - // 1 => $table, - // 2 => implode(', ', $fullTextFields), - //))); - - $formattedQuery = CRM_Utils_QueryFormatter::singleton() - ->format($queryText, CRM_Utils_QueryFormatter::LANG_SQL_LIKE); - $escapedText = $strtolower(CRM_Core_DAO::escapeString($formattedQuery)); - foreach ($fullTextFields as $fieldName) { - $clauses[] = "$tableAlias.$fieldName LIKE '{$escapedText}'"; - } - } - return implode(' OR ', $clauses); + return CRM_Utils_QueryFormatter::singleton()->formatSql($table, $fullTextFields, $queryText); } /** diff --git a/CRM/Utils/QueryFormatter.php b/CRM/Utils/QueryFormatter.php index 63c48386ba..4f61473dc2 100644 --- a/CRM/Utils/QueryFormatter.php +++ b/CRM/Utils/QueryFormatter.php @@ -165,6 +165,67 @@ class CRM_Utils_QueryFormatter { return $text; } + /** + * Create a SQL WHERE expression for matching against a list of + * text columns. + * + * @param string $table + * Eg "civicrm_note" or "civicrm_note mynote". + * @param array|string $columns + * List of columns to search against. + * Eg "first_name" or "activity_details". + * @param string $queryText + * @return string + * SQL, eg "MATCH (col1) AGAINST (queryText)" or "col1 LIKE '%queryText%'" + */ + public function formatSql($table, $columns, $queryText) { + if ($queryText === '*' || $queryText === '%' || empty($queryText)) { + return '(1)'; + } + + $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; + + if (strpos($table, ' ') === FALSE) { + $tableName = $tableAlias = $table; + } + else { + list ($tableName, $tableAlias) = explode(' ', $table); + } + if (is_scalar($columns)) { + $columns = array($columns); + } + + $clauses = array(); + if (CRM_Core_InnoDBIndexer::singleton() + ->hasDeclaredIndex($tableName, $columns) + ) { + $formattedQuery = $this->format($queryText, CRM_Utils_QueryFormatter::LANG_SQL_FTSBOOL); + + $prefixedFieldNames = array(); + foreach ($columns as $fieldName) { + $prefixedFieldNames[] = "$tableAlias.$fieldName"; + } + + $clauses[] = sprintf("MATCH (%s) AGAINST ('%s' IN BOOLEAN MODE)", + implode(',', $prefixedFieldNames), + $strtolower(CRM_Core_DAO::escapeString($formattedQuery)) + ); + } + else { + //CRM_Core_Session::setStatus(ts('Cannot use FTS for %1 (%2)', array( + // 1 => $table, + // 2 => implode(', ', $fullTextFields), + //))); + + $formattedQuery = $this->format($queryText, CRM_Utils_QueryFormatter::LANG_SQL_LIKE); + $escapedText = $strtolower(CRM_Core_DAO::escapeString($formattedQuery)); + foreach ($columns as $fieldName) { + $clauses[] = "$tableAlias.$fieldName LIKE '{$escapedText}'"; + } + } + return implode(' OR ', $clauses); + } + /** * Format Fts. *