diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index eef46735f509a..92afdb6307645 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1355,6 +1355,7 @@ '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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 94bbaaa0f43a9..d32fac449b9fa 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1388,6 +1388,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', diff --git a/lib/private/DB/PreparedStatement.php b/lib/private/DB/PreparedStatement.php index 98d12842a7eec..f88a4ae7f6bd2 100644 --- a/lib/private/DB/PreparedStatement.php +++ b/lib/private/DB/PreparedStatement.php @@ -9,7 +9,6 @@ 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 +25,8 @@ * methods without much magic. */ class PreparedStatement implements IPreparedStatement { + use TDoctrineParameterTypeMap; + /** @var Statement */ private $statement; @@ -58,12 +59,12 @@ public function fetchOne() { 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')]); } diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php index 96acb1cd4a64d..1113ea00185bc 100644 --- a/lib/private/DB/QueryBuilder/QueryBuilder.php +++ b/lib/private/DB/QueryBuilder/QueryBuilder.php @@ -22,6 +22,7 @@ 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 Psr\Log\LoggerInterface; class QueryBuilder implements IQueryBuilder { + use TDoctrineParameterTypeMap; + /** @internal */ protected const SELECT = 0; @@ -350,7 +353,7 @@ public function getSQL() { * @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 @@ public function setParameter($key, $value, $type = null) { * @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 @@ public function resetQueryPart($queryPartName) { * @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 @@ public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $ * @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))); } /** diff --git a/lib/private/DB/TDoctrineParameterTypeMap.php b/lib/private/DB/TDoctrineParameterTypeMap.php new file mode 100644 index 0000000000000..e370c3a20c13c --- /dev/null +++ b/lib/private/DB/TDoctrineParameterTypeMap.php @@ -0,0 +1,51 @@ + + */ + +declare(strict_types=1); +/* + * @copyright Copyright (c) 2024 Joas Schilling + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +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) { + null, + IQueryBuilder::PARAM_DATE, + IQueryBuilder::PARAM_JSON => $type, + IQueryBuilder::PARAM_NULL => ParameterType::NULL, + IQueryBuilder::PARAM_BOOL => ParameterType::BOOLEAN, + IQueryBuilder::PARAM_INT => ParameterType::INTEGER, + IQueryBuilder::PARAM_STR => ParameterType::STRING, + IQueryBuilder::PARAM_LOB => ParameterType::LARGE_OBJECT, + IQueryBuilder::PARAM_INT_ARRAY => ArrayParameterType::INTEGER, + IQueryBuilder::PARAM_STR_ARRAY => ArrayParameterType::STRING, + }; + } + +} diff --git a/lib/public/DB/IPreparedStatement.php b/lib/public/DB/IPreparedStatement.php index d77f677b719e1..e2a72bd4a27ed 100644 --- a/lib/public/DB/IPreparedStatement.php +++ b/lib/public/DB/IPreparedStatement.php @@ -9,7 +9,7 @@ namespace OCP\DB; use Doctrine\DBAL\Exception; -use Doctrine\DBAL\ParameterType; +use OCP\DB\QueryBuilder\IQueryBuilder; use PDO; /** @@ -80,7 +80,7 @@ public function fetchOne(); * * @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 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool; * @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 diff --git a/lib/public/DB/QueryBuilder/IQueryBuilder.php b/lib/public/DB/QueryBuilder/IQueryBuilder.php index 7d9d56a1f905a..4d6ab1bd59299 100644 --- a/lib/public/DB/QueryBuilder/IQueryBuilder.php +++ b/lib/public/DB/QueryBuilder/IQueryBuilder.php @@ -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