Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ServerInfoInterface and its implementation #906

Merged
merged 4 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- New #899: Add `ColumnSchemaInterface::hasDefaultValue()` and `ColumnSchemaInterface::null()` methods (@Tigrov)
- New #902: Add `QueryBuilderInterface::prepareParam()` and `QueryBuilderInterface::prepareValue()` methods (@Tigrov)
- Enh #902: Refactor `Quoter::quoteValue()` method (@Tigrov)
- New #906: Add `ServerInfoInterface` and its implementation (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
3 changes: 3 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
- `QueryBuilderInterface::buildColumnDefinition()` - builds column definition for `CREATE TABLE` statement;
- `QueryBuilderInterface::prepareParam()` - converts a `ParamInterface` object to its SQL representation;
- `QueryBuilderInterface::prepareValue()` - converts a value to its SQL representation;
- `QueryBuilderInterface::getServerInfo()` - returns `ServerInfoInterface` instance which provides server information;
- `ConnectionInterface::getServerInfo()` - returns `ServerInfoInterface` instance which provides server information;

### Remove methods

Expand All @@ -123,6 +125,7 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
- `Quoter::unquoteParts()`
- `AbstractPdoCommand::logQuery()`
- `ColumnSchemaInterface::phpType()`
- `ConnectionInterface::getServerVersion()`

### Remove deprecated parameters

Expand Down
9 changes: 2 additions & 7 deletions src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@ public function getQuoter(): QuoterInterface;
public function getSchema(): SchemaInterface;

/**
* Returns a server version as a string comparable by {@see \version_compare()}.
*
* @throws Exception
* @throws InvalidConfigException
*
* @return string The server version as a string.
* Returns {@see ServerInfoInterface} instance that provides information about the database server.
*/
public function getServerVersion(): string;
public function getServerInfo(): ServerInfoInterface;

/**
* Return table prefix for current DB connection.
Expand Down
16 changes: 16 additions & 0 deletions src/Connection/ServerInfoInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Connection;

/**
* Should be implemented by a class that provides information about the database server.
*/
interface ServerInfoInterface
{
/**
* Returns a server version as a string comparable by {@see version_compare()}.
*/
public function getVersion(): string;
}
5 changes: 3 additions & 2 deletions src/Debug/ConnectionInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Query\BatchQueryResultInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
Expand Down Expand Up @@ -82,9 +83,9 @@ public function getSchema(): SchemaInterface
return $this->connection->getSchema();
}

public function getServerVersion(): string
public function getServerInfo(): ServerInfoInterface
{
return $this->connection->getServerVersion();
return $this->connection->getServerInfo();
}

public function getTablePrefix(): string
Expand Down
13 changes: 4 additions & 9 deletions src/Driver/Pdo/AbstractPdoConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Throwable;
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Connection\AbstractConnection;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -43,7 +44,7 @@ abstract class AbstractPdoConnection extends AbstractConnection implements PdoCo
use ProfilerAwareTrait;

protected PDO|null $pdo = null;
protected string $serverVersion = '';
protected ServerInfoInterface|null $serverInfo = null;
protected bool|null $emulatePrepare = null;
protected QueryBuilderInterface|null $queryBuilder = null;
protected QuoterInterface|null $quoter = null;
Expand Down Expand Up @@ -169,15 +170,9 @@ public function getDriverName(): string
return $this->driver->getDriverName();
}

public function getServerVersion(): string
public function getServerInfo(): ServerInfoInterface
{
if ($this->serverVersion === '') {
/** @psalm-var mixed $version */
$version = $this->getActivePDO()->getAttribute(PDO::ATTR_SERVER_VERSION);
$this->serverVersion = is_string($version) ? $version : 'Version could not be determined.';
}

return $this->serverVersion;
return $this->serverInfo ??= new PdoServerInfo($this);
}

public function isActive(): bool
Expand Down
27 changes: 27 additions & 0 deletions src/Driver/Pdo/PdoServerInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Driver\Pdo;

use PDO;
use Yiisoft\Db\Connection\ServerInfoInterface;

class PdoServerInfo implements ServerInfoInterface
{
protected string|null $version = null;

public function __construct(protected PdoConnectionInterface $db)
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
}

public function getVersion(): string
{
if ($this->version === null) {
/** @var string */
$this->version = $this->db->getActivePDO()?->getAttribute(PDO::ATTR_SERVER_VERSION) ?? '';
}

return $this->version;
}
}
7 changes: 7 additions & 0 deletions src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\ParamInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Constant\GettypeResult;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\Expression;
Expand Down Expand Up @@ -67,6 +68,7 @@ abstract class AbstractQueryBuilder implements QueryBuilderInterface
public function __construct(
private QuoterInterface $quoter,
private SchemaInterface $schema,
private ServerInfoInterface $serverInfo,
private AbstractDDLQueryBuilder $ddlBuilder,
private AbstractDMLQueryBuilder $dmlBuilder,
private AbstractDQLQueryBuilder $dqlBuilder,
Expand Down Expand Up @@ -386,6 +388,11 @@ public function getExpressionBuilder(ExpressionInterface $expression): object
return $this->dqlBuilder->getExpressionBuilder($expression);
}

public function getServerInfo(): ServerInfoInterface
{
return $this->serverInfo;
}

public function insert(string $table, QueryInterface|array $columns, array &$params = []): string
{
return $this->dmlBuilder->insert($table, $columns, $params);
Expand Down
6 changes: 6 additions & 0 deletions src/QueryBuilder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\Db\Command\ParamInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
Expand Down Expand Up @@ -109,6 +110,11 @@ public function getColumnType(ColumnInterface|string $type): string;
*/
public function getExpressionBuilder(ExpressionInterface $expression): object;

/**
* Returns {@see ServerInfoInterface} instance that provides information about the database server.
*/
public function getServerInfo(): ServerInfoInterface;

/**
* @return QuoterInterface The quoter instance.
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/AbstractPdoConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Yiisoft\Db\Driver\Pdo\PdoDriverInterface;
use Yiisoft\Db\Driver\Pdo\PdoServerInfo;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand All @@ -35,11 +36,11 @@ public function testGetDriver(): void
$this->assertInstanceOf(PdoDriverInterface::class, $driver);
}

public function testGetServerVersion(): void
public function testGetServerInfo(): void
{
$db = $this->getConnection();

$this->assertIsString($db->getServerVersion());
$this->assertInstanceOf(PdoServerInfo::class, $db->getServerInfo());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/CommonColumnSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function checkCreateColumn(string $expected, string $type, int|null $l
$db = $this->getConnection();

if (str_contains($expected, 'UUID_TO_BIN')) {
$serverVersion = $db->getServerVersion();
$serverVersion = $db->getServerInfo()->getVersion();
if (str_contains($serverVersion, 'MariaDB')) {
$db->close();
$this->markTestSkipped('UUID_TO_BIN not supported MariaDB as defaultValue');
Expand Down
2 changes: 1 addition & 1 deletion tests/Common/CommonPdoConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public function testTransactionRollbackTransactionOnLevel(): void
'getQueryBuilder',
'getQuoter',
'getSchema',
'getServerVersion',
'getServerInfo',
'isActive',
'open',
'quoteValue',
Expand Down
21 changes: 21 additions & 0 deletions tests/Db/Driver/PDO/PdoServerInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Driver\PDO;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Tests\Support\TestTrait;

class PdoServerInfoTest extends TestCase
{
use TestTrait;

public function testGetVersion(): void
{
$db = $this->getConnection();
$serverInfo = $db->getServerInfo();

$this->assertIsString($serverInfo->getVersion());
}
}
17 changes: 13 additions & 4 deletions tests/Db/QueryBuilder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Tests\Db\QueryBuilder;

use JsonException;
use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function testBatchInsert(
$db = $this->getConnection();

$schemaMock = $this->createMock(Schema::class);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock, $db->getServerInfo());
$params = [];

try {
Expand Down Expand Up @@ -147,7 +148,7 @@ public function testCreateView(): void

$schemaMock = $this->createMock(Schema::class);
$subQuery = (new Query($db))->select('{{bar}}')->from('{{testCreateViewTable}}')->where(['>', 'bar', '5']);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock, $db->getServerInfo());

$this->assertSame(
<<<SQL
Expand Down Expand Up @@ -204,7 +205,7 @@ public function testInsert(
$db = $this->getConnection();

$schemaMock = $this->createMock(Schema::class);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock, $db->getServerInfo());

$this->assertSame($expectedSQL, $qb->insert($table, $columns, $params));
$this->assertSame($expectedParams, $params);
Expand Down Expand Up @@ -265,7 +266,7 @@ public function testUpdate(
$db = $this->getConnection();

$schemaMock = $this->createMock(Schema::class);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock);
$qb = new QueryBuilder($db->getQuoter(), $schemaMock, $db->getServerInfo());

$sql = $qb->update($table, $columns, $condition, $params);
$sql = $qb->quoter()->quoteSql($sql);
Expand Down Expand Up @@ -341,4 +342,12 @@ public function testPrepareValueNonStreamResource(): void

$qb->prepareValue(stream_context_create());
}

public function testGetServerInfo(): void
{
$db = $this->getConnection();
$qb = $db->getQueryBuilder();

$this->assertInstanceOf(ServerInfoInterface::class, $qb->getServerInfo());
}
}
1 change: 1 addition & 0 deletions tests/Support/Stub/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getQueryBuilder(): QueryBuilderInterface
$this->queryBuilder = new QueryBuilder(
$this->getQuoter(),
$this->getSchema(),
$this->getServerInfo(),
);
}

Expand Down
18 changes: 11 additions & 7 deletions tests/Support/Stub/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

namespace Yiisoft\Db\Tests\Support\Stub;

use Yiisoft\Db\Connection\ServerInfoInterface;
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;

final class QueryBuilder extends AbstractQueryBuilder
{
public function __construct(QuoterInterface $quoter, SchemaInterface $schema)
public function __construct(QuoterInterface $quoter, SchemaInterface $schema, ServerInfoInterface $serverInfo)
{
$ddlBuilder = new DDLQueryBuilder($this, $quoter, $schema);
$dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
$dqlBuilder = new DQLQueryBuilder($this, $quoter);
$columnDefinitionBuilder = new ColumnDefinitionBuilder($this);

parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder, $columnDefinitionBuilder);
parent::__construct(
$quoter,
$schema,
$serverInfo,
new DDLQueryBuilder($this, $quoter, $schema),
new DMLQueryBuilder($this, $quoter, $schema),
new DQLQueryBuilder($this, $quoter),
new ColumnDefinitionBuilder($this),
);
}
}
Loading