Skip to content

Commit

Permalink
Factorize database checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rjorel committed Oct 24, 2019
1 parent f4b5a53 commit 8578348
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)
Expand All @@ -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.
*
Expand All @@ -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)';
}
Expand All @@ -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}`"));
Expand Down

0 comments on commit 8578348

Please sign in to comment.