php-comp/lite-db
Composer 安装命令:
composer require php-comp/lite-db
包简介
a simple extended pdo tool library of the php
README 文档
README
Simple database client for mysql,sqlite by PDO
Install
- By composer require
composer require phppkg/pdox
- By composer.json
{
"require": {
"phppkg/pdox": "~1.0.0"
}
}
- Pull directly
git clone https://github.com/phppkg/pdox.git
Usage
create connection
use PhpComp\PdoX\PdoX; $db = PdoX::create([ // open debug, will record query logs. 'debug' => 1, 'driver' => 'mysql', // 'sqlite' 'pgsql' 'mssql' 'host' => 'localhost', 'user' => 'root', 'password' => 'password', 'database' => 'test', ]); // add event listeners. $db->on(PdoX::CONNECT, function ($db) { echo "connect database success\n"; }); $db->on(PdoX::BEFORE_EXECUTE, function ($sql) { echo "Will run SQL: $sql\n"; }); $db->on(PdoX::DISCONNECT, function ($db) { echo "disconnect database success\n"; });
basic
$db->exec('CREATE TABLE IF NOT EXISTS `user` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(32) NOT NULL, `nickname` VARCHAR(32), primary key(id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;'); // fetch all $ret = $db->fetchAll('show tables'); var_dump($ret);
insert
// insert one $ret = $db->insert('user', [ 'username' => 'tom', 'nickname' => 'tom-nick', ], [ 'returnSql' => 1, ]); var_dump($ret); // batch insert $ret = $db->insertBatch('user',[ [ 'username' => 'tom', 'nickname' => 'tom-nick', ], [ 'username' => 'tom1', 'nickname' => 'tom-nick2', ], ], [ 'returnSql' => 1, ]); var_dump($ret);
query
// find one // SQL: SELECT * FROM `user` WHERE `id`= ? LIMIT 1 $ret = $db->queryOne('user', ['id' => 3], '*', [ 'fetchType' => 'assoc', 'returnSql' => 1, ]); var_dump($ret); // find all // SQL: SELECT * FROM `user` WHERE `username` like ? LIMIT 1000 $ret = $db->queryAll('user', [ ['username', 'like', '%tes%'] ], '*', [ 'fetchType' => 'assoc', 'limit' => 10, 'returnSql' => 1, ]); var_dump($ret); // more conditions $ret = $db->queryAll('user', [ 'userId' => 23, // 'AND `userId` = 23' 'title' => 'test', // value will auto add quote, equal to "AND title = 'test'" 'status' => [1, 2], // status IN (1,2) ['publishAt', '>', '0'], // ==> 'AND `publishAt` > 0' ['createdAt', '<=', 1345665427, 'OR'], // ==> 'OR `createdAt` <= 1345665427' ['id', 'IN' ,[4,5,56]], // ==> '`id` IN ('4','5','56')' ['id', 'NOT IN', [4,5,56]], // ==> '`id` NOT IN ('4','5','56')' // a closure function () { return 'a < 5 OR b > 6'; } ]); // SQL: SELECT * FROM `user` WHERE "name"= ? OR ( "type"= ? AND "createAt" <= ? ) AND "status" IN ('1','2') $ret = $db->queryAll('user', [ 'name' => 'tom', 'or' => '(', 'type' => 'admin', ['createAt', '<=', \date('Y-m-d H:i:s')], ')', 'status' => [1,2] ]);
update
$ret = $db->update('user', ['id' => 2], [ 'username' => 'tom', 'nickname' => 'tom-nick', ], [ 'returnSql' => 1, ]); var_dump($ret);
delete
$ret = $db->delete('user', ['id' => 2], [ 'returnSql' => 1, 'limit' => 1, ]); var_dump($ret);
get query logs
var_dump($db->getQueryLogs());
Methods docs
https://phppkg.github.io/pdox/classes/master/Inhere/PdoX/PdoX.html
License
MIT
php-comp/lite-db 适用场景与选型建议
php-comp/lite-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「library」 「db」 「mysql」 「query」 「mongo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-comp/lite-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-comp/lite-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-comp/lite-db 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 60
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-20