Skip to content

Commit

Permalink
switch to nextras/multi-query-parser [closes #59]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 10, 2023
1 parent 65ea548 commit d17c975
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 98 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"nette/utils": "~3.0",
"nette/finder": "~2.5",
"nette/neon": "~3.0",
"nextras/multi-query-parser": "1.0.0",
"phpstan/extension-installer": "1.2.0",
"phpstan/phpstan": "1.10.10",
"phpstan/phpstan-deprecation-rules": "1.1.3",
Expand All @@ -32,6 +33,9 @@
"symfony/http-kernel": "~4.4 || ~5.0",
"tracy/tracy": "~2.7"
},
"suggest": {
"nextras/multi-query-parser": "Install this to support SQL file import."
},
"autoload": {
"psr-4": { "Nextras\\Dbal\\": "src/" }
},
Expand Down
7 changes: 7 additions & 0 deletions src/Bridges/NetteCaching/CachedPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeInterface;
use Nette\Caching\Cache;
use Nextras\Dbal\Platforms\IPlatform;
use Nextras\MultiQueryParser\IMultiQueryParser;


class CachedPlatform implements IPlatform
Expand Down Expand Up @@ -124,6 +125,12 @@ public function formatLimitOffset(?int $limit, ?int $offset): string
}


public function createMultiQueryParser(): IMultiQueryParser
{
return $this->platform->createMultiQueryParser();
}


public function isSupported(int $feature): bool
{
return $this->platform->isSupported($feature);
Expand Down
8 changes: 8 additions & 0 deletions src/Platforms/IPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Nextras\Dbal\Platforms\Data\Column;
use Nextras\Dbal\Platforms\Data\ForeignKey;
use Nextras\Dbal\Platforms\Data\Table;
use Nextras\MultiQueryParser\IMultiQueryParser;


interface IPlatform
Expand Down Expand Up @@ -117,6 +118,13 @@ public function formatBlob(string $value): string;
public function formatLimitOffset(?int $limit, ?int $offset): string;


/**
* Returns SQL file parser
* !!!This function requires nextras/multi-query-parser dependency!!!
*/
public function createMultiQueryParser(): IMultiQueryParser;


/**
* Checks whether any feature from IPlatform::SUPPORT_* is supported.
* @internal
Expand Down
11 changes: 11 additions & 0 deletions src/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Nextras\Dbal\Utils\DateTimeHelper;
use Nextras\Dbal\Utils\JsonHelper;
use Nextras\Dbal\Utils\StrictObjectTrait;
use Nextras\MultiQueryParser\IMultiQueryParser;
use Nextras\MultiQueryParser\MySqlMultiQueryParser;
use function addcslashes;
use function explode;
use function str_replace;
Expand Down Expand Up @@ -220,6 +222,15 @@ public function formatLimitOffset(?int $limit, ?int $offset): string
}


public function createMultiQueryParser(): IMultiQueryParser
{
if (!class_exists(MySqlMultiQueryParser::class)) {
throw new \RuntimeException("Missing nextras/multi-query-parser dependency. Install it first to use IPlatform::createMultiQueryParser().");
}
return new MySqlMultiQueryParser();
}


public function isSupported(int $feature): bool
{
static $supported = [
Expand Down
11 changes: 11 additions & 0 deletions src/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Nextras\Dbal\Utils\DateTimeHelper;
use Nextras\Dbal\Utils\JsonHelper;
use Nextras\Dbal\Utils\StrictObjectTrait;
use Nextras\MultiQueryParser\IMultiQueryParser;
use Nextras\MultiQueryParser\PostgreSqlMultiQueryParser;
use function bin2hex;
use function str_replace;
use function strtr;
Expand Down Expand Up @@ -255,6 +257,15 @@ public function formatLimitOffset(?int $limit, ?int $offset): string
}


public function createMultiQueryParser(): IMultiQueryParser
{
if (!class_exists(PostgreSqlMultiQueryParser::class)) {
throw new \RuntimeException("Missing nextras/multi-query-parser dependency. Install it first to use IPlatform::createMultiQueryParser().");
}
return new PostgreSqlMultiQueryParser();
}


public function isSupported(int $feature): bool
{
static $supported = [
Expand Down
11 changes: 11 additions & 0 deletions src/Platforms/SqlServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Nextras\Dbal\Platforms\Data\Table;
use Nextras\Dbal\Utils\JsonHelper;
use Nextras\Dbal\Utils\StrictObjectTrait;
use Nextras\MultiQueryParser\IMultiQueryParser;
use Nextras\MultiQueryParser\SqlServerMultiQueryParser;


class SqlServerPlatform implements IPlatform
Expand Down Expand Up @@ -244,6 +246,15 @@ public function formatLimitOffset(?int $limit, ?int $offset): string
}


public function createMultiQueryParser(): IMultiQueryParser
{
if (!class_exists(SqlServerMultiQueryParser::class)) {
throw new \RuntimeException("Missing nextras/multi-query-parser dependency. Install it first to use IPlatform::createMultiQueryParser().");
}
return new SqlServerMultiQueryParser();
}


public function isSupported(int $feature): bool
{
static $supported = [
Expand Down
88 changes: 0 additions & 88 deletions src/Utils/FileImporter.php

This file was deleted.

16 changes: 10 additions & 6 deletions tests/inc/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace NextrasTests\Dbal;


use Nextras\Dbal\Connection;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Utils\FileImporter;
use Tester\Environment;


Expand All @@ -20,8 +20,12 @@ class IntegrationTestCase extends TestCase
public function initData(Connection $connection)
{
$this->lockConnection($connection);
$platform = $connection->getPlatform()->getName();
FileImporter::executeFile($connection, __DIR__ . "/../data/$platform-data.sql");
$platform = $connection->getPlatform();
$platformName = $platform->getName();
$parser = $platform->createMultiQueryParser();
foreach ($parser->parseFile(__DIR__ . "/../data/$platformName-data.sql") as $sql) {
$connection->query('%raw', $sql);
}
}


Expand All @@ -35,8 +39,8 @@ protected function lockConnection(Connection $connection)
protected function createConnection($params = [])
{
$options = array_merge([
'user' => NULL,
'password' => NULL,
'user' => null,
'password' => null,
'searchPath' => ['public'],
], Environment::loadData(), $params);
return new Connection($options);
Expand All @@ -46,7 +50,7 @@ protected function createConnection($params = [])
public function __get($name)
{
if ($name === 'connection') {
if ($this->defaultConnection === NULL) {
if ($this->defaultConnection === null) {
$this->defaultConnection = $this->createConnection();
}
return $this->defaultConnection;
Expand Down
12 changes: 8 additions & 4 deletions tests/inc/setup.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php


use Nextras\Dbal\Connection;
use Nextras\Dbal\Utils\FileImporter;


if (@!include __DIR__ . '/../../vendor/autoload.php') {
Expand All @@ -28,11 +28,15 @@
echo "[setup] Bootstrapping '{$name}' structure.\n";

$connection = new Connection($configDatabase);
$platform = $connection->getPlatform()->getName();
$resetFunction = require __DIR__ . "/../data/{$platform}-reset.php";
$platform = $connection->getPlatform();
$platformName = $platform->getName();
$resetFunction = require __DIR__ . "/../data/{$platformName}-reset.php";
$resetFunction($connection, $configDatabase);

FileImporter::executeFile($connection, __DIR__ . "/../data/{$platform}-init.sql");
$parser = $platform->createMultiQueryParser();
foreach ($parser->parseFile(__DIR__ . "/../data/{$platformName}-init.sql") as $sql) {
$connection->query('%raw', $sql);
}
}

echo "[setup] All done.\n\n";

0 comments on commit d17c975

Please sign in to comment.