Skip to content

Commit

Permalink
Add text cast for pgsql regex search #1279.
Browse files Browse the repository at this point in the history
  • Loading branch information
yajra committed Jul 21, 2017
1 parent 6dfcf0f commit 9648770
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/QueryDataTable.php
Expand Up @@ -328,12 +328,14 @@ protected function regexColumnSearch($column, $keyword)
{
switch ($this->connection->getDriverName()) {
case 'oracle':
$sql = !$this->config
->isCaseInsensitive() ? 'REGEXP_LIKE( ' . $column . ' , ? )' : 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )';
$sql = !$this->config->isCaseInsensitive()
? 'REGEXP_LIKE( ' . $column . ' , ? )'
: 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )';
break;

case 'pgsql':
$sql = !$this->config->isCaseInsensitive() ? $column . ' ~ ?' : $column . ' ~* ? ';
$column = $this->castColumn($column);
$sql = !$this->config->isCaseInsensitive() ? $column . ' ~ ?' : $column . ' ~* ? ';
break;

default:
Expand All @@ -344,6 +346,24 @@ protected function regexColumnSearch($column, $keyword)
$this->query->whereRaw($sql, [$keyword]);
}

/**
* Wrap a column and cast based on database driver.
*
* @param string $column
* @return string
*/
protected function castColumn($column)
{
switch ($this->connection->getDriverName()) {
case 'pgsql':
return 'CAST(' . $column . ' as TEXT)';
case 'firebird':
return 'CAST(' . $column . ' as VARCHAR(255))';
default:
return $column;
}
}

/**
* Compile query builder where clause depending on configurations.
*
Expand Down Expand Up @@ -385,24 +405,6 @@ protected function addTablePrefix($query, $column)
return $this->wrap($column);
}

/**
* Wrap a column and cast based on database driver.
*
* @param string $column
* @return string
*/
protected function castColumn($column)
{
switch ($this->connection->getDriverName()) {
case 'pgsql':
return 'CAST(' . $column . ' as TEXT)';
case 'firebird':
return 'CAST(' . $column . ' as VARCHAR(255))';
default:
return $column;
}
}

/**
* Prepare search keyword based on configurations.
*
Expand Down

0 comments on commit 9648770

Please sign in to comment.