Commit e9410bf4 authored by Johan Cwiklinski's avatar Johan Cwiklinski Committed by Johan Cwiklinski

Replace raw UPDATE with DB::update()

parent afdce579
......@@ -889,7 +889,7 @@ class IPNetwork extends CommonImplicitTreeDropdown {
'ipnetworks_id' => 0,
'level' => 1,
'completename' => new \QueryExpression($DB->quoteName('name'))
]
], [true]
);
// Foreach IPNetwork ...
......
......@@ -391,8 +391,8 @@ class Migration {
if (!empty($params['update'])) {
$this->migrationOneTable($table);
$query = "UPDATE `$table`
SET `$field` = ".$params['update']." ".
$params['condition']."";
SET `$field` = ".$params['update']." ".
$params['condition']."";
$DB->queryOrDie($query, $this->version." set $field in $table");
}
return true;
......
......@@ -1031,6 +1031,7 @@ class Plugin extends CommonDBTM {
foreach ($entities as $entID => $val) {
if ($do_recursive) {
// Non recursive ones
// needs DB::update to support subqueries to be migrated
$query3 = "UPDATE `glpi_infocoms`
SET `entities_id` = '$entID',
`is_recursive` = '0'
......@@ -1043,6 +1044,7 @@ class Plugin extends CommonDBTM {
in glpi_infocoms for $name");
// Recursive ones
// needs DB::update to support subqueries to be migrated
$query3 = "UPDATE `glpi_infocoms`
SET `entities_id` = '$entID',
`is_recursive` = '1'
......@@ -1054,6 +1056,7 @@ class Plugin extends CommonDBTM {
$DB->queryOrDie($query3, "update entities_id and is_recursive=1
in glpi_infocoms for $name");
} else {
// needs DB::update to support subqueries to be migrated
$query3 = "UPDATE `glpi_infocoms`
SET `entities_id` = '$entID'
WHERE `itemtype` = '$name'
......
......@@ -192,17 +192,23 @@ class RuleDictionnaryDropdownCollection extends RuleCollection {
if ($data['id'] != $ID) {
$tocheck[$data["id"]][] = $ID;
$sql = "UPDATE `$model_table`
SET `$model_field` = '$ID'
WHERE `$model_field` = '".$data['id']."'";
$where = [
$model_field => $data['id']
];
if (empty($data['idmanu'])) {
$sql .= " AND (`manufacturers_id` IS NULL
OR `manufacturers_id` = '0')";
$where['OR'] = [
['manufacturers_id' => null],
['manufacturers_id' => 0]
];
} else {
$sql .= " AND `manufacturers_id` = '".$data['idmanu']."'";
$where['manufacturers_id'] = $data['idmanu'];
}
$DB->query($sql);
$DB->update(
$model_table,
[$model_field => $ID],
$where
);
}
$i++;
......
......@@ -467,23 +467,30 @@ function step7() {
if (isset($_POST['send_stats'])) {
//user has accepted to send telemetry infos; activate cronjob
$query = 'UPDATE glpi_crontasks SET state = 1 WHERE name=\'telemetry\'';
$DB->query($query);
$DB->update(
'glpi_crontasks',
['state' => 1],
['name' => 'telemetry']
);
}
$url_base = str_replace("/install/install.php", "", $_SERVER['HTTP_REFERER']);
$query = "UPDATE `glpi_configs`
SET `value` = '".$DB->escape($url_base)."'
WHERE `context` = 'core'
AND `name` = 'url_base'";
$DB->query($query);
$DB->update(
'glpi_configs',
['value' => $DB->escape($url_base)], [
'context' => 'core',
'name' => 'url_base'
]
);
$url_base_api = "$url_base/apirest.php/";
$query = "UPDATE `glpi_configs`
SET `value` = '".$DB->escape($url_base_api)."'
WHERE `context` = 'core'
AND `name` = 'url_base_api'";
$DB->query($query);
$DB->update(
'glpi_configs',
['value' => $Db->escape($url_base_api)], [
'context' => 'core',
'name' => 'url_base_api'
]
);
echo "<h2>".__('The installation is finished')."</h2>";
echo "<p>".__('Default logins / passwords are:')."</p>";
......
......@@ -410,6 +410,7 @@ function changeVarcharToID($table1, $table2, $chps) {
ADD `temp` INT";
$DB->queryOrDie($query);
//needs DB::request to support aliases to get migrated
$query = "SELECT `$table1`.`ID` AS row1,
`$table2`.`ID` AS row2
FROM `$table1`, `$table2`
......@@ -417,10 +418,11 @@ function changeVarcharToID($table1, $table2, $chps) {
$result = $DB->queryOrDie($query);
while ($line = $DB->fetch_assoc($result)) {
$query = "UPDATE `$table1`
SET `temp` = ". $line["row2"] ."
WHERE `ID` = '". $line["row1"] ."'";
$DB->queryOrDie($query);
$DB->updateOrDie(
$table1,
['temp' => $line['row2']],
['ID' => $line['row1']]
);
}
$DB->free_result($result);
......
......@@ -1226,8 +1226,13 @@ abstract class APIBaseClass extends \atoum {
$this->integer($id)->isGreaterThan(0);
//drop update access on item_devicesimcard
$query = "UPDATE glpi_profilerights SET rights = 2 WHERE profiles_id = 4 AND name = 'devicesimcard_pinpuk'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 2], [
'profiles_id' => 4,
'name' => 'devicesimcard_pinpuk'
]
);
// Profile changed then login
$backupSessionToken = $this->session_token;
......@@ -1235,8 +1240,13 @@ abstract class APIBaseClass extends \atoum {
$limitedSessionToken = $this->session_token;
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 3 WHERE profiles_id = 4 AND name = 'devicesimcard_pinpuk'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 3], [
'profiles_id' => 4,
'name' => 'devicesimcard_pinpuk'
]
);
$this->session_token = $backupSessionToken;
// test getItem does not disclose sensitive fields when READ disabled
......
......@@ -496,7 +496,11 @@ class AuthLDAP extends DbTestCase {
$this->string($user->fields['name'])->isIdenticalTo('testecuador');
global $DB;
$DB->queryOrDie("UPDATE glpi_authldaps SET sync_field=NULL WHERE id=" . $ldap->getID());
$DB->updateOrDie(
'glpi_authldaps',
['sync_field' => null],
['id' => $ldap->getID()]
);
}
/**
......@@ -594,7 +598,11 @@ class AuthLDAP extends DbTestCase {
$this->resource($auth->ldap_connection)->isOfType('ldap link');
global $DB;
$DB->queryOrDie("UPDATE glpi_authldaps SET sync_field=NULL WHERE id=" . $ldap->getID());
$DB->updateOrDie(
'glpi_authldaps',
['sync_field' => null],
['id' => $ldap->getID()]
);
}
/**
......
......@@ -322,8 +322,7 @@ class AuthLDAP extends DbTestCase {
$this->addLdapServers();
$this->boolean(\AuthLDAP::useAuthLdap())->isTrue();
$sql = "UPDATE `glpi_authldaps` SET `is_active`='0'";
$DB->query($sql);
$DB->update('glpi_authldaps', ['is_active' => 0], [true]);
$this->boolean(\AuthLDAP::useAuthLdap())->isFalse();
}
......@@ -332,8 +331,7 @@ class AuthLDAP extends DbTestCase {
$this->addLdapServers();
$this->integer((int)\AuthLDAP::getNumberOfServers())->isIdenticalTo(3);
$sql = "UPDATE `glpi_authldaps` SET `is_active`='0'";
$DB->query($sql);
$DB->update('glpi_authldaps', ['is_active' => 0], [true]);
$this->integer((int)\AuthLDAP::getNumberOfServers())->isIdenticalTo(0);
}
......
......@@ -810,7 +810,7 @@ class DbUtils extends DbTestCase {
public function testGetAncestorsOf() {
global $DB;
//ensure db cache is unset
$DB->query('UPDATE glpi_entities SET ancestors_cache=NULL');
$DB->update('glpi_entities', ['ancestors_cache' => null], [true]);
$this->runGetAncestorsOf();
$this->integer(countElementsInTable('glpi_entities', ['ancestors_cache' => 'NULL']))->isIdenticalTo(0);
......@@ -961,7 +961,7 @@ class DbUtils extends DbTestCase {
public function testGetSonsOf() {
global $DB;
//ensure db cache is unset
$DB->query('UPDATE glpi_entities SET sons_cache=NULL');
$DB->update('glpi_entities', ['sons_cache' => null], [true]);
$this->runGetSonsOf();
$this->integer($this->testedInstance->countElementsInTable('glpi_entities', ['sons_cache' => 'NULL']))->isIdenticalTo(0);
......
......@@ -218,8 +218,13 @@ class Entity extends DbTestCase {
public function testChangeEntityParent() {
global $DB;
//ensure db cache are unset
$DB->query('UPDATE glpi_entities SET ancestors_cache=NULL');
$DB->query('UPDATE glpi_entities SET sons_cache=NULL');
$DB->update(
'glpi_entities', [
'ancestors_cache' => null,
'sons_cache' => null
],
[true]
);
$this->runChangeEntityParent();
//reset cache (checking for expected defaults) then run a second time: db cache must be set
$this->checkParentsSonsAreReset();
......
......@@ -100,14 +100,24 @@ class Item_DeviceSimcard extends DbTestCase {
public function testDenyPinPukUpdate() {
global $DB;
//drop update access on item_devicesimcard
$query = "UPDATE glpi_profilerights SET rights = 1 WHERE profiles_id = 4 AND name = 'devicesimcard_pinpuk'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 1], [
'profiles_id' => 4,
'name' => 'devicesimcard_pinpuk'
], [true]
);
// Profile changed then login
$this->login();
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 3 WHERE profiles_id = 4 AND name = 'devicesimcard_pinpuk'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 3], [
'profiles_id' => 4,
'name' => 'devicesimcard_pinpuk'
], [true]
);
$obj = new \Item_DeviceSimcard();
......@@ -172,4 +182,4 @@ class Item_DeviceSimcard extends DbTestCase {
$this->boolean($obj->delete($in))->isTrue();
}
}
\ No newline at end of file
}
......@@ -765,7 +765,10 @@ class RuleCriteria extends DbTestCase {
private function updateDateMod($rules_id, $time) {
global $DB;
$query = "UPDATE `glpi_rules` SET `date_mod`='$time' WHERE `id`='$rules_id'";
$DB->query($query);
$DB->update(
'glpi_rules',
['date_mod' => $time],
['id' => $rules_id]
);
}
}
......@@ -50,15 +50,25 @@ class SavedSearch extends DbTestCase {
//add public saved searches read right for normal profile
global $DB;
$query = "UPDATE glpi_profilerights SET rights = 1 WHERE profiles_id = 2 AND name = 'bookmark_public'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 1], [
'profiles_id' => 2,
'name' => 'bookmark_public'
]
);
//ACLs have changed: login again.
$this->login('normal', 'normal');
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 0 WHERE profiles_id = 2 AND name = 'bookmark_public'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 0], [
'profiles_id' => 2,
'name' => 'bookmark_public'
]
);
$this->string(\SavedSearch::addVisibilityRestrict())
->isIdenticalTo('(glpi_savedsearches.is_private=1 AND glpi_savedsearches.users_id=5 OR glpi_savedsearches.is_private=0)');
......
......@@ -528,14 +528,24 @@ class Ticket extends DbTestCase {
//drop update ticket right from tech profile
global $DB;
$query = "UPDATE glpi_profilerights SET rights = 168965 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168965], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
//ACLs have changed: login again.
$this->boolean((boolean)$auth->Login('tech', 'tech', true))->isTrue();
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 168967 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168967], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
$this->boolean((boolean)$ticket->getFromDB($ticket->getID()))->isTrue();
$this->boolean((boolean)$ticket->canAdminActors())->isFalse();
......@@ -630,14 +640,24 @@ class Ticket extends DbTestCase {
//drop update ticket right from tech profile
global $DB;
$query = "UPDATE glpi_profilerights SET rights = 168965 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168965], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
//ACLs have changed: login again.
$this->boolean((boolean)$auth->Login('tech', 'tech', true))->isTrue();
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 168967 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168967], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
$this->boolean((boolean)$ticket->getFromDB($ticket->getID()))->isTrue();
$this->boolean((boolean)$ticket->canAdminActors())->isFalse();
......@@ -1078,8 +1098,13 @@ class Ticket extends DbTestCase {
global $DB;
// set new rights
$DB->query("UPDATE glpi_profilerights SET rights = $rights
WHERE profiles_id = 6 AND name = 'ticket'");
$DB->update(
'glpi_profilerights',
['rights' => $rights], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
//ACLs have changed: login again.
$auth = new \Auth();
......@@ -1087,8 +1112,13 @@ class Ticket extends DbTestCase {
if ($rights != 168967) {
//reset rights. Done here so ACLs are reset even if tests fails.
$DB->query("UPDATE glpi_profilerights SET rights = 168967
WHERE profiles_id = 6 AND name = 'ticket'");
$DB->update(
'glpi_profilerights',
['rights' => 168967], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
}
}
......@@ -1129,14 +1159,25 @@ class Ticket extends DbTestCase {
//Add priority right from tech profile
global $DB;
$query = "UPDATE glpi_profilerights SET rights = 234503 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 234503], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
//ACLs have changed: login again.
$this->boolean((boolean)$auth->Login('tech', 'tech', true))->isTrue();
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 168967 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168967], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
$this->boolean((boolean)\Session::haveRight(\Ticket::$rightname, \Ticket::CHANGEPRIORITY))->isTrue();
//check output with changed ACLs
......@@ -1197,14 +1238,25 @@ class Ticket extends DbTestCase {
//Drop being in charge from tech profile
global $DB;
$query = "UPDATE glpi_profilerights SET rights = 136199 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 136199], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
//ACLs have changed: login again.
$this->boolean((boolean)$auth->Login('tech', 'tech', true))->isTrue();
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 168967 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168967], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
$this->boolean((boolean)$ticket->canAssign())->isFalse();
$this->boolean((boolean)$ticket->canAssignToMe())->isFalse();
......@@ -1228,14 +1280,25 @@ class Ticket extends DbTestCase {
);
//Add assign in charge from tech profile
$query = "UPDATE glpi_profilerights SET rights = 144391 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 144391], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
//ACLs have changed: login again.
$this->boolean((boolean)$auth->Login('tech', 'tech', true))->isTrue();
//reset rights. Done here so ACLs are reset even if tests fails.
$query = "UPDATE glpi_profilerights SET rights = 168967 WHERE profiles_id = 6 AND name = 'ticket'";
$DB->query($query);
$DB->update(
'glpi_profilerights',
['rights' => 168967], [
'profiles_id' => 6,
'name' => 'ticket'
]
);
$this->boolean((boolean)$ticket->canAssign())->isTrue();
$this->boolean((boolean)$ticket->canAssignToMe())->isFalse();
......
......@@ -83,7 +83,11 @@ if ($GLPI_CACHE) {
echo "+ Cache: disabled\n";
}
echo "+ Clear sons cache\n";
$DB->query("UPDATE glpi_entities SET sons_cache=NULL");
$DB->update(
'glpi_entities',
['sons_cache' => null],
[true]
);
$tps = microtime(true);
echo "+ Run with empty cache\n";
......@@ -160,4 +164,4 @@ echo "+ Done\n";
> time: 0.1560
+ Done
*/
\ No newline at end of file
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment