|
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\RequestFailed; |
|
18
|
|
|
use Tarantool\Client\Packer\Packer; |
|
19
|
|
|
use Tarantool\Client\Request\Authenticate; |
|
20
|
|
|
use Tarantool\Client\Request\Call; |
|
21
|
|
|
use Tarantool\Client\Request\Evaluate; |
|
22
|
|
|
use Tarantool\Client\Request\Execute; |
|
23
|
|
|
use Tarantool\Client\Request\Ping; |
|
24
|
|
|
use Tarantool\Client\Request\Request; |
|
25
|
|
|
use Tarantool\Client\Schema\Index; |
|
26
|
|
|
use Tarantool\Client\Schema\Space; |
|
27
|
|
|
|
|
28
|
|
|
final class Client |
|
29
|
|
|
{ |
|
30
|
|
|
private $connection; |
|
31
|
|
|
private $packer; |
|
32
|
|
|
private $salt; |
|
33
|
|
|
private $username; |
|
34
|
|
|
private $password; |
|
35
|
|
|
private $spaces = []; |
|
36
|
|
|
|
|
37
|
262 |
|
public function __construct(Connection $connection, Packer $packer) |
|
38
|
|
|
{ |
|
39
|
262 |
|
$this->connection = $connection; |
|
40
|
262 |
|
$this->packer = $packer; |
|
41
|
262 |
|
} |
|
42
|
|
|
|
|
43
|
4 |
|
public function getConnection() : Connection |
|
44
|
|
|
{ |
|
45
|
4 |
|
return $this->connection; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function getPacker() : Packer |
|
49
|
|
|
{ |
|
50
|
4 |
|
return $this->packer; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
250 |
|
public function connect() : void |
|
54
|
|
|
{ |
|
55
|
250 |
|
$this->salt = $this->connection->open(); |
|
56
|
|
|
|
|
57
|
208 |
|
if ($this->username) { |
|
58
|
4 |
|
$this->authenticate($this->username, $this->password); |
|
59
|
|
|
} |
|
60
|
208 |
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
public function disconnect() : void |
|
63
|
|
|
{ |
|
64
|
4 |
|
$this->connection->close(); |
|
65
|
4 |
|
$this->salt = null; |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
12 |
|
public function isDisconnected() : bool |
|
69
|
|
|
{ |
|
70
|
12 |
|
return $this->connection->isClosed() || !$this->salt; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
12 |
|
public function authenticate(string $username, string $password = '') : void |
|
74
|
|
|
{ |
|
75
|
12 |
|
if ($this->isDisconnected()) { |
|
76
|
8 |
|
$this->salt = $this->connection->open(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
12 |
|
$request = new Authenticate($this->salt, $username, $password); |
|
80
|
12 |
|
$this->sendRequest($request); |
|
81
|
|
|
|
|
82
|
6 |
|
$this->username = $username; |
|
83
|
6 |
|
$this->password = $password; |
|
84
|
|
|
|
|
85
|
6 |
|
$this->flushSpaces(); |
|
86
|
6 |
|
} |
|
87
|
|
|
|
|
88
|
12 |
|
public function ping() : void |
|
89
|
|
|
{ |
|
90
|
12 |
|
$request = new Ping(); |
|
91
|
|
|
|
|
92
|
12 |
|
$this->sendRequest($request); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
122 |
|
public function getSpace(string $spaceName) : Space |
|
96
|
|
|
{ |
|
97
|
122 |
|
if (isset($this->spaces[$spaceName])) { |
|
98
|
6 |
|
return $this->spaces[$spaceName]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
122 |
|
$spaceId = $this->getSpaceIdByName($spaceName); |
|
102
|
|
|
|
|
103
|
118 |
|
return $this->spaces[$spaceName] = $this->spaces[$spaceId] = new Space($this, $spaceId); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
128 |
|
public function getSpaceById(int $spaceId) : Space |
|
107
|
|
|
{ |
|
108
|
128 |
|
if (isset($this->spaces[$spaceId])) { |
|
109
|
4 |
|
return $this->spaces[$spaceId]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
128 |
|
return $this->spaces[$spaceId] = new Space($this, $spaceId); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
10 |
|
public function call(string $funcName, ...$args) : array |
|
116
|
|
|
{ |
|
117
|
10 |
|
$request = new Call($funcName, $args); |
|
118
|
|
|
|
|
119
|
10 |
|
return $this->sendRequest($request)->getBodyField(IProto::DATA); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
8 |
|
public function executeQuery(string $sql, array $params = []) : SqlQueryResult |
|
123
|
|
|
{ |
|
124
|
8 |
|
$request = new Execute($sql, $params); |
|
125
|
8 |
|
$response = $this->sendRequest($request); |
|
126
|
|
|
|
|
127
|
8 |
|
return new SqlQueryResult( |
|
128
|
8 |
|
$response->getBodyField(IProto::DATA), |
|
129
|
8 |
|
$response->getBodyField(IProto::METADATA) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
8 |
|
public function executeUpdate(string $sql, array $params = []) : int |
|
134
|
|
|
{ |
|
135
|
8 |
|
$request = new Execute($sql, $params); |
|
136
|
|
|
|
|
137
|
8 |
|
return $this->sendRequest($request)->getBodyField(IProto::SQL_INFO)[0]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
56 |
|
public function evaluate(string $expr, array $args = []) : array |
|
141
|
|
|
{ |
|
142
|
56 |
|
$request = new Evaluate($expr, $args); |
|
143
|
|
|
|
|
144
|
56 |
|
return $this->sendRequest($request)->getBodyField(IProto::DATA); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
10 |
|
public function flushSpaces() : void |
|
148
|
|
|
{ |
|
149
|
10 |
|
$this->spaces = []; |
|
150
|
10 |
|
} |
|
151
|
|
|
|
|
152
|
216 |
|
public function sendRequest(Request $request) : Response |
|
153
|
|
|
{ |
|
154
|
216 |
|
if ($this->connection->isClosed()) { |
|
155
|
212 |
|
$this->connect(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
212 |
|
$data = $this->packer->pack($request); |
|
159
|
212 |
|
$data = $this->connection->send($data); |
|
160
|
|
|
|
|
161
|
204 |
|
$response = $this->packer->unpack($data); |
|
162
|
204 |
|
if (!$response->isError()) { |
|
163
|
196 |
|
return $response; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
40 |
|
throw RequestFailed::fromErrorResponse($response); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
122 |
|
private function getSpaceIdByName(string $spaceName) : int |
|
170
|
|
|
{ |
|
171
|
122 |
|
$schema = $this->getSpaceById(Space::VSPACE_ID); |
|
172
|
122 |
|
$data = $schema->select([$spaceName], Index::SPACE_NAME); |
|
173
|
|
|
|
|
174
|
122 |
|
if (empty($data)) { |
|
175
|
6 |
|
throw RequestFailed::unknownSpace($spaceName); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
118 |
|
return $data[0][0]; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
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: