<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # 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
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看