1 | <?php |
||
32 | class Client implements ClientInterface |
||
33 | { |
||
34 | |||
35 | const DEFAULT_SERVER = "localhost:4200"; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $availableServers = []; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $serverPool = []; |
||
46 | |||
47 | /** |
||
48 | * Client constructor. |
||
49 | * @param array $servers |
||
50 | * @param array $options |
||
51 | */ |
||
52 | 1 | public function __construct(array $servers, array $options) |
|
64 | |||
65 | /** |
||
66 | * {@Inheritdoc} |
||
67 | */ |
||
68 | 2 | public function execute($query, array $parameters) |
|
69 | { |
||
70 | $body = [ |
||
71 | 2 | 'stmt' => $query, |
|
72 | 'args' => $parameters |
||
73 | 2 | ]; |
|
74 | |||
75 | 2 | while (true) { |
|
76 | 2 | $nextServer = $this->nextServer(); |
|
77 | /** |
||
78 | * @var Server $s |
||
79 | */ |
||
80 | 2 | $s = $this->serverPool[$nextServer]; |
|
81 | |||
82 | try { |
||
83 | |||
84 | 2 | $response = $s->doRequest($body); |
|
85 | 1 | $responseBody = $response->json(); |
|
86 | |||
87 | 1 | return new Collection( |
|
88 | 1 | $responseBody['rows'], |
|
89 | 1 | $responseBody['cols'], |
|
90 | 1 | $responseBody['duration'], |
|
91 | 1 | $responseBody['rowcount'] |
|
92 | 1 | ); |
|
93 | |||
94 | 1 | } catch (ConnectException $exception) { |
|
95 | // drop the server from the list of available servers |
||
96 | $this->dropServer($nextServer); |
||
97 | // break the loop if no more servers are available |
||
98 | $this->raiseIfNoMoreServers($exception); |
||
99 | 1 | } catch (BadResponseException $exception) { |
|
100 | |||
101 | try { |
||
102 | |||
103 | 1 | $json = $exception->getResponse()->json(); |
|
104 | |||
105 | 1 | $errorCode = $json['error']['code']; |
|
106 | 1 | $errorMessage = $json['error']['message']; |
|
107 | |||
108 | 1 | throw new RuntimeException($errorMessage, $errorCode, $exception); |
|
109 | |||
110 | 1 | } catch (ParseException $e) { |
|
111 | throw new RuntimeException('Server returned non-JSON response.', 0, $exception); |
||
112 | } |
||
113 | |||
114 | } |
||
115 | } |
||
116 | return null; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@Inheritdoc} |
||
121 | */ |
||
122 | 1 | public function getServerInfo() |
|
123 | { |
||
124 | 1 | throw new UnsupportedException('Not yet implemented'); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@Inheritdoc} |
||
129 | */ |
||
130 | 1 | public function getServerVersion() |
|
131 | { |
||
132 | 1 | throw new UnsupportedException('Not yet implemented'); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * {@Inheritdoc} |
||
137 | */ |
||
138 | 1 | public function setTimeout($timeout) |
|
139 | { |
||
140 | 1 | foreach ($this->serverPool as $k => &$s) { |
|
141 | /** |
||
142 | * @var $s Server |
||
143 | */ |
||
144 | 1 | $s->setTimeout($timeout); |
|
145 | 1 | } |
|
146 | 1 | } |
|
147 | |||
148 | /** |
||
149 | * {@Inheritdoc} |
||
150 | */ |
||
151 | public function setHttpBasicAuth($username, $passwd) |
||
160 | |||
161 | /** |
||
162 | * {@Inheritdoc} |
||
163 | */ |
||
164 | public function setHttpHeader($name, $value) |
||
173 | |||
174 | /** |
||
175 | * {@Inheritdoc} |
||
176 | */ |
||
177 | 1 | public function setDefaultSchema($schemaName) |
|
178 | { |
||
179 | 1 | $this->setHttpHeader("Default-Schema", $schemaName); |
|
180 | 1 | } |
|
181 | |||
182 | /** |
||
183 | * @return string The next available server instance |
||
184 | */ |
||
185 | 3 | private function nextServer() |
|
191 | |||
192 | /** |
||
193 | * Very simple round-robin implementation |
||
194 | * Pops the first item of the availableServers array and appends it at the end. |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | 3 | private function roundRobin() |
|
208 | |||
209 | /** |
||
210 | * @param string $server |
||
211 | */ |
||
212 | 2 | private function dropServer($server) |
|
218 | |||
219 | /** |
||
220 | * @param ConnectException $exception |
||
221 | */ |
||
222 | 1 | private function raiseIfNoMoreServers($exception) |
|
233 | } |
||
234 |