|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dazzle\PgSQL\Connection; |
|
3
|
|
|
|
|
4
|
|
|
use Dazzle\Promise\Deferred; |
|
5
|
|
|
use Dazzle\PgSQL\Statement\Query; |
|
6
|
|
|
use Dazzle\PgSQL\Statement\PrepareQuery; |
|
7
|
|
|
use Dazzle\PgSQL\Statement\Statement; |
|
8
|
|
|
use Dazzle\Throwable\Exception; |
|
9
|
|
|
|
|
10
|
|
|
class Connector implements ConnectorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $connected; |
|
13
|
|
|
|
|
14
|
|
|
protected $queryQueue; |
|
15
|
|
|
|
|
16
|
|
|
protected $retQueue; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Connection $conn |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $conn; |
|
22
|
|
|
|
|
23
|
|
|
protected $stream; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct($config) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->stream = pg_connect($config, \PGSQL_CONNECT_ASYNC|\PGSQL_CONNECT_FORCE_NEW); |
|
28
|
|
|
$this->conn = new Connection(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getStream() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->stream; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getSock() |
|
37
|
|
|
{ |
|
38
|
|
|
return \pg_socket($this->stream); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getConnection() |
|
42
|
|
|
{ |
|
43
|
|
|
//rfc: could passed stream to each connection,add connection resolver and pool |
|
44
|
|
|
$connection = $this->conn; |
|
45
|
|
|
$promise = $this->conn->getPromise(); |
|
46
|
|
|
|
|
47
|
|
|
return $promise |
|
48
|
|
|
->success(function (ConnectorInterface $connector) use ($connection) { |
|
49
|
|
|
$connection->setConnector($connector); |
|
50
|
|
|
|
|
51
|
|
|
return $connection; |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function connect() |
|
56
|
|
|
{ |
|
57
|
|
|
switch ($polled = $this->poll()) { |
|
|
|
|
|
|
58
|
|
|
case \PGSQL_POLLING_FAILED: |
|
59
|
|
|
return; |
|
60
|
|
|
case \PGSQL_POLLING_OK: |
|
61
|
|
|
if ($this->isConnected() != true) { |
|
|
|
|
|
|
62
|
|
|
$this->connected = true; |
|
63
|
|
|
$this->conn->resolve($this); |
|
64
|
|
|
|
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
$retRsrc = \pg_get_result($this->stream); |
|
68
|
|
|
if ($retRsrc) { |
|
69
|
|
|
if (empty($this->retQueue)) { |
|
70
|
|
|
|
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
$ret = array_shift($this->retQueue); |
|
74
|
|
|
$ret->resolve($ret->handle($retRsrc)); |
|
75
|
|
|
} else { |
|
76
|
|
|
if (empty($this->queryQueue)) { |
|
77
|
|
|
|
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
$query = array_shift($this->queryQueue); |
|
81
|
|
|
$ok = $query->execute($this); |
|
82
|
|
|
if (!$ok) { |
|
83
|
|
|
$query->reject(new \Exception('failed')); |
|
84
|
|
|
|
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
$this->retQueue[] = $query; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function prepare($sql) |
|
95
|
|
|
{ |
|
96
|
|
|
$prepare = new PrepareQuery($sql); |
|
97
|
|
|
$this->appendQuery($prepare); |
|
98
|
|
|
$promise = $prepare->getPromise(); |
|
99
|
|
|
|
|
100
|
|
|
return $promise; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @inheritDoc |
|
105
|
|
|
*/ |
|
106
|
|
|
public function query($sql, $sqlParams = []) |
|
107
|
|
|
{ |
|
108
|
|
|
$stmt = new Query($sql, $sqlParams); |
|
109
|
|
|
$promise = $stmt->getPromise(); |
|
110
|
|
|
$this->appendQuery($stmt); |
|
111
|
|
|
|
|
112
|
|
|
return $promise; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritDoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function execute($sql, $sqlParams = []) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->query($sql, $sqlParams); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function appendQuery(Statement $stmt) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->queryQueue[] = $stmt; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function poll() |
|
129
|
|
|
{ |
|
130
|
|
|
return \pg_connect_poll($this->stream); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function isConnected() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->connected; |
|
136
|
|
|
} |
|
137
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.