hectororm/connection

Hector 连接模块

v1.0.0-beta6 2022-09-05 09:11 UTC

This package is auto-updated.

Last update: 2024-09-19 17:01:27 UTC


README

Latest Version Software license Build Status Quality Grade Total Downloads

Hector 连接模块 是 Hector ORM 的 PDO 连接模块。可以独立于 ORM 使用。

安装

Composer

您可以使用 Composer 安装 Hector 连接模块,这是推荐的安装方式。

$ composer require hectororm/connection

依赖项

  • PHP ^8.0
  • PHP 扩展
    • PDO

用法

创建连接

连接信息必须以 DSN 格式传递,密码包含在内。

创建简单连接

use Hector\Connection\Connection;

$connection = new Connection('mysql:host:...');

您可以使用读写方式创建复杂连接

use Hector\Connection\Connection;

$connection = new Connection(dsn: 'mysql:host:...', readDsn: 'mysql:host:...');

查询

执行

执行语句并获取受影响的行数。

use Hector\Connection\Connection;

$connection = new Connection('mysql:host:...');
$nbAffectedRows = $connection->execute('UPDATE `table` SET `foo` = ? WHERE `id` = ?', ['bar', 1]);

该方法在写 DSN 上执行查询。

获取

有三种方法可用于获取结果。当有多个结果时,方法返回一个 Generator

您可以将参数传递给方法的第二个参数。

所有获取方法都在读 DSN 上执行。

获取一个

从语句中获取一个结果。如果没有结果,返回 NULL

use Hector\Connection\Connection;

$connection = new Connection('mysql:host:...');
$row = $connection->fetchOne('SELECT * FROM `table` WHERE `id` = ?', [1]);
获取全部

从语句中获取所有结果。

use Hector\Connection\Connection;

$connection = new Connection('mysql:host:...');
$rows = $connection->fetchAll('SELECT * FROM `table` WHERE `column` = :bar', ['bar' => 'foo']);

foreach($rows as $row) {
    // ...
}
获取列

从语句中获取所有结果的一个列。

use Hector\Connection\Connection;

$connection = new Connection('mysql:host:...');
$columns = $connection->fetchColumn('SELECT * FROM `table` WHERE `column` = :bar', ['bar' => 'foo'], 2);

foreach($columns as $column) {
    // ...
}

最后插入 ID

您可以使用方法 getLastInsertId() 获取最后插入 ID。

use Hector\Connection\Connection;

$connection = new Connection('mysql:host:...');
$nbAffectedRows = $connection->execute('INSERT INTO `table` ...');
$lastInsertId = $connection->getLastInsertId();

事务

Connection 类有 4 个方法用于处理事务

  • Connection::beginTransaction(): void
  • Connection::commit(): void
  • Connection::rollBack(): void
  • Connection::inTransaction(): bool

如果您开始了一个新的事务,尽管已经有一个事务开始了,新的事务将被忽略,并且需要多次调用 commitrollBack 方法。

连接设置

您可以使用 ConnectionSet 类创建一组 Connection 对象。需要命名不同的连接,默认名称存储在常量 Connection::DEFAULT_NAME 中。

use Hector\Connection\Connection;
use Hector\Connection\ConnectionSet;

$connectionSet = new ConnectionSet();
$connectionSet->addConnection(new Connection('mysql:host:...'));
$connectionSet->addConnection(new Connection('mysql:host:...', name: 'foo'));

$connectionSet->hasConnection(); // TRUE
$connectionSet->hasConnection(Connection::DEFAULT_NAME); // TRUE
$connectionSet->hasConnection('foo'); // TRUE
$connectionSet->hasConnection('bar'); // FALSE

$connectionSet->getConnection(); // FIRST CONNECTION
$connectionSet->getConnection(Connection::DEFAULT_NAME); // FIRST CONNECTION
$connectionSet->getConnection('foo'); // SECOND CONNECTION
$connectionSet->getConnection('bar'); // THROW NotFoundException EXCEPTION

记录器

有一个记录器可用于记录查询。

use Hector\Connection\Connection;
use Hector\Connection\Log\LogEntry;
use Hector\Connection\Log\Logger;

$logger = new Logger();
$connection = new Connection('mysql:host:...', logger: $logger);

/** @var LogEntry[] $logEntries */
$logEntries = $logger->getLogs();