| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 4.3.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Database\QueryTests;
- use Cake\Database\Driver\Mysql;
- use Cake\Database\Driver\Postgres;
- use Cake\Database\Driver\Sqlite;
- use Cake\Database\Driver\Sqlserver;
- use Cake\Database\Expression\TupleComparison;
- use Cake\Database\Query;
- use Cake\Database\StatementInterface;
- use Cake\Database\TypeMap;
- use Cake\Datasource\ConnectionManager;
- use Cake\TestSuite\TestCase;
- use PDOException;
- use RuntimeException;
- /**
- * Tuple comparison query tests.
- *
- * These tests are specifically relevant in the context of Sqlite and
- * Sqlserver, for which the tuple comparison will be transformed when
- * composite fields are used.
- *
- * @see \Cake\Database\Driver\TupleComparisonTranslatorTrait::_transformTupleComparison()
- */
- class TupleComparisonQueryTest extends TestCase
- {
- /**
- * @inheritDoc
- */
- protected $fixtures = [
- 'core.Articles',
- ];
- /**
- * @var \Cake\Database\Connection
- */
- protected $connection;
- /**
- * @var \Cake\Database\Query
- */
- protected $query;
- public function setUp(): void
- {
- parent::setUp();
- $this->connection = ConnectionManager::get('test');
- $this->query = new Query($this->connection);
- }
- public function tearDown(): void
- {
- parent::tearDown();
- unset($this->query);
- unset($this->connection);
- }
- public function testTransformWithInvalidOperator(): void
- {
- $driver = $this->connection->getDriver();
- if (
- $driver instanceof Sqlite ||
- $driver instanceof Sqlserver
- ) {
- $this->expectException(RuntimeException::class);
- $this->expectExceptionMessage(
- 'Tuple comparison transform only supports the `IN` and `=` operators, `NOT IN` given.'
- );
- } else {
- $this->markTestSkipped('Tuple comparisons are only being transformed for Sqlite and Sqlserver.');
- }
- $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- (new Query($this->connection))
- ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
- ->from(['ArticlesAlias' => 'articles'])
- ->where(['ArticlesAlias.author_id' => 1]),
- [],
- 'NOT IN'
- ),
- ])
- ->orderAsc('articles.id')
- ->execute();
- }
- public function testInWithMultiResultSubquery(): void
- {
- $typeMap = new TypeMap([
- 'id' => 'integer',
- 'author_id' => 'integer',
- ]);
- $query = $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- (new Query($this->connection))
- ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
- ->from(['ArticlesAlias' => 'articles'])
- ->where(['ArticlesAlias.author_id' => 1]),
- [],
- 'IN'
- ),
- ])
- ->orderAsc('articles.id')
- ->setSelectTypeMap($typeMap);
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- ],
- [
- 'id' => 3,
- 'author_id' => 1,
- ],
- ];
- $this->assertSame($expected, $query->execute()->fetchAll(StatementInterface::FETCH_TYPE_ASSOC));
- }
- public function testInWithSingleResultSubquery(): void
- {
- $typeMap = new TypeMap([
- 'id' => 'integer',
- 'author_id' => 'integer',
- ]);
- $query = $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- (new Query($this->connection))
- ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
- ->from(['ArticlesAlias' => 'articles'])
- ->where(['ArticlesAlias.id' => 1]),
- [],
- 'IN'
- ),
- ])
- ->setSelectTypeMap($typeMap);
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- ],
- ];
- $this->assertSame($expected, $query->execute()->fetchAll(StatementInterface::FETCH_TYPE_ASSOC));
- }
- public function testInWithMultiArrayValues(): void
- {
- $typeMap = new TypeMap([
- 'id' => 'integer',
- 'author_id' => 'integer',
- ]);
- $query = $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- [[1, 1], [3, 1]],
- ['integer', 'integer'],
- 'IN'
- ),
- ])
- ->orderAsc('articles.id')
- ->setSelectTypeMap($typeMap);
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- ],
- [
- 'id' => 3,
- 'author_id' => 1,
- ],
- ];
- $this->assertSame($expected, $query->execute()->fetchAll(StatementInterface::FETCH_TYPE_ASSOC));
- }
- public function testEqualWithMultiResultSubquery(): void
- {
- $driver = $this->connection->getDriver();
- if (
- $driver instanceof Mysql ||
- $driver instanceof Postgres
- ) {
- $this->expectException(PDOException::class);
- $this->expectExceptionMessageMatches('/cardinality violation/i');
- } else {
- // Due to the way tuple comparisons are being translated, the DBMS will
- // not run into a cardinality violation scenario.
- $this->markTestSkipped(
- 'Sqlite and Sqlserver currently do not fail with subqueries returning incompatible results.'
- );
- }
- $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- (new Query($this->connection))
- ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
- ->from(['ArticlesAlias' => 'articles'])
- ->where(['ArticlesAlias.author_id' => 1]),
- [],
- '='
- ),
- ])
- ->orderAsc('articles.id')
- ->execute();
- }
- public function testEqualWithSingleResultSubquery(): void
- {
- $typeMap = new TypeMap([
- 'id' => 'integer',
- 'author_id' => 'integer',
- ]);
- $query = $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- (new Query($this->connection))
- ->select(['ArticlesAlias.id', 'ArticlesAlias.author_id'])
- ->from(['ArticlesAlias' => 'articles'])
- ->where(['ArticlesAlias.id' => 1]),
- [],
- '='
- ),
- ])
- ->setSelectTypeMap($typeMap);
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- ],
- ];
- $this->assertSame($expected, $query->execute()->fetchAll(StatementInterface::FETCH_TYPE_ASSOC));
- }
- public function testEqualWithSingleArrayValue(): void
- {
- $typeMap = new TypeMap([
- 'id' => 'integer',
- 'author_id' => 'integer',
- ]);
- $query = $this->query
- ->select(['articles.id', 'articles.author_id'])
- ->from('articles')
- ->where([
- new TupleComparison(
- ['articles.id', 'articles.author_id'],
- [1, 1],
- ['integer', 'integer'],
- '='
- ),
- ])
- ->orderAsc('articles.id')
- ->setSelectTypeMap($typeMap);
- $expected = [
- [
- 'id' => 1,
- 'author_id' => 1,
- ],
- ];
- $this->assertSame($expected, $query->execute()->fetchAll(StatementInterface::FETCH_TYPE_ASSOC));
- }
- }
|