Skip to content

Commit

Permalink
Cast datetime columns in sqlite before comparing
Browse files Browse the repository at this point in the history
Move the logic to prepare a column to the parent ExpressionBuilder so
that it can be reused for OCI and sqlite

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Dec 29, 2023
1 parent cdc2723 commit 86dc766
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 86 deletions.
37 changes: 23 additions & 14 deletions lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function orX(...$x): ICompositeExpression {
* @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);
}

Expand All @@ -140,8 +140,8 @@ public function comparison($x, string $operator, $y, $type = null): string {
* @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);
}

Expand All @@ -162,8 +162,8 @@ public function eq($x, $y, $type = null): string {
* @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);
}

Expand All @@ -184,8 +184,8 @@ public function neq($x, $y, $type = null): string {
* @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);
}

Expand All @@ -206,8 +206,8 @@ public function lt($x, $y, $type = null): string {
* @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);
}

Expand All @@ -228,8 +228,8 @@ public function lte($x, $y, $type = null): string {
* @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);
}

Expand All @@ -250,8 +250,8 @@ public function gt($x, $y, $type = null): string {
* @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);
}

Expand Down Expand Up @@ -435,4 +435,13 @@ public function castColumn($column, $type): IQueryFunction {
$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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,4 +40,33 @@ public function like($x, $y, $type = null): string {
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);
}
}

0 comments on commit 86dc766

Please sign in to comment.