Skip to content

Commit

Permalink
Implement LazyConnectionDependencies::class. (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 23, 2021
1 parent a48e69b commit 03d3d0c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 63 deletions.
12 changes: 6 additions & 6 deletions src/ArrayExpressionBuilder.php
Expand Up @@ -4,24 +4,24 @@

namespace Yiisoft\Db\Pgsql;

use function get_class;
use function implode;
use function in_array;
use function is_array;
use function str_repeat;
use Traversable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ArrayExpression;

use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionBuilderTrait;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Query\Query;

use function get_class;
use function implode;
use function in_array;
use function is_array;
use function str_repeat;

final class ArrayExpressionBuilder implements ExpressionBuilderInterface
{
use ExpressionBuilderTrait;
Expand Down
14 changes: 7 additions & 7 deletions src/ColumnSchema.php
Expand Up @@ -4,18 +4,18 @@

namespace Yiisoft\Db\Pgsql;

use JsonException;
use Yiisoft\Db\Expression\ArrayExpression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;

use function array_walk_recursive;
use function in_array;
use function is_array;
use function is_string;
use function json_decode;

use JsonException;
use function strtolower;
use Yiisoft\Db\Expression\ArrayExpression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema;

final class ColumnSchema extends AbstractColumnSchema
{
Expand Down Expand Up @@ -98,7 +98,7 @@ public function phpTypecast($value)
*
* @throws JsonException
*
* @return bool|mixed|null
* @return bool|int|null
*/
protected function phpTypecastValue($value)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Connection.php
Expand Up @@ -13,8 +13,6 @@
*/
final class Connection extends AbstractConnection
{
private ?Schema $schema = null;

/**
* Creates a command for execution.
*
Expand All @@ -23,13 +21,13 @@ final class Connection extends AbstractConnection
*
* @return Command the DB command
*/
public function createCommand(?string $sql = null, array $params = []): Command
public function createCommand(string $sql = null, array $params = []): Command
{
if ($sql !== null) {
$sql = $this->quoteSql($sql);
}

$command = new Command($this->getProfiler(), $this->getLogger(), $this, $this->getQueryCache(), $sql);
$command = new Command($this, $sql);

return $command->bindValues($params);
}
Expand All @@ -41,7 +39,7 @@ public function createCommand(?string $sql = null, array $params = []): Command
*/
public function getSchema(): Schema
{
return $this->schema = new Schema($this, $this->getSchemaCache());
return new Schema($this);
}

/**
Expand Down
45 changes: 22 additions & 23 deletions src/QueryBuilder.php
Expand Up @@ -4,25 +4,9 @@

namespace Yiisoft\Db\Pgsql;

use function array_diff;
use function array_merge;
use function array_unshift;
use function count;
use function explode;
use Generator;
use function implode;
use function is_bool;
use function is_float;
use function is_string;
use JsonException;
use PDO;
use function preg_match;
use function preg_replace;
use function reset;
use function strpos;
use function strrpos;

use function version_compare;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
Expand All @@ -38,6 +22,22 @@
use Yiisoft\Db\Query\QueryBuilder as AbstractQueryBuilder;
use Yiisoft\Strings\NumericHelper;

use function array_diff;
use function array_merge;
use function array_unshift;
use function count;
use function explode;
use function implode;
use function is_bool;
use function is_float;
use function is_string;
use function preg_match;
use function preg_replace;
use function reset;
use function strpos;
use function strrpos;
use function version_compare;

final class QueryBuilder extends AbstractQueryBuilder
{
/**
Expand Down Expand Up @@ -216,8 +216,8 @@ public function renameTable(string $oldName, string $newName): string
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set, the next new
* row's primary key will have a value 1.
*
* @throws Exception|InvalidArgumentException if the table does not exist or there is no sequence associated with
* the table.
* @throws Exception|InvalidArgumentException|JsonException if the table does not exist or there is no sequence
* associated with the table.
*
* @return string the SQL statement for resetting sequence.
*/
Expand Down Expand Up @@ -255,6 +255,8 @@ public function resetSequence(string $tableName, $value = null): string
* @param string $table the table name.
* @param bool $check whether to turn on or off the integrity check.
*
* @throws Exception|NotSupportedException
*
* @return string the SQL statement for checking integrity.
*/
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
Expand Down Expand Up @@ -379,10 +381,7 @@ public function alterColumn(string $table, string $column, $type): string
* @param array $params the binding parameters that will be generated by this method. They should be bound to the
* DB command later.
*
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException
*
* @return string the INSERT SQL
*/
Expand Down Expand Up @@ -618,7 +617,7 @@ private function oldUpsert(string $table, $insertColumns, $updateColumns, array
* @param array $params the binding parameters that will be modified by this method so that they can be bound to the
* DB command later.
*
* @throws Exception|InvalidArgumentException
* @throws Exception|InvalidArgumentException|JsonException
*
* @return string the UPDATE SQL.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Schema.php
Expand Up @@ -918,6 +918,6 @@ private function createColumnSchema(): ColumnSchema
*/
public function createColumnSchemaBuilder(string $type, $length = null): ColumnSchemaBuilder
{
return new ColumnSchemaBuilder($type, $length, $this->getDb());
return new ColumnSchemaBuilder($type, $length);
}
}
10 changes: 2 additions & 8 deletions tests/ConnectionTest.php
Expand Up @@ -28,10 +28,6 @@ public function testConstruct(): void
{
$db = $this->getConnection();

$this->assertEquals($this->logger, $db->getLogger());
$this->assertEquals($this->profiler, $db->getProfiler());
$this->assertEquals($this->queryCache, $db->getQueryCache());
$this->assertEquals($this->schemaCache, $db->getSchemaCache());
$this->assertEquals($this->params()['yiisoft/db-pgsql']['dsn'], $db->getDsn());
}

Expand Down Expand Up @@ -59,7 +55,7 @@ public function testOpenClose(): void
$this->assertFalse($db->isActive());
$this->assertNull($db->getPDO());

$db = new Connection($this->logger, $this->profiler, $this->queryCache, $this->schemaCache, 'unknown::memory:');
$db = new Connection('unknown::memory:', $this->dependencies);

$this->expectException(Exception::class);
$this->expectExceptionMessage('could not find driver');
Expand Down Expand Up @@ -273,8 +269,6 @@ public function testServerStatusCacheCanBeDisabled(): void
{
$cacheKeyNormalizer = new CacheKeyNormalizer();

$this->cache->psr()->clear();

$db = $this->getConnection();

$db->setMasters(
Expand All @@ -289,7 +283,7 @@ public function testServerStatusCacheCanBeDisabled(): void
]
);

$db->getSchemaCache()->setEnable(false);
$this->schemaCache->setEnable(false);

$db->setShuffleMasters(false);

Expand Down
2 changes: 1 addition & 1 deletion tests/SchemaTest.php
Expand Up @@ -644,7 +644,7 @@ public function testTableSchemaCacheWithTablePrefixes(
$db = $this->getConnection();
$schema = $this->getConnection()->getSchema();

$db->getSchemaCache()->setEnable(true);
$this->schemaCache->setEnable(true);
$db->setTablePrefix($tablePrefix);

$noCacheTable = $schema->getTableSchema($tableName, true);
Expand Down
24 changes: 12 additions & 12 deletions tests/TestCase.php
Expand Up @@ -19,6 +19,7 @@
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\Dsn;
use Yiisoft\Db\Connection\LazyConnectionDependencies;
use Yiisoft\Db\Factory\DatabaseFactory;
use Yiisoft\Db\Pgsql\Connection;
use Yiisoft\Db\TestUtility\IsOneOfAssert;
Expand All @@ -35,14 +36,15 @@

class TestCase extends AbstractTestCase
{
protected Aliases $aliases;
protected CacheInterface $cache;
protected Connection $connection;
protected ContainerInterface $container;
protected array $dataProvider;
protected array $expectedSchemas = ['public'];
protected string $likeEscapeCharSql = '';
protected array $likeParameterReplacements = [];
protected Aliases $aliases;
protected CacheInterface $cache;
protected Connection $connection;
protected ContainerInterface $container;
protected LazyConnectionDependencies $dependencies;
protected LoggerInterface $logger;
protected ProfilerInterface $profiler;
protected QueryCache $queryCache;
Expand Down Expand Up @@ -105,15 +107,16 @@ protected function configContainer(): void
{
$this->container = new Container($this->config());

DatabaseFactory::initialize($this->container, []);

$this->aliases = $this->container->get(Aliases::class);
$this->logger = $this->container->get(LoggerInterface::class);
$this->profiler = $this->container->get(ProfilerInterface::class);
$this->cache = $this->container->get(CacheInterface::class);
$this->connection = $this->container->get(ConnectionInterface::class);
$this->dependencies = $this->container->get(LazyConnectionDependencies::class);
$this->logger = $this->container->get(LoggerInterface::class);
$this->profiler = $this->container->get(ProfilerInterface::class);
$this->queryCache = $this->container->get(QueryCache::class);
$this->schemaCache = $this->container->get(SchemaCache::class);

DatabaseFactory::initialize($this->container, []);
}

/**
Expand Down Expand Up @@ -195,8 +198,6 @@ protected function prepareDatabase(?string $fixture = null): void
* @param string $propertyName
* @param bool $revoke whether to make property inaccessible after getting.
*
* @throws ReflectionException
*
* @return mixed
*/
protected function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true)
Expand Down Expand Up @@ -239,8 +240,6 @@ protected function replaceQuotes($sql)
* @param string $propertyName
* @param $value
* @param bool $revoke whether to make property inaccessible after setting
*
* @throws ReflectionException
*/
protected function setInaccessibleProperty(object $object, string $propertyName, $value, bool $revoke = true): void
{
Expand Down Expand Up @@ -292,6 +291,7 @@ private function config(): array
],

LoggerInterface::class => Logger::class,

ProfilerInterface::class => Profiler::class,

ConnectionInterface::class => [
Expand Down

0 comments on commit 03d3d0c

Please sign in to comment.