Skip to content

Commit 641316b

Browse files
committed
Merge pull request #144 from F21/fullcount
fullCount support
2 parents b4d4684 + a95f0d7 commit 641316b

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

lib/triagens/ArangoDb/Cursor.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ class Cursor implements
7474
* @var int
7575
*/
7676
private $_length;
77-
77+
78+
/**
79+
* full count of the result set (ignoring the outermost LIMIT)
80+
*
81+
* @var int
82+
*/
83+
private $_fullCount;
7884

7985
/**
8086
* result entry for cursor id
@@ -90,6 +96,11 @@ class Cursor implements
9096
* result entry for result documents
9197
*/
9298
const ENTRY_RESULT = 'result';
99+
100+
/**
101+
* result entry for the full count (ignoring the outermost LIMIT)
102+
*/
103+
const FULL_COUNT = 'fullCount';
93104

94105
/**
95106
* sanitize option entry
@@ -118,6 +129,10 @@ public function __construct(Connection $connection, array $data, array $options)
118129
if (isset($data[self::ENTRY_ID])) {
119130
$this->_id = $data[self::ENTRY_ID];
120131
}
132+
133+
if (isset($data['extra'][self::FULL_COUNT])) {
134+
$this->_fullCount = $data['extra'][self::FULL_COUNT];
135+
}
121136

122137
// attribute must be there
123138
assert(isset($data[self::ENTRY_HASMORE]));
@@ -174,6 +189,16 @@ public function getCount()
174189

175190
return $this->_length;
176191
}
192+
193+
/**
194+
* Get the full count of the cursor (ignoring the outermost LIMIT)
195+
*
196+
* @return int - total number of results
197+
*/
198+
public function getFullCount()
199+
{
200+
return $this->_fullCount;
201+
}
177202

178203

179204
/**

lib/triagens/ArangoDb/Statement.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ class Statement
7676
* @var bool
7777
*/
7878
private $_doCount = false;
79+
80+
/**
81+
* The count flag (should server return total number of results ignoring the limit)
82+
* Be careful! This option also prevents ArangoDB from using some server side optimizations!
83+
*
84+
* @var bool
85+
*/
86+
private $_fullCount = false;
7987

8088
/**
8189
* The query string
@@ -117,6 +125,11 @@ class Statement
117125
* Bind variables index
118126
*/
119127
const ENTRY_BINDVARS = 'bindVars';
128+
129+
/**
130+
* Full count option index
131+
*/
132+
const FULL_COUNT = 'fullCount';
120133

121134
/**
122135
* Initialise the statement
@@ -156,6 +169,10 @@ public function __construct(Connection $connection, array $data)
156169
if (isset($data[self::ENTRY_BINDVARS])) {
157170
$this->_bindVars->set($data[self::ENTRY_BINDVARS]);
158171
}
172+
173+
if (isset($data[self::FULL_COUNT])) {
174+
$this->_fullCount = (bool) $data[Cursor::FULL_COUNT];
175+
}
159176

160177
if (isset($data[Cursor::ENTRY_SANITIZE])) {
161178
$this->_sanitize = (bool) $data[Cursor::ENTRY_SANITIZE];
@@ -336,6 +353,28 @@ public function getCount()
336353
{
337354
return $this->_doCount;
338355
}
356+
357+
/**
358+
* Set the full count option for the statement
359+
*
360+
* @param bool $value - value for full count option
361+
*
362+
* @return void
363+
*/
364+
public function setFullCount($value)
365+
{
366+
$this->_fullCount = (bool) $value;
367+
}
368+
369+
/**
370+
* Get the full count option value of the statement
371+
*
372+
* @return bool - current value of full count option
373+
*/
374+
public function getFullCount()
375+
{
376+
return $this->_fullCount;
377+
}
339378

340379
/**
341380
* Set the batch size for the statement
@@ -382,6 +421,10 @@ private function buildData()
382421
$data = array(
383422
self::ENTRY_QUERY => $this->_query,
384423
self::ENTRY_COUNT => $this->_doCount,
424+
425+
'options' => array(
426+
self::FULL_COUNT => $this->_fullCount
427+
)
385428
);
386429

387430
if ($this->_bindVars->getCount() > 0) {

tests/StatementTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,38 @@ public function testStatementThatReturnsScalarResponses()
209209
$this->assertNotInstanceOf('\triagens\ArangoDb\Document', $row, "A document object was in the result set!");
210210
}
211211
}
212+
213+
public function testStatementWithFullCount()
214+
{
215+
$connection = $this->connection;
216+
$collection = $this->collection;
217+
218+
$documentHandler = new DocumentHandler($connection);
219+
220+
$document = new Document();
221+
$document->name = 'john';
222+
$documentHandler->add($collection->getId(), $document);
223+
224+
$document = new Document();
225+
$document->name = 'peter';
226+
$documentHandler->add($collection->getId(), $document);
227+
228+
$document = new Document();
229+
$document->name = 'jane';
230+
$documentHandler->add($collection->getId(), $document);
231+
232+
$statement = new Statement($connection, array(
233+
"query" => 'FOR a IN `ArangoDB_PHP_TestSuite_TestCollection_01` LIMIT 2 RETURN a.name',
234+
"count" => true,
235+
"fullCount" => true,
236+
"_sanitize" => true
237+
));
238+
239+
$cursor = $statement->execute();
240+
241+
$this->assertEquals(2, $cursor->getCount(), "The number of results in the cursor should be 2");
242+
$this->assertEquals(3, $cursor->getFullCount(), "The fullCount should be 3");
243+
}
212244

213245
public function tearDown()
214246
{

0 commit comments

Comments
 (0)