Skip to content

Commit 0dbb8d9

Browse files
author
Charlotte Dunois
authored
Cursor (#7)
1 parent 6729d07 commit 0dbb8d9

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.idea
2+
.idea/*
13
cache
24
cache/*
35
sami/*

src/Client.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ function runQuery(\Plasma\QueryBuilderInterface $query): \React\Promise\PromiseI
330330
}
331331
}
332332

333+
/**
334+
* Creates a new cursor to seek through SELECT query results.
335+
* @param string $query
336+
* @param array $params
337+
* @return \React\Promise\PromiseInterface
338+
* @throws \Plasma\Exception
339+
*/
340+
function createCursor(string $query, array $params = array()): \React\Promise\PromiseInterface {
341+
if($this->goingAway) {
342+
return \React\Promise\reject((new \Plasma\Exception('Client is closing all connections')));
343+
}
344+
345+
$connection = $this->getOptimalConnection();
346+
347+
try {
348+
return $connection->createCursor($this, $query, $params);
349+
} catch (\Throwable $e) {
350+
$this->checkinConnection($connection);
351+
throw $e;
352+
}
353+
}
354+
333355
/**
334356
* Get the optimal connection.
335357
* @return \Plasma\DriverInterface

src/ClientInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ function quit(): void;
8181
* @throws \Plasma\Exception Thrown if the client is closing all connections.
8282
*/
8383
function runCommand(\Plasma\CommandInterface $command);
84+
85+
/**
86+
* Creates a new cursor to seek through SELECT query results.
87+
* @param string $query
88+
* @param array $params
89+
* @return \React\Promise\PromiseInterface
90+
* @throws \Plasma\Exception
91+
*/
92+
function createCursor(string $query, array $params = array()): \React\Promise\PromiseInterface;
8493
}

src/CursorInterface.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Plasma Core component
4+
* Copyright 2018-2019 PlasmaPHP, All Rights Reserved
5+
*
6+
* Website: https://github.com/PlasmaPHP
7+
* License: https://github.com/PlasmaPHP/core/blob/master/LICENSE
8+
*/
9+
10+
namespace Plasma;
11+
12+
/**
13+
* The cursor interface describes how a cursor can be accessed to fetch rows
14+
* from the server interactively.
15+
*/
16+
interface CursorInterface {
17+
/**
18+
* Whether the cursor has been closed.
19+
* @return bool
20+
*/
21+
function isClosed(): bool;
22+
23+
/**
24+
* Closes the cursor and frees the associated resources on the server.
25+
* Closing a cursor more than once has no effect.
26+
* @return \React\Promise\PromiseInterface
27+
*/
28+
function close(): \React\Promise\PromiseInterface;
29+
30+
/**
31+
* Fetches the given amount of rows using the cursor. Resolves with the row, an array of rows (if amount > 1), or false if no more results exist.
32+
* @param int $amount
33+
* @return \React\Promise\PromiseInterface
34+
* @throws \Plasma\Exception Thrown if the underlying statement has been closed.
35+
*/
36+
function fetch(int $amount = 1): \React\Promise\PromiseInterface;
37+
}

src/DriverInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,14 @@ function runCommand(\Plasma\ClientInterface $client, \Plasma\CommandInterface $c
250250
* @throws \Plasma\Exception
251251
*/
252252
function runQuery(\Plasma\ClientInterface $client, \Plasma\QueryBuilderInterface $query): \React\Promise\PromiseInterface;
253+
254+
/**
255+
* Creates a new cursor to seek through SELECT query results.
256+
* @param \Plasma\ClientInterface $client
257+
* @param string $query
258+
* @param array $params
259+
* @return \React\Promise\PromiseInterface
260+
* @throws \Plasma\Exception
261+
*/
262+
function createCursor(\Plasma\ClientInterface $client, string $query, array $params = array()): \React\Promise\PromiseInterface;
253263
}

tests/ClientTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,33 @@ function testRunQueryGoingAway() {
413413
$this->await($promise);
414414
}
415415

416+
function testCreateCursor() {
417+
$client = $this->createClient();
418+
419+
$query = 'SELECT 1';
420+
421+
$this->driver
422+
->expects($this->once())
423+
->method('createCursor')
424+
->with($client, $query, array())
425+
->will($this->returnValue(\React\Promise\resolve()));
426+
427+
$promise = $client->createCursor('SELECT 1', array());
428+
$this->assertInstanceOf(\React\Promise\PromiseInterface::class, $promise);
429+
}
430+
431+
function testCreateCursorGoingAway() {
432+
$client = $this->createClient();
433+
434+
$this->assertNull($client->quit());
435+
436+
$promise = $client->createCursor('SELECT 1', array());
437+
$this->assertInstanceOf(\React\Promise\PromiseInterface::class, $promise);
438+
439+
$this->expectException(\Plasma\Exception::class);
440+
$this->await($promise);
441+
}
442+
416443
function testLazyCreateConnection() {
417444
$client = $this->createClient(array('connections.lazy' => true));
418445

tests/ClientTestHelpers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function getDriverMock(): \Plasma\DriverInterface {
9595
'endTransaction',
9696
'runCommand',
9797
'runQuery',
98+
'createCursor',
9899
'listeners',
99100
'on',
100101
'once',

0 commit comments

Comments
 (0)