#13264 added regex in sql

This commit is contained in:
Daniel Cebrian 2024-04-23 16:56:28 +02:00
parent 7a78283eed
commit 64013c0416
2 changed files with 36 additions and 41 deletions

View File

@ -1155,7 +1155,7 @@ function events_get_all(
} }
// Free search. // Free search.
if (empty($filter['search']) === false && (bool) $filter['regex'] === false) { if (empty($filter['search']) === false) {
if (isset($config['dbconnection']->server_version) === true if (isset($config['dbconnection']->server_version) === true
&& $config['dbconnection']->server_version > 50600 && $config['dbconnection']->server_version > 50600
) { ) {
@ -1185,23 +1185,39 @@ function events_get_all(
$array_search[] = 'lower(ta.alias)'; $array_search[] = 'lower(ta.alias)';
} }
// Disregard repeated whitespaces when searching. if ((bool) $filter['regex'] === true) {
$collapsed_spaces_search = preg_replace('/( )+/', ' ', $filter['search']); $sql_search = ' AND (';
foreach ($array_search as $key => $field) {
$sql_search .= sprintf(
'%s %s %s REGEXP "%s" ',
($key === 0) ? '' : $nexo,
$field,
$not_search,
io_safe_output($filter['search'])
);
$sql_search .= ' ';
}
$sql_search = ' AND ('; $sql_search .= ' )';
foreach ($array_search as $key => $field) { } else {
$sql_search .= sprintf( // Disregard repeated whitespaces when searching.
'%s LOWER(REGEXP_REPLACE(%s, "( )+", " ")) %s like LOWER("%%%s%%")', $collapsed_spaces_search = preg_replace('/( )+/', ' ', $filter['search']);
($key === 0) ? '' : $nexo,
$field, $sql_search = ' AND (';
$not_search, foreach ($array_search as $key => $field) {
$collapsed_spaces_search $sql_search .= sprintf(
); '%s LOWER(REGEXP_REPLACE(%s, "( )+", " ")) %s like LOWER("%%%s%%")',
$sql_search .= ' '; ($key === 0) ? '' : $nexo,
$field,
$not_search,
$collapsed_spaces_search
);
$sql_search .= ' ';
}
$sql_search .= ' )';
} }
$sql_search .= ' )';
$sql_filters[] = $sql_search; $sql_filters[] = $sql_search;
} }

View File

@ -1249,27 +1249,7 @@ if (is_ajax() === true) {
} }
} }
$regex_validation = false; $carry[] = $tmp;
if (empty($tmp) === false && $regex !== '') {
foreach (json_decode(json_encode($tmp), true) as $key => $field) {
if ($key === 'b64') {
continue;
}
$field = strip_tags($field);
if (preg_match('/'.io_safe_output($regex).'/', $field)) {
$regex_validation = true;
}
}
if ($regex_validation === true) {
$carry[] = $tmp;
}
} else {
$carry[] = $tmp;
}
return $carry; return $carry;
} }
); );
@ -1289,11 +1269,10 @@ if (is_ajax() === true) {
// RecordsTotal && recordsfiltered resultados totales. // RecordsTotal && recordsfiltered resultados totales.
echo json_encode( echo json_encode(
[ [
'data' => ($data ?? []), 'data' => ($data ?? []),
'buffers' => $buffers, 'buffers' => $buffers,
'recordsTotal' => $count, 'recordsTotal' => $count,
'recordsFiltered' => $count, 'recordsFiltered' => $count,
'showAlwaysPagination' => (empty($regex) === false) ? true : false,
] ]
); );
} catch (Exception $e) { } catch (Exception $e) {