大华家/数据库

3.0 2024-01-07 15:48 UTC

This package is auto-updated.

Last update: 2024-09-07 17:40:27 UTC


README

Tests

大华家/数据库是一个小巧的PHP库,它提供了一个基于PDO的MySQL包装器。其主要功能是为基本的PDO对象提供一些额外的功能。

DavaHome\Database\Adapter\MySQL类直接继承自PDO,并提供其所有方法。还有一些附加功能,例如

  • PDO语句缓存(如果查询未更改,则重用PDO语句)
  • 基本操作方法(如选择、删除等)

安装

php composer.phar require davahome/database

基本操作方法

这些方法由DavaHome\Database\Adapter\AdapterInterface强制执行,并支持所有数据库处理器

select

从数据库中选择行。where语句是一个关联数组,采用tableColumn => value约定。

/**
 * Select from database
 *
 * @param string $table
 * @param array  $where
 *
 * @return mixed
 */
public function select(string $table, array $where): mixed;

update

更新数据库中现有的行。值是一个关联数组,与select方法中的where语句完全相同。

/**
 * Update a row
 *
 * @param string $table
 * @param array  $values key=>value
 * @param array  $where  key=>value where condition (will be combined using AND)
 * @param bool   $allowEmptyWhere
 *
 * @return mixed
 * @throws \DavaHome\Database\DatabaseException
 */
public function update(string $table, array $values, array $where, bool $allowEmptyWhere = false): mixed;

insert

向数据库中插入一行新数据。值是一个关联数组,与select方法中的where语句完全相同。

/**
 * Insert a new row
 *
 * @param string $table
 * @param array  $values key=>value
 *
 * @return mixed
 */
public function insert(string $table, array $values): mixed;

delete

从数据库中删除现有的行。where语句与select方法中的完全相同。

/**
 * Delete from database
 *
 * @param string $table
 * @param array  $where
 * @param bool   $allowEmptyWhere
 *
 * @return mixed
 * @throws \DavaHome\Database\DatabaseException
 */
public function delete(string $table, array $where, bool $allowEmptyWhere = false): mixed;

MySQL

示例

use DavaHome\Database\Adapter\Mysql;

$db = Mysql::create(
    Mysql::DRIVER_MYSQL,
    'localhost',
    'root',
    '',
    'database',
    [
        Mysql::ATTR_DEFAULT_FETCH_MODE => Mysql::FETCH_ASSOC,
        Mysql::ATTR_AUTOCOMMIT         => 1,
    ]
);

// Select row
$pdoStatement = $db->select('table', ['id' => 1]); // Returns \PDOStatement

// Update row
$pdoStatement = $db->update('table', ['foo' => 'bar'], ['id' => 1]); // Returns \PDOStatement

// Insert row
$pdoStatement = $db->insert('table', ['foo' => 'bar']); // Returns \PDOStatement

// Delete row
$pdoStatement = $db->delete('table', ['id' => 1]); // Returns \PDOStatement

其他MySQL方法

createUuid

此方法创建一个uuid,可以作为非自增唯一索引使用。有关更多信息,请参阅MySQL文档

/**
 * Let the database create a UUID
 *
 * @return string
 */
public function createUuid(): string;

execute

创建一个将要直接执行的准备语句

/**
 * Create and execute a prepared statement immediately
 *
 * @param string $statement
 * @param array  $inputParameters
 * @param array  $driverOptions
 *
 * @return mixed|\PDOStatement
 */
public function execute($statement, array $inputParameters = [], array $driverOptions = []): PDOStatement;

setIsolationLevel

设置当前连接的事务隔离级别

/**
 * Set the isolation level
 *
 * @param string $isolationLevel
 *
 * @return bool
 */
public function setIsolationLevel(string $isolationLevel): bool;

高级查询

为了提供更高级的基本操作方法功能,有一些附加的类。

DirectValue

DirectValue类允许通过基本操作方法使用MySQL函数或增量查询。所有传递给DirectValue类的参数都将1-2-1传递到查询中。这些值将不会进行转义!

use DavaHome\Database\Extension\DirectValue;

// The query will look like this: UPDATE `table` SET `last_updated` = NOW() WHERE `id` = 1
$db->update('table', ['last_updated' => new DirectValue('NOW()')], ['id' => 1]);

// The query will look like this: UPDATE `table` SET `count` = `count` + 1 WHERE `id` = 1
$db->update('table', ['count' => new DirectValue('`count` + 1')], ['id' => 1]);

CustomOperator

CustomOperator类允许覆盖所有基本操作方法使用的默认操作符(=)。您还可以将CustomOperator与DirectValue类结合使用。

use DavaHome\Database\Extension\CustomOperator;

// The query will look like this: SELECT * FROM `table` WHERE `count` >= 2
$db->select('table', ['count' => new CustomOperator('>=', 2)]);

// The query will look like this: SELECT * FROM `table` WHERE `last_updated` <= NOW()
$db->select('table', ['last_updated' => new CustomOperator('<=', new DirectValue('NOW()'))]);