2727use Crate \PDO \Exception \RuntimeException ;
2828use Crate \PDO \PDO ;
2929use Crate \PDO \PDOInterface ;
30+ use Crate \Stdlib \BulkResponse ;
31+ use Crate \Stdlib \BulkResponseInterface ;
3032use Crate \Stdlib \Collection ;
3133use Crate \Stdlib \CollectionInterface ;
3234use GuzzleHttp \Client ;
@@ -67,7 +69,7 @@ final class ServerPool implements ServerInterface
6769 /**
6870 * Client constructor.
6971 *
70- * @param array $servers
72+ * @param array $servers
7173 * @param ClientInterface|null $client
7274 */
7375 public function __construct (array $ servers , ClientInterface $ client = null )
@@ -92,6 +94,25 @@ public function __construct(array $servers, ClientInterface $client = null)
9294 */
9395 public function execute (string $ query , array $ parameters ): CollectionInterface
9496 {
97+ return $ this ->executeGeneric ($ query , $ parameters , false );
98+ }
99+
100+ /**
101+ * {@Inheritdoc}
102+ * @throws \GuzzleHttp\Exception\ConnectException
103+ */
104+ public function executeBulk (string $ query , array $ parameters ): BulkResponseInterface
105+ {
106+ return $ this ->executeGeneric ($ query , $ parameters , true );
107+ }
108+
109+ /**
110+ * {@Inheritdoc}
111+ * @throws \GuzzleHttp\Exception\ConnectException
112+ */
113+ private function executeGeneric (string $ query , array $ parameters , bool $ bulk_mode = false )
114+ {
115+ $ exception = null ;
95116 $ numServers = count ($ this ->availableServers ) - 1 ;
96117
97118 for ($ i = 0 ; $ i <= $ numServers ; $ i ++) {
@@ -101,24 +122,8 @@ public function execute(string $query, array $parameters): CollectionInterface
101122 // Move the selected server to the end of the stack
102123 $ this ->availableServers [] = array_shift ($ this ->availableServers );
103124
104- $ options = array_merge ($ this ->httpOptions , [
105- 'base_uri ' => sprintf ('%s://%s ' , $ this ->protocol , $ server ),
106- 'json ' => [
107- 'stmt ' => $ query ,
108- 'args ' => $ parameters ,
109- ],
110- ]);
111-
112125 try {
113- $ response = $ this ->httpClient ->request ('POST ' , '/_sql ' , $ options );
114- $ responseBody = json_decode ((string )$ response ->getBody (), true );
115-
116- return new Collection (
117- $ responseBody ['rows ' ],
118- $ responseBody ['cols ' ],
119- $ responseBody ['duration ' ],
120- $ responseBody ['rowcount ' ]
121- );
126+ return $ this ->sendRequest ($ server , $ query , $ parameters , $ bulk_mode );
122127 } catch (ConnectException $ exception ) {
123128 // Catch it before the BadResponseException but do nothing.
124129 continue ;
@@ -130,18 +135,53 @@ public function execute(string $query, array $parameters): CollectionInterface
130135 throw new RuntimeException (sprintf ('Server returned non-JSON response: %s ' , $ body ), 0 , $ exception );
131136 }
132137
133- $ errorCode = $ json ['error ' ]['code ' ];
138+ $ errorCode = $ json ['error ' ]['code ' ];
134139 $ errorMessage = $ json ['error ' ]['message ' ];
135140
136141 throw new RuntimeException ($ errorMessage , $ errorCode , $ exception );
137142 }
138143 }
139144
140- throw new ConnectException (
141- sprintf ('No more servers available, exception from last server: %s ' , $ exception ->getMessage ()),
142- $ exception ->getRequest (),
143- $ exception
144- );
145+ if ($ exception !== null ) {
146+ throw new ConnectException (
147+ sprintf ('No more servers available, exception from last server: %s ' , $ exception ->getMessage ()),
148+ $ exception ->getRequest (),
149+ $ exception
150+ );
151+ }
152+ }
153+
154+ private function sendRequest (string $ server , string $ query , array $ parameters , bool $ bulk_mode = false )
155+ {
156+ $ args_name = 'args ' ;
157+ if ($ bulk_mode ) {
158+ $ args_name = 'bulk_args ' ;
159+ }
160+ $ options = array_merge ($ this ->httpOptions , [
161+ 'base_uri ' => sprintf ('%s://%s ' , $ this ->protocol , $ server ),
162+ 'json ' => [
163+ 'stmt ' => $ query ,
164+ $ args_name => $ parameters ,
165+ ],
166+ ]);
167+
168+ $ response = $ this ->httpClient ->request ('POST ' , '/_sql ' , $ options );
169+ $ responseBody = json_decode ((string )$ response ->getBody (), true );
170+
171+ if ($ bulk_mode ) {
172+ return new BulkResponse (
173+ $ responseBody ['results ' ],
174+ $ responseBody ['cols ' ],
175+ $ responseBody ['duration ' ]
176+ );
177+ } else {
178+ return new Collection (
179+ $ responseBody ['rows ' ],
180+ $ responseBody ['cols ' ],
181+ $ responseBody ['duration ' ],
182+ $ responseBody ['rowcount ' ]
183+ );
184+ }
145185 }
146186
147187 /**
0 commit comments