diff options
author | Robin Appelman <robin@icewind.nl> | 2022-04-28 15:15:39 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2022-06-02 15:52:08 +0200 |
commit | 813b50ed428a8bc36817d19c84444e96dbe3b668 (patch) | |
tree | 2b004925bc92113fbd14637f89f987e35d8f551f /lib/private | |
parent | ca45e6a0640cc1bc758da38629d6f5741df511de (diff) | |
download | nextcloud-server-813b50ed428a8bc36817d19c84444e96dbe3b668.tar.gz nextcloud-server-813b50ed428a8bc36817d19c84444e96dbe3b668.zip |
make expression build return IQueryFunction instead of string
this allows passing the expressions to further expressions without them being escaped as column names
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private')
6 files changed, 92 insertions, 85 deletions
diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php index ae4f19f5d18..333984bde71 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php @@ -114,12 +114,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function comparison($x, string $operator, $y, $type = null): string { + public function comparison($x, string $operator, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->comparison($x, $operator, $y); + return new QueryFunction($this->expressionBuilder->comparison($x, $operator, $y)); } /** @@ -137,12 +137,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function eq($x, $y, $type = null): string { + public function eq($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->eq($x, $y); + return new QueryFunction($this->expressionBuilder->eq($x, $y)); } /** @@ -159,12 +159,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function neq($x, $y, $type = null): string { + public function neq($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->neq($x, $y); + return new QueryFunction($this->expressionBuilder->neq($x, $y)); } /** @@ -181,12 +181,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function lt($x, $y, $type = null): string { + public function lt($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->lt($x, $y); + return new QueryFunction($this->expressionBuilder->lt($x, $y)); } /** @@ -203,12 +203,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function lte($x, $y, $type = null): string { + public function lte($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->lte($x, $y); + return new QueryFunction($this->expressionBuilder->lte($x, $y)); } /** @@ -225,12 +225,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function gt($x, $y, $type = null): string { + public function gt($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->gt($x, $y); + return new QueryFunction($this->expressionBuilder->gt($x, $y)); } /** @@ -247,12 +247,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function gte($x, $y, $type = null): string { + public function gte($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->gte($x, $y); + return new QueryFunction($this->expressionBuilder->gte($x, $y)); } /** @@ -260,11 +260,11 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be restricted by IS NULL. * - * @return string + * @return IQueryFunction */ - public function isNull($x): string { + public function isNull($x): IQueryFunction { $x = $this->helper->quoteColumnName($x); - return $this->expressionBuilder->isNull($x); + return new QueryFunction($this->expressionBuilder->isNull($x)); } /** @@ -272,11 +272,11 @@ class ExpressionBuilder implements IExpressionBuilder { * * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be restricted by IS NOT NULL. * - * @return string + * @return IQueryFunction */ - public function isNotNull($x): string { + public function isNotNull($x): IQueryFunction { $x = $this->helper->quoteColumnName($x); - return $this->expressionBuilder->isNotNull($x); + return new QueryFunction($this->expressionBuilder->isNotNull($x)); } /** @@ -287,12 +287,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function like($x, $y, $type = null): string { + public function like($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->like($x, $y); + return new QueryFunction($this->expressionBuilder->like($x, $y)); } /** @@ -303,11 +303,11 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction * @since 9.0.0 */ - public function iLike($x, $y, $type = null): string { - return $this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y)); + public function iLike($x, $y, $type = null): IQueryFunction { + return new QueryFunction($this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y))); } /** @@ -318,12 +318,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function notLike($x, $y, $type = null): string { + public function notLike($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->notLike($x, $y); + return new QueryFunction($this->expressionBuilder->notLike($x, $y)); } /** @@ -334,12 +334,12 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function in($x, $y, $type = null): string { + public function in($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnNames($y); - return $this->expressionBuilder->in($x, $y); + return new QueryFunction($this->expressionBuilder->in($x, $y)); } /** @@ -350,34 +350,34 @@ class ExpressionBuilder implements IExpressionBuilder { * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants * required when comparing text fields for oci compatibility * - * @return string + * @return IQueryFunction */ - public function notIn($x, $y, $type = null): string { + public function notIn($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnNames($y); - return $this->expressionBuilder->notIn($x, $y); + return new QueryFunction($this->expressionBuilder->notIn($x, $y)); } /** * Creates a $x = '' statement, because Oracle needs a different check * * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison. - * @return string + * @return IQueryFunction * @since 13.0.0 */ - public function emptyString($x): string { - return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR)); + public function emptyString($x): IQueryFunction { + return new QueryFunction($this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR))); } /** * Creates a `$x <> ''` statement, because Oracle needs a different check * * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison. - * @return string + * @return IQueryFunction * @since 13.0.0 */ - public function nonEmptyString($x): string { - return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR)); + public function nonEmptyString($x): IQueryFunction { + return new QueryFunction($this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR))); } /** diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/MySqlExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/MySqlExpressionBuilder.php index 3bb54d4b26e..74209d0c3da 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/MySqlExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/MySqlExpressionBuilder.php @@ -49,10 +49,10 @@ class MySqlExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc */ - public function iLike($x, $y, $type = null): string { + public function iLike($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->collation . ' LIKE', $y); + return new QueryFunction($this->expressionBuilder->comparison($x, ' COLLATE ' . $this->collation . ' LIKE', $y)); } /** diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php index f9b58d7d8ed..20d68b30b33 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php @@ -49,101 +49,101 @@ class OCIExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc */ - public function comparison($x, string $operator, $y, $type = null): string { + public function comparison($x, string $operator, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->comparison($x, $operator, $y); + return new QueryFunction($this->expressionBuilder->comparison($x, $operator, $y)); } /** * @inheritdoc */ - public function eq($x, $y, $type = null): string { + public function eq($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->eq($x, $y); + return new QueryFunction($this->expressionBuilder->eq($x, $y)); } /** * @inheritdoc */ - public function neq($x, $y, $type = null): string { + public function neq($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->neq($x, $y); + return new QueryFunction($this->expressionBuilder->neq($x, $y)); } /** * @inheritdoc */ - public function lt($x, $y, $type = null): string { + public function lt($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->lt($x, $y); + return new QueryFunction($this->expressionBuilder->lt($x, $y)); } /** * @inheritdoc */ - public function lte($x, $y, $type = null): string { + public function lte($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->lte($x, $y); + return new QueryFunction($this->expressionBuilder->lte($x, $y)); } /** * @inheritdoc */ - public function gt($x, $y, $type = null): string { + public function gt($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->gt($x, $y); + return new QueryFunction($this->expressionBuilder->gt($x, $y)); } /** * @inheritdoc */ - public function gte($x, $y, $type = null): string { + public function gte($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->gte($x, $y); + return new QueryFunction($this->expressionBuilder->gte($x, $y)); } /** * @inheritdoc */ - public function in($x, $y, $type = null): string { + public function in($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->in($x, $y); + return new QueryFunction($this->expressionBuilder->in($x, $y)); } /** * @inheritdoc */ - public function notIn($x, $y, $type = null): string { + public function notIn($x, $y, $type = null): IQueryFunction { $x = $this->prepareColumn($x, $type); $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->notIn($x, $y); + return new QueryFunction($this->expressionBuilder->notIn($x, $y)); } /** * Creates a $x = '' statement, because Oracle needs a different check * * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison. - * @return string + * @return IQueryFunction * @since 13.0.0 */ - public function emptyString($x): string { + public function emptyString($x): IQueryFunction { return $this->isNull($x); } @@ -151,10 +151,10 @@ class OCIExpressionBuilder extends ExpressionBuilder { * Creates a `$x <> ''` statement, because Oracle needs a different check * * @param string|ILiteral|IParameter|IQueryFunction $x The field in string format to be inspected by the comparison. - * @return string + * @return IQueryFunction * @since 13.0.0 */ - public function nonEmptyString($x): string { + public function nonEmptyString($x): IQueryFunction { return $this->isNotNull($x); } @@ -182,14 +182,14 @@ class OCIExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc */ - public function like($x, $y, $type = null): string { - return parent::like($x, $y, $type) . " ESCAPE '\\'"; + public function like($x, $y, $type = null): IQueryFunction { + return new QueryFunction(parent::like($x, $y, $type) . " ESCAPE '\\'"); } /** * @inheritdoc */ - public function iLike($x, $y, $type = null): string { + public function iLike($x, $y, $type = null): IQueryFunction { return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y)); } } diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/PgSqlExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/PgSqlExpressionBuilder.php index 0fba5363a28..cbebe97ae87 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/PgSqlExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/PgSqlExpressionBuilder.php @@ -52,9 +52,9 @@ class PgSqlExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc */ - public function iLike($x, $y, $type = null): string { + public function iLike($x, $y, $type = null): IQueryFunction { $x = $this->helper->quoteColumnName($x); $y = $this->helper->quoteColumnName($y); - return $this->expressionBuilder->comparison($x, 'ILIKE', $y); + return new QueryFunction($this->expressionBuilder->comparison($x, 'ILIKE', $y)); } } diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php index 289aa09b003..5425138fa6c 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php @@ -23,15 +23,18 @@ */ namespace OC\DB\QueryBuilder\ExpressionBuilder; +use OC\DB\QueryBuilder\QueryFunction; +use OCP\DB\QueryBuilder\IQueryFunction; + class SqliteExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc */ - public function like($x, $y, $type = null): string { - return parent::like($x, $y, $type) . " ESCAPE '\\'"; + public function like($x, $y, $type = null): IQueryFunction { + return new QueryFunction(parent::like($x, $y, $type) . " ESCAPE '\\'"); } - public function iLike($x, $y, $type = null): string { - return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y), $type); + public function iLike($x, $y, $type = null): IQueryFunction { + return new QueryFunction($this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y), $type)); } } diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php index 71dd18fd68b..8fc66755a99 100644 --- a/lib/private/DB/QueryBuilder/QueryBuilder.php +++ b/lib/private/DB/QueryBuilder/QueryBuilder.php @@ -715,11 +715,12 @@ class QueryBuilder implements IQueryBuilder { * @param string $fromAlias The alias that points to a from clause. * @param string $join The table name to join. * @param string $alias The alias of the join table. - * @param string|ICompositeExpression|null $condition The condition for the join. + * @param string|IQueryFunction|ICompositeExpression|null $condition The condition for the join. * * @return $this This QueryBuilder instance. */ public function join($fromAlias, $join, $alias, $condition = null) { + $condition = $condition !== null ? (string)$condition : null; $this->queryBuilder->join( $this->quoteAlias($fromAlias), $this->getTableName($join), @@ -743,11 +744,12 @@ class QueryBuilder implements IQueryBuilder { * @param string $fromAlias The alias that points to a from clause. * @param string $join The table name to join. * @param string $alias The alias of the join table. - * @param string|ICompositeExpression|null $condition The condition for the join. + * @param string|IQueryFunction|ICompositeExpression|null $condition The condition for the join. * * @return $this This QueryBuilder instance. */ public function innerJoin($fromAlias, $join, $alias, $condition = null) { + $condition = $condition !== null ? (string)$condition : null; $this->queryBuilder->innerJoin( $this->quoteAlias($fromAlias), $this->getTableName($join), @@ -771,11 +773,12 @@ class QueryBuilder implements IQueryBuilder { * @param string $fromAlias The alias that points to a from clause. * @param string $join The table name to join. * @param string $alias The alias of the join table. - * @param string|ICompositeExpression|null $condition The condition for the join. + * @param string|IQueryFunction|ICompositeExpression|null $condition The condition for the join. * * @return $this This QueryBuilder instance. */ public function leftJoin($fromAlias, $join, $alias, $condition = null) { + $condition = $condition !== null ? (string)$condition : null; $this->queryBuilder->leftJoin( $this->quoteAlias($fromAlias), $this->getTableName($join), @@ -799,11 +802,12 @@ class QueryBuilder implements IQueryBuilder { * @param string $fromAlias The alias that points to a from clause. * @param string $join The table name to join. * @param string $alias The alias of the join table. - * @param string|ICompositeExpression|null $condition The condition for the join. + * @param string|IQueryFunction|ICompositeExpression|null $condition The condition for the join. * * @return $this This QueryBuilder instance. */ public function rightJoin($fromAlias, $join, $alias, $condition = null) { + $condition = $condition !== null ? (string)$condition : null; $this->queryBuilder->rightJoin( $this->quoteAlias($fromAlias), $this->getTableName($join), |