Skip to content

Commit

Permalink
Merge pull request #30471 from nextcloud/enh/cast-char-mysql
Browse files Browse the repository at this point in the history
Explicitly cast char in the query builder
  • Loading branch information
nickvergessen authored Jan 20, 2022
2 parents a9b098e + eebed2c commit de9fe8f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ public function literal($input, $type = IQueryBuilder::PARAM_STR): ILiteral {
*
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @psalm-param IQueryBuilder::PARAM_* $type
* @return IQueryFunction
*/
public function castColumn($column, $type): IQueryFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
namespace OC\DB\QueryBuilder\ExpressionBuilder;

use OC\DB\ConnectionAdapter;
use OC\DB\QueryBuilder\QueryFunction;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;

class MySqlExpressionBuilder extends ExpressionBuilder {

Expand All @@ -52,4 +54,21 @@ public function iLike($x, $y, $type = null): string {
$y = $this->helper->quoteColumnName($y);
return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->collation . ' LIKE', $y);
}

/**
* Returns a IQueryFunction that casts the column to the given type
*
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @psalm-param IQueryBuilder::PARAM_* $type
* @return IQueryFunction
*/
public function castColumn($column, $type): IQueryFunction {
switch ($type) {
case IQueryBuilder::PARAM_STR:
return new QueryFunction('CAST(' . $this->helper->quoteColumnName($column) . ' AS CHAR)');
default:
return parent::castColumn($column, $type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public function nonEmptyString($x): string {
*
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @psalm-param IQueryBuilder::PARAM_* $type
* @return IQueryFunction
*/
public function castColumn($column, $type): IQueryFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PgSqlExpressionBuilder extends ExpressionBuilder {
*
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @psalm-param IQueryBuilder::PARAM_* $type
* @return IQueryFunction
*/
public function castColumn($column, $type): IQueryFunction {
Expand Down
1 change: 1 addition & 0 deletions lib/public/DB/QueryBuilder/IExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ public function literal($input, $type = IQueryBuilder::PARAM_STR): ILiteral;
*
* @param string|IQueryFunction $column
* @param mixed $type One of IQueryBuilder::PARAM_*
* @psalm-param IQueryBuilder::PARAM_* $type
* @return IQueryFunction
* @since 9.0.0
*
Expand Down

0 comments on commit de9fe8f

Please sign in to comment.