Skip to content

Commit

Permalink
An attempt to fix MySQL 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Sep 22, 2023
1 parent 76a0b99 commit a56d91d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-pdo": "*",
"php": "^8.0",
"yiisoft/db": "^1.0",
"yiisoft/rbac": "dev-165-improve-perfomance"
"yiisoft/rbac": "dev-master"
},
"require-dev": {
"ext-pdo_sqlite": "*",
Expand Down
20 changes: 10 additions & 10 deletions src/ItemTreeTraversal/CteItemTreeTraversal.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,40 @@ public function __construct(

public function getParentRows(string $name): array
{
$baseOuterQuery = (new Query($this->database))->select('item.*')->where(['!=','item.name', $name]);
$baseOuterQuery = (new Query($this->database))->select('item.*')->where(['!=', 'item.name', $name]);

/** @psalm-var RawItem[] */
return $this->getRowsStatement($name, baseOuterQuery: $baseOuterQuery)->queryAll();
return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}

public function getChildrenRows(string $name): array
{
$baseOuterQuery = (new Query($this->database))->select('item.*')->where(['!=','item.name', $name]);
$baseOuterQuery = (new Query($this->database))->select('item.*')->where(['!=', 'item.name', $name]);

/** @psalm-var RawItem[] */
return $this->getRowsStatement($name, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}

public function getChildPermissionRows(string $name): array
{
$baseOuterQuery = (new Query($this->database))
->select('item.*')
->where(['!=','item.name', $name])
->where(['!=', 'item.name', $name])
->andWhere(['item.type' => Item::TYPE_PERMISSION]);

/** @psalm-var RawItem[] */
return $this->getRowsStatement($name, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}

public function getChildRoleRows(string $name): array
{
$baseOuterQuery = (new Query($this->database))
->select('item.*')
->where(['!=','item.name', $name])
->where(['!=', 'item.name', $name])
->andWhere(['item.type' => Item::TYPE_ROLE]);

/** @psalm-var RawItem[] */
return $this->getRowsStatement($name, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}

public function hasChild(string $parentName, string $childName): bool
Expand All @@ -88,7 +88,7 @@ public function hasChild(string $parentName, string $childName): bool
->andWhere(['item.name' => $childName]);
/** @psalm-var array<0, 1>|false $result */
$result = $this
->getRowsStatement($parentName, baseOuterQuery: $baseOuterQuery, areParents: false)
->getRowsCommand($parentName, baseOuterQuery: $baseOuterQuery, areParents: false)
->queryScalar();

return $result !== false;
Expand All @@ -107,7 +107,7 @@ protected function getWithExpression(): string
return 'WITH RECURSIVE';
}

private function getRowsStatement(
private function getRowsCommand(
string $name,
QueryInterface $baseOuterQuery,
bool $areParents = true,
Expand Down
15 changes: 8 additions & 7 deletions src/ItemTreeTraversal/MysqlItemTreeTraversal.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getChildrenRows(string $name): array
$baseOuterQuery = (new Query($this->database))->select([new Expression('item.*')])->distinct();

/** @psalm-var RawItem[] */
return $this->getChildrenRowsStatement($name, baseOuterQuery: $baseOuterQuery)->queryAll();
return $this->getChildrenRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}

public function getChildPermissionRows(string $name): array
Expand All @@ -72,7 +72,7 @@ public function getChildPermissionRows(string $name): array
->where(['item.type' => Item::TYPE_PERMISSION]);

/** @psalm-var RawItem[] */
return $this->getChildrenRowsStatement($name, baseOuterQuery: $baseOuterQuery)->queryAll();
return $this->getChildrenRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}

public function getChildRoleRows(string $name): array
Expand All @@ -83,7 +83,7 @@ public function getChildRoleRows(string $name): array
->where(['item.type' => Item::TYPE_ROLE]);

/** @psalm-var RawItem[] */
return $this->getChildrenRowsStatement($name, baseOuterQuery: $baseOuterQuery)->queryAll();
return $this->getChildrenRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}

public function hasChild(string $parentName, string $childName): bool
Expand All @@ -92,21 +92,22 @@ public function hasChild(string $parentName, string $childName): bool
->select([new Expression('1 AS item_child_exists')])
->andWhere(['item.name' => $childName]);
/** @psalm-var array<0, 1>|false $result */
$result = $this->getChildrenRowsStatement($parentName, baseOuterQuery: $baseOuterQuery)->queryScalar();
$result = $this->getChildrenRowsCommand($parentName, baseOuterQuery: $baseOuterQuery)->queryScalar();

return $result !== false;
}

private function getChildrenRowsStatement(string $name, QueryInterface $baseOuterQuery): CommandInterface
private function getChildrenRowsCommand(string $name, QueryInterface $baseOuterQuery): CommandInterface
{
$fromSql = "SELECT DISTINCT child
FROM (SELECT * FROM $this->childrenTableName ORDER by parent) item_child_sorted,
(SELECT @pv := :name) init
WHERE find_in_set(parent, @pv) AND length(@pv := concat(@pv, ',', child))";
$outerQuery = $baseOuterQuery
->from(new Expression("($fromSql) s"))
->leftJoin($this->tableName . ' AS item', ['item.name' => new Expression('s.child')]);
->leftJoin($this->tableName . ' AS item', ['item.name' => new Expression('s.child')])
->addParams([':name' => $name]);

return $this->database->createCommand($outerQuery->createCommand()->getSql(), [':name' => $name]);
return $this->database->createCommand($outerQuery->createCommand()->getRawSql());
}
}

0 comments on commit a56d91d

Please sign in to comment.