1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Tarantool Client package. |
7
|
|
|
* |
8
|
|
|
* (c) Eugene Leonovich <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client; |
15
|
|
|
|
16
|
|
|
use Tarantool\Client\Connection\Connection; |
17
|
|
|
use Tarantool\Client\Exception\Exception; |
18
|
|
|
use Tarantool\Client\Packer\Packer; |
19
|
|
|
use Tarantool\Client\Request\AuthenticateRequest; |
20
|
|
|
use Tarantool\Client\Request\CallRequest; |
21
|
|
|
use Tarantool\Client\Request\EvaluateRequest; |
22
|
|
|
use Tarantool\Client\Request\ExecuteRequest; |
23
|
|
|
use Tarantool\Client\Request\PingRequest; |
24
|
|
|
use Tarantool\Client\Request\Request; |
25
|
|
|
use Tarantool\Client\Response\BinaryResponse; |
26
|
|
|
use Tarantool\Client\Response\RawResponse; |
27
|
|
|
use Tarantool\Client\Response\SqlQueryResponse; |
28
|
|
|
use Tarantool\Client\Response\SqlUpdateResponse; |
29
|
|
|
use Tarantool\Client\Schema\Index; |
30
|
|
|
use Tarantool\Client\Schema\Space; |
31
|
|
|
|
32
|
|
|
final class Client |
33
|
|
|
{ |
34
|
|
|
private $connection; |
35
|
|
|
private $packer; |
36
|
|
|
private $salt; |
37
|
|
|
private $username; |
38
|
|
|
private $password; |
39
|
|
|
private $spaces = []; |
40
|
|
|
|
41
|
123 |
|
public function __construct(Connection $connection, Packer $packer) |
42
|
|
|
{ |
43
|
123 |
|
$this->connection = $connection; |
44
|
123 |
|
$this->packer = $packer; |
45
|
123 |
|
} |
46
|
|
|
|
47
|
2 |
|
public function getConnection() : Connection |
48
|
|
|
{ |
49
|
2 |
|
return $this->connection; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function getPacker() : Packer |
53
|
|
|
{ |
54
|
2 |
|
return $this->packer; |
55
|
|
|
} |
56
|
|
|
|
57
|
117 |
|
public function connect() : void |
58
|
|
|
{ |
59
|
117 |
|
$this->salt = $this->connection->open(); |
60
|
|
|
|
61
|
96 |
|
if ($this->username) { |
62
|
2 |
|
$this->authenticate($this->username, $this->password); |
63
|
|
|
} |
64
|
96 |
|
} |
65
|
|
|
|
66
|
2 |
|
public function disconnect() : void |
67
|
|
|
{ |
68
|
2 |
|
$this->connection->close(); |
69
|
2 |
|
$this->salt = null; |
70
|
2 |
|
} |
71
|
|
|
|
72
|
6 |
|
public function isDisconnected() : bool |
73
|
|
|
{ |
74
|
6 |
|
return $this->connection->isClosed() || !$this->salt; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
public function authenticate(string $username, string $password = '') : BinaryResponse |
78
|
|
|
{ |
79
|
6 |
|
if ($this->isDisconnected()) { |
80
|
4 |
|
$this->salt = $this->connection->open(); |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
$request = new AuthenticateRequest($this->salt, $username, $password); |
84
|
6 |
|
$rawResponse = $this->sendRequest($request); |
85
|
|
|
|
86
|
3 |
|
$this->username = $username; |
87
|
3 |
|
$this->password = $password; |
88
|
|
|
|
89
|
3 |
|
$this->flushSpaces(); |
90
|
|
|
|
91
|
3 |
|
return BinaryResponse::createFromRaw($rawResponse); |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
public function ping() : BinaryResponse |
95
|
|
|
{ |
96
|
6 |
|
$request = new PingRequest(); |
97
|
|
|
|
98
|
6 |
|
return BinaryResponse::createFromRaw($this->sendRequest($request)); |
99
|
|
|
} |
100
|
|
|
|
101
|
58 |
|
public function getSpace(string $spaceName) : Space |
102
|
|
|
{ |
103
|
58 |
|
if (isset($this->spaces[$spaceName])) { |
104
|
3 |
|
return $this->spaces[$spaceName]; |
105
|
|
|
} |
106
|
|
|
|
107
|
58 |
|
$spaceId = $this->getSpaceIdByName($spaceName); |
108
|
|
|
|
109
|
56 |
|
return $this->spaces[$spaceName] = $this->spaces[$spaceId] = new Space($this, $spaceId); |
110
|
|
|
} |
111
|
|
|
|
112
|
61 |
|
public function getSpaceById(int $spaceId) : Space |
113
|
|
|
{ |
114
|
61 |
|
if (isset($this->spaces[$spaceId])) { |
115
|
2 |
|
return $this->spaces[$spaceId]; |
116
|
|
|
} |
117
|
|
|
|
118
|
61 |
|
return $this->spaces[$spaceId] = new Space($this, $spaceId); |
119
|
|
|
} |
120
|
|
|
|
121
|
5 |
|
public function call(string $funcName, ...$args) : BinaryResponse |
122
|
|
|
{ |
123
|
5 |
|
$request = new CallRequest($funcName, $args); |
124
|
|
|
|
125
|
5 |
|
return BinaryResponse::createFromRaw($this->sendRequest($request)); |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
public function executeQuery(string $sql, array $params = []) : SqlQueryResponse |
129
|
|
|
{ |
130
|
4 |
|
$request = new ExecuteRequest($sql, $params); |
131
|
|
|
|
132
|
4 |
|
return SqlQueryResponse::createFromRaw($this->sendRequest($request)); |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
public function executeUpdate(string $sql, array $params = []) : SqlUpdateResponse |
136
|
|
|
{ |
137
|
4 |
|
$request = new ExecuteRequest($sql, $params); |
138
|
|
|
|
139
|
4 |
|
return SqlUpdateResponse::createFromRaw($this->sendRequest($request)); |
140
|
|
|
} |
141
|
|
|
|
142
|
23 |
|
public function evaluate(string $expr, array $args = []) : BinaryResponse |
143
|
|
|
{ |
144
|
23 |
|
$request = new EvaluateRequest($expr, $args); |
145
|
|
|
|
146
|
23 |
|
return BinaryResponse::createFromRaw($this->sendRequest($request)); |
147
|
|
|
} |
148
|
|
|
|
149
|
5 |
|
public function flushSpaces() : void |
150
|
|
|
{ |
151
|
5 |
|
$this->spaces = []; |
152
|
5 |
|
} |
153
|
|
|
|
154
|
100 |
|
public function sendRequest(Request $request) : RawResponse |
155
|
|
|
{ |
156
|
100 |
|
if ($this->connection->isClosed()) { |
157
|
98 |
|
$this->connect(); |
158
|
|
|
} |
159
|
|
|
|
160
|
98 |
|
$data = $this->packer->pack($request); |
161
|
98 |
|
$data = $this->connection->send($data); |
162
|
|
|
|
163
|
94 |
|
$response = $this->packer->unpack($data); |
164
|
94 |
|
if (!$response->isError()) { |
165
|
90 |
|
return $response; |
166
|
|
|
} |
167
|
|
|
|
168
|
20 |
|
throw new Exception( |
169
|
20 |
|
$response->getBodyField(IProto::ERROR), |
170
|
20 |
|
$response->getHeaderField(IProto::CODE) & (RawResponse::TYPE_ERROR - 1) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
58 |
|
private function getSpaceIdByName(string $spaceName) : int |
175
|
|
|
{ |
176
|
58 |
|
$schema = $this->getSpaceById(Space::VSPACE); |
177
|
58 |
|
$response = $schema->select([$spaceName], Index::SPACE_NAME); |
178
|
58 |
|
$data = $response->getData(); |
179
|
|
|
|
180
|
58 |
|
if (empty($data)) { |
181
|
3 |
|
throw new Exception("Space '$spaceName' does not exist"); |
182
|
|
|
} |
183
|
|
|
|
184
|
56 |
|
return $data[0][0]; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: