/** * [send 兼容Base类封装的send方法,调度器可以不感知client类型] * @param callable $callback */ public function send(callable $callback) { if (!isset($this->db)) { echo " db not init \n"; return; } $config = $this->conf; $this->callback = $callback; $this->calltime = microtime(true); $this->key = md5($this->calltime . $config['host'] . $config['port'] . rand(0, 10000)); $this->db->connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']); if (!empty($config['charset'])) { $this->db->set_charset($config['charset']); } \swoole_mysql_query($this->db, $this->sql, [$this, 'onSqlReady']); }
function async_callback($db, $result) { global $sqls, $start_time; echo "RESULT: " . count($result) . "rows\n"; if (count($sqls[$db->_id]) > 0) { $sql = array_pop($sqls[$db->_id]); swoole_mysql_query($db, $sql, 'async_callback'); } else { unset($sqls[$db->_id]); echo "---------------------------------#{$db->_id} finish--------------------------------\n"; } //全部完成 if (count($sqls) == 0) { echo "time=" . (microtime(true) - $start_time) . "\n"; } }
/** * @param string $sql * @param callable $callback */ protected function doQuery($sql, callable $callback) { deQueue: //remove from idle pool $db = array_pop($this->idle_pool); /** * @var \mysqli $mysqli */ $mysqli = $db['object']; if ($this->haveSwooleAsyncMySQL) { $result = swoole_mysql_query($mysqli, $sql, array($this, 'onSQLReady')); } else { $result = $mysqli->query($sql, MYSQLI_ASYNC); } if ($result === false) { if ($mysqli->errno == 2013 or $mysqli->errno == 2006 or isset($mysqli->_errno) and $mysqli->_errno == 2006) { $mysqli->close(); unset($mysqli); $this->connection_num--; //创建连接成功 if ($this->createConnection() === 0) { goto deQueue; } } else { $this->wait_queue->push(array('sql' => $sql, 'callback' => $callback)); return; } } $task['sql'] = $sql; $task['callback'] = $callback; $task['mysql'] = $db; //join to work pool $this->work_pool[$db['socket']] = $task; }
$db->connect('127.0.0.1', 'root', 'root', 'test'); //$db->connect('10.10.2.205', 'root', '', 'msg_push', 3500); //$sql = "UPDATE `test`.`userinfo` SET `passwd` = '999999' WHERE `userinfo`.`id` =2"; $sql = "INSERT INTO `test`.`userinfo` (`id`, `name`, `passwd`, `regtime`, `lastlogin_ip`) VALUES (NULL, 'jack', 'xuyou', CURRENT_TIMESTAMP, '');"; //$sql = "DELETE FROM `test`.`userinfo` WHERE `userinfo`.`id` = 59;"; //$sql = "SELECT * FROM `userinfo` LIMIT 0, 100"; //$sql = "SELECT id,device_token,os from ec_push_token where 1 and app_key='QueryViolations' and os=2 and version >='5.0.0' and id > 0 limit 10000"; $s = microtime(true); swoole_mysql_query($db, $sql, function (mysqli $db, $r) { global $s; if ($r == false) { var_dump($db->_error, $db->_errno); } elseif ($r == true) { var_dump($db->_affected_rows, $db->_insert_id); } echo "count=" . count($r) . ", time=" . (microtime(true) - $s), "\n"; var_dump($r); swoole_mysql_query($db, "show tables", function ($db, $r) { var_dump($r); }); }); // //$db->query($sql, MYSQLI_ASYNC); //swoole_event_add(swoole_get_mysqli_sock($db), function($__db_sock) { // global $db; // $s = microtime(true); // $res = $db->reap_async_query(); // $r = $res->fetch_all(); // echo "count=".count($r).", time=".(microtime(true) - $s), "\n"; // exit(0); //});