Skip to content

Commit 33eb0e1

Browse files
authored
Merge pull request #60 from ydb-platform/fix-prepare
Removed query id in prepare statement
2 parents 200d890 + 340816c commit 33eb0e1

File tree

7 files changed

+66
-36
lines changed

7 files changed

+66
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* removed query id in prepare statement
2+
13
## 1.5.2
24

35
* fixed refresh token when it expired

src/Session.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Session
6868
/**
6969
* @var bool
7070
*/
71-
protected $keep_query_in_cache = false;
71+
protected $keep_query_in_cache = null;
7272

7373
/**
7474
* @param Table $table
@@ -359,7 +359,7 @@ public function query($yql, array $parameters = null)
359359
$query = $this->newQuery($yql)
360360
->parameters($parameters)
361361
->txControl($tx_control)
362-
->keepInCache($this->keep_query_in_cache);
362+
->keepInCache($this->keep_query_in_cache ?? ($parameters&&count($parameters)>0));
363363

364364
return $this->executeQuery($query);
365365
}
@@ -426,9 +426,7 @@ public function prepare($yql)
426426
'yql_text' => $yql,
427427
]);
428428

429-
$query_id = $result->getQueryId();
430-
431-
$statement->saveQueryId($query_id);
429+
$statement->saveInCache();
432430

433431
return $statement;
434432
}

src/Statement.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class Statement
2121
*/
2222
protected $yql;
2323

24-
/**
25-
* @var string
26-
*/
27-
protected $query_id;
28-
2924
/**
3025
* @var string
3126
*/
@@ -67,7 +62,7 @@ public function __construct(Session $session, $yql)
6762
public function execute(array $parameters = [])
6863
{
6964
$q = new Query([
70-
'id' => $this->query_id,
65+
'yql_text' => $this->yql,
7166
]);
7267

7368
return $this->session->query($q, $this->prepareParameters($parameters));
@@ -81,23 +76,6 @@ public function isCached()
8176
return $this->cached;
8277
}
8378

84-
/**
85-
* @return string
86-
*/
87-
public function getQueryId()
88-
{
89-
return $this->query_id;
90-
}
91-
92-
/**
93-
* @param string $query_id
94-
*/
95-
public function saveQueryId($query_id)
96-
{
97-
$this->query_id = $query_id;
98-
static::$qcache[$this->qhash] = $this->query_id;
99-
}
100-
10179
/**
10280
* @param array $parameters
10381
* @return array
@@ -132,9 +110,8 @@ protected function prepareParameters($parameters)
132110
*/
133111
protected function checkQueryCache()
134112
{
135-
$this->qhash = sha1($this->session->id() . '~' . trim($this->yql));
136-
$this->query_id = static::$qcache[$this->qhash] ?? null;
137-
if ($this->query_id)
113+
$this->qhash = sha1(trim($this->yql));
114+
if (in_array($this->qhash, static::$qcache))
138115
{
139116
$this->cached = true;
140117
}
@@ -154,4 +131,8 @@ protected function detectParams()
154131
}
155132
}
156133
}
134+
135+
public function saveInCache(){
136+
static::$qcache[$this->qhash] = $this->qhash;
137+
}
157138
}

tests/PreparedQueryTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Ydb;
8+
9+
class PreparedQueryTest extends TestCase
10+
{
11+
public function test(){
12+
$config = [
13+
14+
// Database path
15+
'database' => '/local',
16+
17+
// Database endpoint
18+
'endpoint' => 'localhost:2136',
19+
20+
// Auto discovery (dedicated server only)
21+
'discovery' => false,
22+
23+
// IAM config
24+
'iam_config' => [
25+
'insecure' => true,
26+
],
27+
'credentials' => new AnonymousAuthentication()
28+
];
29+
$ydb = new Ydb($config);
30+
$table = $ydb->table();
31+
32+
$session = $table->createSession();
33+
34+
$prepared_query = $session->prepare('
35+
declare $pk as Int64;
36+
select $pk;');
37+
$x = 2;
38+
$result = $session->transaction(function($session) use ($prepared_query, $x){
39+
return $prepared_query->execute([
40+
'pk' => $x,
41+
]);
42+
});
43+
self::assertEquals($x,
44+
$result->rows()[0]['column0']
45+
);
46+
}
47+
}

tests/RefreshTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function test(){
7070
];
7171
$ydb = new Ydb($config);
7272
$table = $ydb->table();
73-
$session = $table->session();
73+
$session = $table->createSession();
7474
$token = MetaGetter::getMeta($session)["x-ydb-auth-ticket"][0];
7575
self::assertEquals(
7676
1,

tests/RetryOnBadSessionTest.php

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

55
use PHPUnit\Framework\TestCase;
66
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Table;
78
use YdbPlatform\Ydb\Ydb;
89

910
class SessionManager extends \YdbPlatform\Ydb\Session{
@@ -39,14 +40,15 @@ public function test(){
3940
];
4041
$ydb = new Ydb($config);
4142
$table = $ydb->table();
42-
$session = $table->session();
43+
$session = $table->createSession();
4344
$oldSessionId = SessionManager::getSessionId($session);
4445
$session->delete();
45-
$session = $table->session();
46+
$session = $table->createSession();
4647
SessionManager::setSessionId($session,$oldSessionId);
48+
$tres = $session->query('select 1 as res')->rows()[0]['res'];
4749
self::assertEquals(
4850
1,
49-
$session->query('select 1 as res')->rows()[0]['res']
51+
$tres
5052
);
5153
}
5254
}

tests/RunTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private function getYdbResult(array $config) : array
3838
$ydb = new Ydb($config);
3939
$table = $ydb->table();
4040

41-
$session = $table->session();
41+
$session = $table->createSession();
4242

4343
$session->createTable(
4444
'episodes',

0 commit comments

Comments
 (0)