Skip to content

Commit

Permalink
Merge pull request #27852 from nextcloud/bugfix/noid/allow-casting-qu…
Browse files Browse the repository at this point in the history
…ery-functions
  • Loading branch information
juliusknorr authored Jul 13, 2021
2 parents 49554c6 + aae16c2 commit 0031152
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,11 @@ public function literal($input, $type = null): ILiteral {
/**
* 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)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ public function nonEmptyString($x): string {
/**
* 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 . ')');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/public/DB/QueryBuilder/IExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ public function literal($input, $type = null): ILiteral;
/**
* 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
*
* @psalm-taint-sink sql $column
* @psalm-taint-sink sql $type
*/
public function castColumn(string $column, $type): IQueryFunction;
public function castColumn($column, $type): IQueryFunction;
}
34 changes: 34 additions & 0 deletions tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace Test\DB\QueryBuilder;

use OC\DB\QueryBuilder\Literal;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Test\TestCase;

/**
Expand Down Expand Up @@ -109,4 +110,37 @@ public function testILike($param1, $param2, $match) {
$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();
}
}

0 comments on commit 0031152

Please sign in to comment.