Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit 953e3f9

Browse files
Added a toArray function to Cursor in order to fetch everything at once
1 parent 88dde84 commit 953e3f9

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/cursor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ namespace arangodb { namespace fuerte { namespace php {
125125
return static_cast<int>(this->number);
126126
}
127127

128+
Php::Value Cursor::toArray() {
129+
Php::Array result;
130+
131+
int counter = 0;
132+
while(this->valid()) {
133+
result[counter++] = this->current();
134+
this->next();
135+
}
136+
137+
return result;
138+
}
139+
128140
}}}

src/cursor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ namespace arangodb { namespace fuerte { namespace php {
5454
void next();
5555
void rewind();
5656

57+
Php::Value toArray();
58+
5759
virtual ~Cursor() = default;
5860
};
5961

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ extern "C" {
156156
cursor.method<&arangodb::fuerte::php::Cursor::rewind>("rewind");
157157
cursor.method<&arangodb::fuerte::php::Cursor::getCount>("count");
158158

159+
cursor.method<&arangodb::fuerte::php::Cursor::toArray>("toArray");
160+
159161
cursor.property("ENTRY_TYPE", arangodb::fuerte::php::Cursor::ENTRY_TYPE, Php::Const);
160162
cursor.property("ENTRY_TYPE_JSON", arangodb::fuerte::php::Cursor::ENTRY_TYPE_JSON, Php::Const);
161163
cursor.property("ENTRY_TYPE_ARRAY", arangodb::fuerte::php::Cursor::ENTRY_TYPE_ARRAY, Php::Const);

tests/ConnectionTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,25 @@ public function it_sends_query_and_returns_result_as_array()
225225

226226
$this->assertSame(100, $iterations);
227227
}
228+
229+
/**
230+
* @test
231+
*/
232+
public function it_creates_array_from_cursor()
233+
{
234+
$this->connection = TestUtil::getConnection();
235+
236+
$cursor = $this->connection->query(
237+
Vpack::fromArray([
238+
'query' => 'FOR i IN 1..100 RETURN [i, i+1]',
239+
'batchSize' => 10
240+
]), [
241+
Cursor::ENTRY_TYPE => Cursor::ENTRY_TYPE_ARRAY
242+
]
243+
);
244+
245+
$data = $cursor->toArray();
246+
247+
$this->assertCount(100, $data);
248+
}
228249
}

0 commit comments

Comments
 (0)