代码拉取完成,页面将自动刷新
同步操作将从 sethink/swoole-orm 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
基于swoole的mysql协程连接池,简单封装。
实现多个协程间共用同一个协程客户端
>composer require sethink/swoole-orm
<?php
namespace Demo;
include_once "./vendor/autoload.php";
use sethink\swooleOrm\Db;
use sethink\swooleOrm\MysqlPool;
use swoole;
class Demo
{
protected $server;
protected $MysqlPool;
public function __construct()
{
$this->server = new Swoole\Http\Server("0.0.0.0", 9501);
$this->server->set(array(
'worker_num' => 4,
'max_request' => 50000,
'reload_async' => true,
'max_wait_time' => 30,
));
$this->server->on('Start', function ($server) {});
$this->server->on('ManagerStart', function ($server) {});
$this->server->on('WorkerStart', array($this, 'onWorkerStart'));
$this->server->on('WorkerStop', function ($server, $worker_id) {});
$this->server->on('open', function ($server, $request) {});
$this->server->on('Request', array($this, 'onRequest'));
$this->server->start();
}
public function onWorkerStart($server, $worker_id)
{
$config = [
'host' => '127.0.0.1', //服务器地址
'port' => 3306, //端口
'user' => 'root', //用户名
'password' => 'root', //密码
'charset' => 'utf8', //编码
'database' => 'test', //数据库名
'prefix' => 'sethink_', //表前缀
'poolMin' => 5, //空闲时,保存的最大链接,默认为5
'poolMax' => 1000, //地址池最大连接数,默认1000
'clearTime' => 60000, //清除空闲链接定时器,默认60秒,单位ms
'clearAll' => 300000 //空闲多久清空所有连接,默认5分钟,单位ms
];
$this->MysqlPool = new MysqlPool($config);
unset($config);
//执行定时器
$this->MysqlPool->clearTimer($server);
}
public function onRequest($request, $response)
{
$rs = Db::init($this->MysqlPool)
->name('tt')
->select();
var_dump($rs);
}
}
new Demo();
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username,info')
->where(['username'=>'sethink','password'=>'sethink'])
->find();
<?php
Db::init($this->MysqlPool)
->name('info')
->field('id,username,password,info')
->select();
<?php
$data = [
'username' => 'sethink2',
'password' => 'sethink2',
'info' => 'ceshi2'
];
Db::init($this->MysqlPool)
->name('user_info')
->insert($data);
<?php
$data = [
[
'username' => 'sethink3',
'password' => 'sethink3',
'info' => 'ceshi3'
],
[
'username' => 'sethink4',
'password' => 'password4',
'info' => 'ceshi4'
]
];
Db::init($this->MysqlPool)
->name('user_info')
->insertAll($data);
<?php
Db::init($this->MysqlPool)
->name('user_info')
->where(['username'=>'sethink4'])
->update(['password'=>'sethink4-4']);
<?php
Db::init($this->MysqlPool)
->name('user_info')
->where(['username'=>'sethink4'])
->delete();
$server为swoole服务器
$tableName为表名 -- 字符串
$field为查询的字段名 -- 字符串
order by排序 -- 数组(一维数组或者二维数组)
例子: $order为一维数组时
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->order(['id'=>'desc'])
->select();
$order为二维数组时
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->order([['id'=>'desc'],['info'=>'asc']])
->select();
group by分组 -- 字符串
例子:
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->group('info')
->select();
### having($having)
用于配置group从分组中筛选数据 -- 字符串
例子:
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->group('info')
->having('count(info) > 5')
->select();
数据去重
$distinct为bool值
例子:
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->distinct(true)
->select();
加锁
例子:
<?php
//1、传入bool值
Db::init($this->MysqlPool)
->name('user_info')
->where(['id'=>1])
->lock(true)
->find();
//会自动在sql语句加上FOR UPDATE
//2、传入字符串
Db::init($this->MysqlPool)
->name('user_info')
->where(['id'=>1])
->lock('lock in share mode')
->find();
//特殊锁要求
获取sql语句
例子:
<?php
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->fetchSql()
->select();
$whereArray为数组
例子1:
<?php
//1、
$where = [
'id'=>'1'
];
//2、
$where = [
'id'=>['>',5]
];
//3、
$where = [
'username'=>['LIKE','%seth%']
];
//4、
$where = [
'id'=>['in',['1','5']]
];
Db::init($this->MysqlPool)
->name('user_info')
->field('id,username')
->where($where)
->select();
查询一条数据,返回一维数组
查询一条或多条数据,返回二维数组
插入单条数据
$data为一维数组
插入多条数据
$data为二维数组
更新数据
$data为一维数组
删除数据
执行sql语句 -- 字符串
例子:
<?php
$sql = 'select * from `user_info`';
Db::init($this->MysqlPool)->query($sql);
开启日志功能
$logArray = [
'类型',
'信息'
];
$logArray为一维数组,长度为2
$logArray[0]是日志类型
$logArray[1]是日志信息
例子:
<?php
$Db::init($this->MysqlPool)
->name('user_info')
->where(['username'=>'sethink'])
->log(['查询用户信息','用户名sethink'])
->find();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。