phossa/phossa-db

PHP 数据库库

1.0.2 2016-04-18 02:24 UTC

This package is not auto-updated.

Last update: 2024-09-12 00:16:00 UTC


README

Build Status HHVM Latest Stable Version License

简介

phossa-db 是一个 PHP 数据库连接管理库,用于处理与数据库的交互。

它需要 PHP 5.4,并支持 PHP 7.0+、HHVM。它符合 PSR-1PSR-2PSR-4

特性

  • 简单接口。没有你不需要的。

  • 支持多种数据库平台/驱动,目前支持 PDO(所有 PDO 驱动)和 Mysqli。

  • 通过驱动管理器处理多个连接

    • 轮询负载均衡

      使用轮询方式使用多个数据库连接,并支持权重因子(1-10)。每个连接都会被监控(pinged)。

    • 驱动标签,用户可以将不同的数据库连接标记为 'reader' 或 'writer'

  • 易于分析,获取每个执行的 SQL 和其执行时间。

  • 安全。所有 SQL 都通过低级驱动器的 prepare/execute 执行。

入门

  • 安装

    通过 composer 工具安装。

    composer require "phossa/phossa-db=1.*"
    

    或者在您的 composer.json 中添加以下行

    {
        "require": {
          "phossa/phossa-db": "1.*"
        }
    }

使用方法

  • 驱动

    • 使用 execute() 执行 DDL

      $db = new Phossa\Db\Pdo\Driver([
          'dsn' => 'mysql:dbname=test;host=127.0.0.1;charset=utf8'
      ]);
      
      // simple delete
      $res = $db->execute("DELETE FROM test WHERE id < 10");
      if (false === $res) {
        	echo $db->getError() . \PHP_EOL;
      } else {
        	echo sprintf("Deleted %d records", $res) . \PHP_EOL;
      }
      
      // with parameters
      $res = $db->execute("INSERT INTO test (name) VALUES (?)", [ 100 ]);
      if ($res) {
      	$id = (int) $db->getLastInsertId();
      }
    • 使用 query() 执行 SELECT

      // simple select
      $res = $db->query("SELECT * FROM test WHERE id < 10");
      if (false === $res) {
        	echo $db->getError() . \PHP_EOL;
      } else {
        	$rows = $res->fetchAll();
      }
      
      // with parameters & fetch first 5 rows
      $res = $db->query("SELECT * FROM test WHERE id > ? LIMIT ?", [10, 20]);
      if ($res && $res->isQuery()) {
      	$firstFiveRows = $res->fetchRow(5);
      }
      
      // fetch first field
      $res = $db->query("SELECT id, name FROM test WHERE id < :id", ['id' => 10]);
      if ($res && $res->isQuery()) {
      	$firstCols = $res->fetchCol('id');
      }
  • 语句

    Statement$db->prepare() 返回。

    // PREPARE using prepare()
    $stmt = $db->prepare("SELECT * FROM test WHERE id < :id");
    if (false === $stmt) {
        echo $db->getError() . \PHP_EOL;
    } else {
        $res = $stmt->execute(['id' => 10]);
        if (false === $res) {
           echo $db->getError() . \PHP_EOL;
        } else {
           $rows = $res->fetchAll();
        }
    }
  • 结果

    Result$db->execute()$db->query()$stmt->execute() 返回。

    $res = $db->query(...);
    if ($res) {
        // SELECT
        if ($res->isQuery()) {
            // get fields count
            $fieldCount = $res->fieldCount();
            // row count
            $rowCount   = $res->rowCount();
    
        // DDL
        } else {
            $affectedRows = $res->affectedRows();
        }
    }

驱动管理器

驱动管理器管理多个数据库连接。权重因子 N 表示虚拟添加 N 次驱动。将权重因子为 5 的驱动 A 和权重因子为 1 的驱动 B 添加到池中,意味着在调用 getDriver() 时,用户将获得 A 五次,而 B 一次。

// writable connect 1
$db1 = (new Phossa\Db\Pdo\Driver($conf1))->addTag('RW');

// dbreader 2
$db2 = (new Phossa\Db\Pdo\Driver($conf2))->addTag('RO');

// dbreader 3
$db3 = (new Phossa\Db\Pdo\Driver($conf3))->addTag('RO');

// db manager
$dbm = (new Phossa\Db\Manager\Manager())
    ->addDriver($db1, 1)    // writable connection with factor 1
    ->addDriver($db2, 5)	// read_only, factor 5
    ->addDriver($db3, 5)	// read_only, factor 5

// get a db connect, no matter writable or read only
$db = $dbm->getDriver();

// get a readonly driver
$db = $dbm->getDriver('RO');

SQL 分析

获取执行的 SQL 和其执行时间。

// init driver
$db = new Phossa\Db\Pdo\Driver($conf);

// enable profiling
$db->enableProfiling();

// execute a DELETE
$db->execute("DELETE FROM test WHERE test_id > 10");

// get sql
$sql  = $db->getProfiler()->getSql();
$time = $db->getProfiler()->getExecutionTime();

依赖

  • PHP >= 5.4.0

  • phossa/phossa-shared ~1.0.10

许可

MIT 许可证