Unverified Commit 0180408c authored by Johan Cwiklinski's avatar Johan Cwiklinski Committed by GitHub
Browse files

Drop SQL conditions (#7722)



* Convert to iterator

Use iterator for search join conditions and replace hardcoded conditions

* Apply suggestions from code review
Co-authored-by: default avatarCédric Anne <cedric.anne@gmail.com>
Co-authored-by: default avatarCédric Anne <cedric.anne@gmail.com>
parent 3e965d90
......@@ -1219,6 +1219,7 @@ abstract class API extends CommonGLPI {
$already_linked_table = [];
$join = Search::addDefaultJoin($itemtype, $table, $already_linked_table);
$where = Search::addDefaultWhere($itemtype);
if ($where == '') {
$where = "1=1 ";
}
......
......@@ -311,8 +311,8 @@ class CartridgeItem extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_use` IS NOT NULL
AND NEWTABLE.`date_out` IS NULL'
'condition' => ['NOT' => ['NEWTABLE.date_use' => null],
'NEWTABLE.date_out' => null]
]
];
......@@ -327,7 +327,7 @@ class CartridgeItem extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_out` IS NOT NULL'
'condition' => ['NOT' => ['NEWTABLE.date_out' => null]]
]
];
......@@ -342,8 +342,8 @@ class CartridgeItem extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_use` IS NULL
AND NEWTABLE.`date_out` IS NULL'
'condition' => ['NEWTABLE.date_use' => null,
'NEWTABLE.date_out' => null]
]
];
......
......@@ -3384,7 +3384,7 @@ abstract class CommonITILObject extends CommonDBTM {
if (!Session::isCron() // no filter for cron
&& Session::getCurrentInterface() == 'helpdesk') {
$newtab['condition'] = "`is_helpdeskvisible`";
$newtab['condition'] = ['is_helpdeskvisible' => 1];
}
$tab[] = $newtab;
......@@ -3460,6 +3460,7 @@ abstract class CommonITILObject extends CommonDBTM {
* @since 0.85
**/
function getSearchOptionsSolution() {
global $DB;
$tab = [];
$tab[] = [
......@@ -3526,14 +3527,18 @@ abstract class CommonITILObject extends CommonDBTM {
'joinparams' => [
'jointype' => 'itemtype_item',
// Get only last created solution
'condition' => '
AND NEWTABLE.`id` = (
SELECT `id` FROM `' . ITILSolution::getTable() . '`
WHERE `' . ITILSolution::getTable() . '`.`items_id` = REFTABLE.`id`
AND `' . ITILSolution::getTable() . '`.`itemtype` = \'' . static::getType() . '\'
ORDER BY `' . ITILSolution::getTable() . '`.`id` DESC
LIMIT 1
)'
'condition' => [
'NEWTABLE.id' => ['=', new QuerySubQuery([
'SELECT' => 'id',
'FROM' => ITILSolution::getTable(),
'WHERE' => [
ITILSolution::getTable() . '.items_id' => new QueryExpression($DB->quoteName('REFTABLE.id')),
ITILSolution::getTable() . '.itemtype' => static::getType()
],
'ORDER' => ITILSolution::getTable() . '.id DESC',
'LIMIT' => 1
])]
]
]
];
......@@ -3605,7 +3610,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->userlinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::REQUESTER
'condition' => ['NEWTABLE.type' => CommonITILActor::REQUESTER]
]
]
]
......@@ -3631,7 +3636,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->grouplinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::REQUESTER
'condition' => ['NEWTABLE.type' => CommonITILActor::REQUESTER]
]
]
]
......@@ -3681,7 +3686,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->userlinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::OBSERVER
'condition' => ['NEWTABLE.type' => CommonITILActor::OBSERVER]
]
]
]
......@@ -3701,7 +3706,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->grouplinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::OBSERVER
'condition' => ['NEWTABLE.type' => CommonITILActor::OBSERVER]
]
]
]
......@@ -3726,7 +3731,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->userlinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::ASSIGN
'condition' => ['NEWTABLE.type' => CommonITILActor::ASSIGN]
]
]
]
......@@ -3745,7 +3750,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->supplierlinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::ASSIGN
'condition' => ['NEWTABLE.type' => CommonITILActor::ASSIGN]
]
]
]
......@@ -3765,7 +3770,7 @@ abstract class CommonITILObject extends CommonDBTM {
'table' => getTableForItemType($this->grouplinkclass),
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::ASSIGN
'condition' => ['NEWTABLE.type' => CommonITILActor::ASSIGN]
]
]
]
......@@ -3785,7 +3790,7 @@ abstract class CommonITILObject extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::REQUESTER
'condition' => ['NEWTABLE.type' => CommonITILActor::REQUESTER]
]
];
......@@ -3798,7 +3803,7 @@ abstract class CommonITILObject extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`type` = '.CommonITILActor::REQUESTER
'condition' => ['NEWTABLE.type' => CommonITILActor::REQUESTER]
]
];
......
......@@ -666,8 +666,12 @@ abstract class CommonITILTask extends CommonDBTM implements CalDAVCompatibleItem
$task_condition = '';
if ($task->maybePrivate() && !Session::haveRight("task", CommonITILTask::SEEPRIVATE)) {
$task_condition = "AND (`NEWTABLE`.`is_private` = 0
OR `NEWTABLE`.`users_id` = '".Session::getLoginUserID()."')";
$task_condition = [
'OR' => [
'NEWTABLE.is_private' => 0,
'NEWTABLE.users_id' => Session::getLoginUserID()
]
];
}
$tab[] = [
......@@ -792,7 +796,7 @@ abstract class CommonITILTask extends CommonDBTM implements CalDAVCompatibleItem
'linkfield' => 'groups_id_tech',
'name' => __('Group in charge'),
'datatype' => 'itemlink',
'condition' => 'is_task',
'condition' => ['is_task' => 1],
'forcegroupby' => true,
'massiveaction' => false,
'joinparams' => [
......
......@@ -711,7 +711,7 @@ abstract class CommonTreeDropdown extends CommonDropdown {
'datatype' => 'dropdown',
'massiveaction' => false,
// Add virtual condition to relink table
'joinparams' => ['condition' => "AND 1=1"]
'joinparams' => ['condition' => [new QueryExpression("1=1")]]
];
$tab[] = [
......
......@@ -283,7 +283,7 @@ class ConsumableItem extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_out` IS NOT NULL'
'condition' => ['NOT' => ['NEWTABLE.date_out' => null]]
]
];
......@@ -298,7 +298,7 @@ class ConsumableItem extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_out` IS NULL'
'condition' => ['NEWTABLE.date_out' => null]
]
];
......
......@@ -512,7 +512,7 @@ class Group extends CommonTreeDropdown {
'table' => 'glpi_groups_users',
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`is_manager` = 1'
'condition' => ['NEWTABLE.is_manager' => 1]
]
]
]
......@@ -532,7 +532,7 @@ class Group extends CommonTreeDropdown {
'table' => 'glpi_groups_users',
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`is_userdelegate` = 1'
'condition' => ['NEWTABLE.is_userdelegate' => 1]
]
]
]
......
......@@ -579,8 +579,12 @@ class ITILFollowup extends CommonDBChild {
$followup_condition = '';
if (!Session::haveRight('followup', self::SEEPRIVATE)) {
$followup_condition = "AND (`NEWTABLE`.`is_private` = 0
OR `NEWTABLE`.`users_id` = '".Session::getLoginUserID()."')";
$followup_condition = [
'OR' => [
'NEWTABLE.is_private' => 0,
'NEWTABLE.users_id' => Session::getLoginUserID()
]
];
}
$tab[] = [
......
......@@ -218,7 +218,7 @@ class NetworkName extends FQDNLabel {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'mainitemtype_mainitem',
'condition' => 'AND NEWTABLE.`is_deleted` = 0'
'condition' => ['NEWTABLE.is_deleted' => 0]
]
];
......
......@@ -951,7 +951,7 @@ class NetworkPort extends CommonDBChild {
$networkNameJoin = ['jointype' => 'itemtype_item',
'specific_itemtype' => 'NetworkPort',
'condition' => 'AND NEWTABLE.`is_deleted` = 0',
'condition' => ['NEWTABLE.is_deleted' => 0],
'beforejoin' => ['table' => 'glpi_networkports',
'joinparams' => $joinparams]];
NetworkName::rawSearchOptionsToAdd($tab, $networkNameJoin, $itemtype);
......
......@@ -703,8 +703,8 @@ class Printer extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_use` IS NOT NULL
AND NEWTABLE.`date_out` IS NULL'
'condition' => ['NOT' => ['NEWTABLE.date_use' => null],
'NEWTABLE.date_out' => null]
]
];
......@@ -719,7 +719,7 @@ class Printer extends CommonDBTM {
'massiveaction' => false,
'joinparams' => [
'jointype' => 'child',
'condition' => 'AND NEWTABLE.`date_out` IS NOT NULL'
'condition' => ['NOT' => ['NEWTABLE.date_out' => null]]
]
];
......
......@@ -1800,7 +1800,7 @@ class Profile extends CommonDBTM {
'rightname' => 'computer',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'computer'"
'condition' => ['NEWTABLE.name' => 'computer']
]
];
......@@ -1814,7 +1814,7 @@ class Profile extends CommonDBTM {
'rightname' => 'monitor',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'monitor'"
'condition' => ['NEWTABLE.name' => 'monitor']
]
];
......@@ -1828,7 +1828,7 @@ class Profile extends CommonDBTM {
'rightname' => 'software',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'software'"
'condition' => ['NEWTABLE.name' => 'software']
]
];
......@@ -1842,7 +1842,7 @@ class Profile extends CommonDBTM {
'rightname' => 'networking',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'networking'"
'condition' => ['NEWTABLE.name' => 'networking']
]
];
......@@ -1856,7 +1856,7 @@ class Profile extends CommonDBTM {
'rightname' => 'printer',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'printer'"
'condition' => ['NEWTABLE.name' => 'printer']
]
];
......@@ -1870,7 +1870,7 @@ class Profile extends CommonDBTM {
'rightname' => 'peripheral',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'peripheral'"
'condition' => ['NEWTABLE.name' => 'peripheral']
]
];
......@@ -1884,7 +1884,7 @@ class Profile extends CommonDBTM {
'rightname' => 'cartridge',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'cartridge'"
'condition' => ['NEWTABLE.name' => 'cartridge']
]
];
......@@ -1898,7 +1898,7 @@ class Profile extends CommonDBTM {
'rightname' => 'consumable',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'consumable'"
'condition' => ['NEWTABLE.name' => 'consumable']
]
];
......@@ -1912,7 +1912,7 @@ class Profile extends CommonDBTM {
'rightname' => 'phone',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'phone'"
'condition' => ['NEWTABLE.name' => 'phone']
]
];
......@@ -1926,7 +1926,7 @@ class Profile extends CommonDBTM {
'rightname' => 'internet',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'internet'"
'condition' => ['NEWTABLE.name' => 'internet']
]
];
......@@ -1945,7 +1945,7 @@ class Profile extends CommonDBTM {
'rightname' => 'contact_entreprise',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'contact_enterprise'"
'condition' => ['NEWTABLE.name' => 'contact_enterprise']
]
];
......@@ -1959,7 +1959,7 @@ class Profile extends CommonDBTM {
'rightname' => 'document',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'document'"
'condition' => ['NEWTABLE.name' => 'document']
]
];
......@@ -1973,7 +1973,7 @@ class Profile extends CommonDBTM {
'rightname' => 'contract',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'contract'"
'condition' => ['NEWTABLE.name' => 'contract']
]
];
......@@ -1987,7 +1987,7 @@ class Profile extends CommonDBTM {
'rightname' => 'infocom',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'infocom'"
'condition' => ['NEWTABLE.name' => 'infocom']
]
];
......@@ -2001,7 +2001,7 @@ class Profile extends CommonDBTM {
'rightname' => 'budget',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'budget'"
'condition' => ['NEWTABLE.name' => 'budget']
]
];
......@@ -2020,7 +2020,7 @@ class Profile extends CommonDBTM {
'rightname' => 'knowbase',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'knowbase'"
'condition' => ['NEWTABLE.name' => 'knowbase']
]
];
......@@ -2034,7 +2034,7 @@ class Profile extends CommonDBTM {
'rightname' => 'reservation',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'reservation'"
'condition' => ['NEWTABLE.name' => 'reservation']
]
];
......@@ -2049,7 +2049,7 @@ class Profile extends CommonDBTM {
'nowrite' => true,
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'reports'"
'condition' => ['NEWTABLE.name' => 'reports']
]
];
......@@ -2068,7 +2068,7 @@ class Profile extends CommonDBTM {
'rightname' => 'dropdown',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'dropdown'"
'condition' => ['NEWTABLE.name' => 'dropdown']
]
];
......@@ -2083,7 +2083,7 @@ class Profile extends CommonDBTM {
'noread' => true,
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'device'"
'condition' => ['NEWTABLE.name' => 'device']
]
];
......@@ -2097,7 +2097,7 @@ class Profile extends CommonDBTM {
'rightname' => 'notification',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'notification'"
'condition' => ['NEWTABLE.name' => 'notification']
]
];
......@@ -2111,7 +2111,7 @@ class Profile extends CommonDBTM {
'rightname' => 'typedoc',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'typedoc'"
'condition' => ['NEWTABLE.name' => 'typedoc']
]
];
......@@ -2125,7 +2125,7 @@ class Profile extends CommonDBTM {
'rightname' => 'link',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'link'"
'condition' => ['NEWTABLE.name' => 'link']
]
];
......@@ -2140,7 +2140,7 @@ class Profile extends CommonDBTM {
'noread' => true,
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'config'"
'condition' => ['NEWTABLE.name' => 'config']
]
];
......@@ -2155,7 +2155,7 @@ class Profile extends CommonDBTM {
'noread' => true,
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'personalization'"
'condition' => ['NEWTABLE.name' => 'personalization']
]
];
......@@ -2170,7 +2170,7 @@ class Profile extends CommonDBTM {
'noread' => true,
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'search_config'"
'condition' => ['NEWTABLE.name' => 'search_config']
]
];
......@@ -2184,7 +2184,7 @@ class Profile extends CommonDBTM {
'rightname' => 'calendar',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'calendar'"
'condition' => ['NEWTABLE.name' => 'calendar']
]
];
......@@ -2204,7 +2204,7 @@ class Profile extends CommonDBTM {
'nowrite' => true,
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_ticket'"
'condition' => ['NEWTABLE.name' => 'rule_ticket']
]
];
......@@ -2218,7 +2218,7 @@ class Profile extends CommonDBTM {
'rightname' => 'rule_mailcollector',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_mailcollector'"
'condition' => ['NEWTABLE.name' => 'rule_mailcollector']
]
];
......@@ -2232,7 +2232,7 @@ class Profile extends CommonDBTM {
'rightname' => 'rule_import',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_import'"
'condition' => ['NEWTABLE.name' => 'rule_import']
]
];
......@@ -2246,7 +2246,7 @@ class Profile extends CommonDBTM {
'rightname' => 'rule_ldap',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_ldap'"
'condition' => ['NEWTABLE.name' => 'rule_ldap']
]
];
......@@ -2260,7 +2260,7 @@ class Profile extends CommonDBTM {
'rightname' => 'rule_softwarecategories',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_softwarecategories'"
'condition' => ['NEWTABLE.name' => 'rule_softwarecategories']
]
];
......@@ -2274,7 +2274,7 @@ class Profile extends CommonDBTM {
'rightname' => 'rule_dictionnary_software',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_dictionnary_software'"
'condition' => ['NEWTABLE.name' => 'rule_dictionnary_software']
]
];
......@@ -2288,7 +2288,7 @@ class Profile extends CommonDBTM {
'rightname' => 'rule_dictionnary_dropdown',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'rule_dictionnary_dropdown'"
'condition' => ['NEWTABLE.name' => 'rule_dictionnary_dropdown']
]
];
......@@ -2302,7 +2302,7 @@ class Profile extends CommonDBTM {
'rightname' => 'profile',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'profile'"
'condition' => ['NEWTABLE.name' => 'profile']
]
];
......@@ -2316,7 +2316,7 @@ class Profile extends CommonDBTM {
'rightname' => 'user',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'user'"
'condition' => ['NEWTABLE.name' => 'user']
]
];
......@@ -2330,7 +2330,7 @@ class Profile extends CommonDBTM {
'rightname' => 'group',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'group'"
'condition' => ['NEWTABLE.name' => 'group']
]
];
......@@ -2344,7 +2344,7 @@ class Profile extends CommonDBTM {
'rightname' => 'entity',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'entity'"
'condition' => ['NEWTABLE.name' => 'entity']
]
];
......@@ -2358,7 +2358,7 @@ class Profile extends CommonDBTM {
'rightname' => 'transfer',
'joinparams' => [
'jointype' => 'child',
'condition' => "AND `NEWTABLE`.`name`= 'transfer'"
'condition' => ['NEWTABLE.name' => 'transfer']
]
];