diff options
author | Julius Härtl <jus@bitgrid.net> | 2023-12-29 11:52:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-29 11:52:32 +0100 |
commit | 6a63974bf5c75cee556a386e0d112a4dba3fb49d (patch) | |
tree | e98052345a6478b780d0bab769ee8a28450cf421 /lib | |
parent | 77cec80febeb6a0fb6bf93da241e7f0ca492fa58 (diff) | |
parent | e50cb0a30ff36181504b7d5f1b46c2f53c5bc9b5 (diff) | |
download | nextcloud-server-6a63974bf5c75cee556a386e0d112a4dba3fb49d.tar.gz nextcloud-server-6a63974bf5c75cee556a386e0d112a4dba3fb49d.zip |
Merge pull request #26540 from nextcloud/bugfix/noid/sqlite-datetime
Cast datetime columns in sqlite before comparing
Diffstat (limited to 'lib')
3 files changed, 59 insertions, 86 deletions
diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php index ae4f19f5d18..ad45f77e5ea 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php @@ -117,8 +117,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function comparison($x, string $operator, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->comparison($x, $operator, $y); } @@ -140,8 +140,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function eq($x, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->eq($x, $y); } @@ -162,8 +162,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function neq($x, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->neq($x, $y); } @@ -184,8 +184,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function lt($x, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->lt($x, $y); } @@ -206,8 +206,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function lte($x, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->lte($x, $y); } @@ -228,8 +228,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function gt($x, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->gt($x, $y); } @@ -250,8 +250,8 @@ class ExpressionBuilder implements IExpressionBuilder { * @return string */ public function gte($x, $y, $type = null): string { - $x = $this->helper->quoteColumnName($x); - $y = $this->helper->quoteColumnName($y); + $x = $this->prepareColumn($x, $type); + $y = $this->prepareColumn($y, $type); return $this->expressionBuilder->gte($x, $y); } @@ -435,4 +435,13 @@ class ExpressionBuilder implements IExpressionBuilder { $this->helper->quoteColumnName($column) ); } + + /** + * @param mixed $column + * @param mixed|null $type + * @return array|IQueryFunction|string + */ + protected function prepareColumn($column, $type) { + return $this->helper->quoteColumnNames($column); + } } diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php index caeb8009885..8184e369317 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php @@ -39,80 +39,9 @@ class OCIExpressionBuilder extends ExpressionBuilder { protected function prepareColumn($column, $type) { if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { $column = $this->castColumn($column, $type); - } else { - $column = $this->helper->quoteColumnNames($column); } - return $column; - } - - /** - * @inheritdoc - */ - public function comparison($x, string $operator, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - - return $this->expressionBuilder->comparison($x, $operator, $y); - } - - /** - * @inheritdoc - */ - public function eq($x, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - - return $this->expressionBuilder->eq($x, $y); - } - - /** - * @inheritdoc - */ - public function neq($x, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - - return $this->expressionBuilder->neq($x, $y); - } - - /** - * @inheritdoc - */ - public function lt($x, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - - return $this->expressionBuilder->lt($x, $y); - } - - /** - * @inheritdoc - */ - public function lte($x, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - - return $this->expressionBuilder->lte($x, $y); - } - - /** - * @inheritdoc - */ - public function gt($x, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - - return $this->expressionBuilder->gt($x, $y); - } - - /** - * @inheritdoc - */ - public function gte($x, $y, $type = null): string { - $x = $this->prepareColumn($x, $type); - $y = $this->prepareColumn($y, $type); - return $this->expressionBuilder->gte($x, $y); + return parent::prepareColumn($column, $type); } /** diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php index 289aa09b003..4e2797761d6 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/SqliteExpressionBuilder.php @@ -23,6 +23,12 @@ */ namespace OC\DB\QueryBuilder\ExpressionBuilder; +use OC\DB\QueryBuilder\QueryFunction; +use OCP\DB\QueryBuilder\ILiteral; +use OCP\DB\QueryBuilder\IParameter; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\DB\QueryBuilder\IQueryFunction; + class SqliteExpressionBuilder extends ExpressionBuilder { /** * @inheritdoc @@ -34,4 +40,33 @@ class SqliteExpressionBuilder extends ExpressionBuilder { public function iLike($x, $y, $type = null): string { return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y), $type); } + + /** + * @param mixed $column + * @param mixed|null $type + * @return array|IQueryFunction|string + */ + protected function prepareColumn($column, $type) { + if ($type === IQueryBuilder::PARAM_DATE && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { + return $this->castColumn($column, $type); + } + + return parent::prepareColumn($column, $type); + } + + /** + * Returns a IQueryFunction that casts the column to the given type + * + * @param string $column + * @param mixed $type One of IQueryBuilder::PARAM_* + * @return IQueryFunction + */ + public function castColumn($column, $type): IQueryFunction { + if ($type === IQueryBuilder::PARAM_DATE) { + $column = $this->helper->quoteColumnName($column); + return new QueryFunction('DATETIME(' . $column . ')'); + } + + return parent::castColumn($column, $type); + } } |