Skip to content

Commit ca34159

Browse files
committed
Added ScanQueryMode for Table::scanQuery
1 parent 5486f7a commit ca34159

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* added `ScanQueryMode` for `Table::scanQuery`
2+
13
## 1.13.2
24
### Bugs
35

protos/Ydb/Table/ExecuteScanQueryRequest.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Enums/ScanQueryMode.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Enums;
4+
5+
use UnexpectedValueException;
6+
use Ydb\Table\ExecuteScanQueryRequest\Mode;
7+
8+
class ScanQueryMode
9+
{
10+
const MODE_UNSPECIFIED = Mode::MODE_UNSPECIFIED;
11+
const MODE_EXPLAIN = Mode::MODE_EXPLAIN;
12+
const MODE_EXEC = Mode::MODE_EXEC;
13+
private static $valueToName = [
14+
self::MODE_UNSPECIFIED => 'MODE_UNSPECIFIED',
15+
self::MODE_EXPLAIN => 'MODE_EXPLAIN',
16+
self::MODE_EXEC => 'MODE_EXEC',
17+
];
18+
19+
public static function name($value)
20+
{
21+
if (!isset(self::$valueToName[$value])) {
22+
throw new UnexpectedValueException(sprintf(
23+
'Enum %s has no name defined for value %s', __CLASS__, $value));
24+
}
25+
return self::$valueToName[$value];
26+
}
27+
28+
29+
public static function value($name)
30+
{
31+
$const = __CLASS__ . '::' . strtoupper($name);
32+
if (!defined($const)) {
33+
throw new UnexpectedValueException(sprintf(
34+
'Enum %s has no value defined for name %s', __CLASS__, $name));
35+
}
36+
return constant($const);
37+
}
38+
}

src/Table.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Ydb\Table\Query;
99
use Ydb\Table\V1\TableServiceClient as ServiceClient;
1010
use YdbPlatform\Ydb\Contracts\SessionPoolContract;
11+
use YdbPlatform\Ydb\Enums\ScanQueryMode;
1112
use YdbPlatform\Ydb\Exceptions\Grpc\InvalidArgumentException;
1213
use YdbPlatform\Ydb\Exceptions\Grpc\UnknownException;
1314
use YdbPlatform\Ydb\Exceptions\NonRetryableException;
@@ -395,14 +396,16 @@ public function describeTable($table)
395396

396397
/**
397398
* @param string $yql
399+
* @param ScanQueryMode $mode
398400
* @return \Generator
399401
*/
400-
public function scanQuery($yql)
402+
public function scanQuery($yql, $mode = ScanQueryMode::MODE_EXEC)
401403
{
402404
$q = new Query(['yql_text' => $yql]);
403405

404406
return $this->streamRequest('StreamExecuteScanQuery', [
405407
'query' => $q,
408+
'mode' => $mode
406409
]);
407410
}
408411

tests/ScanQueryTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Enums\ScanQueryMode;
8+
use YdbPlatform\Ydb\Ydb;
9+
use YdbPlatform\Ydb\YdbTable;
10+
11+
class ScanQueryTest extends TestCase
12+
{
13+
14+
function testScanQueryWith()
15+
{
16+
self::expectNotToPerformAssertions();
17+
18+
$config = [
19+
20+
// Database path
21+
'database' => '/local',
22+
23+
// Database endpoint
24+
'endpoint' => 'localhost:2136',
25+
26+
// Auto discovery (dedicated server only)
27+
'discovery' => true,
28+
29+
// IAM config
30+
'iam_config' => [
31+
'insecure' => true,
32+
],
33+
34+
'credentials' => new AnonymousAuthentication()
35+
];
36+
$ydb = new Ydb($config, new \YdbPlatform\Ydb\Logger\SimpleStdLogger(7));
37+
$table = $ydb->table();
38+
39+
$table->retrySession(function (\YdbPlatform\Ydb\Session $session){
40+
$session->createTable(
41+
'episodes',
42+
YdbTable::make()
43+
->addColumn('series_id', 'UINT64')
44+
->addColumn('title', 'UTF8')
45+
->addColumn('episode_id', 'UINT64')
46+
->addColumn('season_id', 'UINT64')
47+
->primaryKey('series_id')
48+
);
49+
}, true);
50+
51+
$yql = 'SELECT series_id, season_id, title, first_aired
52+
FROM seasons
53+
WHERE series_id = 1;';
54+
55+
$scanWithOutParam = $table->scanQuery($yql);
56+
$scanWithExplainParam = $table->scanQuery($yql, ScanQueryMode::MODE_EXPLAIN);
57+
$scanWithExecParam = $table->scanQuery($yql, ScanQueryMode::MODE_EXEC);
58+
59+
// These `foreach` needs for requests
60+
foreach ($scanWithOutParam as $value){}
61+
foreach ($scanWithExplainParam as $value){}
62+
foreach ($scanWithExecParam as $value){}
63+
}
64+
}

0 commit comments

Comments
 (0)