Skip to content

Commit e5f686c

Browse files
committed
update examples
1 parent 47f7d68 commit e5f686c

File tree

3 files changed

+123
-39
lines changed

3 files changed

+123
-39
lines changed

examples/aql-query.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace ArangoDBClient;
4+
5+
require __DIR__ . '/init.php';
6+
7+
/* set up some example statements */
8+
$statements = [
9+
'FOR u IN users RETURN u' => null,
10+
'FOR u IN users FILTER u.id == @id RETURN u' => ['id' => 6],
11+
'FOR u IN users FILTER u.id == @id && u.name != @name RETURN u' => ['id' => 1, 'name' => 'fox'],
12+
];
13+
14+
15+
try {
16+
$connection = new Connection($connectionOptions);
17+
$collectionHandler = new CollectionHandler($connection);
18+
$documentHandler = new DocumentHandler($connection);
19+
20+
// set up a document collection "testCollection"
21+
$collection = new Collection('users');
22+
try {
23+
$collectionHandler->create($collection);
24+
} catch (\Exception $e) {
25+
// collection may already exist - ignore this error for now
26+
//
27+
// make sure it is empty
28+
$collectionHandler->truncate($collection);
29+
}
30+
31+
$docs = [
32+
Document::createFromArray(['name' => 'foo', 'id' => 1]),
33+
Document::createFromArray(['name' => 'bar', 'id' => 2]),
34+
Document::createFromArray(['name' => 'baz', 'id' => 3]),
35+
Document::createFromArray(['name' => 'fox', 'id' => 4]),
36+
Document::createFromArray(['name' => 'qaa', 'id' => 5]),
37+
Document::createFromArray(['name' => 'qux', 'id' => 6]),
38+
Document::createFromArray(['name' => 'quu', 'id' => 7]),
39+
];
40+
foreach ($docs as $doc) {
41+
$documentHandler->save($collection, $doc);
42+
}
43+
44+
foreach ($statements as $query => $bindVars) {
45+
$statement = new Statement($connection, [
46+
'query' => $query,
47+
'count' => true,
48+
'batchSize' => 1000,
49+
'bindVars' => $bindVars,
50+
'sanitize' => true,
51+
]
52+
);
53+
54+
echo 'RUNNING STATEMENT ' . $statement . PHP_EOL;
55+
56+
$cursor = $statement->execute();
57+
foreach ($cursor->getAll() as $doc) {
58+
echo '- RETURN VALUE: ' . json_encode($doc) . PHP_EOL;
59+
}
60+
61+
echo PHP_EOL;
62+
}
63+
} catch (ConnectException $e) {
64+
print $e . PHP_EOL;
65+
} catch (ServerException $e) {
66+
print $e . PHP_EOL;
67+
} catch (ClientException $e) {
68+
print $e . PHP_EOL;
69+
}

examples/select.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/streaming-aql-query.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace ArangoDBClient;
4+
5+
require __DIR__ . '/init.php';
6+
7+
try {
8+
$connection = new Connection($connectionOptions);
9+
$collectionHandler = new CollectionHandler($connection);
10+
11+
// set up a document collection "testCollection"
12+
$collection = new Collection('testCollection');
13+
try {
14+
$collectionHandler->create($collection);
15+
} catch (\Exception $e) {
16+
// collection may already exist - ignore this error for now
17+
//
18+
// make sure it is empty
19+
$collectionHandler->truncate($collection);
20+
}
21+
22+
$statement = new Statement($connection, [
23+
'query' => 'FOR i IN 1..10000 INSERT { _key: CONCAT("test", i) } INTO @@collection',
24+
'bindVars' => [ '@collection' => 'testCollection' ],
25+
]);
26+
27+
$statement->execute();
28+
29+
echo 'COUNT AFTER AQL INSERT: ' . $collectionHandler->count($collection) . PHP_EOL;
30+
31+
// next query has a potentially huge result - therefore set the "stream" flag
32+
$statement = new Statement($connection, [
33+
'query' => 'FOR doc IN @@collection RETURN doc',
34+
'bindVars' => [ '@collection' => 'testCollection' ],
35+
'stream' => true
36+
]);
37+
38+
$cursor = $statement->execute();
39+
40+
$counter = 0;
41+
foreach ($cursor as $document) {
42+
++$counter;
43+
print '- DOCUMENT KEY: ' . $document->getKey() . PHP_EOL;
44+
}
45+
46+
print 'QUERY RETURNED ' . $counter . ' DOCUMENTS' . PHP_EOL;
47+
48+
} catch (ConnectException $e) {
49+
print $e . PHP_EOL;
50+
} catch (ServerException $e) {
51+
print $e . PHP_EOL;
52+
} catch (ClientException $e) {
53+
print $e . PHP_EOL;
54+
}

0 commit comments

Comments
 (0)