From 90e3b635c3edf0f185c3da87eccfc19382bddca6 Mon Sep 17 00:00:00 2001 From: Raphael Jorel Date: Thu, 24 Oct 2019 17:28:55 +0200 Subject: [PATCH 1/3] Remove unused 'query' parameter in several methods --- src/SearchableTrait.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/SearchableTrait.php b/src/SearchableTrait.php index f770509..f3088ed 100644 --- a/src/SearchableTrait.php +++ b/src/SearchableTrait.php @@ -63,15 +63,15 @@ public function scopeSearchRestricted(Builder $q, $search, $restriction, $thresh $relevance_count += $relevance; if (!$entireTextOnly) { - $queries = $this->getSearchQueriesForColumn($query, $column, $relevance, $words); + $queries = $this->getSearchQueriesForColumn($column, $relevance, $words); } else { $queries = []; } if ( ($entireText === true && count($words) > 1) || $entireTextOnly === true ) { - $queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 50, '', ''); - $queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 30, '%', '%'); + $queries[] = $this->getSearchQuery($column, $relevance, [$search], 50, '', ''); + $queries[] = $this->getSearchQuery($column, $relevance, [$search], 30, '%', '%'); } foreach ($queries as $select) @@ -257,19 +257,18 @@ protected function filterQueryWithRelevance(Builder $query, array $selects, $rel /** * Returns the search queries for the specified column. * - * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param float $relevance * @param array $words * @return array */ - protected function getSearchQueriesForColumn(Builder $query, $column, $relevance, array $words) + protected function getSearchQueriesForColumn($column, $relevance, array $words) { $queries = []; - $queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 15); - $queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 5, '', '%'); - $queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 1, '%', '%'); + $queries[] = $this->getSearchQuery($column, $relevance, $words, 15); + $queries[] = $this->getSearchQuery($column, $relevance, $words, 5, '', '%'); + $queries[] = $this->getSearchQuery($column, $relevance, $words, 1, '%', '%'); return $queries; } @@ -277,7 +276,6 @@ protected function getSearchQueriesForColumn(Builder $query, $column, $relevance /** * Returns the sql string for the given parameters. * - * @param \Illuminate\Database\Eloquent\Builder $query * @param string $column * @param string $relevance * @param array $words @@ -287,7 +285,7 @@ protected function getSearchQueriesForColumn(Builder $query, $column, $relevance * @param string $post_word * @return string */ - protected function getSearchQuery(Builder $query, $column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '') + protected function getSearchQuery($column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '') { $like_comparator = $this->getDatabaseDriver() == 'pgsql' ? 'ILIKE' : 'LIKE'; $cases = []; From f4b5a53af44a48fbee87f940ed9edc3e165c1904 Mon Sep 17 00:00:00 2001 From: Raphael Jorel Date: Thu, 24 Oct 2019 17:31:42 +0200 Subject: [PATCH 2/3] Return direct array for search queries --- src/SearchableTrait.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/SearchableTrait.php b/src/SearchableTrait.php index f3088ed..88911a2 100644 --- a/src/SearchableTrait.php +++ b/src/SearchableTrait.php @@ -264,13 +264,11 @@ protected function filterQueryWithRelevance(Builder $query, array $selects, $rel */ protected function getSearchQueriesForColumn($column, $relevance, array $words) { - $queries = []; - - $queries[] = $this->getSearchQuery($column, $relevance, $words, 15); - $queries[] = $this->getSearchQuery($column, $relevance, $words, 5, '', '%'); - $queries[] = $this->getSearchQuery($column, $relevance, $words, 1, '%', '%'); - - return $queries; + return [ + $this->getSearchQuery($column, $relevance, $words, 15), + $this->getSearchQuery($column, $relevance, $words, 5, '', '%'), + $this->getSearchQuery($column, $relevance, $words, 1, '%', '%') + ]; } /** From 8578348d8be62879a018e1c63e3b37cd0134a5a9 Mon Sep 17 00:00:00 2001 From: Raphael Jorel Date: Thu, 24 Oct 2019 17:36:29 +0200 Subject: [PATCH 3/3] Factorize database checks --- src/SearchableTrait.php | 45 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/SearchableTrait.php b/src/SearchableTrait.php index 88911a2..34eb6cc 100644 --- a/src/SearchableTrait.php +++ b/src/SearchableTrait.php @@ -195,9 +195,7 @@ protected function makeGroupBy(Builder $query) if ($groupBy = $this->getGroupBy()) { $query->groupBy($groupBy); } else { - $driver = $this->getDatabaseDriver(); - - if ($driver == 'sqlsrv') { + if ($this->isSqlsrvDatabase()) { $columns = $this->getTableColumns(); } else { $columns = $this->getTable() . '.' .$this->primaryKey; @@ -217,6 +215,16 @@ protected function makeGroupBy(Builder $query) } } + /** + * Check if used database is SQLSRV. + * + * @return bool + */ + protected function isSqlsrvDatabase() + { + return $this->getDatabaseDriver() == 'sqlsrv'; + } + /** * Puts all the select clauses to the main query. * @@ -239,11 +247,11 @@ protected function addSelectsToQuery(Builder $query, array $selects) */ protected function filterQueryWithRelevance(Builder $query, array $selects, $relevance_count) { - $comparator = $this->getDatabaseDriver() != 'mysql' ? implode(' + ', $selects) : $this->getRelevanceField(); + $comparator = $this->isMysqlDatabase() ? $this->getRelevanceField() : implode(' + ', $selects); $relevance_count=number_format($relevance_count,2,'.',''); - if ($this->getDatabaseDriver() == 'mysql') { + if ($this->isMysqlDatabase()) { $bindings = []; } else { $bindings = $this->search_bindings; @@ -254,6 +262,17 @@ protected function filterQueryWithRelevance(Builder $query, array $selects, $rel // add bindings to postgres } + + /** + * Check if used database is MySQL. + * + * @return bool + */ + private function isMysqlDatabase() + { + return $this->getDatabaseDriver() == 'mysql'; + } + /** * Returns the search queries for the specified column. * @@ -285,7 +304,7 @@ protected function getSearchQueriesForColumn($column, $relevance, array $words) */ protected function getSearchQuery($column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '') { - $like_comparator = $this->getDatabaseDriver() == 'pgsql' ? 'ILIKE' : 'LIKE'; + $like_comparator = $this->isPostgresqlDatabase() ? 'ILIKE' : 'LIKE'; $cases = []; foreach ($words as $word) @@ -297,6 +316,16 @@ protected function getSearchQuery($column, $relevance, array $words, $relevance_ return implode(' + ', $cases); } + /** + * Check if used database is PostgreSQL. + * + * @return bool + */ + private function isPostgresqlDatabase() + { + return $this->getDatabaseDriver() == 'pgsql'; + } + /** * Returns the comparison string. * @@ -306,7 +335,7 @@ protected function getSearchQuery($column, $relevance, array $words, $relevance_ * @return string */ protected function getCaseCompare($column, $compare, $relevance) { - if($this->getDatabaseDriver() == 'pgsql') { + if ($this->isPostgresqlDatabase()) { $field = "LOWER(" . $column . ") " . $compare . " ?"; return '(case when ' . $field . ' then ' . $relevance . ' else 0 end)'; } @@ -324,7 +353,7 @@ protected function getCaseCompare($column, $compare, $relevance) { */ protected function mergeQueries(Builder $clone, Builder $original) { $tableName = DB::connection($this->connection)->getTablePrefix() . $this->getTable(); - if ($this->getDatabaseDriver() == 'pgsql') { + if ($this->isPostgresqlDatabase()) { $original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as {$tableName}")); } else { $original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as `{$tableName}`"));