fix(db): Map parameter types to doctrine's enums

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2024-07-02 14:50:22 +02:00
parent 98b6c7c48a
commit 6b2f1367cd
No known key found for this signature in database
GPG key ID: 74434EFE0D2E2205
7 changed files with 55 additions and 18 deletions

View file

@ -1361,6 +1361,7 @@ return array(
'OC\\DB\\ResultAdapter' => $baseDir . '/lib/private/DB/ResultAdapter.php',
'OC\\DB\\SQLiteMigrator' => $baseDir . '/lib/private/DB/SQLiteMigrator.php',
'OC\\DB\\SchemaWrapper' => $baseDir . '/lib/private/DB/SchemaWrapper.php',
'OC\\DB\\TDoctrineParameterTypeMap' => $baseDir . '/lib/private/DB/TDoctrineParameterTypeMap.php',
'OC\\Dashboard\\Manager' => $baseDir . '/lib/private/Dashboard/Manager.php',
'OC\\DatabaseException' => $baseDir . '/lib/private/DatabaseException.php',
'OC\\DatabaseSetupException' => $baseDir . '/lib/private/DatabaseSetupException.php',

View file

@ -1394,6 +1394,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\DB\\ResultAdapter' => __DIR__ . '/../../..' . '/lib/private/DB/ResultAdapter.php',
'OC\\DB\\SQLiteMigrator' => __DIR__ . '/../../..' . '/lib/private/DB/SQLiteMigrator.php',
'OC\\DB\\SchemaWrapper' => __DIR__ . '/../../..' . '/lib/private/DB/SchemaWrapper.php',
'OC\\DB\\TDoctrineParameterTypeMap' => __DIR__ . '/../../..' . '/lib/private/DB/TDoctrineParameterTypeMap.php',
'OC\\Dashboard\\Manager' => __DIR__ . '/../../..' . '/lib/private/Dashboard/Manager.php',
'OC\\DatabaseException' => __DIR__ . '/../../..' . '/lib/private/DatabaseException.php',
'OC\\DatabaseSetupException' => __DIR__ . '/../../..' . '/lib/private/DatabaseSetupException.php',

View file

@ -8,8 +8,6 @@ declare(strict_types=1);
*/
namespace OC\DB;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Statement;
use OCP\DB\IPreparedStatement;
use OCP\DB\IResult;
@ -26,6 +24,8 @@ use PDO;
* methods without much magic.
*/
class PreparedStatement implements IPreparedStatement {
use TDoctrineParameterTypeMap;
/** @var Statement */
private $statement;
@ -58,12 +58,12 @@ class PreparedStatement implements IPreparedStatement {
return $this->getResult()->fetchOne();
}
public function bindValue($param, $value, $type = ParameterType::STRING): bool {
$this->statement->bindValue($param, $value, $type);
public function bindValue($param, $value, $type = IQueryBuilder::PARAM_STR): bool {
$this->statement->bindValue($param, $value, $this->convertParameterTypeToDoctrine($type));
return true;
}
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool {
public function bindParam($param, &$variable, $type = IQueryBuilder::PARAM_STR, $length = null): bool {
if ($type !== IQueryBuilder::PARAM_STR) {
\OC::$server->getLogger()->warning('PreparedStatement::bindParam() is no longer supported. Use bindValue() instead.', ['exception' => new \BadMethodCallException('bindParam() is no longer supported')]);
}

View file

@ -22,6 +22,7 @@ use OC\DB\QueryBuilder\FunctionBuilder\OCIFunctionBuilder;
use OC\DB\QueryBuilder\FunctionBuilder\PgSqlFunctionBuilder;
use OC\DB\QueryBuilder\FunctionBuilder\SqliteFunctionBuilder;
use OC\DB\ResultAdapter;
use OC\DB\TDoctrineParameterTypeMap;
use OC\SystemConfig;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\ICompositeExpression;
@ -33,6 +34,8 @@ use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
class QueryBuilder implements IQueryBuilder {
use TDoctrineParameterTypeMap;
/** @internal */
protected const SELECT = 0;
@ -350,7 +353,7 @@ class QueryBuilder implements IQueryBuilder {
* @return $this This QueryBuilder instance.
*/
public function setParameter($key, $value, $type = null) {
$this->queryBuilder->setParameter($key, $value, $type);
$this->queryBuilder->setParameter($key, $value, $this->convertParameterTypeToDoctrine($type));
return $this;
}
@ -375,6 +378,7 @@ class QueryBuilder implements IQueryBuilder {
* @return $this This QueryBuilder instance.
*/
public function setParameters(array $params, array $types = []) {
$types = array_map($this->convertParameterTypeToDoctrine(...), $types);
$this->queryBuilder->setParameters($params, $types);
return $this;
@ -1223,7 +1227,7 @@ class QueryBuilder implements IQueryBuilder {
* @return IParameter the placeholder name used.
*/
public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) {
return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder));
return new Parameter($this->queryBuilder->createNamedParameter($value, $this->convertParameterTypeToDoctrine($type), $placeHolder));
}
/**
@ -1249,7 +1253,7 @@ class QueryBuilder implements IQueryBuilder {
* @return IParameter
*/
public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) {
return new Parameter($this->queryBuilder->createPositionalParameter($value, $type));
return new Parameter($this->queryBuilder->createPositionalParameter($value, $this->convertParameterTypeToDoctrine($type)));
}
/**

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\DB;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;
use OCP\DB\QueryBuilder\IQueryBuilder;
trait TDoctrineParameterTypeMap {
protected function convertParameterTypeToDoctrine(string|int|null $type): ArrayParameterType|ParameterType|string {
return match($type) {
IQueryBuilder::PARAM_DATE,
IQueryBuilder::PARAM_JSON => $type,
IQueryBuilder::PARAM_NULL => ParameterType::NULL,
IQueryBuilder::PARAM_BOOL => ParameterType::BOOLEAN,
IQueryBuilder::PARAM_INT => ParameterType::INTEGER,
null,
IQueryBuilder::PARAM_STR => ParameterType::STRING,
IQueryBuilder::PARAM_LOB => ParameterType::LARGE_OBJECT,
IQueryBuilder::PARAM_INT_ARRAY => ArrayParameterType::INTEGER,
IQueryBuilder::PARAM_STR_ARRAY => ArrayParameterType::STRING,
};
}
}

View file

@ -9,7 +9,7 @@ declare(strict_types=1);
namespace OCP\DB;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use OCP\DB\QueryBuilder\IQueryBuilder;
use PDO;
/**
@ -80,7 +80,7 @@ interface IPreparedStatement {
*
* @since 21.0.0
*/
public function bindValue($param, $value, $type = ParameterType::STRING): bool;
public function bindValue($param, $value, $type = IQueryBuilder::PARAM_STR): bool;
/**
* @param int|string $param
@ -95,7 +95,7 @@ interface IPreparedStatement {
* @since 21.0.0
* @deprecated 30.0.0 Use {@see self::bindValue()} instead
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool;
public function bindParam($param, &$variable, $type = IQueryBuilder::PARAM_STR, $length = null): bool;
/**
* @param mixed[]|null $params

View file

@ -23,23 +23,23 @@ interface IQueryBuilder {
/**
* @since 9.0.0
*/
public const PARAM_NULL = ParameterType::NULL;
public const PARAM_NULL = 0; // Translates to ParameterType::NULL;
/**
* @since 9.0.0
*/
public const PARAM_BOOL = ParameterType::BOOLEAN;
public const PARAM_BOOL = 4; // Translates to ParameterType::BOOLEAN;
/**
* @since 9.0.0
*/
public const PARAM_INT = ParameterType::INTEGER;
public const PARAM_INT = 1; // Translates to ParameterType::INTEGER;
/**
* @since 9.0.0
*/
public const PARAM_STR = ParameterType::STRING;
public const PARAM_STR = 2; // Translates to ParameterType::STRING;
/**
* @since 9.0.0
*/
public const PARAM_LOB = ParameterType::LARGE_OBJECT;
public const PARAM_LOB = 3; // Translates to ParameterType::LARGE_OBJECT;
/**
* @since 9.0.0
*/
@ -53,11 +53,11 @@ interface IQueryBuilder {
/**
* @since 9.0.0
*/
public const PARAM_INT_ARRAY = ArrayParameterType::INTEGER;
public const PARAM_INT_ARRAY = 100; // Translates to ArrayParameterType::INTEGER;
/**
* @since 9.0.0
*/
public const PARAM_STR_ARRAY = ArrayParameterType::STRING;
public const PARAM_STR_ARRAY = 101; // Translates to ArrayParameterType::STRING;
/**
* @since 24.0.0 Indicates how many rows can be deleted at once with MySQL