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

Commit 88dde84

Browse files
Added cursor options to get return values as an array instead of json
1 parent 9eb1e89 commit 88dde84

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

src/connection.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ namespace arangodb { namespace fuerte { namespace php {
238238
throw Php::Exception("Expected vpack to be of type Vpack");
239239

240240
Cursor* cursor = new Cursor(this, (Vpack*)params[0].implementation());
241+
242+
for(auto option : params[1]) {
243+
cursor->setOption(option.first, option.second);
244+
}
245+
241246
return Php::Object("ArangoDb\\Cursor", cursor);
242247
}
243248

src/cursor.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ namespace arangodb { namespace fuerte { namespace php {
77
this->loadFirstBatch();
88
}
99

10+
void Cursor::setOption(int option, int value)
11+
{
12+
if(this->options.size() <= option) {
13+
throw Php::Exception("Invalid option provided for Cursor");
14+
}
15+
16+
this->options[option] = value;
17+
}
18+
1019
void Cursor::loadFirstBatch()
1120
{
1221
Request request("/_api/cursor", this->vpack);
@@ -82,7 +91,16 @@ namespace arangodb { namespace fuerte { namespace php {
8291
throw Php::Exception(e.what());
8392
}
8493

85-
return body;
94+
switch(this->options[Cursor::ENTRY_TYPE]) {
95+
case Cursor::ENTRY_TYPE_JSON:
96+
return body;
97+
case Cursor::ENTRY_TYPE_ARRAY:
98+
return Php::call("json_decode", body, true);
99+
case Cursor::ENTRY_TYPE_OBJECT:
100+
return Php::call("json_decode", body, false);
101+
default:
102+
return body;
103+
}
86104
}
87105

88106
Php::Value Cursor::key()

src/cursor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,21 @@ namespace arangodb { namespace fuerte { namespace php {
3030
void loadFirstBatch();
3131
void loadMore();
3232

33+
std::vector<int> options = {
34+
{ Cursor::ENTRY_TYPE, Cursor::ENTRY_TYPE_JSON }
35+
};
36+
3337
public:
38+
static const int ENTRY_TYPE = 0;
39+
40+
static const int ENTRY_TYPE_JSON = 100;
41+
static const int ENTRY_TYPE_ARRAY = 101;
42+
static const int ENTRY_TYPE_OBJECT = 102;
43+
3444
Cursor(Connection* connection, Vpack* vpack);
3545

46+
void setOption(int option, int value);
47+
3648
virtual long count() override;
3749
Php::Value getCount();
3850

src/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ extern "C" {
7575

7676
connection.method<&arangodb::fuerte::php::Connection::wait>("wait");
7777

78-
connection.method<&arangodb::fuerte::php::Connection::query>("query");
78+
connection.method<&arangodb::fuerte::php::Connection::query>("query", {
79+
Php::ByVal("vpack", "ArangoDb\\Vpack", true),
80+
Php::ByVal("options", Php::Type::Array, false)
81+
});
7982

8083
connection.property("HOST", "host", Php::Const);
8184
connection.property("USER", "user", Php::Const);
@@ -153,6 +156,11 @@ extern "C" {
153156
cursor.method<&arangodb::fuerte::php::Cursor::rewind>("rewind");
154157
cursor.method<&arangodb::fuerte::php::Cursor::getCount>("count");
155158

159+
cursor.property("ENTRY_TYPE", arangodb::fuerte::php::Cursor::ENTRY_TYPE, Php::Const);
160+
cursor.property("ENTRY_TYPE_JSON", arangodb::fuerte::php::Cursor::ENTRY_TYPE_JSON, Php::Const);
161+
cursor.property("ENTRY_TYPE_ARRAY", arangodb::fuerte::php::Cursor::ENTRY_TYPE_ARRAY, Php::Const);
162+
cursor.property("ENTRY_TYPE_OBJECT", arangodb::fuerte::php::Cursor::ENTRY_TYPE_OBJECT, Php::Const);
163+
156164
extension->add(std::move(cursor));
157165
}
158166
}

tests/ConnectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use ArangoDb\Connection;
99
use ArangoDb\Vpack;
10+
use ArangoDb\Cursor;
1011

1112
/**
1213
* @group connection
@@ -195,4 +196,33 @@ public function it_sends_query_with_bind_params()
195196

196197
$this->assertSame(3, $iterations);
197198
}
199+
200+
/**
201+
* @test
202+
*/
203+
public function it_sends_query_and_returns_result_as_array()
204+
{
205+
$this->connection = TestUtil::getConnection();
206+
207+
$cursor = $this->connection->query(
208+
Vpack::fromArray([
209+
'query' => 'FOR i IN 1..100 RETURN [i, i+1]',
210+
'batchSize' => 10
211+
]), [
212+
Cursor::ENTRY_TYPE => Cursor::ENTRY_TYPE_ARRAY
213+
]
214+
);
215+
216+
$cursor->rewind();
217+
$iterations = 0;
218+
219+
while ($cursor->valid()) {
220+
$data = $cursor->current();
221+
$this->assertArrayHasKey(0, $data);
222+
$iterations++;
223+
$cursor->next();
224+
}
225+
226+
$this->assertSame(100, $iterations);
227+
}
198228
}

0 commit comments

Comments
 (0)