Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from spiral/feature/query-caching
Browse files Browse the repository at this point in the history
- added sql compiler caching, up to 5x times faster query generation
  • Loading branch information
wolfy-j authored Jan 13, 2020
2 parents c24d01e + 77553ec commit 6229ba8
Show file tree
Hide file tree
Showing 190 changed files with 5,514 additions and 4,379 deletions.
22 changes: 11 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ install:
- composer install --no-interaction --prefer-source

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml
- vendor/bin/spiral-cs check src tests
- vendor/bin/phpunit --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash) -f coverage.xml
Expand All @@ -34,9 +34,9 @@ jobs:
- docker-compose up -d
- cd ..
script:
- vendor/bin/phpunit tests/Database/SQLite
- vendor/bin/phpunit tests/Database/MySQL
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/SQLite
- vendor/bin/phpunit tests/Database/Driver//MySQL
- vendor/bin/phpunit tests/Database/Driver/Postgres

# Testing various PostgresSQL versions
- stage: Test
Expand All @@ -48,7 +48,7 @@ jobs:
before_install:
- psql -c 'create database spiral;' -U postgres
script:
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/Postgres

- stage: Test
php: 7.2
Expand All @@ -59,7 +59,7 @@ jobs:
before_install:
- psql -c 'create database spiral;' -U postgres
script:
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/Postgres

- stage: Test
php: 7.2
Expand All @@ -70,7 +70,7 @@ jobs:
before_install:
- psql -c 'create database spiral;' -U postgres
script:
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/Postgres

- stage: Test
php: 7.3
Expand All @@ -87,7 +87,7 @@ jobs:
- psql --version
- psql -c 'create database spiral;' -U postgres
script:
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/Postgres

- stage: Test
php: 7.3
Expand All @@ -105,7 +105,7 @@ jobs:
- psql -c 'create database spiral;' -U postgres
- psql -c 'CREATE ROLE travis SUPERUSER LOGIN CREATEDB;' -U postgres
script:
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/Postgres

- stage: Test
php: 7.3
Expand All @@ -123,7 +123,7 @@ jobs:
- psql -c 'create database spiral;' -U postgres
- psql -c 'CREATE ROLE travis SUPERUSER LOGIN CREATEDB;' -U postgres
script:
- vendor/bin/phpunit tests/Database/Postgres
- vendor/bin/phpunit tests/Database/Driver/Postgres

# MariaDB
- stage: Test
Expand All @@ -135,4 +135,4 @@ jobs:
before_install:
- mysql -e 'CREATE DATABASE spiral;'
script:
- vendor/bin/phpunit tests/Database/MySQL
- vendor/bin/phpunit tests/Database/Driver/MySQL
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
CHANGELOG for 0.9.0 RC
======================

2.7.0 (13.01.2020)
-----
- added sql compiler caching, up to 5x times faster query generation
- added prepared statement caching
- refactor of SchemaHandler
- refactor of Query builders
- added ComparatorInterface
- deprecated MySQL 5.5 support

2.6.10 (26.12.2019)
-----
- [bugfix] invalid change detection for nullable and zeroed default values
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
"php": "^7.2",
"ext-pdo": "*",
"spiral/core": "^1.1",
"spiral/logger": "^1.1",
"spiral/logger": "^1.0",
"spiral/pagination": "^2.2"
},
"require-dev": {
"spiral/debug": "^1.3",
"phpunit/phpunit": "~7.0",
"mockery/mockery": "^1.1",
"spiral/code-style": "^1.0"
"spiral/dumper": "^1.0",
"spiral/code-style": "^1.0",
"spiral/tokenizer": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion src/Config/DatabaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public function getDriver(string $driver): Autowire
}

$config = $this->config['connections'][$driver] ?? $this->config['drivers'][$driver];

if ($config instanceof Autowire) {
return $config;
}
Expand Down
51 changes: 33 additions & 18 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Spiral\Database\Query\InsertQuery;
use Spiral\Database\Query\SelectQuery;
use Spiral\Database\Query\UpdateQuery;
use Throwable;

/**
* Database class is high level abstraction at top of Driver. Databases usually linked to real
Expand Down Expand Up @@ -96,7 +97,7 @@ public function getType(): string
*/
public function getDriver(int $type = DatabaseInterface::WRITE): DriverInterface
{
if ($type == self::READ && !empty($this->readDriver)) {
if ($type === self::READ && $this->readDriver !== null) {
return $this->readDriver;
}

Expand Down Expand Up @@ -132,7 +133,7 @@ public function getPrefix(): string
*/
public function hasTable(string $name): bool
{
return $this->getDriver()->hasTable($this->prefix . $name);
return $this->getDriver()->getSchemaHandler()->hasTable($this->prefix . $name);
}

/**
Expand All @@ -142,14 +143,16 @@ public function hasTable(string $name): bool
*/
public function getTables(): array
{
$schemaHandler = $this->getDriver(self::READ)->getSchemaHandler();

$result = [];
foreach ($this->getDriver(self::READ)->tableNames() as $table) {
foreach ($schemaHandler->getTableNames() as $table) {
if ($this->prefix && strpos($table, $this->prefix) !== 0) {
//Logical partitioning
// logical partitioning
continue;
}

$result[] = $this->table(substr($table, strlen($this->prefix)));
$result[] = new Table($this, substr($table, strlen($this->prefix)));
}

return $result;
Expand All @@ -170,39 +173,47 @@ public function table(string $name): TableInterface
*/
public function execute(string $query, array $parameters = []): int
{
return $this->getDriver(self::WRITE)->execute($query, $parameters);
return $this->getDriver(self::WRITE)
->execute($query, $parameters);
}

/**
* {@inheritdoc}
*/
public function query(string $query, array $parameters = []): StatementInterface
{
return $this->getDriver(self::READ)->query($query, $parameters);
return $this->getDriver(self::READ)
->query($query, $parameters);
}

/**
* {@inheritdoc}
*/
public function insert(string $table = null): InsertQuery
{
return $this->getDriver(self::WRITE)->insertQuery($this->prefix, $table);
return $this->getDriver(self::WRITE)
->getQueryBuilder()
->insertQuery($this->prefix, $table);
}

/**
* {@inheritdoc}
*/
public function update(string $table = null, array $values = [], array $where = []): UpdateQuery
{
return $this->getDriver(self::WRITE)->updateQuery($this->prefix, $table, $where, $values);
return $this->getDriver(self::WRITE)
->getQueryBuilder()
->updateQuery($this->prefix, $table, $where, $values);
}

/**
* {@inheritdoc}
*/
public function delete(string $table = null, array $where = []): DeleteQuery
{
return $this->getDriver(self::WRITE)->deleteQuery($this->prefix, $table, $where);
return $this->getDriver(self::WRITE)
->getQueryBuilder()
->deleteQuery($this->prefix, $table, $where);
}

/**
Expand All @@ -216,24 +227,28 @@ public function select($column = '*'): SelectQuery
$columns = $columns[0];
}

return $this->getDriver(self::READ)->selectQuery($this->prefix, [], $columns);
return $this->getDriver(self::READ)
->getQueryBuilder()
->selectQuery($this->prefix, [], $columns);
}

/**
* {@inheritdoc}
*
* @param bool $cacheStatements
*/
public function transaction(callable $callback, string $isolationLevel = null, bool $cacheStatements = false)
{
$this->begin($isolationLevel, $cacheStatements);
public function transaction(
callable $callback,
string $isolationLevel = null
) {
$this->begin($isolationLevel);

try {
$result = call_user_func($callback, $this);
$result = $callback($this);
$this->commit();

return $result;
} catch (\Throwable $e) {
} catch (Throwable $e) {
$this->rollBack();
throw $e;
}
Expand All @@ -242,11 +257,11 @@ public function transaction(callable $callback, string $isolationLevel = null, b
/**
* {@inheritdoc}
*/
public function begin(string $isolationLevel = null, bool $cacheStatements = false): bool
public function begin(string $isolationLevel = null): bool
{
$driver = $this->getDriver(self::WRITE);
if ($driver instanceof Driver) {
return $driver->beginTransaction($isolationLevel, $cacheStatements);
return $driver->beginTransaction($isolationLevel);
}

return $driver->beginTransaction($isolationLevel);
Expand Down
10 changes: 2 additions & 8 deletions src/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ interface DatabaseInterface
public const WRITE = 0;
public const READ = 1;

// Known database types. More to be added?
public const MYSQL = 'MySQL';
public const POSTGRES = 'Postgres';
public const SQLITE = 'SQLite';
public const SQL_SERVER = 'SQLServer';

/**
* @return string
*/
Expand Down Expand Up @@ -122,9 +116,9 @@ public function insert(string $table = ''): InsertQuery;
/**
* Get instance of UpdateBuilder associated with current Database.
*
* @param string $table Table where rows should be updated in.
* @param string $table Table where rows should be updated in.
* @param array $values Initial set of columns to update associated with their values.
* @param array $where Initial set of where rules specified as array.
* @param array $where Initial set of where rules specified as array.
* @return UpdateQuery
*/
public function update(string $table = '', array $values = [], array $where = []): UpdateQuery;
Expand Down
45 changes: 36 additions & 9 deletions src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
namespace Spiral\Database;

use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\NullLogger;
use Spiral\Core\Container;
use Spiral\Core\Container\InjectorInterface;
use Spiral\Core\Container\SingletonInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Database\Config\DatabaseConfig;
use Spiral\Database\Config\DatabasePartial;
use Spiral\Database\Driver\Driver;
use Spiral\Database\Driver\DriverInterface;
use Spiral\Database\Exception\DatabaseException;
use Spiral\Database\Exception\DBALException;
use Spiral\Logger\Traits\LoggerTrait;

/**
* Automatic factory and configurator for Drivers and Databases.
Expand Down Expand Up @@ -83,13 +84,18 @@
*
* echo $manager->database('runtime')->select()->from('users')->count();
*/
final class DatabaseManager implements DatabaseProviderInterface, SingletonInterface, InjectorInterface
final class DatabaseManager implements
DatabaseProviderInterface,
Container\SingletonInterface,
Container\InjectorInterface
{
use LoggerTrait;

/** @var FactoryInterface */
protected $factory = null;
/** @var DatabaseConfig */
private $config = null;
private $config;

/** @var FactoryInterface */
private $factory;

/** @var Database[] */
private $databases = [];
Expand Down Expand Up @@ -125,7 +131,12 @@ public function createInjection(\ReflectionClass $class, string $context = null)
*/
public function getDatabases(): array
{
$names = array_unique(array_merge(array_keys($this->databases), array_keys($this->config->getDatabases())));
$names = array_unique(
array_merge(
array_keys($this->databases),
array_keys($this->config->getDatabases())
)
);

$result = [];
foreach ($names as $name) {
Expand Down Expand Up @@ -192,7 +203,12 @@ public function addDatabase(Database $database): void
*/
public function getDrivers(): array
{
$names = array_unique(array_merge(array_keys($this->drivers), array_keys($this->config->getDrivers())));
$names = array_unique(
array_merge(
array_keys($this->drivers),
array_keys($this->config->getDrivers())
)
);

$result = [];
foreach ($names as $name) {
Expand All @@ -215,8 +231,19 @@ public function driver(string $driver): DriverInterface
if (isset($this->drivers[$driver])) {
return $this->drivers[$driver];
}

try {
return $this->drivers[$driver] = $this->config->getDriver($driver)->resolve($this->factory);
$driverObject = $this->config->getDriver($driver)->resolve($this->factory);
$this->drivers[$driver] = $driverObject;

if ($driverObject instanceof LoggerAwareInterface) {
$logger = $this->getLogger(get_class($driverObject));
if (!$logger instanceof NullLogger) {
$driverObject->setLogger($logger);
}
}

return $this->drivers[$driver];
} catch (ContainerExceptionInterface $e) {
throw new DBALException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
Loading

0 comments on commit 6229ba8

Please sign in to comment.