Merge pull request #27852 from nextcloud/bugfix/noid/allow-casting-query-functions

pull/27565/head
Julius Härtl 2021-07-13 08:48:49 +07:00 committed by GitHub
commit 0031152de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 7 deletions

@ -425,11 +425,11 @@ class ExpressionBuilder implements IExpressionBuilder {
/**
* Returns a IQueryFunction that casts the column to the given type
*
* @param string $column
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @return IQueryFunction
*/
public function castColumn(string $column, $type): IQueryFunction {
public function castColumn($column, $type): IQueryFunction {
return new QueryFunction(
$this->helper->quoteColumnName($column)
);

@ -161,11 +161,11 @@ class OCIExpressionBuilder extends ExpressionBuilder {
/**
* Returns a IQueryFunction that casts the column to the given type
*
* @param string $column
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @return IQueryFunction
*/
public function castColumn(string $column, $type): IQueryFunction {
public function castColumn($column, $type): IQueryFunction {
if ($type === IQueryBuilder::PARAM_STR) {
$column = $this->helper->quoteColumnName($column);
return new QueryFunction('to_char(' . $column . ')');

@ -33,7 +33,7 @@ class PgSqlExpressionBuilder extends ExpressionBuilder {
/**
* Returns a IQueryFunction that casts the column to the given type
*
* @param string $column
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @return IQueryFunction
*/

@ -431,7 +431,7 @@ interface IExpressionBuilder {
/**
* Returns a IQueryFunction that casts the column to the given type
*
* @param string $column
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @return IQueryFunction
* @since 9.0.0
@ -439,5 +439,5 @@ interface IExpressionBuilder {
* @psalm-taint-sink sql $column
* @psalm-taint-sink sql $type
*/
public function castColumn(string $column, $type): IQueryFunction;
public function castColumn($column, $type): IQueryFunction;
}

@ -22,6 +22,7 @@
namespace Test\DB\QueryBuilder;
use OC\DB\QueryBuilder\Literal;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Test\TestCase;
/**
@ -109,4 +110,37 @@ class ExpressionBuilderDBTest extends TestCase {
$result->closeCursor();
$this->assertEquals($match, $column);
}
public function testCastColumn(): void {
$appId = $this->getUniqueID('testing');
$this->createConfig($appId, '1', '4');
$query = $this->connection->getQueryBuilder();
$query->update('appconfig')
->set('configvalue',
$query->expr()->castColumn(
$query->createFunction(
'(' . $query->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT)
. ' + 1)'
)
, IQueryBuilder::PARAM_STR
)
)
->where($query->expr()->eq('appid', $query->createNamedParameter($appId)))
->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('1')));
$result = $query->executeStatement();
$this->assertEquals(1, $result);
}
protected function createConfig($appId, $key, $value) {
$query = $this->connection->getQueryBuilder();
$query->insert('appconfig')
->values([
'appid' => $query->createNamedParameter($appId),
'configkey' => $query->createNamedParameter((string) $key),
'configvalue' => $query->createNamedParameter((string) $value),
])
->execute();
}
}