Skip to content

Commit

Permalink
fix(db): Map parameter types to doctrine's enums
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jul 2, 2024
1 parent e3f5740 commit 7868c3c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions lib/private/DB/PreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +25,8 @@
* methods without much magic.
*/
class PreparedStatement implements IPreparedStatement {
use TDoctrineParameterTypeMap;

/** @var Statement */
private $statement;

Expand Down Expand Up @@ -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')]);
}
Expand Down
10 changes: 7 additions & 3 deletions lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +34,8 @@
use Psr\Log\LoggerInterface;

class QueryBuilder implements IQueryBuilder {
use TDoctrineParameterTypeMap;

/** @internal */
protected const SELECT = 0;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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)));
}

/**
Expand Down
51 changes: 51 additions & 0 deletions lib/private/DB/TDoctrineParameterTypeMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
*/

declare(strict_types=1);
/*
* @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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,
};
}

}
6 changes: 3 additions & 3 deletions lib/public/DB/IPreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace OCP\DB;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use OCP\DB\QueryBuilder\IQueryBuilder;
use PDO;

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/public/DB/QueryBuilder/IQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down

0 comments on commit 7868c3c

Please sign in to comment.