Skip to content

Commit

Permalink
Revert "MySqlDriver driver uses subqueries (#265)"
Browse files Browse the repository at this point in the history
This reverts commit e89a34a.

"MySQL does not support LIMIT in subqueries for certain subquery operators" dev.mysql.com/doc/refman/8.0/en/subquery-restrictions.html
  • Loading branch information
dg committed Apr 22, 2021
1 parent 3cc79b1 commit 8caf468
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
14 changes: 4 additions & 10 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class MySqlDriver implements Nette\Database\Driver
/** @var Nette\Database\Connection */
private $connection;

/** @var string */
private $version;


/**
* Driver options:
Expand All @@ -39,8 +36,8 @@ class MySqlDriver implements Nette\Database\Driver
public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
$this->version = $connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
$charset = $options['charset'] ?? (version_compare($this->version, '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
$charset = $options['charset']
?? (version_compare($connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
if ($charset) {
$connection->query('SET NAMES ?', $charset);
}
Expand Down Expand Up @@ -206,10 +203,7 @@ public function isSupported(string $item): bool
// MULTI_COLUMN_AS_OR_COND due to mysql bugs:
// - http://bugs.mysql.com/bug.php?id=31188
// - http://bugs.mysql.com/bug.php?id=35819
// SUPPORT_SUBSELECT is slow before 5.7
// - http://mysqlserverteam.com/derived-tables-in-mysql-5-7/
return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS
|| $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND
|| ($item === self::SUPPORT_SUBSELECT && version_compare($this->version, '5.7', '>='));
// and more.
return $item === self::SUPPORT_SELECT_UNGROUPED_COLUMNS || $item === self::SUPPORT_MULTI_COLUMN_AS_OR_COND;
}
}
11 changes: 10 additions & 1 deletion tests/Database/Table/SqlBuilder.addWhere().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ test('?name', function () use ($explorer) {
test('test Selection as a parameter', function () use ($explorer) {
$sqlBuilder = new SqlBuilder('book', $explorer);
$sqlBuilder->addWhere('id', $explorer->table('book'));
Assert::equal(reformat(['SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book]))']), $sqlBuilder->buildSelectQuery());
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book]))',
]), $sqlBuilder->buildSelectQuery());
});


Expand All @@ -47,6 +50,7 @@ test('test more Selection as a parameter', function () use ($explorer) {
$sqlBuilder->addWhere('id', $explorer->table('book'));
$sqlBuilder->addWhere('id', $explorer->table('book_tag')->select('book_id'));
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?)) AND (`id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book])) AND ([id] IN (SELECT [book_id] FROM [book_tag]))',
]), $sqlBuilder->buildSelectQuery());
});
Expand All @@ -56,6 +60,7 @@ test('test more Selection as one of more argument', function () use ($explorer)
$sqlBuilder = new SqlBuilder('book', $explorer);
$sqlBuilder->addWhere('id ? AND id ?', $explorer->table('book')->where('id', 2), $explorer->table('book_tag')->select('book_id'));
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?) AND `id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book] WHERE ([id] = ?)) AND [id] IN (SELECT [book_id] FROM [book_tag]))',
]), $sqlBuilder->buildSelectQuery());
});
Expand All @@ -77,6 +82,7 @@ test('test Selection with parameters as a parameter', function () use ($explorer
$sqlBuilder->addWhere('id', $explorer->table('book')->having('COUNT(:book_tag.tag_id) >', 1));
$schemaSupported = $explorer->getConnection()->getDriver()->isSupported(Driver::SUPPORT_SCHEMA);
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book] LEFT JOIN ' . ($schemaSupported ? '[public].[book_tag] ' : '') . '[book_tag] ON [book].[id] = [book_tag].[book_id] HAVING COUNT([book_tag].[tag_id]) > ?))',
]), $sqlBuilder->buildSelectQuery());
Assert::count(1, $sqlBuilder->getParameters());
Expand All @@ -87,6 +93,7 @@ test('test Selection with column as a parameter', function () use ($explorer) {
$sqlBuilder = new SqlBuilder('book', $explorer);
$sqlBuilder->addWhere('id', $explorer->table('book')->select('id'));
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book]))',
]), $sqlBuilder->buildSelectQuery());
});
Expand All @@ -96,6 +103,7 @@ test('test multiple placeholder parameter', function () use ($explorer) {
$sqlBuilder = new SqlBuilder('book', $explorer);
$sqlBuilder->addWhere('id ? OR id ?', null, $explorer->table('book'));
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IS NULL OR `id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IS NULL OR [id] IN (SELECT [id] FROM [book]))',
]), $sqlBuilder->buildSelectQuery());
});
Expand Down Expand Up @@ -168,6 +176,7 @@ test('tests NOT', function () use ($explorer) {
$sqlBuilder->addWhere('id NOT', null);
$sqlBuilder->addWhere('id NOT', $explorer->table('book')->select('id'));
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` NOT IN (?)) AND (`id` IS NOT NULL) AND (`id` NOT IN (?))',
'SELECT * FROM [book] WHERE ([id] NOT IN (?)) AND ([id] IS NOT NULL) AND ([id] NOT IN (SELECT [id] FROM [book]))',
]), $sqlBuilder->buildSelectQuery());
});
Expand Down

0 comments on commit 8caf468

Please sign in to comment.