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