Skip to content

Commit 13bab98

Browse files
authored
Merge pull request #15 from kuchy/disable-split-word-search-support
Support for exactSearch/splitWordsSearch properties in FilterText.
2 parents df4690c + 637579d commit 13bab98

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/NetteDatabaseDataSource.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,24 @@ public function applyFilterText(Filter\FilterText $filter)
293293
$big_or = '(';
294294
$big_or_args = [];
295295
$condition = $filter->getCondition();
296+
$isSeachExact = $filter->isExactSearch();
297+
$operator = $isSeachExact ? '=' : 'LIKE';
296298

297299
foreach ($condition as $column => $value) {
298-
$words = explode(' ', $value);
299300

300301
$like = '(';
301302
$args = [];
302303

303-
foreach ($words as $word) {
304-
$like .= "$column LIKE ? OR ";
305-
$args[] = "%$word%";
304+
if ($filter->hasSplitWordsSearch() === FALSE) {
305+
$like .= "$column $operator ? OR ";
306+
$args[] = $isSeachExact ? $value :"%$value%";
307+
}else{
308+
$words = explode(' ', $value);
309+
310+
foreach ($words as $word) {
311+
$like .= "$column $operator ? OR ";
312+
$args[] = $isSeachExact ? $word : "%$word%";
313+
}
306314
}
307315

308316
$like = substr($like, 0, strlen($like) - 4).')';

tests/Cases/NetteDatabaseDataSourceTest.phpt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,81 @@ final class NetteDatabaseDataSourceTest extends TestCase
8585
Assert::same([1], $q[1]);
8686
}
8787

88+
public function testApplyFilterText()
89+
{
90+
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM user');
91+
$filter = new FilterText($this->grid, 'name', 'Name', ['name']);
92+
$filter->setValue('text');
93+
$s->applyFilterText($filter);
94+
$q = $s->getQuery();
95+
96+
Assert::same('SELECT * FROM user WHERE (name LIKE ?)', $q[0]);
97+
Assert::same(['%text%'], $q[1]);
98+
}
99+
100+
public function testApplyFilterTextDouble()
101+
{
102+
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM user');
103+
$filter = new FilterText($this->grid, 'name', 'Name or id', ['name', 'id']);
104+
$filter->setValue('text');
105+
$s->applyFilterText($filter);
106+
$q = $s->getQuery();
107+
108+
Assert::same('SELECT * FROM user WHERE ((name LIKE ?) OR (id LIKE ?))', $q[0]);
109+
Assert::same(['%text%', '%text%'], $q[1]);
110+
}
111+
112+
public function testApplyFilterTextSplitWordsSearch()
113+
{
114+
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM user');
115+
$filter = new FilterText($this->grid, 'name', 'Name or id', ['name', 'id']);
116+
$filter->setValue('text alternative');
117+
$s->applyFilterText($filter);
118+
$q = $s->getQuery();
119+
120+
Assert::same('SELECT * FROM user WHERE ((name LIKE ? OR name LIKE ?) OR (id LIKE ? OR id LIKE ?))', $q[0]);
121+
Assert::same(['%text%', '%alternative%', '%text%', '%alternative%'], $q[1]);
122+
}
123+
124+
public function testApplyFilterTextSplitWordsSearchDisabled()
125+
{
126+
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM user');
127+
$filter = new FilterText($this->grid, 'name', 'Name or id', ['name', 'id']);
128+
$filter->setValue('text alternative');
129+
$filter->setSplitWordsSearch(False);
130+
$s->applyFilterText($filter);
131+
$q = $s->getQuery();
132+
133+
Assert::same('SELECT * FROM user WHERE ((name LIKE ?) OR (id LIKE ?))', $q[0]);
134+
Assert::same(['%text alternative%', '%text alternative%'], $q[1]);
135+
}
136+
137+
public function testApplyFilterTextExactSearch()
138+
{
139+
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM user');
140+
$filter = new FilterText($this->grid, 'name', 'Name or id', ['name', 'id']);
141+
$filter->setValue('text');
142+
$filter->setExactSearch();
143+
$s->applyFilterText($filter);
144+
$q = $s->getQuery();
145+
146+
Assert::same('SELECT * FROM user WHERE ((name = ?) OR (id = ?))', $q[0]);
147+
Assert::same(['text', 'text'], $q[1]);
148+
}
149+
150+
public function testApplyFilterTextSplitWordsSearchDisabledExact()
151+
{
152+
$s = new NetteDatabaseDataSource($this->db, 'SELECT * FROM user');
153+
$filter = new FilterText($this->grid, 'name', 'Name or id', ['name', 'id']);
154+
$filter->setValue('text with space');
155+
$filter->setSplitWordsSearch(False);
156+
$filter->setExactSearch();
157+
$s->applyFilterText($filter);
158+
$q = $s->getQuery();
159+
160+
Assert::same('SELECT * FROM user WHERE ((name = ?) OR (id = ?))', $q[0]);
161+
Assert::same(['text with space', 'text with space'], $q[1]);
162+
}
88163

89164
public function testComplexQuery()
90165
{

0 commit comments

Comments
 (0)