drift/dbal

基于 Doctrine 的 ReactPHP DBAL

0.1.5 2022-04-14 16:36 UTC

This package is auto-updated.

Last update: 2024-09-19 21:30:53 UTC


README

CircleCI

这是一个基于 ReactPHP SQL 客户端和 Doctrine 模型实现的 DBAL。您将能够使用

  • Doctrine QueryBuilder 模型
  • Doctrine Schema 模型
  • 常用操作的简单快捷方式
  • 以及其他更多支持正在添加中

注意。目前仅作为概念验证。在第一个稳定版本标记之前,不要在生产环境中使用此库。

示例

让我们创建一个示例,看看这个库真正能做什么。对于这个示例,我们将创建一个 Mysql 适配器,并使用 Doctrine QueryBuilder 在数据库中创建一个新元素并查询一些行。

由于我们将使用 Mysql 适配器,您应该已经安装了基于 ReactPHP 的 mysql 库 react/mysql。因为此库处于开发阶段,所有适配器依赖项都将加载以进行测试目的。

首先,我们需要使用所选平台驱动程序创建一个 Connection 实例。我们还需要创建一个包含所有连接数据的 Credentials 实例。

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Drift\DBAL\Connection;
use Drift\DBAL\Driver\Mysql\MysqlDriver;
use Drift\DBAL\Credentials;
use React\EventLoop\Factory as LoopFactory;

$loop = LoopFactory::create();
$mysqlPlatform = new MySqlPlatform();
$mysqlDriver = new MysqlDriver($loop);
$credentials = new Credentials(
   '127.0.0.1',
   '3306',
   'root',
   'root',
   'test'
);

$connection = Connection::createConnected(
    $mysqlDriver,
    $credentials,
    $mysqlPlatform
);

一旦我们有了连接,我们就可以通过使用 Doctrine QueryBuilder 或内置的直接方法在数据库中创建一个新的注册。所有这些调用的结果将是一个 Promise 接口,最终将返回一个 Result 实例。

use Drift\DBAL\Connection;
use Drift\DBAL\Result;

/**
* @var Connection $connection
 */
$promise = $connection
    ->insert('test', [
        'id' => '1',
        'field1' => 'val1',
        'field2' => 'val2',
    ])
    ->then(function(Result $_) use ($connection) {
        $queryBuilder = $connection->createQueryBuilder();
        
        return $connection
            ->query($queryBuilder)
            ->select('*')
            ->from('test', 't')
            ->where($queryBuilder->expr()->orX(
                $queryBuilder->expr()->eq('t.id', '?'),
                $queryBuilder->expr()->eq('t.id', '?')
            ))
            ->setParameters(['1', '2']);
    })
    ->then(function(Result $result) {
        $numberOfRows = $result->fetchCount();
        $firstRow = $result->fetchFirstRow();
        $allRows = $result->fetchAllRows();
    });

您现在可以使用 mysqlpostgresqlsqlite 的适配器。

连接快捷方式

此 DBAL 引入了一些对在 Doctrine 查询构建器和参数化转义之上的项目有用的快捷方式。

插入

在表中插入一行。需要表和包含字段及其值的数组。返回一个 Promise。

$connection->insert('test', [
    'id' => '1',
    'field1' => 'value1'
]);

更新

更新表中现有的一行。需要表、一个作为数组的标识符以及一个包含字段及其值的数组的字段。返回一个 Promise。

$connection->update(
    'test',
    ['id' => '1'],
    [
        'field1' => 'value1',
        'field2' => 'value2',
    ]
);

Upsert

如果不存在则插入一行。否则,它将使用给定的值更新现有行。需要表、一个作为数组的标识符以及一个包含字段及其值的数组的字段。返回一个 Promise。

$connection->upsert(
    'test',
    ['id' => '1'],
    [
        'field1' => 'value1',
        'field2' => 'value2',
    ]
);

删除

如果存在则删除一行。需要表和一个作为数组的标识符。返回一个 Promise。

$connection->delete('test', [
    'id' => '1'
]);

根据 where 子句查找一行。需要表和一个包含字段及其值的数组。返回一个包含最终结果的 Promise,该结果为找到的所有行的数组。

$connection
    ->findOneById('test', [
        'id' => '1'
    ])
    ->then(function(?array $result) {
        if (is_null($result)) {
            // Row with ID=1 not found
        } else {
            // Row with ID=1 found.
            echo $result['id'];
        }   
    });

根据一个包含 where 子句的数组查找所有行。需要表和一个包含字段及其值的数组。返回一个包含最终结果的 Promise,该结果为找到的所有行的数组。

$connection
    ->findBy('test', [
        'age' => '33'
    ])
    ->then(function(array $result) {
        echo 'Found ' . count($result) . ' rows'; 
    });

创建表

您可以使用基本信息轻松创建一个新表。需要表名和一个包含字段及其类型的数组。字符串被视为长度为 255。数组中的第一个字段位置将被视为主键。返回一个包含最终连接的 Promise。

$connection->createTable('test', [
    'id' => 'string',
    'name' => 'string',
]);

这是一个基本的表创建方法。要创建更复杂的表,您可以使用 Doctrine 的 Schema 模型。您可以使用此方法生成的所有 Schema SQL 在名为 executeSchema 的连接中执行。您可以在 Doctrine 文档 中找到有关此 Schema 模型的更多信息

$schema = new Schema();
$table = $schema->createTable('test');
$table->addColumn('id', 'string');
// ...

$connection->executeSchema($schema);

删除表

您可以轻松删除现有的表。只需要表名,最终会返回连接。

$connection->dropTable('test');

截断表

您可以轻松截断现有的表。只需要表名,最终会返回连接。

$connection->truncateTable('test');

测试

您可以通过运行 docker-compose up 和执行 php vendor/bin/phpunit 来运行测试。