Skip to content

Commit 069a07e

Browse files
authored
Merge pull request #134 from ilyakharev/main
Added `ScanQueryMode` for `Table::scanQuery`
2 parents 5486f7a + 2b00631 commit 069a07e

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-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: 10 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,22 @@ public function describeTable($table)
395396

396397
/**
397398
* @param string $yql
399+
* @param array $parameters
400+
* @param int $mode
398401
* @return \Generator
402+
* @throws Exception
399403
*/
400-
public function scanQuery($yql)
404+
public function scanQuery($yql, $parameters = [], $mode = ScanQueryMode::MODE_EXEC)
401405
{
406+
if($parameters != []){
407+
throw new Exception("Not implemented");
408+
}
409+
402410
$q = new Query(['yql_text' => $yql]);
403411

404412
return $this->streamRequest('StreamExecuteScanQuery', [
405413
'query' => $q,
414+
'mode' => $mode
406415
]);
407416
}
408417

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 testScanQuery()
15+
{
16+
17+
$config = [
18+
19+
// Database path
20+
'database' => '/local',
21+
22+
// Database endpoint
23+
'endpoint' => 'localhost:2136',
24+
25+
// Auto discovery (dedicated server only)
26+
'discovery' => true,
27+
28+
// IAM config
29+
'iam_config' => [
30+
'insecure' => true,
31+
],
32+
33+
'credentials' => new AnonymousAuthentication()
34+
];
35+
$ydb = new Ydb($config, new \YdbPlatform\Ydb\Logger\SimpleStdLogger(7));
36+
$table = $ydb->table();
37+
38+
$table->retrySession(function (\YdbPlatform\Ydb\Session $session){
39+
$session->createTable(
40+
'scan_table',
41+
YdbTable::make()
42+
->addColumn('id', 'UINT64')
43+
->primaryKey('id')
44+
);
45+
}, true);
46+
47+
$yql = 'SELECT id
48+
FROM scan_table
49+
WHERE id = 1;';
50+
51+
$scanWithOutMode = $table->scanQuery($yql);
52+
$scanWithExplainMode = $table->scanQuery($yql, [], ScanQueryMode::MODE_EXPLAIN);
53+
$scanWithExecMode = $table->scanQuery($yql, [], ScanQueryMode::MODE_EXEC);
54+
55+
// These `foreach` needs for requests
56+
foreach ($scanWithOutMode as $value){}
57+
foreach ($scanWithExplainMode as $value){}
58+
foreach ($scanWithExecMode as $value){}
59+
60+
self::expectExceptionMessage("Not implemented");
61+
$scanWithParams = $table->scanQuery($yql, ["value"=>"some"]);
62+
foreach ($scanWithParams as $value){}
63+
}
64+
}

0 commit comments

Comments
 (0)