Skip to content

Commit

Permalink
Merge branch 'master' into feature/76
Browse files Browse the repository at this point in the history
  • Loading branch information
tbolier authored Nov 19, 2018
2 parents 4099c7a + 9bd082a commit becbdcc
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 17 deletions.
95 changes: 78 additions & 17 deletions docs/examples/operations.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
# ReQL operation examples

*Work in progress document.*
## between

## changes
Insert three documents, select `between` 1 and 2, assert the result.

Insert five documents and consume the changefeed.
```php
// We can do our operations meanwhile.
$res = $r
->table('tabletest')
->insert([
[
'id' => 1,
'title' => 'Test document 1',
'description' => 'A document description.',
],
[
'id' => 2,
'title' => 'Test document 2',
'description' => 'A document description.',
],
[
'id' => 3,
'title' => 'Test document 3',
'description' => 'A document description.',
],
])
->run();

/** @var Cursor $cursor */
$cursor = $r
->table('tabletest')
->between(1, 2)
->run();

echo $cursor->count() === 2 ? 'equals' : 'different';
foreach ($cursor as $document) {
print_r($document);
}
```

## changes
Insert five documents and consume the `changes` afterwards.
```php
// In this example we set the time limit to 5 seconds before the process is terminated.
set_time_limit(5);

// The feed is an iterable cursor.
$feed = $this->r()
$feed = $r
->table('tabletest')
->changes()
->run();

// We can do our operations meanwhile.
$res = $this->r()
$res = $r
->table('tabletest')
->insert([
[
Expand All @@ -25,7 +61,7 @@ $res = $this->r()
'description' => 'A document description.',
],
[
'id' => 2,
'id' => 1,
'title' => 'Test document 1',
'description' => 'A document description.',
],
Expand All @@ -43,12 +79,12 @@ foreach ($feed as $change) {
Insert one document, update it, and consume the change feed with a squashed cursor.
For all possible options, please visit the Java documentation [here](https://rethinkdb.com/api/java/changes/).
```php
$feed = $this->r()
$feed = $r
->table('tabletest')
->changes(['squash' => true])
->run();

$res = $this->r()
$res = $r
->table('tabletest')
->insert([
[
Expand All @@ -59,7 +95,7 @@ $res = $this->r()
])
->run();

$this->r()
$r
->table('tabletest')
->filter(['id' => 1])
->update(['description' => 'cool!'])
Expand All @@ -76,36 +112,52 @@ $r->db()
->run();
```

## insert
## insert
Insert one or more documents.
```php
$r->table('tableName')
->insert([
'documentId' => 1,
'title' => 'Test document 1',
'description' => 'A document description.'
])
->run();

$r->table('tableName')
->insert([
[
'documentId' => 1,
'title' => 'Test document',
'description' => 'My first document.'
],
'documentId' => 2,
'title' => 'Test document 2',
'description' => 'A document description.'
],
[
'documentId' => 3,
'title' => 'Test document 3',
'description' => 'A document description.'
],
])
->run();
```

## update
## update
Update one or more documents.
```php
$r->table('tableName')
->filter([
[
'title' => 'Test document',
'title' => 'A document description.',
],
])
->update([
[
'title' => 'Updated document',
'title' => 'Updated document description.',
],
])
->run();
```

## filter
Filter and count the results.
```php
$r->table('tableName')
->filter([
Expand All @@ -116,3 +168,12 @@ $r->table('tableName')
->count()
->run();
```

## sync
Save writes to permanent storage.

```php
$r->table('tableName')
->sync()
->run();
```
37 changes: 37 additions & 0 deletions src/Query/Operation/Sync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Operation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class Sync extends AbstractQuery
{
/**
* @var QueryInterface
*/
private $query;

public function __construct(
RethinkInterface $rethink,
QueryInterface $query
) {
parent::__construct($rethink);

$this->query = $query;
$this->rethink = $rethink;
}

public function toArray(): array
{
return [
TermType::SYNC,
[
$this->query->toArray(),
],
];
}
}
6 changes: 6 additions & 0 deletions src/Query/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use TBolier\RethinkQL\Query\Operation\IndexList;
use TBolier\RethinkQL\Query\Operation\IndexRename;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\Operation\Sync;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;
Expand Down Expand Up @@ -84,6 +85,11 @@ public function hasFields(...$keys)
return new HasFields($this->rethink, $this, $keys);
}

public function sync()
{
return new Sync($this->rethink, $this);
}

public function toArray(): array
{
return $this->query;
Expand Down
29 changes: 29 additions & 0 deletions test/integration/Operation/SyncTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\IntegrationTest\Operation;

use TBolier\RethinkQL\Response\ResponseInterface;

class SyncTest extends AbstractTableTest
{
/**
* @throws \Exception
*/
public function testSync(): void
{
$this->insertDocument(1);
$this->insertDocument(2);
$this->insertDocument(3);
$this->insertDocument(4);
$this->insertDocument(5);

/** @var ResponseInterface $result */
$result = $this->r()
->table('tabletest')
->sync()
->run();

$this->assertEquals(['synced' => true], $result->getData());
}
}

0 comments on commit becbdcc

Please sign in to comment.