Skip to content

Commit bf87860

Browse files
author
Илья
committed
Use isNeedSetKeepQueryInCache
1 parent a078477 commit bf87860

File tree

3 files changed

+20
-197
lines changed

3 files changed

+20
-197
lines changed

src/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public function query($yql, array $parameters = null, array $options = [])
363363
$query = $this->newQuery($yql)
364364
->parameters($parameters)
365365
->txControl($tx_control)
366-
->keepInCache($this->keep_query_in_cache ?? ($parameters&&count($parameters)>0));
366+
->keepInCache($this->keep_query_in_cache);
367367

368368
if(isset($options['collectStats'])){
369369
$query->collectStats($options['collectStats']);

src/YdbQuery.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,17 @@ public function getRequestData()
213213
*/
214214
public function execute()
215215
{
216-
if (is_null($this->keep_query_in_cache)){
217-
$this->keep_query_in_cache = $this->parameters&&count($this->parameters)>0;
218-
}
216+
$this->keep_query_in_cache = self::isNeedSetKeepQueryInCache($this->keep_query_in_cache, $this->parameters);
219217
return $this->session->executeQuery($this);
220218
}
219+
220+
/**
221+
* @param bool|null $currentParams
222+
* @param array|null $queryDeclaredParams
223+
* @return bool
224+
*/
225+
protected static function isNeedSetKeepQueryInCache(?bool $currentParams, ?array $queryDeclaredParams): bool
226+
{
227+
return $currentParams ?? $queryDeclaredParams&&count($queryDeclaredParams)>0;
228+
}
221229
}

tests/KeepInCacheTest.php

Lines changed: 8 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -3,220 +3,35 @@
33
namespace YdbPlatform\Ydb\Test;
44

55
use PHPUnit\Framework\TestCase;
6-
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7-
use YdbPlatform\Ydb\Logger\SimpleStdLogger;
86
use YdbPlatform\Ydb\Session;
9-
use YdbPlatform\Ydb\Table;
10-
use YdbPlatform\Ydb\Types\Int64Type;
11-
use YdbPlatform\Ydb\Ydb;
12-
use YdbPlatform\Ydb\YdbQuery;
137

148
class KeepInCacheTest extends TestCase
159
{
16-
/**
17-
* @var Ydb
18-
*/
19-
protected $ydb;
20-
/**
21-
* @var Table
22-
*/
23-
protected $table;
24-
protected $yqlPrepare = '
25-
declare $pk as Int64;
26-
select $pk;';
27-
protected $yql = 'select 1;';
28-
29-
public function __construct(?string $name = null, array $data = [], $dataName = '')
30-
{
31-
parent::__construct($name, $data, $dataName);
32-
$config = [
33-
34-
// Database path
35-
'database' => '/local',
36-
37-
// Database endpoint
38-
'endpoint' => 'localhost:2136',
39-
40-
// Auto discovery (dedicated server only)
41-
'discovery' => false,
42-
43-
// IAM config
44-
'iam_config' => [
45-
'insecure' => true,
46-
],
47-
'credentials' => new AnonymousAuthentication()
48-
];
49-
$this->ydb = new Ydb($config, new SimpleStdLogger(SimpleStdLogger::DEBUG));
50-
$this->table = $this->ydb->table();
51-
$this->sessionId = $this->table->createSession()->id();
52-
}
10+
protected $sampleParams = ["x", 1];
5311

5412
public function testPreparedQueryWithParamsWithoutConfig()
5513
{
56-
$session = new EditablePreparedSession($this->table, "");
57-
SessionWithEditable::setId($session, $this->sessionId);
58-
SessionWithEditable::setIdle($session);
59-
TableWithEditableSessions::addSession($this->table, $session);
60-
$prepared_query = $this->table->session()->prepare($this->yqlPrepare);
61-
$x = 2;
62-
$result = $prepared_query->execute([
63-
'pk' => $x,
64-
]);
65-
self::assertEquals(true, $result);
66-
TableWithEditableSessions::removeSession($this->table, $session);
14+
self::assertEquals(true, YdbQuery::isNeedSetKeepQueryInCache(null, $this->sampleParams));
6715
}
6816

6917
public function testPreparedQueryWithParamsWithConfig()
7018
{
71-
$session = new EditablePreparedSession($this->table, "");
72-
SessionWithEditable::setId($session, $this->sessionId);
73-
SessionWithEditable::setIdle($session);
74-
TableWithEditableSessions::addSession($this->table, $session);
75-
$session->keepInCache(false);
76-
$prepared_query = $this->table->session()->prepare($this->yqlPrepare);
77-
$x = 2;
78-
$result = $prepared_query->execute([
79-
'pk' => $x,
80-
]);
81-
self::assertEquals(false, $result);
82-
TableWithEditableSessions::removeSession($this->table, $session);
19+
self::assertEquals(false, YdbQuery::isNeedSetKeepQueryInCache(false, $this->sampleParams));
8320
}
8421

8522
public function testPreparedQueryWithoutParamsWithoutConfig()
8623
{
87-
$session = new EditablePreparedSession($this->table, "");
88-
SessionWithEditable::setId($session, $this->sessionId);
89-
SessionWithEditable::setIdle($session);
90-
TableWithEditableSessions::addSession($this->table, $session);
91-
$prepared_query = $this->table->session()->prepare($this->yql);
92-
$result = $prepared_query->execute();
93-
self::assertEquals(false, $result);
94-
TableWithEditableSessions::removeSession($this->table, $session);
24+
self::assertEquals(false, YdbQuery::isNeedSetKeepQueryInCache(null, null));
9525
}
9626

9727
public function testPreparedQueryWithoutParamsWithConfig()
9828
{
99-
$session = new EditablePreparedSession($this->table, "");
100-
SessionWithEditable::setId($session, $this->sessionId);
101-
SessionWithEditable::setIdle($session);
102-
TableWithEditableSessions::addSession($this->table, $session);
103-
$session->keepInCache(true);
104-
$prepared_query = $this->table->session()->prepare($this->yql);
105-
$result = $prepared_query->execute();
106-
self::assertEquals(true, $result);
107-
TableWithEditableSessions::removeSession($this->table, $session);
108-
}
109-
110-
public function testNewQueryWithParamsWithoutConfig()
111-
{
112-
$session = new EditablePreparedSession($this->table, "");
113-
SessionWithEditable::setId($session, $this->sessionId);
114-
SessionWithEditable::setIdle($session);
115-
TableWithEditableSessions::addSession($this->table, $session);
116-
$query = $session->newQuery($this->yqlPrepare);
117-
$query->parameters([
118-
'$pk' => (new Int64Type(2))->toTypedValue(),
119-
]);
120-
$query->beginTx('stale');
121-
$result = $query->execute();
122-
self::assertEquals(true, $result);
123-
TableWithEditableSessions::removeSession($this->table, $session);
124-
}
125-
126-
public function testNewQueryWithoutParamsWithoutConfig()
127-
{
128-
$session = new EditablePreparedSession($this->table, "");
129-
SessionWithEditable::setId($session, $this->sessionId);
130-
SessionWithEditable::setIdle($session);
131-
TableWithEditableSessions::addSession($this->table, $session);
132-
$query = $session->newQuery($this->yql);
133-
$query->parameters();
134-
$query->beginTx('stale');
135-
$result = $query->execute();
136-
self::assertEquals(false, $result);
137-
TableWithEditableSessions::removeSession($this->table, $session);
138-
}
139-
140-
public function testNewQueryWithParamsWithConfig()
141-
{
142-
$session = new EditablePreparedSession($this->table, "");
143-
SessionWithEditable::setId($session, $this->sessionId);
144-
SessionWithEditable::setIdle($session);
145-
TableWithEditableSessions::addSession($this->table, $session);
146-
$query = $session->newQuery($this->yqlPrepare);
147-
$query->parameters([
148-
'$pk' => (new Int64Type(2))->toTypedValue(),
149-
]);
150-
$query->beginTx('stale');
151-
$query->keepInCache(false);
152-
$result = $query->execute();
153-
self::assertEquals(false, $result);
154-
TableWithEditableSessions::removeSession($this->table, $session);
155-
}
156-
157-
public function testNewQueryWithoutParamsWithConfig()
158-
{
159-
$session = new EditablePreparedSession($this->table, "");
160-
SessionWithEditable::setId($session, $this->sessionId);
161-
SessionWithEditable::setIdle($session);
162-
TableWithEditableSessions::addSession($this->table, $session);
163-
$query = $session->newQuery($this->yql);
164-
$query->parameters();
165-
$query->beginTx('stale');
166-
$query->keepInCache(true);
167-
$result = $query->execute();
168-
self::assertEquals(true, $result);
169-
TableWithEditableSessions::removeSession($this->table, $session);
170-
}
171-
}
172-
173-
class TableWithEditableSessions extends Table
174-
{
175-
public static function addSession(Table $table, Session $session)
176-
{
177-
$table::$session_pool->addSession($session);
29+
self::assertEquals(false, YdbQuery::isNeedSetKeepQueryInCache(null, null));
17830
}
179-
public static function removeSession(Table $table, Session $session)
180-
{
181-
$table::$session_pool->dropSession($session->id());
182-
}
183-
}
184-
185-
class SessionWithEditable extends Session
186-
{
187-
public static function setId(Session $session, string $id)
188-
{
189-
$session->session_id = $id;
190-
}
191-
192-
public static function setIdle(Session $session)
193-
{
194-
$session->is_busy = false;
195-
}
196-
197-
public function executeQuery(YdbQuery $query)
198-
{
199-
return $query->getRequestData()['query_cache_policy']->getKeepInCache();
200-
}
201-
20231
}
203-
204-
class EditablePreparedSession extends Session
205-
{
206-
public static function setId(Session $session, string $id)
207-
{
208-
$session->session_id = $id;
209-
}
210-
211-
public static function setIdle(Session $session)
212-
{
213-
$session->is_busy = false;
214-
}
215-
216-
217-
public function executeQuery(YdbQuery $query)
32+
class YdbQuery extends \YdbPlatform\Ydb\YdbQuery{
33+
public static function isNeedSetKeepQueryInCache(?bool $currentParams, ?array $queryDeclaredParams): bool
21834
{
219-
return $query->getRequestData()['query_cache_policy']->getKeepInCache();
35+
return parent::isNeedSetKeepQueryInCache($currentParams, $queryDeclaredParams);
22036
}
221-
22237
}

0 commit comments

Comments
 (0)