# Mysql
[TOC]
SD提供了Mysql異步連接池。這里提供3.1版本全新的API,新版Mysql支持SQL預處理請求,不用再擔心安全問題了。
## 遷移指南
之前的版本遷移到新版時需要注意:
1. coroutineSend已經被棄用,更換為query。
2. 事務已改版
```
public function query($sql = null, callable $set = null)
```
## 創建Mysql連接池
在SD的initAsynPools方法中已經創建好了redis和mysql默認的連接池。
```
/**
* 初始化各種連接池
* @param $workerId
*/
public function initAsynPools($workerId)
{
$this->asynPools = [];
if ($this->config->get('redis.enable', true)) {
$this->asynPools['redisPool'] = new RedisAsynPool($this->config, $this->config->get('redis.active'));
}
if ($this->config->get('mysql.enable', true)) {
$this->asynPools['mysqlPool'] = new MysqlAsynPool($this->config, $this->config->get('mysql.active'));
}
$this->redis_pool = $this->asynPools['redisPool'] ?? null;
$this->mysql_pool = $this->asynPools['mysqlPool'] ?? null;
}
```
如果你想創建多個Mysql連接池可以仿照上面的方法。
```
$this->addAsynPool('mysqlPool2', new MysqlAsynPool($this->config, ‘mysql2’);
```
## 獲取Mysql連接池
在Controller,Model,Task中默認獲取了MysqlPool。
```
/**
* 當被loader時會調用這個方法進行初始化
* @param $context
*/
public function initialization(&$context)
{
$this->setContext($context);
$this->redis = $this->loader->redis("redisPool");
$this->db = $this->loader->mysql("mysqlPool",$this);
}
```
### 返回結構
通過query默認會返回一個數組。
```
{
"result": [
],
"affected_rows": 0,
"insert_id": 0,
"client_id": 0
}
```
result是返回的結果必定是個數組,affected_rows影響的行數,insert_id插入的id。
### 直接執行sql
```
$this->db->query("
CREATE TABLE IF NOT EXISTS `MysqlTest` (
`peopleid` smallint(6) NOT NULL AUTO_INCREMENT,
`firstname` char(50) NOT NULL,
`lastname` char(50) NOT NULL,
`age` smallint(6) NOT NULL,
`townid` smallint(6) NOT NULL,
PRIMARY KEY (`peopleid`),
UNIQUE KEY `unique_fname_lname`(`firstname`,`lastname`),
KEY `fname_lname_age` (`firstname`,`lastname`,`age`)
) ;
");
```
### Select
```
$value = $this->db->Select('*')
->from('MysqlTest')
->where('townid', 10000)->query();
```
### Update
```
$value = $this->db->update('MysqlTest')
->set('age', '20')
->where('townid', 10000)->query();
```
### Update 字段值的增加/減少
```
$value = $this->db->update('MysqlTest')
->set("score","score+1",false)
->where('townid', 10000)->query();
```
### Replace
```
$value = $this->mysql_pool->dbQueryBuilder->replace('MysqlTest')
->set('firstname', 'White')
->set('lastname', 'Cat')
->set('age', '26')
->set('townid', '10000')->query();
```
### Delete
```
$value = $this->mysql_pool->dbQueryBuilder->delete()
->from('MysqlTest')
->where('townid', 10000)->query();
```
### Raw混合SQL
```
$selectMiner = $this->db->select('*')->from('account');
$selectMiner = $selectMiner->where('', '(status = 1 and dec in ("ss", "cc")) or name = "kk"', Miner::LOGICAL_RAW)->query();
```
### 獲取SQL命令
```
$value = $this->db->insertInto('account')->intoColumns(['uid', 'static'])->intoValues([[36, 0], [37, 0]])->getStatement(true);
```
通過getStatement可以獲取構建器構建的sql語法。
### insertInto,updateInto,replaceInto 批量操作
```
$value = $this->db->insertInto('account')->intoColumns(['uid', 'static'])->intoValues([[36, 0], [37, 0]])->query();
```
批量命令配合intoColumns和intoValues設置批量操作
### 超時
超時設置
```
$result = $this->db->select('*')->from('account')->limit(1)->query(null,function (MySqlCoroutine $mySqlCoroutine){
$mySqlCoroutine->setTimeout(1000);
$mySqlCoroutine->noException("test");
$mySqlCoroutine->dump();
});
echo $result->num_rows();
```
除了超時Mysql提供了一些其他的API
* result_array 直接返回result
* row_array 返回某一個
* row 返回第一個
* num_rows 返回數量
* insert_id 返回insert_id
* dump 打印執行的sql命令
### 事務
begin開啟一個事務,在回調中執行事務,如果拋出異常則自動回滾,如果正常則自動提交。
```
public function http_mysql_begin_coroutine_test()
{
$this->db->begin(function ()
{
$result = $this->db->select("*")->from("account")->query();
var_dump($result['client_id']);
$result = $this->db->select("*")->from("account")->query();
var_dump($result['client_id']);
});
$this->http_output->end(1);
}
```
### 同步Mysql
```
$mysqlSync = $this->mysql_pool->getSync();
```
通過getSync返回一個同步PDO的mysql連接。
同步mysql可以使用pdo開頭的方法。
* pdoQuery
* pdoBeginTrans
* pdoCommitTrans
* pdoRollBackTrans
* pdoInsertId
- SD3.X簡介
- 捐贈SD項目
- VIP服務
- 基礎篇
- 搭建環境
- 使用Composer安裝/更新SD框架
- 啟動命令
- 開發注意事項
- 框架配置
- 配置文件夾
- server.php
- ports.php
- business.php
- mysql.php
- redis.php
- timerTask.php
- log.php
- consul.php
- catCache.php
- client.php
- 自定義配置
- 框架入口
- MVC架構
- 加載器-Loader
- 控制器-Controller
- 模型-Model
- 視圖-View
- 同步任務-Task
- 封裝器
- Swoole編程指南-EOF協議
- Swoole編程指南-固定包頭協議
- 封裝器-Pack
- 路由器
- TCP相關
- 綁定UID
- Send系列
- Sub/Pub
- 獲取服務器信息
- Http相關
- HttpInput
- HttpOutput
- 默認路由規則
- WebSocket相關
- 使用SSL
- 公共函數
- 進階篇
- 內核優化
- 封裝器路由器原理剖析
- 對象池
- 上下文-Context
- 中間件
- 進程管理
- 創建自定義進程
- 進程間RPC
- 自定義進程如何使用連接池
- 異步連接池
- Redis
- Mysql
- Mqtt
- HttpClient
- Client
- AMQP
- RPC
- 日志工具-GrayLog
- 微服務-Consul
- Consul基礎
- 搭建Consul服務器
- SD中Consul配置
- 微服務
- 選舉-Leader
- Consul動態配置定時任務
- 熔斷與降級
- 集群-Cluster
- 高速緩存-CatCache
- 萬物-Actor
- Actor原型
- Actor的創建
- Actor間的通訊
- 消息派發-EventDispatcher
- 延遲隊列-TimerCallBack
- 協程
- 訂閱與發布
- MQTT簡易服務器
- AMQP異步任務調度
- 自定義命令-Console
- 調試工具Channel
- 特別注意事項
- 日常問題總結
- 實踐案例
- 物聯網自定義協議
- Actor在游戲的應用
- Mongodb以及一些同步擴展的使用
- 自定義進程使用MQTT客戶端
- 開發者工具
- SDHelper