swlib/swpdo 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

swlib/swpdo

最新稳定版本:1.0.0-alpha

Composer 安装命令:

composer require swlib/swpdo

包简介

Swoole Coroutine SQL component like PDO

README 文档

README

Latest Version Build Status Php Version Swoole Version SwPDO License

Introduction

Traditional PDO to Swoole Coroutine migration plan without cost.

中文文档

Install

The best way to install: Composer :

composer require swlib/swpdo

Coroutine

The bottom layer of Swoole implements coroutine scheduling, and the business layer does not need to be aware. Developers can use synchronization code writing methods to achieve the effect and ultra-high performance of asynchronous IO without perception, avoiding the discrete code logic and trapping caused by traditional asynchronous callbacks. Too many callback layers causes the code too difficult to maintain.

It needs to be used in event callback functions such as onRequet, onReceive, and onConnect, or wrapped using the go keyword (swoole.use_shortname is on by default).


Example

Because PDO uses multiple engines, it is difficult at the PHP level to return different instances with class - implemented constructors.

Except that the constructor is different, all methods use in the same way.

query

$options = [
    'mysql:host=127.0.0.1;dbname=test;charset=UTF8',
    'root',
    'root'
];
$sql = 'select * from `user` LIMIT 1';

//PDO
$pdo = new \PDO(...$options);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //strong type
$pdo_both = $pdo->query($sql)->fetch();
$pdo_assoc = $pdo->query($sql)->fetch(\PDO::FETCH_ASSOC);
$pdo_object = $pdo->query($sql)->fetch(\PDO::FETCH_OBJ);
$pdo_number = $pdo->query($sql)->fetch(\PDO::FETCH_NUM);

//SwPDO
$swpdo = SwPDO::construct(...$options); //default is strong type
$swpdo_both = $swpdo->query($sql)->fetch();
$swpdo_assoc = $swpdo->query($sql)->fetch(\PDO::FETCH_ASSOC);
$swpdo_object = $swpdo->query($sql)->fetch(\PDO::FETCH_OBJ);
$swpdo_number = $swpdo->query($sql)->fetch(\PDO::FETCH_NUM);

var_dump($pdo_both === $swpdo_both);
var_dump($pdo_assoc === $swpdo_assoc);
var_dump($pdo_object == $swpdo_object);
var_dump($pdo_number === $swpdo_number);
//output: true true true true

prepare

//PDO
$pdo = new \PDO(...$options);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //strong type
$statement = $pdo->prepare($sql);
$statement->execute();
$pdo_fetch = $statement->fetch(\PDO::FETCH_ASSOC);
$statement->execute();
$pdo_fetch_all = $statement->fetchAll();
$statement->execute();
$pdo_fetch_all_column = $statement->fetchAll(\PDO::FETCH_COLUMN, 1);
$statement->execute();
$pdo_fetch_column = $statement->fetchColumn();
$statement->execute();

//SwPDO
$swpdo = SwPDO::construct(...$options);
$statement = $swpdo->prepare($sql);
$statement->execute();
$swpdo_fetch = $statement->fetch(\PDO::FETCH_ASSOC);
$statement->execute();
$swpdo_fetch_all = $statement->fetchAll();
$statement->execute();
$swpdo_fetch_all_column = $statement->fetchAll(\PDO::FETCH_COLUMN, 1);
$statement->execute();
$swpdo_fetch_column = $statement->fetchColumn();
$statement->execute();

var_dump($pdo_fetch === $swpdo_fetch); //true
var_dump($pdo_fetch_all === $swpdo_fetch_all); //true
var_dump($pdo_fetch_all_column === $swpdo_fetch_all_column); //true
var_dump($pdo_fetch_column === $swpdo_fetch_column); //true

bind

//PDO
$pdo = new \PDO(...$options);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //strong type
$statement = $pdo->prepare($sql);
$statement->execute(['id' => 1]);
$pdo_bind_exec = $statement->fetch(\PDO::FETCH_ASSOC);

$statement->bindValue(':id', 1);
$statement->execute();
$pdo_bind_val = $statement->fetch(\PDO::FETCH_ASSOC);

$statement->bindParam(':id', $id);
$statement->execute();
$pdo_bind_param = $statement->fetch(\PDO::FETCH_ASSOC);

//SwPDO
$swpdo = SwPDO::construct(...$options);
$statement = $swpdo->prepare($sql);
$statement->execute(['id' => 1]);
$swpdo_bind_exec = $statement->fetch(\PDO::FETCH_ASSOC);

$statement->bindValue(':id', 1);
$statement->execute();
$swpdo_bind_val = $statement->fetch(\PDO::FETCH_ASSOC);

$statement->bindParam(':id', $id);
$statement->execute();
$swpdo_bind_param = $statement->fetch(\PDO::FETCH_ASSOC);

var_dump($pdo_bind_exec === $swpdo_bind_exec); //true
var_dump($pdo_bind_val === $swpdo_bind_val); //true
var_dump($pdo_bind_param === $swpdo_bind_param); //true

统计信息

  • 总下载量: 11.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 61
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 61
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2018-04-07