Skip to content

Commit

Permalink
Fix #17: Improve performance of SQL queries (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Apr 5, 2022
1 parent bdbf2cf commit d915b99
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
16 changes: 11 additions & 5 deletions src/AssignmentsStorage.php
Expand Up @@ -69,10 +69,8 @@ public function get(string $itemName, string $userId): ?Assignment
->where(['itemName' => $itemName, 'userId' => $userId])
->run()
->fetch();
if (!empty($assignment)) {
return new Assignment($userId, $itemName, (int)$assignment['createdAt']);
}
return null;

return empty($assignment) ? null : new Assignment($userId, $itemName, (int)$assignment['createdAt']);
}

/**
Expand All @@ -97,7 +95,15 @@ public function add(string $itemName, string $userId): void
*/
public function hasItem(string $name): bool
{
return $this->database->select('itemName')->from($this->tableName)->where(['itemName' => $name])->count() > 0;
$result = $this
->database
->select('1')
->from($this->tableName)
->where(['itemName' => $name])
->run()
->fetch();

return $result !== false;
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Command/RbacCycleInit.php
Expand Up @@ -41,12 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var non-empty-string $itemsChildrenTable */
$itemsChildrenTable = $this->config['itemsChildrenTable'] ?? $this->config['itemsTable'] . '_child';
$force = $input->getOption('force');
if ($force === false) {
$reCreate = false;
} else {
$reCreate = true;
}
$reCreate = $input->getOption('force') !== false;
/** @var Table $table */
if ($reCreate && $this->dbal->database()->hasTable($itemsChildrenTable) === true) {
$this->dropTable($itemsChildrenTable);
Expand Down
22 changes: 12 additions & 10 deletions src/ItemsStorage.php
Expand Up @@ -66,10 +66,8 @@ public function getAll(): array
public function get(string $name): ?Item
{
$item = $this->database->select()->from($this->tableName)->where(['name' => $name])->run()->fetch();
if (!empty($item)) {
return $this->populateItem($item);
}
return null;

return empty($item) ? null : $this->populateItem($item);
}

/**
Expand Down Expand Up @@ -183,7 +181,15 @@ public function getChildren(string $name): array
*/
public function hasChildren(string $name): bool
{
return $this->database->select('parent')->from($this->childrenTableName)->where(['parent' => $name])->count() > 0;
$result = $this
->database
->select('1')
->from($this->childrenTableName)
->where(['parent' => $name])
->run()
->fetch();

return $result !== false;
}

/**
Expand Down Expand Up @@ -229,11 +235,7 @@ private function getItemByTypeAndName(string $type, string $name): ?Item
{
$item = $this->database->select()->from($this->tableName)->where(['type' => $type, 'name' => $name])->run()->fetch();

if (empty($item)) {
return null;
}

return $this->populateItem($item);
return empty($item) ? null : $this->populateItem($item);
}

/**
Expand Down
29 changes: 16 additions & 13 deletions tests/ItemsStorageTest.php
Expand Up @@ -32,6 +32,7 @@ public function testGet(): void

$this->assertInstanceOf(Permission::class, $item);
$this->assertSame(Item::TYPE_PERMISSION, $item->getType());
$this->assertSame('Parent 3', $item->getName());
}

public function testGetPermission(): void
Expand All @@ -40,6 +41,7 @@ public function testGetPermission(): void
$permission = $storage->getPermission('Child 1');

$this->assertInstanceOf(Permission::class, $permission);
$this->assertSame('Child 1', $permission->getName());
}

public function testAddChild(): void
Expand Down Expand Up @@ -120,6 +122,7 @@ public function testGetRole(): void

$this->assertNotEmpty($role);
$this->assertInstanceOf(Role::class, $role);
$this->assertSame('Parent 1', $role->getName());
}

public function testAdd(): void
Expand All @@ -142,9 +145,8 @@ public function testRemoveChild(): void
public function testGetAll(): void
{
$storage = $this->getStorage();
$all = $storage->getAll();

$this->assertCount(5, $all);
$this->assertCount(5, $storage->getAll());
}

public function testHasChildren(): void
Expand Down Expand Up @@ -173,36 +175,37 @@ public function testClearRoles(): void

protected function populateDb(): void
{
$time = time();
$items = [
[
'name' => 'Parent 1',
'type' => Item::TYPE_ROLE,
'createdAt' => time(),
'updatedAt' => time(),
'createdAt' => $time,
'updatedAt' => $time,
],
[
'name' => 'Parent 2',
'type' => Item::TYPE_ROLE,
'createdAt' => time(),
'updatedAt' => time(),
'createdAt' => $time,
'updatedAt' => $time,
],
[
'name' => 'Parent 3',
'type' => Item::TYPE_PERMISSION,
'createdAt' => time(),
'updatedAt' => time(),
'createdAt' => $time,
'updatedAt' => $time,
],
[
'name' => 'Child 1',
'type' => Item::TYPE_PERMISSION,
'createdAt' => time(),
'updatedAt' => time(),
'createdAt' => $time,
'updatedAt' => $time,
],
[
'name' => 'Child 2',
'type' => Item::TYPE_ROLE,
'createdAt' => time(),
'updatedAt' => time(),
'createdAt' => $time,
'updatedAt' => $time,
],
];
$items_child = [
Expand Down Expand Up @@ -233,7 +236,7 @@ protected function populateDb(): void
}
}

private function getStorage()
private function getStorage(): ItemsStorage
{
return new ItemsStorage('auth_item', $this->getDbal());
}
Expand Down

0 comments on commit d915b99

Please sign in to comment.