Skip to content

Commit 2bbc527

Browse files
committed
update collection returns
1 parent 523fc6d commit 2bbc527

26 files changed

Lines changed: 441 additions & 103 deletions

.github/workflows/benchmark.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: "phpbench diff"
5353
run: "vendor/bin/phpbench run tests/Benchmark --progress=none --report=diff --ref=base > bench.txt"
5454

55-
- name: "Get Bench Result"
55+
- name: "Get Bench Cursor"
5656
id: phpbench
5757
run: |
5858
echo 'BENCH_RESULT<<EOF' >> $GITHUB_ENV

src/BulkWriteResult.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Rango;
6+
7+
use function count;
8+
9+
final class BulkWriteResult
10+
{
11+
/**
12+
* @param array<int, mixed> $insertedIds
13+
* @param array<int, mixed> $upsertedIds
14+
*/
15+
public function __construct(
16+
private readonly int $insertedCount,
17+
private readonly int $matchedCount,
18+
private readonly int $modifiedCount,
19+
private readonly int $deletedCount,
20+
private readonly int $upsertedCount,
21+
private readonly array $insertedIds,
22+
private readonly array $upsertedIds,
23+
) {
24+
}
25+
26+
public function getInsertedCount(): int
27+
{
28+
return $this->insertedCount;
29+
}
30+
31+
public function getMatchedCount(): int
32+
{
33+
return $this->matchedCount;
34+
}
35+
36+
public function getModifiedCount(): int
37+
{
38+
return $this->modifiedCount;
39+
}
40+
41+
public function getDeletedCount(): int
42+
{
43+
return $this->deletedCount;
44+
}
45+
46+
public function getUpsertedCount(): int
47+
{
48+
return $this->upsertedCount;
49+
}
50+
51+
/** @return array<int, mixed> */
52+
public function getInsertedIds(): array
53+
{
54+
return $this->insertedIds;
55+
}
56+
57+
/** @return array<int, mixed> */
58+
public function getUpsertedIds(): array
59+
{
60+
return $this->upsertedIds;
61+
}
62+
63+
public function isAcknowledged(): bool
64+
{
65+
return true;
66+
}
67+
}

src/Client.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
use PDO;
88

9-
use function array_map;
10-
use function bin2hex;
11-
use function json_decode;
12-
use function json_encode;
13-
use function random_bytes;
149
use function sprintf;
1510

1611
final class Client

src/Collection.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Patchlevel\Rango;
66

7-
class Collection
7+
/**
8+
* @template T of array
9+
*/
10+
final class Collection
811
{
912
public function __construct(
1013
private readonly Client $client,
@@ -26,7 +29,7 @@ public function countDocuments(array $filter = [], array $options = []): int
2629
* @param array<string, mixed> $filter
2730
* @param array<string, mixed> $options
2831
*/
29-
public function deleteMany(array $filter, array $options = []): Result
32+
public function deleteMany(array $filter, array $options = []): DeleteResult
3033
{
3134
return $this->client->run(new Operation\Delete($this->databaseName, $this->collectionName, $filter, true));
3235
}
@@ -35,7 +38,7 @@ public function deleteMany(array $filter, array $options = []): Result
3538
* @param array<string, mixed> $filter
3639
* @param array<string, mixed> $options
3740
*/
38-
public function deleteOne(array $filter, array $options = []): Result
41+
public function deleteOne(array $filter, array $options = []): DeleteResult
3942
{
4043
return $this->client->run(new Operation\Delete($this->databaseName, $this->collectionName, $filter, false));
4144
}
@@ -48,21 +51,25 @@ public function drop(): void
4851
/**
4952
* @param array<string, mixed> $filter
5053
* @param array<string, mixed> $options
54+
*
55+
* @return Cursor<T>
5156
*/
52-
public function find(array $filter = [], array $options = []): Result
57+
public function find(array $filter = [], array $options = []): Cursor
5358
{
5459
return $this->client->run(new Operation\Find($this->databaseName, $this->collectionName, $filter, $options));
5560
}
5661

5762
/**
5863
* @param array<string, mixed> $filter
5964
* @param array<string, mixed> $options
65+
*
66+
* @return T|null
6067
*/
61-
public function findOne(array $filter = [], array $options = []): array|object|null
68+
public function findOne(array $filter = [], array $options = []): array|null
6269
{
6370
$result = $this->client->run(new Operation\FindOne($this->databaseName, $this->collectionName, $filter, $options));
6471

65-
if (!$result instanceof Result) {
72+
if (!$result instanceof Cursor) {
6673
return null;
6774
}
6875

@@ -75,7 +82,7 @@ public function findOne(array $filter = [], array $options = []): array|object|n
7582
* @param list<array<string, mixed>> $documents
7683
* @param array<string, mixed> $options
7784
*/
78-
public function insertMany(array $documents, array $options = []): Result
85+
public function insertMany(array $documents, array $options = []): InsertManyResult
7986
{
8087
return $this->client->run(new Operation\InsertMany($this->databaseName, $this->collectionName, $documents, $options));
8188
}
@@ -84,7 +91,7 @@ public function insertMany(array $documents, array $options = []): Result
8491
* @param array<string, mixed> $document
8592
* @param array<string, mixed> $options
8693
*/
87-
public function insertOne(array $document, array $options = []): Result
94+
public function insertOne(array $document, array $options = []): InsertOneResult
8895
{
8996
return $this->client->run(new Operation\InsertOne($this->databaseName, $this->collectionName, $document, $options));
9097
}
@@ -94,7 +101,7 @@ public function insertOne(array $document, array $options = []): Result
94101
* @param array<string, mixed> $replacement
95102
* @param array<string, mixed> $options
96103
*/
97-
public function replaceOne(array $filter, array $replacement, array $options = []): Result
104+
public function replaceOne(array $filter, array $replacement, array $options = []): UpdateResult
98105
{
99106
return $this->client->run(new Operation\ReplaceOne($this->databaseName, $this->collectionName, $filter, $replacement, $options));
100107
}
@@ -104,7 +111,7 @@ public function replaceOne(array $filter, array $replacement, array $options = [
104111
* @param array<string, mixed> $update
105112
* @param array<string, mixed> $options
106113
*/
107-
public function updateMany(array $filter, array $update, array $options = []): Result
114+
public function updateMany(array $filter, array $update, array $options = []): UpdateResult
108115
{
109116
return $this->client->run(new Operation\Update($this->databaseName, $this->collectionName, $filter, $update, $options, true));
110117
}
@@ -114,16 +121,18 @@ public function updateMany(array $filter, array $update, array $options = []): R
114121
* @param array<string, mixed> $update
115122
* @param array<string, mixed> $options
116123
*/
117-
public function updateOne(array $filter, array $update, array $options = []): Result
124+
public function updateOne(array $filter, array $update, array $options = []): UpdateResult
118125
{
119126
return $this->client->run(new Operation\Update($this->databaseName, $this->collectionName, $filter, $update, $options, false));
120127
}
121128

122129
/**
123130
* @param list<array<string, mixed>> $pipeline
124131
* @param array<string, mixed> $options
132+
*
133+
* @return Cursor<T>
125134
*/
126-
public function aggregate(array $pipeline, array $options = []): Result
135+
public function aggregate(array $pipeline, array $options = []): Cursor
127136
{
128137
return $this->client->run(new Operation\Aggregate($this->databaseName, $this->collectionName, $pipeline, $options));
129138
}
@@ -142,12 +151,14 @@ public function distinct(string $fieldName, array $filter = [], array $options =
142151
/**
143152
* @param array<string, mixed> $filter
144153
* @param array<string, mixed> $options
154+
*
155+
* @return T|null
145156
*/
146-
public function findOneAndDelete(array $filter, array $options = []): array|object|null
157+
public function findOneAndDelete(array $filter, array $options = []): array|null
147158
{
148159
$result = $this->client->run(new Operation\FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options));
149160

150-
if (!$result instanceof Result) {
161+
if (!$result instanceof Cursor) {
151162
return null;
152163
}
153164

@@ -160,12 +171,14 @@ public function findOneAndDelete(array $filter, array $options = []): array|obje
160171
* @param array<string, mixed> $filter
161172
* @param array<string, mixed> $replacement
162173
* @param array<string, mixed> $options
174+
*
175+
* @return T|null
163176
*/
164-
public function findOneAndReplace(array $filter, array $replacement, array $options = []): array|object|null
177+
public function findOneAndReplace(array $filter, array $replacement, array $options = []): array|null
165178
{
166179
$result = $this->client->run(new Operation\FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options));
167180

168-
if (!$result instanceof Result) {
181+
if (!$result instanceof Cursor) {
169182
return null;
170183
}
171184

@@ -178,12 +191,14 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
178191
* @param array<string, mixed> $filter
179192
* @param array<string, mixed> $update
180193
* @param array<string, mixed> $options
194+
*
195+
* @return T|null
181196
*/
182-
public function findOneAndUpdate(array $filter, array $update, array $options = []): array|object|null
197+
public function findOneAndUpdate(array $filter, array $update, array $options = []): array|null
183198
{
184199
$result = $this->client->run(new Operation\FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options));
185200

186-
if (!$result instanceof Result) {
201+
if (!$result instanceof Cursor) {
187202
return null;
188203
}
189204

@@ -196,9 +211,9 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
196211
* @param list<array<string, array<string, mixed>>> $operations
197212
* @param array<string, mixed> $options
198213
*/
199-
public function bulkWrite(array $operations, array $options = []): void
214+
public function bulkWrite(array $operations, array $options = []): BulkWriteResult
200215
{
201-
$this->client->run(new Operation\BulkWrite($this->databaseName, $this->collectionName, $operations, $options));
216+
return $this->client->run(new Operation\BulkWrite($this->databaseName, $this->collectionName, $operations, $options));
202217
}
203218

204219
/**

src/Result.php renamed to src/Cursor.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,14 @@
1717
* @template T
1818
* @implements IteratorAggregate<int, T>
1919
*/
20-
class Result implements IteratorAggregate, Countable
20+
class Cursor implements IteratorAggregate, Countable
2121
{
2222
/** @param list<string>|PDOStatement $data */
2323
public function __construct(
2424
private readonly array|PDOStatement $data = [],
2525
) {
2626
}
2727

28-
public function getInsertedId(): string|null
29-
{
30-
if ($this->data instanceof PDOStatement) {
31-
return null;
32-
}
33-
34-
if (count($this->data) === 0) {
35-
return null;
36-
}
37-
38-
$first = json_decode($this->data[0], true);
39-
40-
return $first['_id'] ?? null;
41-
}
42-
4328
/** @return Traversable<int, array<string, mixed>> */
4429
public function getIterator(): Traversable
4530
{

src/DeleteResult.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Rango;
6+
7+
final class DeleteResult
8+
{
9+
public function __construct(
10+
private readonly int $deletedCount,
11+
) {
12+
}
13+
14+
public function getDeletedCount(): int
15+
{
16+
return $this->deletedCount;
17+
}
18+
19+
public function isAcknowledged(): bool
20+
{
21+
return true;
22+
}
23+
}

src/Exception/DecodeException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Rango\Exception;
6+
7+
use function sprintf;
8+
9+
final class DecodeException extends \RuntimeException implements Exception
10+
{
11+
public function __construct(string $data, string $error, int $code = 0, \Throwable|null $previous = null)
12+
{
13+
parent::__construct(
14+
sprintf("Could not decode JSON: %s\nError: %s", $data, $error),
15+
$code,
16+
$previous
17+
);
18+
}
19+
}

src/Exception/Exception.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Rango\Exception;
6+
7+
use RuntimeException;
8+
9+
interface Exception
10+
{
11+
}

src/Exception/QueryException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Rango\Exception;
6+
7+
use function sprintf;
8+
9+
final class QueryException extends \RuntimeException implements Exception
10+
{
11+
public function __construct(string $query, string $error, int $code = 0, \Throwable|null $previous = null)
12+
{
13+
parent::__construct(
14+
sprintf("Query failed: %s\nError: %s", $query, $error),
15+
$code,
16+
$previous
17+
);
18+
}
19+
}

src/Exception/RangoException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Rango\Exception;
6+
7+
use RuntimeException;
8+
9+
final class RangoException extends RuntimeException implements Exception
10+
{
11+
}

0 commit comments

Comments
 (0)