Skip to content

Commit

Permalink
- DataScout bugfix + code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
timopruesse committed Jun 19, 2017
1 parent 1052e8b commit 9dcb8fe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
26 changes: 13 additions & 13 deletions src/Models/DataComponents/DataScout.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ public function __construct(DataSearch $dataSearch, $remember = false)
$this->_rememberState = $remember;
}

/**
* @return Builder
*/
public function _shapeData() : Builder
{
if ($this->_dataSearch->queryCount() === 0)
{
return $this->_dataTable->query();
}

return $this->_dataSearch->searchData($this->_dataTable->query());
}

/**
* How should the dataset be searched?
* Define the search algorithm, e.g. SimpleSearch or FulltextSearch
Expand Down Expand Up @@ -112,6 +99,19 @@ public function getSearchUrl() : string
return $this->_dataSearch->getSearchUrl();
}

/**
* @return Builder
*/
protected function _shapeData() : Builder
{
if ($this->_dataSearch->queryCount() === 0)
{
return $this->_dataTable->query();
}

return $this->_dataSearch->searchData($this->_dataTable->query());
}

protected function _afterInit() : void
{
$search = \request()->get('search');
Expand Down
21 changes: 20 additions & 1 deletion src/Models/DataComponents/Search/FulltextSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
class FulltextSearch extends DataSearch {

const SUPPORTED_DRIVERS = ['mysql', 'sqlite'];

/** @var string */
private $_mode;

Expand All @@ -28,6 +30,23 @@ public function __construct(array $searchableFields = [], string $mode = null)
$this->_databaseDriver = \config('database.connections.' . \config('database.default') . '.driver');
}

/**
* @param string $driver
* @return FulltextSearch
* @throws \RuntimeException
*/
public function forceDatabaseDriver(string $driver) : FulltextSearch
{
if (!\in_array($driver, self::SUPPORTED_DRIVERS, true))
{
throw new \RuntimeException($driver . ' is not supported at the moment');
}

$this->_databaseDriver = $driver;

return $this;
}

/**
* Set the mode for the full text search.
* Available modes:
Expand Down Expand Up @@ -73,6 +92,6 @@ private function _getMatchQuery(string $value) : string
return $matchQuery;
}

return 'MATCH(' . \implode(',', $this->_searchableFields) . ') AGAINST (\'' . $value . '\'' . (!empty($this->_mode) ? $this->_mode : '') . ')';
return 'MATCH(' . \implode(',', $this->_searchableFields) . ') AGAINST (\'' . $value . '\'' . (!empty($this->_mode) ? (' ' . $this->_mode) : '') . ')';
}
}
35 changes: 21 additions & 14 deletions tests/FulltextSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,36 @@ public function searching_reduces_the_dataset()
/**
* @test
*/
public function search_in_other_mode()
public function can_force_database_driver()
{
/** @var \hamburgscleanest\DataTables\Models\DataTable $dataTable */
$dataTable = DataTable::model(TestModel::class, ['id', 'created_at', 'name']);

/** @var FulltextSearch $searcher */
$searcher = new FulltextSearch(['name']);
$searcher->setMode('WITH QUERY EXPANSION');

/** @var DataScout $dataScout */
$dataScout = new DataScout(new FulltextSearch(['name']));
$searcher->forceDatabaseDriver('mysql');
$searcher->addQuery('test');

/** @var \hamburgscleanest\DataTables\Models\DataTable $dataTable */
$dataTable = DataTable::model(TestModel::class, ['id', 'created_at', 'name'])
->addComponent($dataScout);

$queryString = 'test-123';
self::assertEquals('SELECT * FROM "TESTMODELS" WHERE (MATCH(NAME) AGAINST (\'TEST\'))', \mb_strtoupper($searcher->searchData($dataTable->query())->toSql()));
}

TestModel::create(['id' => 1337, 'name' => $queryString, 'created_at' => Carbon::now()]);
/**
* @test
*/
public function search_in_other_mode()
{
/** @var \hamburgscleanest\DataTables\Models\DataTable $dataTable */
$dataTable = DataTable::model(TestModel::class, ['id', 'created_at', 'name']);

static::assertEquals(1, $dataScout->getQueryCount());
/** @var FulltextSearch $searcher */
$searcher = new FulltextSearch(['name']);
$searcher->forceDatabaseDriver('mysql');
$searcher->addQuery('test');
$searcher->setMode('WITH QUERY EXTENSION');

$dataScout->addQuery($queryString);
$dataTable->render();

static::assertEquals(0, $dataScout->getQueryCount());
self::assertEquals('SELECT * FROM "TESTMODELS" WHERE (MATCH(NAME) AGAINST (\'TEST\' WITH QUERY EXTENSION))', \mb_strtoupper($searcher->searchData($dataTable->query())->toSql()));
}

protected function setUpDb()
Expand Down

0 comments on commit 9dcb8fe

Please sign in to comment.