Skip to content

Commit f4dd1aa

Browse files
authored
Added snapshot mode in noninteractive transaction
Add snapshot mode in noninteractive transaction
2 parents 46de874 + 00e9c0b commit f4dd1aa

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* added snapshot mode in noninteractive transaction
2+
13
## 1.12.0
24
* added StaticAuthentication
35
* added query timeout and canceled params

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ Methods of the query builder:
542542
- online
543543
- online_inconsistent
544544
- serializable
545+
- snapshot
545546
- `txControl(\Ydb\Table\TransactionControl $tx_control)` - transaction control with custom settings
546547

547548
You can chain these methods for convenience.

src/YdbQuery.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Ydb\Table\Query;
66
use Ydb\Table\QueryCachePolicy;
7+
use Ydb\Table\SnapshotModeSettings;
78
use Ydb\Table\StaleModeSettings;
89
use Ydb\Table\OnlineModeSettings;
910
use Ydb\Table\TransactionControl;
@@ -170,6 +171,11 @@ public function beginTx($mode)
170171
]);
171172
break;
172173

174+
case 'snapshot':
175+
case 'snapshot_read_only':
176+
$tx_settings['snapshot_read_only'] = new SnapshotModeSettings;
177+
break;
178+
173179
case 'serializable':
174180
case 'serializable_read_write':
175181
default:

tests/CheckTxSettingsTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Logger\SimpleStdLogger;
8+
use YdbPlatform\Ydb\Ydb;
9+
10+
class CheckTxSettingsTest extends TestCase
11+
{
12+
13+
/**
14+
* @var Ydb
15+
*/
16+
protected $ydb;
17+
/**
18+
* @var \YdbPlatform\Ydb\Table
19+
*/
20+
protected $table;
21+
/**
22+
* @var \YdbPlatform\Ydb\Session|null
23+
*/
24+
protected $session;
25+
26+
public function __construct(?string $name = null, array $data = [], $dataName = '')
27+
{
28+
parent::__construct($name, $data, $dataName);
29+
$config = [
30+
31+
// Database path
32+
'database' => '/local',
33+
34+
// Database endpoint
35+
'endpoint' => 'localhost:2136',
36+
37+
// Auto discovery (dedicated server only)
38+
'discovery' => false,
39+
40+
// IAM config
41+
'iam_config' => [
42+
'insecure' => true,
43+
],
44+
'credentials' => new AnonymousAuthentication()
45+
];
46+
$this->ydb = new Ydb($config, new SimpleStdLogger(SimpleStdLogger::DEBUG));
47+
$this->table = $this->ydb->table();
48+
$this->session = $this->table->session();
49+
}
50+
51+
public function testSerializableTxConfig(){
52+
$this->checkTx('serializable', 'serializable_read_write');
53+
}
54+
55+
public function testSnapshotTxConfig(){
56+
$this->checkTx('snapshot', 'snapshot_read_only');
57+
}
58+
public function testStaleTxConfig(){
59+
$this->checkTx('stale', 'stale_read_only');
60+
}
61+
public function testOnlineTxConfig(){
62+
$this->checkTx('online', 'online_read_only');
63+
}
64+
65+
protected function checkTx(string $mode, string $value)
66+
{
67+
$query= $this->session->newQuery("SELECT 1;")
68+
->beginTx($mode);
69+
self::assertEquals($value, $query->getRequestData()['tx_control']->getBeginTx()->getTxMode());
70+
$query->execute();
71+
}
72+
}

0 commit comments

Comments
 (0)