Skip to content

Commit 0837ee1

Browse files
authored
Merge branch 'main' into keep-in-cache
2 parents f7711af + 04b50d9 commit 0837ee1

File tree

6 files changed

+169
-3
lines changed

6 files changed

+169
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
* fix keepInCache param in YdbQuery
2+
* add logger as Ydb config
3+
* added snapshot mode in noninteractive transaction
24

35
## 1.12.0
46
* added StaticAuthentication

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,23 @@ 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.
549+
550+
## Logging
551+
552+
For logging purposes, you need use class, which implements `\Psr\Log\LoggerInterface`.
553+
YDB-PHP-SDK has build-in loggers in `YdbPlatform\Ydb\Logger` namespace:
554+
* `NullLogger` - default
555+
* `SimpleStdLogger($level)` - logger, which push logs in STDERR.
556+
557+
Example of using:
558+
```php
559+
$config = [
560+
'logger' => new \YdbPlatform\Ydb\Logger\SimpleStdLogger(\YdbPlatform\Ydb\Logger\SimpleStdLogger::INFO)
561+
]
562+
$ydb = new \YdbPlatform\Ydb\Ydb($config);
563+
```
564+

src/Ydb.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ public function __construct($config = [], LoggerInterface $logger = null)
107107
$this->database = $config['database'] ?? null;
108108
$this->iam_config = $config['iam_config'] ?? [];
109109

110-
if ($logger){
111-
$this->logger = $logger;
110+
if (!is_null($logger) && isset($config['logger'])){
111+
throw new \Exception('Logger set in 2 places');
112+
} else if (isset($config['logger'])) {
113+
$this->setLogger($config['logger']);
114+
} else if ($logger) {
115+
$this->setLogger($logger);
112116
} else {
113-
$this->logger = new NullLogger();
117+
$this->setLogger(new NullLogger());
114118
}
115119

116120
$this->retry = new Retry($this->logger);
@@ -351,4 +355,21 @@ public function discoveryInterval()
351355
{
352356
return $this->discoveryInterval;
353357
}
358+
359+
/**
360+
* @return LoggerInterface|NullLogger
361+
*/
362+
public function getLogger()
363+
{
364+
return $this->logger;
365+
}
366+
367+
/**
368+
* @param LoggerInterface $logger
369+
* @return void
370+
*/
371+
protected function setLogger(LoggerInterface $logger){
372+
$this->logger = $logger;
373+
}
374+
354375
}

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+
}

tests/LoggerCheckTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\NullLogger;
8+
use YdbPlatform\Ydb\Logger\SimpleStdLogger;
9+
use YdbPlatform\Ydb\Ydb;
10+
11+
class LoggerCheckTest extends TestCase{
12+
13+
public function testExceptExceptionInConfigs(){
14+
$config = [
15+
'logger' => new SimpleStdLogger(7)
16+
];
17+
$this->expectException('Exception');
18+
new Ydb($config, new NullLogger());
19+
}
20+
public function testCheckUseLoggerFromConfig(){
21+
$logger = new SimpleStdLogger(7);
22+
$config = [
23+
'logger' => $logger
24+
];
25+
$ydb = new Ydb($config);
26+
$this->assertEquals($logger, $ydb->getLogger());
27+
}
28+
public function testCheckUseLoggerFromParam(){
29+
$logger = new SimpleStdLogger(7);
30+
$config = [];
31+
$ydb = new Ydb($config, $logger);
32+
$this->assertEquals($logger, $ydb->getLogger());
33+
}
34+
public function testCheckUseNullLogger(){
35+
$config = [];
36+
$ydb = new Ydb($config);
37+
$this->assertInstanceOf(NullLogger::class, $ydb->getLogger());
38+
}
39+
40+
public function testThrowExceptionOnNonLoggerObject(){
41+
$config = [
42+
'logger' => new AnonymousAuthentication()
43+
];
44+
$this->expectException('TypeError');
45+
$ydb = new Ydb($config);
46+
$this->assertInstanceOf(NullLogger::class, $ydb->getLogger());
47+
}
48+
}

0 commit comments

Comments
 (0)