Repository navigation
Expand file tree
/
Copy pathMySQL.php
More file actions
73 lines (67 loc) · 1.58 KB
/
Copy pathMySQL.php
File metadata and controls
73 lines (67 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace MyQEE\Server\Async;
/**
* MySQLi 的异步连接池
*
* @package MyQEE\Server\Async
*/
class MySQL extends Pool
{
const DEFAULT_PORT = 3306;
/**
* 连接服务器
*/
protected function connect()
{
$db = new \Swoole\MySQL();
$db->on('close', function($db)
{
$this->remove($db);
});
$db->connect($this->config, function($db, $result)
{
if ($result)
{
$this->join($db);
}
else
{
$this->failure();
trigger_error("connect to mysql server[{$this->config['host']}:{$this->config['port']}] failed. Error: {$db->connect_error}[{$db->connect_errno}].");
}
});
}
/**
* 关闭连接池
*/
public function close()
{
foreach ($this->resourcePool as $conn)
{
/**
* @var $conn \Swoole\MySQL
*/
$conn->close();
}
}
/**
* 发起一个异步查询请求
*
* 成功返回 true(但还没执行 $callback)
*
* @param $sql
* @param callable $callback
* @return bool
*/
function query($sql, callable $callback)
{
return $this->request(function(\Swoole\MySQL $db) use ($callback, $sql)
{
return $db->query($sql, function(\Swoole\MySQL $db, $result) use ($callback)
{
call_user_func($callback, $db, $result);
$this->release($db);
});
});
}
}