From 86dc7662763ebc053480c55bc1d113d8c316d01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 13 Apr 2021 16:57:48 +0200 Subject: [PATCH 1/2] Cast datetime columns in sqlite before comparing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../ExpressionBuilder/ExpressionBuilder.php | 37 ++++++---- .../OCIExpressionBuilder.php | 73 +------------------ .../SqliteExpressionBuilder.php | 35 +++++++++ 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 ae4f19f5d1884..ad45f77e5ea1f 100644 --- a/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php +++ b/lib/private/DB/QueryBuilder/ExpressionBuilder/ExpressionBuilder.php @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); + } } diff --git a/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php b/lib/private/DB/QueryBuilder/ExpressionBuilder/OCIExpressionBuilder.php index caeb800988585..8184e3693178a 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 289aa09b0039f..4e2797761d6ab 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 @@ 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); + } } From e50cb0a30ff36181504b7d5f1b46c2f53c5bc9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 10 Jul 2023 19:41:46 +0200 Subject: [PATCH 2/2] tests(DB): Add test for date time comparisons in query builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../QueryBuilder/ExpressionBuilderDBTest.php | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php b/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php index 35d8b4faa343a..33b5824a0dd08 100644 --- a/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php +++ b/tests/lib/DB/QueryBuilder/ExpressionBuilderDBTest.php @@ -21,8 +21,12 @@ namespace Test\DB\QueryBuilder; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Types\Types; use OC\DB\QueryBuilder\Literal; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IConfig; +use OCP\Server; use Test\TestCase; /** @@ -31,11 +35,13 @@ class ExpressionBuilderDBTest extends TestCase { /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */ protected $connection; + protected $schemaSetup = false; protected function setUp(): void { parent::setUp(); $this->connection = \OC::$server->getDatabaseConnection(); + $this->prepareTestingTable(); } public function likeProvider() { @@ -150,6 +156,59 @@ public function testLongText(): void { self::assertEquals('myvalue', $entries[0]['configvalue']); } + public function testDateTimeEquals() { + $dateTime = new \DateTime('2023-01-01'); + $insert = $this->connection->getQueryBuilder(); + $insert->insert('testing') + ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)]) + ->executeStatement(); + + $query = $this->connection->getQueryBuilder(); + $result = $query->select('*') + ->from('testing') + ->where($query->expr()->eq('datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE))) + ->executeQuery(); + $entries = $result->fetchAll(); + $result->closeCursor(); + self::assertCount(1, $entries); + } + + public function testDateTimeLess() { + $dateTime = new \DateTime('2022-01-01'); + $dateTimeCompare = new \DateTime('2022-01-02'); + $insert = $this->connection->getQueryBuilder(); + $insert->insert('testing') + ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)]) + ->executeStatement(); + + $query = $this->connection->getQueryBuilder(); + $result = $query->select('*') + ->from('testing') + ->where($query->expr()->lt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE))) + ->executeQuery(); + $entries = $result->fetchAll(); + $result->closeCursor(); + self::assertCount(1, $entries); + } + + public function testDateTimeGreater() { + $dateTime = new \DateTime('2023-01-02'); + $dateTimeCompare = new \DateTime('2023-01-01'); + $insert = $this->connection->getQueryBuilder(); + $insert->insert('testing') + ->values(['datetime' => $insert->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE)]) + ->executeStatement(); + + $query = $this->connection->getQueryBuilder(); + $result = $query->select('*') + ->from('testing') + ->where($query->expr()->gt('datetime', $query->createNamedParameter($dateTimeCompare, IQueryBuilder::PARAM_DATE))) + ->executeQuery(); + $entries = $result->fetchAll(); + $result->closeCursor(); + self::assertCount(1, $entries); + } + protected function createConfig($appId, $key, $value) { $query = $this->connection->getQueryBuilder(); $query->insert('appconfig') @@ -160,4 +219,31 @@ protected function createConfig($appId, $key, $value) { ]) ->execute(); } + + protected function prepareTestingTable(): void { + if ($this->schemaSetup) { + $this->connection->getQueryBuilder()->delete('testing')->executeStatement(); + } + + $prefix = Server::get(IConfig::class)->getSystemValueString('dbtableprefix', 'oc_'); + $schema = $this->connection->createSchema(); + try { + $schema->getTable($prefix . 'testing'); + $this->connection->getQueryBuilder()->delete('testing')->executeStatement(); + } catch (SchemaException $e) { + $this->schemaSetup = true; + $table = $schema->createTable($prefix . 'testing'); + $table->addColumn('id', Types::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + ]); + + $table->addColumn('datetime', Types::DATETIME_MUTABLE, [ + 'notnull' => false, + ]); + + $table->setPrimaryKey(['id']); + $this->connection->migrateToSchema($schema); + } + } }