|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
|
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
|
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
|
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. You may |
|
8
|
|
|
* obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations |
|
16
|
|
|
* under the License. |
|
17
|
|
|
* |
|
18
|
|
|
* However, if you have executed another commercial license agreement |
|
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
|
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace Crate\PDO\Http; |
|
24
|
|
|
|
|
25
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
26
|
|
|
use Crate\PDO\Exception\RuntimeException; |
|
27
|
|
|
use Crate\PDO\Exception\UnsupportedException; |
|
28
|
|
|
use Crate\Stdlib\Collection; |
|
29
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
|
30
|
|
|
use GuzzleHttp\Exception\ParseException; |
|
31
|
|
|
|
|
32
|
|
|
class Client implements ClientInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var HttpClient |
|
36
|
|
|
*/ |
|
37
|
|
|
private $client; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $uri |
|
41
|
|
|
* @param array $options |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function __construct($uri, array $options) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->client = new HttpClient(['base_url' => $uri] + $options); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@Inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function execute($query, array $parameters) |
|
52
|
|
|
{ |
|
53
|
|
|
$body = [ |
|
54
|
2 |
|
'stmt' => $query, |
|
55
|
2 |
|
'args' => $parameters |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
2 |
|
$response = $this->client->post(null, ['json' => $body]); |
|
60
|
1 |
|
$responseBody = $response->json(); |
|
61
|
|
|
|
|
62
|
1 |
|
return new Collection( |
|
63
|
1 |
|
$responseBody['rows'], |
|
64
|
1 |
|
$responseBody['cols'], |
|
65
|
1 |
|
$responseBody['duration'], |
|
66
|
1 |
|
$responseBody['rowcount'] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
1 |
|
} catch (BadResponseException $exception) { |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
|
|
73
|
1 |
|
$json = $exception->getResponse()->json(); |
|
74
|
|
|
|
|
75
|
1 |
|
$errorCode = $json['error']['code']; |
|
76
|
1 |
|
$errorMessage = $json['error']['message']; |
|
77
|
|
|
|
|
78
|
1 |
|
throw new RuntimeException($errorMessage, $errorCode); |
|
79
|
|
|
|
|
80
|
1 |
|
} catch (ParseException $e) { |
|
81
|
|
|
throw new RuntimeException('Unparsable response from server', 0, $exception); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@Inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getServerInfo() |
|
90
|
|
|
{ |
|
91
|
1 |
|
throw new UnsupportedException('Not yet implemented'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@Inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function getServerVersion() |
|
98
|
|
|
{ |
|
99
|
1 |
|
throw new UnsupportedException('Not yet implemented'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@Inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function setTimeout($timeout) |
|
106
|
|
|
{ |
|
107
|
1 |
|
$this->client->setDefaultOption('timeout', (float) $timeout); |
|
108
|
1 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@Inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setHttpBasicAuth($username, $passwd) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->client->setDefaultOption('auth', [$username, $passwd]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@Inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function setHttpHeader($name, $value) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->client->setDefaultOption('headers/'.$name, $value); |
|
124
|
1 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|