Skip to content

Commit c4514b0

Browse files
committed
Add sum, avg, min & max operations
1 parent e3f166d commit c4514b0

File tree

13 files changed

+415
-7
lines changed

13 files changed

+415
-7
lines changed

docs/roadmap.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181
| reduce | ✘ | ✘ | |
8282
| fold | ✘ | ✘ | |
8383
| count | ✓ | ✓ | |
84-
| sum | ✘ | | |
85-
| avg | ✘ | | |
86-
| min | ✘ | | |
87-
| max | ✘ | | |
84+
| sum | ✘ | | |
85+
| avg | ✘ | | |
86+
| min | ✘ | | |
87+
| max | ✘ | | |
8888
| distinct | ✘ | ✘ | |
8989
| contains | ✘ | ✘ | |
9090
| | | | |

src/Query/Aggregation/AbstractAggregation.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,36 @@ public function orderBy($key): AggregationInterface
3939
{
4040
return new OrderBy($this->rethink, $this->message, $this, $key);
4141
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function sum($key): AggregationInterface
47+
{
48+
return new Sum($this->rethink, $this->message, $this, $key);
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function avg($key): AggregationInterface
55+
{
56+
return new Avg($this->rethink, $this->message, $this, $key);
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
public function min($key): AggregationInterface
63+
{
64+
return new Min($this->rethink, $this->message, $this, $key);
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
public function max($key): AggregationInterface
71+
{
72+
return new Max($this->rethink, $this->message, $this, $key);
73+
}
4274
}

src/Query/Aggregation/AggregationInterface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ public function skip($n): AggregationInterface;
3131
*/
3232
public function orderBy($key): AggregationInterface;
3333

34+
/**
35+
* @param string $key
36+
* @return AggregationInterface
37+
*/
38+
public function sum($key): AggregationInterface;
39+
40+
/**
41+
* @param string $key
42+
* @return AggregationInterface
43+
*/
44+
public function avg($key): AggregationInterface;
45+
46+
/**
47+
* @param string $key
48+
* @return AggregationInterface
49+
*/
50+
public function min($key): AggregationInterface;
51+
52+
/**
53+
* @param string $key
54+
* @return AggregationInterface
55+
*/
56+
public function max($key): AggregationInterface;
57+
3458
/**
3559
* @return Iterable|ResponseInterface
3660
*/

src/Query/Aggregation/Avg.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\Query\Aggregation;
5+
6+
use TBolier\RethinkQL\Message\MessageInterface;
7+
use TBolier\RethinkQL\Query\QueryInterface;
8+
use TBolier\RethinkQL\RethinkInterface;
9+
use TBolier\RethinkQL\Types\Term\TermType;
10+
11+
class Avg extends AbstractAggregation
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $key;
17+
18+
/**
19+
* @var QueryInterface
20+
*/
21+
private $query;
22+
23+
/**
24+
* @param RethinkInterface $rethink
25+
* @param MessageInterface $message
26+
* @param QueryInterface $query
27+
* @param string $key
28+
*/
29+
public function __construct(
30+
RethinkInterface $rethink,
31+
MessageInterface $message,
32+
QueryInterface $query,
33+
string $key
34+
) {
35+
parent::__construct($rethink, $message);
36+
37+
$this->query = $query;
38+
$this->key = $key;
39+
$this->rethink = $rethink;
40+
$this->message = $message;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
TermType::AVG,
50+
[
51+
$this->query->toArray(),
52+
$this->key
53+
],
54+
];
55+
}
56+
}

src/Query/Aggregation/Max.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\Query\Aggregation;
5+
6+
use TBolier\RethinkQL\Message\MessageInterface;
7+
use TBolier\RethinkQL\Query\QueryInterface;
8+
use TBolier\RethinkQL\RethinkInterface;
9+
use TBolier\RethinkQL\Types\Term\TermType;
10+
11+
class Max extends AbstractAggregation
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $key;
17+
18+
/**
19+
* @var QueryInterface
20+
*/
21+
private $query;
22+
23+
/**
24+
* @param RethinkInterface $rethink
25+
* @param MessageInterface $message
26+
* @param QueryInterface $query
27+
* @param string $key
28+
*/
29+
public function __construct(
30+
RethinkInterface $rethink,
31+
MessageInterface $message,
32+
QueryInterface $query,
33+
string $key
34+
) {
35+
parent::__construct($rethink, $message);
36+
37+
$this->query = $query;
38+
$this->key = $key;
39+
$this->rethink = $rethink;
40+
$this->message = $message;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
TermType::MAX,
50+
[
51+
$this->query->toArray(),
52+
$this->key
53+
],
54+
];
55+
}
56+
}

src/Query/Aggregation/Min.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\Query\Aggregation;
5+
6+
use TBolier\RethinkQL\Message\MessageInterface;
7+
use TBolier\RethinkQL\Query\QueryInterface;
8+
use TBolier\RethinkQL\RethinkInterface;
9+
use TBolier\RethinkQL\Types\Term\TermType;
10+
11+
class Min extends AbstractAggregation
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $key;
17+
18+
/**
19+
* @var QueryInterface
20+
*/
21+
private $query;
22+
23+
/**
24+
* @param RethinkInterface $rethink
25+
* @param MessageInterface $message
26+
* @param QueryInterface $query
27+
* @param string $key
28+
*/
29+
public function __construct(
30+
RethinkInterface $rethink,
31+
MessageInterface $message,
32+
QueryInterface $query,
33+
string $key
34+
) {
35+
parent::__construct($rethink, $message);
36+
37+
$this->query = $query;
38+
$this->key = $key;
39+
$this->rethink = $rethink;
40+
$this->message = $message;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
TermType::MIN,
50+
[
51+
$this->query->toArray(),
52+
$this->key
53+
],
54+
];
55+
}
56+
}

src/Query/Aggregation/Sum.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\Query\Aggregation;
5+
6+
use TBolier\RethinkQL\Message\MessageInterface;
7+
use TBolier\RethinkQL\Query\QueryInterface;
8+
use TBolier\RethinkQL\RethinkInterface;
9+
use TBolier\RethinkQL\Types\Term\TermType;
10+
11+
class Sum extends AbstractAggregation
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $key;
17+
18+
/**
19+
* @var QueryInterface
20+
*/
21+
private $query;
22+
23+
/**
24+
* @param RethinkInterface $rethink
25+
* @param MessageInterface $message
26+
* @param QueryInterface $query
27+
* @param string $key
28+
*/
29+
public function __construct(
30+
RethinkInterface $rethink,
31+
MessageInterface $message,
32+
QueryInterface $query,
33+
string $key
34+
) {
35+
parent::__construct($rethink, $message);
36+
37+
$this->query = $query;
38+
$this->key = $key;
39+
$this->rethink = $rethink;
40+
$this->message = $message;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
TermType::SUM,
50+
[
51+
$this->query->toArray(),
52+
$this->key
53+
],
54+
];
55+
}
56+
}

src/Query/Table.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public function get($key): AbstractQuery
5454
public function indexCreate(string $name): AbstractQuery
5555
{
5656
return new IndexCreate($this->rethink, $this->message, $this, $name);
57-
5857
}
5958

6059
/**
@@ -63,7 +62,6 @@ public function indexCreate(string $name): AbstractQuery
6362
public function indexDrop(string $name): AbstractQuery
6463
{
6564
return new IndexDrop($this->rethink, $this->message, $this, $name);
66-
6765
}
6866

6967
/**
@@ -80,7 +78,6 @@ public function indexList(): AbstractQuery
8078
public function indexRename(string $oldValue, string $newValue): AbstractQuery
8179
{
8280
return new IndexRename($this->rethink, $this->message, $this, $oldValue, $newValue);
83-
8481
}
8582

8683
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace TBolier\RethinkQL\IntegrationTest\Aggregation;
5+
6+
use TBolier\RethinkQL\IntegrationTest\Query\AbstractTableTest;
7+
use TBolier\RethinkQL\Response\ResponseInterface;
8+
9+
class AvgTest extends AbstractTableTest
10+
{
11+
/**
12+
* @return void
13+
* @throws \Exception
14+
*/
15+
public function testAvg(): void
16+
{
17+
$this->insertDocument(5);
18+
$this->insertDocument(4);
19+
$this->insertDocument(3);
20+
$this->insertDocument(2);
21+
$this->insertDocument(1);
22+
23+
/** @var ResponseInterface $res */
24+
$res = $this->r()
25+
->table('tabletest')
26+
->avg('number')
27+
->run();
28+
29+
$this->assertInternalType('float', $res->getData());
30+
}
31+
}

0 commit comments

Comments
 (0)