Skip to content

Commit

Permalink
Updated cakephp query builder to v5 [SLE-47]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Oct 18, 2023
1 parent 68add78 commit 6641288
Show file tree
Hide file tree
Showing 33 changed files with 153 additions and 118 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ jobs:
- name: Set MySQL timezone to swiss time
run: mysql -uroot -proot -e "SET GLOBAL time_zone = '+01:00';"

- name: Increase max allowed packet size
run: mysql -uroot -proot -e "SET GLOBAL max_allowed_packet=1073741824;"

- name: Increase max connections
run: mysql -uroot -proot -e "SET GLOBAL max_connections = 4096;"

- name: Create database
run: mysql -uroot -proot -e 'CREATE DATABASE IF NOT EXISTS ${{ matrix.test-database }} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"php-di/php-di": "^7.0",
"ext-pdo": "*",
"ext-json": "*",
"cakephp/database": "4.4.17",
"cakephp/database": "^5",
"odan/session": "^6",
"slim/php-view": "^3.0",
"selective/basepath": "^2.0",
Expand Down Expand Up @@ -37,11 +37,11 @@
"require-dev": {
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^9 || ^10",
"selective/test-traits": "^3",
"selective/test-traits": "^4",
"phpstan/phpstan": "^0.12.81",
"jetbrains/phpstorm-attributes": "^1.0",
"friendsofphp/php-cs-fixer": "^3",
"odan/phinx-migrations-generator": "^5"
"odan/phinx-migrations-generator": "^6"
},
"scripts": {
"stan": "phpstan analyse -c phpstan.neon --no-progress --ansi",
Expand Down
9 changes: 6 additions & 3 deletions config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@
return new Connection($settings);
},
PDO::class => function (ContainerInterface $container) {
$connection = $container->get(Connection::class);
$connection->getDriver()->connect();
$driver = $container->get(Connection::class)->getDriver();

return $connection->getDriver()->getConnection();
$class = new ReflectionClass($driver);
$method = $class->getMethod('getPdo');
$method->setAccessible(true);

return $method->invoke($driver);
},
// Used by command line to generate `schema.sql` for integration testing
'DatabaseSqlSchemaGenerator' => function (ContainerInterface $container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function buildWhereArrayWithFilterParams(array $filterParams = ['deleted_
],
];
}
$query = $this->queryFactory->newQuery();
$query = $this->queryFactory->selectQuery();
// Name is always an AND condition
$firstAndLastNameConcat = $query->newExpr()->like(
$query->func()->concat(
Expand Down
12 changes: 6 additions & 6 deletions src/Infrastructure/Authentication/UserRoleFinderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public function __construct(
*
* @param int $userId
*
* @return int
* @throws PersistenceRecordNotFoundException if entry not found
*
* @return int
*/
public function getRoleIdFromUser(int $userId): int
{
$query = $this->queryFactory->newQuery()->select(['user_role_id'])->from('user')->where(
$query = $this->queryFactory->selectQuery()->select(['user_role_id'])->from('user')->where(
['deleted_at IS' => null, 'id' => $userId]
);
$roleId = $query->execute()->fetch('assoc')['user_role_id'];
Expand All @@ -45,7 +45,7 @@ public function getRoleIdFromUser(int $userId): int
*/
public function getUserRoleDataFromUser(int $userId): UserRoleData
{
$query = $this->queryFactory->newQuery()
$query = $this->queryFactory->selectQuery()
->select(['user_role.id', 'user_role.name', 'user_role.hierarchy'])
->from('user')
->leftJoin('user_role', ['user.user_role_id = user_role.id'])
Expand All @@ -72,7 +72,7 @@ public function getUserRoleDataFromUser(int $userId): UserRoleData
*/
public function getUserRolesHierarchies(bool $mappedById = false): array
{
$query = $this->queryFactory->newQuery()->select(['id', 'name', 'hierarchy'])->from('user_role');
$query = $this->queryFactory->selectQuery()->select(['id', 'name', 'hierarchy'])->from('user_role');
$resultRows = $query->execute()->fetchAll('assoc');

$userRoles = [];
Expand All @@ -94,7 +94,7 @@ public function getUserRolesHierarchies(bool $mappedById = false): array
*/
public function findAllUserRolesForDropdown(): array
{
$query = $this->queryFactory->newQuery()->from('user_role');
$query = $this->queryFactory->selectQuery()->from('user_role');

$query->select(['id', 'name']);
$resultRows = $query->execute()->fetchAll('assoc') ?: [];
Expand All @@ -115,7 +115,7 @@ public function findAllUserRolesForDropdown(): array
*/
public function findUserRoleIdByName(string $roleName): ?int
{
$query = $this->queryFactory->newQuery()->from('user_role');
$query = $this->queryFactory->selectQuery()->from('user_role');

$query->select(['id'])->where(['name' => $roleName]);
$resultRow = $query->execute()->fetch('assoc') ?: [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function __construct(
*/
public function insertUserVerification(array $data): int
{
return (int)$this->queryFactory->newInsert($data)->into('user_verification')->execute()->lastInsertId();
return (int)$this->queryFactory->insertQueryWithData($data)->into('user_verification')->execute()->lastInsertId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
*/
public function deleteVerificationToken(int $userId): bool
{
$query = $this->queryFactory->newDelete('user_verification')->where(['user_id' => $userId]);
$query = $this->queryFactory->softDeleteQuery('user_verification')->where(['user_id' => $userId]);

return $query->execute()->rowCount() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
*/
public function findUserVerification(int $id): UserVerificationData
{
$query = $this->queryFactory->newQuery()->select(['*'])->from('user_verification')->where(
$query = $this->queryFactory->selectQuery()->select(['*'])->from('user_verification')->where(
['deleted_at IS' => null, 'id' => $id]
);
$userVerificationRow = $query->execute()->fetch('assoc') ?: [];
Expand All @@ -39,7 +39,7 @@ public function findUserVerification(int $id): UserVerificationData
*/
public function getUserIdFromVerification(int $verificationId): int
{
$query = $this->queryFactory->newQuery()->select(['user_id'])->from('user_verification')->where(
$query = $this->queryFactory->selectQuery()->select(['user_id'])->from('user_verification')->where(
['deleted_at IS' => null, 'id' => $verificationId]
);
// Cake query builder return value is string
Expand All @@ -63,7 +63,7 @@ public function getUserIdFromVerification(int $verificationId): int
*/
public function findUserDetailsByVerificationIncludingDeleted(int $verificationId): UserData
{
$query = $this->queryFactory->newQuery()->from('user_verification');
$query = $this->queryFactory->selectQuery()->from('user_verification');

$query->select(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
*/
public function updateUserVerificationRow(int $verificationId, array $updateRow): bool
{
$query = $this->queryFactory->newQuery();
$query = $this->queryFactory->updateQuery();
$query->update('user_verification')->set($updateRow)->where(
['id' => $verificationId]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Client/ClientCreatorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function __construct(
*/
public function insertClient(array $data): int
{
return (int)$this->queryFactory->newInsert($data)->into('client')->execute()->lastInsertId();
return (int)$this->queryFactory->insertQueryWithData($data)->into('client')->execute()->lastInsertId();
}
}
6 changes: 3 additions & 3 deletions src/Infrastructure/Client/ClientDeleterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
*/
public function deleteClient(int $id): bool
{
$query = $this->queryFactory->newDelete('client')->where(['id' => $id]);
$query = $this->queryFactory->softDeleteQuery('client')->where(['id' => $id]);

return $query->execute()->rowCount() > 0;
}
Expand All @@ -34,7 +34,7 @@ public function deleteClient(int $id): bool
*/
public function hardDeleteClient(int $id): bool
{
$query = $this->queryFactory->newQuery()->delete('client')->where(['id' => $id]);
$query = $this->queryFactory->hardDeleteQuery()->delete('client')->where(['id' => $id]);

return $query->execute()->rowCount() > 0;
}
Expand All @@ -48,7 +48,7 @@ public function hardDeleteClient(int $id): bool
*/
public function deletePostsFromUser(int $userId): bool
{
$query = $this->queryFactory->newDelete('post')->where(['user_id' => $userId]);
$query = $this->queryFactory->softDeleteQuery('post')->where(['user_id' => $userId]);

return $query->execute()->rowCount() > 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Infrastructure/Client/ClientFinderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(
*/
public function findClientsWithResultAggregate(array $whereArray = ['client.deleted_at IS' => null]): array
{
$query = $this->queryFactory->newQuery()->from('client');
$query = $this->queryFactory->selectQuery()->from('client');
$query->select(
$this->clientListAggregateSelectFields
)// Multiple joins doc: https://book.cakephp.org/4/en/orm/query-builder.html#adding-joins
Expand Down Expand Up @@ -97,7 +97,7 @@ public function findClientsWithResultAggregate(array $whereArray = ['client.dele
*/
public function findClientById(string|int $id): ClientData
{
$query = $this->queryFactory->newQuery()->select(['*'])->from('client')->where(
$query = $this->queryFactory->selectQuery()->select(['*'])->from('client')->where(
['deleted_at IS' => null, 'id' => $id]
);
$postRow = $query->execute()->fetch('assoc') ?: [];
Expand All @@ -114,7 +114,7 @@ public function findClientById(string|int $id): ClientData
*/
public function findClientAggregateByIdIncludingDeleted(int $id): ClientResultData
{
$query = $this->queryFactory->newQuery()->from('client');
$query = $this->queryFactory->selectQuery()->from('client');

$query->select($this->clientReadAggregateSelectFields)
->join([
Expand Down Expand Up @@ -150,7 +150,7 @@ public function findClientAggregateByIdIncludingDeleted(int $id): ClientResultDa
*/
public function findAllClientsByUserId(int $userId): array
{
$query = $this->queryFactory->newQuery()->from('client');
$query = $this->queryFactory->selectQuery()->from('client');

$query->select(
$this->clientListAggregateSelectFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
*/
public function findClientStatusByName(ClientStatus $clientStatus): ?int
{
$query = $this->queryFactory->newQuery()->from('client_status');
$query = $this->queryFactory->selectQuery()->from('client_status');

$query->select(['id', 'name'])
->where(
Expand All @@ -43,7 +43,7 @@ public function findClientStatusByName(ClientStatus $clientStatus): ?int
*/
public function findAllClientStatusesMappedByIdName(bool $withoutTranslation = false): array
{
$query = $this->queryFactory->newQuery()->from('client_status');
$query = $this->queryFactory->selectQuery()->from('client_status');

$query->select(['id', 'name'])
->andWhere(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
*/
public function changeClientStatus(int $clientId, array $clientStatusUpdateData): bool
{
$query = $this->queryFactory->newQuery()->update('client_status')->set($clientStatusUpdateData)
$query = $this->queryFactory->updateQuery()->update('client_status')->set($clientStatusUpdateData)
->where(['id' => $clientId]);

return $query->execute()->rowCount() > 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Client/ClientUpdaterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
*/
public function updateClient(array $data, int $clientId): bool
{
$query = $this->queryFactory->newQuery()->update('client')->set($data)->where(['id' => $clientId]);
$query = $this->queryFactory->updateQuery()->update('client')->set($data)->where(['id' => $clientId]);

return $query->execute()->rowCount() > 0;
}
Expand Down
Loading

0 comments on commit 6641288

Please sign in to comment.