Skip to content

Commit 22e3b9f

Browse files
committed
start removing search sql quirks
1 parent 5976122 commit 22e3b9f

33 files changed

+1843
-1494
lines changed

phpunit/functional/SearchTest.php

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,8 +1756,10 @@ public function testAddOrderBy($itemtype, $sort_fields, $expected)
17561756
{
17571757
$result = \Search::addOrderBy($itemtype, $sort_fields);
17581758
$this->assertEquals($expected, $result);
1759+
}
17591760

1760-
// Complex cases
1761+
public function testAddOrderByComplex()
1762+
{
17611763
$table_addtable = 'glpi_users_af1042e23ce6565cfe58c6db91f84692';
17621764
$table_ticket_user = 'glpi_tickets_users_019878060c6d5f06cbe3c4d7c31dec24';
17631765

@@ -1769,17 +1771,17 @@ public function testAddOrderBy($itemtype, $sort_fields, $expected)
17691771
]
17701772
]);
17711773
$this->assertEquals(
1772-
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(
1773-
IFNULL(`$table_addtable`.`firstname`, ''),
1774-
IFNULL(`$table_addtable`.`realname`, ''),
1775-
IFNULL(`$table_addtable`.`name`, ''),
1776-
IFNULL(`$table_ticket_user`.`alternative_email`, '')
1777-
) ORDER BY CONCAT(
1778-
IFNULL(`$table_addtable`.`firstname`, ''),
1779-
IFNULL(`$table_addtable`.`realname`, ''),
1780-
IFNULL(`$table_addtable`.`name`, ''),
1781-
IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC
1782-
) ASC",
1774+
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(" .
1775+
"IFNULL(`$table_addtable`.`firstname`, '')," .
1776+
" IFNULL(`$table_addtable`.`realname`, '')," .
1777+
" IFNULL(`$table_addtable`.`name`, '')," .
1778+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')" .
1779+
") ORDER BY CONCAT(" .
1780+
"IFNULL(`$table_addtable`.`firstname`, '')," .
1781+
" IFNULL(`$table_addtable`.`realname`, '')," .
1782+
" IFNULL(`$table_addtable`.`name`, '')," .
1783+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC" .
1784+
") ASC",
17831785
$user_order_1
17841786
);
17851787
$user_order_2 = \Search::addOrderBy('Ticket', [
@@ -1789,17 +1791,17 @@ public function testAddOrderBy($itemtype, $sort_fields, $expected)
17891791
]
17901792
]);
17911793
$this->assertEquals(
1792-
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(
1793-
IFNULL(`$table_addtable`.`firstname`, ''),
1794-
IFNULL(`$table_addtable`.`realname`, ''),
1795-
IFNULL(`$table_addtable`.`name`, ''),
1796-
IFNULL(`$table_ticket_user`.`alternative_email`, '')
1797-
) ORDER BY CONCAT(
1798-
IFNULL(`$table_addtable`.`firstname`, ''),
1799-
IFNULL(`$table_addtable`.`realname`, ''),
1800-
IFNULL(`$table_addtable`.`name`, ''),
1801-
IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC
1802-
) DESC",
1794+
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(" .
1795+
"IFNULL(`$table_addtable`.`firstname`, '')," .
1796+
" IFNULL(`$table_addtable`.`realname`, '')," .
1797+
" IFNULL(`$table_addtable`.`name`, '')," .
1798+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')" .
1799+
") ORDER BY CONCAT(" .
1800+
"IFNULL(`$table_addtable`.`firstname`, '')," .
1801+
" IFNULL(`$table_addtable`.`realname`, '')," .
1802+
" IFNULL(`$table_addtable`.`name`, '')," .
1803+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC" .
1804+
") DESC",
18031805
$user_order_2
18041806
);
18051807

@@ -1811,17 +1813,17 @@ public function testAddOrderBy($itemtype, $sort_fields, $expected)
18111813
]
18121814
]);
18131815
$this->assertEquals(
1814-
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(
1815-
IFNULL(`$table_addtable`.`realname`, ''),
1816-
IFNULL(`$table_addtable`.`firstname`, ''),
1817-
IFNULL(`$table_addtable`.`name`, ''),
1818-
IFNULL(`$table_ticket_user`.`alternative_email`, '')
1819-
) ORDER BY CONCAT(
1820-
IFNULL(`$table_addtable`.`realname`, ''),
1821-
IFNULL(`$table_addtable`.`firstname`, ''),
1822-
IFNULL(`$table_addtable`.`name`, ''),
1823-
IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC
1824-
) ASC",
1816+
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(" .
1817+
"IFNULL(`$table_addtable`.`realname`, '')," .
1818+
" IFNULL(`$table_addtable`.`firstname`, '')," .
1819+
" IFNULL(`$table_addtable`.`name`, '')," .
1820+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')" .
1821+
") ORDER BY CONCAT(" .
1822+
"IFNULL(`$table_addtable`.`realname`, '')," .
1823+
" IFNULL(`$table_addtable`.`firstname`, '')," .
1824+
" IFNULL(`$table_addtable`.`name`, '')," .
1825+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC" .
1826+
") ASC",
18251827
$user_order_3
18261828
);
18271829
$user_order_4 = \Search::addOrderBy('Ticket', [
@@ -1831,17 +1833,17 @@ public function testAddOrderBy($itemtype, $sort_fields, $expected)
18311833
]
18321834
]);
18331835
$this->assertEquals(
1834-
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(
1835-
IFNULL(`$table_addtable`.`realname`, ''),
1836-
IFNULL(`$table_addtable`.`firstname`, ''),
1837-
IFNULL(`$table_addtable`.`name`, ''),
1838-
IFNULL(`$table_ticket_user`.`alternative_email`, '')
1839-
) ORDER BY CONCAT(
1840-
IFNULL(`$table_addtable`.`realname`, ''),
1841-
IFNULL(`$table_addtable`.`firstname`, ''),
1842-
IFNULL(`$table_addtable`.`name`, ''),
1843-
IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC
1844-
) DESC",
1836+
" ORDER BY GROUP_CONCAT(DISTINCT CONCAT(" .
1837+
"IFNULL(`$table_addtable`.`realname`, '')," .
1838+
" IFNULL(`$table_addtable`.`firstname`, '')," .
1839+
" IFNULL(`$table_addtable`.`name`, '')," .
1840+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')" .
1841+
") ORDER BY CONCAT(" .
1842+
"IFNULL(`$table_addtable`.`realname`, '')," .
1843+
" IFNULL(`$table_addtable`.`firstname`, '')," .
1844+
" IFNULL(`$table_addtable`.`name`, '')," .
1845+
" IFNULL(`$table_ticket_user`.`alternative_email`, '')) ASC" .
1846+
") DESC",
18451847
$user_order_4
18461848
);
18471849
}
@@ -2613,7 +2615,7 @@ public static function providerAddWhere()
26132615
'searchtype' => 'contains',
26142616
'val' => '< 192.168.1.10',
26152617
'meta' => false,
2616-
'expected' => "AND (INET_ATON(`glpi_ipaddresses`.`name`) < INET_ATON('192.168.1.10'))",
2618+
'expected' => "AND INET_ATON(`glpi_ipaddresses`.`name`) < INET_ATON('192.168.1.10')",
26172619
],
26182620
[
26192621
'link' => ' AND ',
@@ -2623,7 +2625,7 @@ public static function providerAddWhere()
26232625
'searchtype' => 'contains',
26242626
'val' => '> 192.168.1.10',
26252627
'meta' => false,
2626-
'expected' => "AND (INET_ATON(`glpi_ipaddresses`.`name`) > INET_ATON('192.168.1.10'))",
2628+
'expected' => "AND INET_ATON(`glpi_ipaddresses`.`name`) > INET_ATON('192.168.1.10')",
26272629
],
26282630
];
26292631
}
@@ -2731,11 +2733,7 @@ public function testSearchWithMultipleFkeysOnSameTable()
27312733
"`glpi_users_users_id_recipient`.`id` = '{$user_normal_id}'",
27322734

27332735
// Check that ORDER applies on corresponding table alias
2734-
"CONCAT(
2735-
IFNULL(`glpi_users_users_id_recipient`.`realname`, ''),
2736-
IFNULL(`glpi_users_users_id_recipient`.`firstname`, ''),
2737-
IFNULL(`glpi_users_users_id_recipient`.`name`, '')
2738-
) ASC"
2736+
"CONCAT(IFNULL(`glpi_users_users_id_recipient`.`realname`, ''), IFNULL(`glpi_users_users_id_recipient`.`firstname`, ''), IFNULL(`glpi_users_users_id_recipient`.`name`, '')) ASC"
27392737
];
27402738
foreach ($contains as $contain) {
27412739
$this->assertStringContainsString($contain, $data['sql']['search']);

src/Change.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ class Change extends CommonITILObject
4747
protected static $forward_entity_to = ['ChangeValidation', 'ChangeCost'];
4848

4949
// From CommonITIL
50+
/** @var class-string<CommonITILActor> */
5051
public $userlinkclass = 'Change_User';
52+
/** @var class-string<CommonITILActor> */
5153
public $grouplinkclass = 'Change_Group';
54+
/** @var class-string<CommonITILActor> */
5255
public $supplierlinkclass = 'Change_Supplier';
5356

5457
public static $rightname = 'change';

src/CommonDBTM.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
use Glpi\Application\View\TemplateRenderer;
3737
use Glpi\Asset\Asset_PeripheralAsset;
38+
use Glpi\DBAL\QueryExpression;
3839
use Glpi\DBAL\QueryFunction;
3940
use Glpi\DBAL\QueryParam;
4041
use Glpi\Event;
@@ -6858,4 +6859,89 @@ public static function getByUuid(string $uuid): ?self
68586859

68596860
return null;
68606861
}
6862+
6863+
/**
6864+
* Get the default SELECT criteria for the itemtype.
6865+
* This method is called on the itemtype being searched on.
6866+
* @return array The SELECT criteria to use
6867+
*/
6868+
public static function getSQLDefaultSelectCriteria(): array
6869+
{
6870+
return [];
6871+
}
6872+
6873+
/**
6874+
* Get the default WHERE criteria for the itemtype.
6875+
* This method is called on the itemtype being searched on.
6876+
* @return array The WHERE criteria to use
6877+
*/
6878+
public static function getSQLDefaultWhereCriteria(): array
6879+
{
6880+
if (is_subclass_of(static::class, ExtraVisibilityCriteria::class)) {
6881+
return static::getVisibilityCriteria()['WHERE'] ?? [];
6882+
}
6883+
return [];
6884+
}
6885+
6886+
/**
6887+
* Get the default JOIN criteria for the itemtype.
6888+
* This method is called on the itemtype being searched on.
6889+
* @return array The JOIN criteria to use
6890+
*/
6891+
public static function getSQLDefaultJoinCriteria(string $ref_table, array &$already_link_tables): array
6892+
{
6893+
if (is_subclass_of(static::class, ExtraVisibilityCriteria::class)) {
6894+
$leftjoin = static::getVisibilityCriteria()['LEFT JOIN'] ?? [];
6895+
$out = ['LEFT JOIN' => $leftjoin];
6896+
foreach ($leftjoin as $table => $criteria) {
6897+
$already_link_tables[] = $table;
6898+
}
6899+
return $out;
6900+
}
6901+
return [];
6902+
}
6903+
6904+
/**
6905+
* Get the SELECT criteria for the provided itemtype and search option.
6906+
* This method is called on the class that the search option belongs to (based on the table).
6907+
* @param class-string<CommonDBTM> $itemtype The itemtype being searched on
6908+
* @param SearchOption $opt The search option being handled
6909+
* @param bool $meta Whether the search option is for a meta field
6910+
* @param class-string<CommonDBTM>|'' $meta_type The meta itemtype being searched on
6911+
* @return array|null The SELECT criteria to use, or null to use the default handling
6912+
*/
6913+
public static function getSQLSelectCriteria(string $itemtype, SearchOption $opt, bool $meta = false, string $meta_type = ''): ?array
6914+
{
6915+
return null;
6916+
}
6917+
6918+
/**
6919+
* Get an array of criteria to handle the search option in a non-standard way.
6920+
* This method is called on the class that the search option belongs to (based on the table).
6921+
* @param class-string<CommonDBTM> $itemtype The main itemtype being searched on
6922+
* @param SearchOption $opt The search option being handled
6923+
* @param bool $nott Whether the search option is negated
6924+
* @param string $searchtype The search type (e.g. 'contains')
6925+
* @param mixed $val The value to search for
6926+
* @param bool $meta Whether the search option is for a meta field
6927+
* @param callable $fn_append_with_search A helper function to append a criterion to a criteria array in a standardized way
6928+
* @phpstan-param callable(array &$criteria, string|QueryFunction $value): void $fn_append_with_search
6929+
* @return array|null The criteria to use, or null to use the default handling
6930+
*/
6931+
public static function getSQLWhereCriteria(string $itemtype, SearchOption $opt, bool $nott, string $searchtype, mixed $val, bool $meta, callable $fn_append_with_search): ?array
6932+
{
6933+
return null;
6934+
}
6935+
6936+
/**
6937+
* Get the ORDER BY criteria for the provided itemtype and search option.
6938+
* @param class-string<CommonDBTM> $itemtype The main itemtype being searched on
6939+
* @param SearchOption $opt The search option being handled
6940+
* @param 'ASC'|'DESC' $order The order direction ('ASC' or 'DESC')
6941+
* @return QueryExpression|null The ORDER BY criteria to use, or null to use the default handling
6942+
*/
6943+
public static function getSQLOrderByCriteria(string $itemtype, SearchOption $opt, string $order): ?\Glpi\DBAL\QueryExpression
6944+
{
6945+
return null;
6946+
}
68616947
}

0 commit comments

Comments
 (0)