Browse Source

Santize table names to prevent conllisions with MySQL reserved words. Fixes #25.

master
Tom Lingham 9 years ago
parent
commit
7c840af2f6
  1. 2
      src/Matchers/StudlyCaseMatcher.php
  2. 21
      src/SearchDrivers/BaseSearchDriver.php

2
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)";
}
}

21
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
*

Loading…
Cancel
Save