From 7c840af2f6a15d97c52a4fcd686abb61d0be0fa6 Mon Sep 17 00:00:00 2001 From: Tom Lingham Date: Sun, 31 Jan 2016 04:12:40 +0000 Subject: [PATCH] Santize table names to prevent conllisions with MySQL reserved words. Fixes #25. --- src/Matchers/StudlyCaseMatcher.php | 2 +- src/SearchDrivers/BaseSearchDriver.php | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Matchers/StudlyCaseMatcher.php b/src/Matchers/StudlyCaseMatcher.php index ac1f585..8656b2b 100755 --- a/src/Matchers/StudlyCaseMatcher.php +++ b/src/Matchers/StudlyCaseMatcher.php @@ -30,6 +30,6 @@ class StudlyCaseMatcher extends BaseMatcher public function buildQueryString($column, $searchString) { - return "IF( CHAR_LENGTH( TRIM($column)) = CHAR_LENGTH( REPLACE( TRIM($column), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)"; + return "IF( CHAR_LENGTH( TRIM( $column )) = CHAR_LENGTH( REPLACE( TRIM( $column ), ' ', '')) AND $column {$this->operator} '{$this->formatSearchString($searchString)}', {$this->multiplier}, 0)"; } } diff --git a/src/SearchDrivers/BaseSearchDriver.php b/src/SearchDrivers/BaseSearchDriver.php index 4f067fd..7e4cbe5 100755 --- a/src/SearchDrivers/BaseSearchDriver.php +++ b/src/SearchDrivers/BaseSearchDriver.php @@ -139,16 +139,31 @@ abstract class BaseSearchDriver implements SearchDriverInterface foreach ($searchFields as $searchField) { if (strpos($searchField, '::')) { - $concatString = str_replace('::', ", ' ', ", $searchField); + + $concatString = implode(', ', array_map( [$this, 'sanitizeColumnName'] , explode('::', $searchField))); + $query[] = $this->buildSelectCriteria("CONCAT({$concatString})"); } else { - $query[] = $this->buildSelectCriteria($searchField); + $query[] = $this->buildSelectCriteria($this->sanitizeColumnName($searchField)); } } - return \DB::raw(implode(' + ', $query).' AS '.$this->relevanceFieldName); + return \DB::raw( implode(' + ', $query) . ' AS ' . $this->relevanceFieldName); + } + + + /** + * Sanitize column names to prevent collisions with MySQL reserved words + * + * @param $name + * @return string + */ + protected function sanitizeColumnName( $name ){ + $name = trim($name, '` '); + return "`${name}`"; } + /** * @param null $searchField *