Skip to content

Commit 9ef712f

Browse files
committed
WIP
1 parent 8a8239d commit 9ef712f

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

etc/schema/postgresql.schema.sql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,24 @@ CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP()
1212
PARALLEL SAFE
1313
AS $$
1414
BEGIN
15-
RETURN EXTRACT(EPOCH FROM now());
15+
RETURN EXTRACT(EPOCH FROM now());
1616
END;
1717
$$;
1818

19+
-- IPL ORM renders SQL queries with LIKE operators for all suggestions in the search bar,
20+
-- which fails for numeric and enum types on PostgreSQL. Just like in Icinga DB Web!!
21+
CREATE OR REPLACE FUNCTION anynonarrayliketext(anynonarray, text)
22+
RETURNS bool
23+
LANGUAGE plpgsql
24+
IMMUTABLE
25+
PARALLEL SAFE
26+
AS $$
27+
BEGIN
28+
RETURN $1::TEXT LIKE $2;
29+
END;
30+
$$;
31+
CREATE OPERATOR ~~ (LEFTARG=anynonarray, RIGHTARG=text, PROCEDURE=anynonarrayliketext);
32+
1933
CREATE TABLE x509_certificate (
2034
id serial PRIMARY KEY,
2135
subject varchar(255) NOT NULL,

library/X509/Web/Control/SearchBar/ObjectSuggestions.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use ipl\Orm\Resolver;
1212
use ipl\Orm\UnionModel;
1313
use ipl\Stdlib\Filter;
14-
use ipl\Stdlib\Seq;
1514
use ipl\Stdlib\Str;
1615
use ipl\Web\Control\SearchBar\SearchException;
1716
use ipl\Web\Control\SearchBar\Suggestions;
@@ -45,7 +44,12 @@ protected function shouldShowRelationFor(string $column): bool
4544
{
4645
$columns = Str::trimSplit($column, '.');
4746

48-
return $columns[0] !== $this->model->getTableName();
47+
switch (count($columns)) {
48+
case 2:
49+
return $columns[0] !== $this->model->getTableName();
50+
default:
51+
return true;
52+
}
4953
}
5054

5155
protected function createQuickSearchFilter($searchTerm)
@@ -69,54 +73,40 @@ protected function fetchValueSuggestions($column, $searchTerm, Filter\Chain $sea
6973
$query = $model::on($this->getDb());
7074
$query->limit(self::DEFAULT_LIMIT);
7175

72-
if (strpos($column, ' ') !== false) {
73-
// $column may be a label
74-
list($path, $_) = Seq::find(
75-
self::collectFilterColumns($query->getModel(), $query->getResolver()),
76-
$column,
77-
false
78-
);
79-
80-
if ($path !== null) {
81-
$column = $path;
82-
}
83-
}
84-
8576
$columnPath = $query->getResolver()->qualifyPath($column, $model->getTableName());
8677
$inputFilter = Filter::like($columnPath, $searchTerm);
8778

88-
$query->columns($columnPath);
79+
$query->columns([$columnPath]);
8980
$query->orderBy($columnPath);
9081

9182
if ($searchFilter instanceof Filter\None) {
9283
$query->filter($inputFilter);
9384
} elseif ($searchFilter instanceof Filter\All) {
9485
$searchFilter->add($inputFilter);
95-
96-
$searchFilter->metaData()->set('forceOptimization', true);
97-
$inputFilter->metaData()->set('forceOptimization', false);
9886
} else {
9987
$searchFilter = $inputFilter;
10088
}
10189

10290
$query->filter($searchFilter);
91+
// Not to suggest something like Port=443,443,443....
92+
$query->getSelectBase()->distinct();
10393

10494
try {
10595
foreach ($query as $item) {
106-
try {
107-
$columns = Str::trimSplit($column, '.');
108-
if ($columns[0] === $model->getTableName()) {
109-
array_shift($columns);
110-
}
96+
$columns = Str::trimSplit($column, '.');
97+
if ($columns[0] === $model->getTableName()) {
98+
array_shift($columns);
99+
}
111100

112-
$value = null;
101+
$value = $item;
113102

103+
try {
114104
do {
115105
$col = array_shift($columns);
116-
$value = $item->$col;
106+
$value = $value->$col;
117107
} while (! empty($columns));
118108

119-
if ($value !== '' && ! ctype_print($value)) { // Is binary
109+
if ($value && ! ctype_print($value)) { // Is binary
120110
$value = sprintf('\\x%s', bin2hex($value));
121111
}
122112

@@ -171,7 +161,7 @@ protected static function collectRelations(Resolver $resolver, Model $subject, a
171161
$relationPath = [$name];
172162

173163
if (! isset($models[$name]) && ! in_array($name, $path, true)) {
174-
if ($isHasOne) {
164+
if ($isHasOne || empty($path)) {
175165
array_unshift($relationPath, $subject->getTableName());
176166
}
177167

@@ -183,6 +173,8 @@ protected static function collectRelations(Resolver $resolver, Model $subject, a
183173
self::collectRelations($resolver, $relation->getTarget(), $models, $relationPath);
184174
return;
185175
}
176+
} else {
177+
$path = [];
186178
}
187179
}
188180
}

0 commit comments

Comments
 (0)