diff --git a/CHANGELOG.md b/CHANGELOG.md index e0de7f6..d229092 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Make the plugin compatible with the Content Security Policy +### Fixed + +- Fewer SQL queries when generating inventory numbers, especially with the massive actions + ## [2.10.1] - 2026-10-05 ### Fixed diff --git a/inc/config.class.php b/inc/config.class.php index 1c1d15a..80f47a9 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -38,6 +38,13 @@ class PluginGeninventorynumberConfig extends CommonDBTM public static $rightname = 'config'; public $dohistory = true; + /** + * Generation switch, read once per request (null: not read yet). + * + * @var mixed + */ + private static $generation_active = null; + /** * Never reset the index * @var int @@ -345,10 +352,28 @@ public static function updateIndex() public static function isGenerationActive() { - $config = new self(); - $config->getFromDB(1); + if (self::$generation_active === null) { + $config = new self(); + $config->getFromDB(1); + self::$generation_active = $config->fields['is_active']; + } + + return self::$generation_active; + } - return $config->fields['is_active']; + /** + * Forget the configuration read during the request, so the next read sees the saved values. + */ + public static function clearCache(): void + { + self::$generation_active = null; + PluginGeninventorynumberConfigField::clearCache(); + } + + public function post_updateItem($history = true) + { + parent::post_updateItem($history); + self::clearCache(); } public static function getIcon() diff --git a/inc/configfield.class.php b/inc/configfield.class.php index dd15024..8ba3355 100644 --- a/inc/configfield.class.php +++ b/inc/configfield.class.php @@ -42,6 +42,15 @@ class PluginGeninventorynumberConfigField extends CommonDBChild public static $itemtype = 'PluginGeninventorynumberConfig'; public static $items_id = 'plugin_geninventorynumber_configs_id'; + /** + * Configuration rows by itemtype, read once per request (null: not read yet). + * Their `index` and `date_last_generated` columns go stale as numbers are generated: + * the index helpers always read them from the database. + * + * @var array>|null + */ + private static ?array $rows_by_itemtype = null; + public static function getTypeName($nb = 0) { return __s('Specific configuration', 'geninventorynumber'); @@ -49,12 +58,42 @@ public static function getTypeName($nb = 0) public static function getConfigFieldByItemType($itemtype) { - $infos = getAllDataFromTable(getTableForItemType(self::class), ['itemtype' => $itemtype]); - if (!empty($infos)) { - return array_pop($infos); - } else { - return $infos; + return self::getRowsByItemtype()[$itemtype] ?? []; + } + + /** + * @return array> + */ + private static function getRowsByItemtype(): array + { + if (self::$rows_by_itemtype === null) { + self::$rows_by_itemtype = []; + foreach (getAllDataFromTable(self::getTable(), ['ORDER' => 'id']) as $row) { + self::$rows_by_itemtype[$row['itemtype']] = $row; + } } + + return self::$rows_by_itemtype; + } + + /** + * Forget the rows read during the request, so the next read sees the saved values. + */ + public static function clearCache(): void + { + self::$rows_by_itemtype = null; + } + + public function post_addItem() + { + parent::post_addItem(); + self::clearCache(); + } + + public function post_purgeItem() + { + parent::post_purgeItem(); + self::clearCache(); } public static function install(Migration $migration) @@ -242,6 +281,7 @@ public function prepareInputForUpdate($input) public function post_updateItem($history = true) { parent::post_updateItem($history); + self::clearCache(); // Check if itemtype is a custom asset and if is_active changed if (isset($this->fields['itemtype']) && in_array('is_active', $this->updates)) { @@ -339,38 +379,15 @@ public static function disableCapacityForAsset(AssetDefinition $definition): boo public static function getEnabledItemTypes() { - /** @var DBmysql $DB */ - global $DB; - - $it = $DB->request([ - 'SELECT' => ['itemtype'], - 'DISTINCT' => true, - 'FROM' => getTableForItemType(self::class), - 'ORDER' => ['itemtype'], - ]); - $types = []; - foreach ($it as $data) { - $types[] = $data['itemtype']; - } + $types = array_keys(self::getRowsByItemtype()); + sort($types, SORT_STRING | SORT_FLAG_CASE); return $types; } public static function isActiveForItemType($itemtype) { - /** @var DBmysql $DB */ - global $DB; - - $it = $DB->request([ - 'SELECT' => ['is_active'], - 'FROM' => getTableForItemType(self::class), - 'WHERE' => ['itemtype' => $itemtype], - ]); - if (count($it) > 0) { - return $it->current()['is_active']; - } - - return false; + return self::getRowsByItemtype()[$itemtype]['is_active'] ?? false; } /** @@ -495,6 +512,7 @@ public static function unregisterNewItemType($itemtype) if (countElementsInTable(getTableForItemType(self::class), ['itemtype' => $itemtype])) { $config = new self(); $is_delete = $config->deleteByCriteria(['itemtype' => $itemtype]); + self::clearCache(); if ($is_delete) { unset($GENINVENTORYNUMBER_TYPES[$itemtype]); } diff --git a/tests/GenInventoryNumberTestCase.php b/tests/GenInventoryNumberTestCase.php index 48d3a51..3968d59 100644 --- a/tests/GenInventoryNumberTestCase.php +++ b/tests/GenInventoryNumberTestCase.php @@ -41,6 +41,14 @@ */ abstract class GenInventoryNumberTestCase extends DbTestCase { + public function setUp(): void + { + parent::setUp(); + + // The configuration is cached for the request, and each test rolls the database back + PluginGeninventorynumberConfig::clearCache(); + } + /** * Initialize plugin configuration * diff --git a/tests/Units/GenerationTest.php b/tests/Units/GenerationTest.php index 46a8050..204a7f0 100644 --- a/tests/Units/GenerationTest.php +++ b/tests/Units/GenerationTest.php @@ -613,4 +613,35 @@ public function testItemLifecycleWithConfigFieldInactive(): void $config = $this->getConfig(); $this->assertEquals(0, $config->fields['index'], "[UPDATE] Index should still not be incremented"); } + + /** + * The configuration is cached for the request: a saved change must be seen by the next + * generation, and the counter must keep increasing between generations. + */ + public function testConfigurationChangeIsSeenInTheSameRequest(): void + { + $this->initConfig(['is_active' => 1]); + $this->setConfigField(Computer::class, [ + 'is_active' => 1, + 'use_index' => 0, + 'template' => 'A-<####>', + 'index' => 0, + ]); + + // Two generations in a row: consecutive numbers + $first = $this->createItem(Computer::class, ['name' => 'cache 1', 'entities_id' => 0], ['otherserial']); + $second = $this->createItem(Computer::class, ['name' => 'cache 2', 'entities_id' => 0], ['otherserial']); + $this->assertSame('A-0001', $first->fields['otherserial']); + $this->assertSame('A-0002', $second->fields['otherserial']); + + // New template: used by the next generation + $this->setConfigField(Computer::class, ['template' => 'B-<####>']); + $third = $this->createItem(Computer::class, ['name' => 'cache 3', 'entities_id' => 0], ['otherserial']); + $this->assertSame('B-0003', $third->fields['otherserial']); + + // Generation switched off: no number anymore + $this->updateConfig(['is_active' => 0]); + $fourth = $this->createItem(Computer::class, ['name' => 'cache 4', 'entities_id' => 0]); + $this->assertEmpty($fourth->fields['otherserial'] ?? ''); + } }