Skip to content

Commit 6fa264f

Browse files
committed
Remove query id in prepare statement
1 parent 83b0925 commit 6fa264f

File tree

3 files changed

+49
-70
lines changed

3 files changed

+49
-70
lines changed

src/Session.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -416,20 +416,11 @@ public function prepare($yql)
416416
{
417417
$statement = new Statement($this, $yql);
418418

419-
if ($statement->isCached())
420-
{
421-
return $statement;
422-
}
423-
424419
$result = $this->request('PrepareDataQuery', [
425420
'session_id' => $this->session_id,
426421
'yql_text' => $yql,
427422
]);
428423

429-
$query_id = $result->getQueryId();
430-
431-
$statement->saveQueryId($query_id);
432-
433424
return $statement;
434425
}
435426

src/Statement.php

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,11 @@ class Statement
2121
*/
2222
protected $yql;
2323

24-
/**
25-
* @var string
26-
*/
27-
protected $query_id;
28-
29-
/**
30-
* @var string
31-
*/
32-
protected $qhash;
33-
34-
/**
35-
* @var bool
36-
*/
37-
protected $cached = false;
38-
3924
/**
4025
* @var array
4126
*/
4227
protected $params = [];
4328

44-
/**
45-
* @var array
46-
*/
47-
protected static $qcache = [];
48-
4929
/**
5030
* @param Session $session
5131
* @param string $yql
@@ -55,7 +35,6 @@ public function __construct(Session $session, $yql)
5535
$this->session = $session;
5636
$this->yql = $yql;
5737

58-
$this->checkQueryCache();
5938
$this->detectParams();
6039
}
6140

@@ -67,37 +46,12 @@ public function __construct(Session $session, $yql)
6746
public function execute(array $parameters = [])
6847
{
6948
$q = new Query([
70-
'id' => $this->query_id,
49+
'yql_text' => $this->yql,
7150
]);
7251

7352
return $this->session->query($q, $this->prepareParameters($parameters));
7453
}
7554

76-
/**
77-
* @return bool
78-
*/
79-
public function isCached()
80-
{
81-
return $this->cached;
82-
}
83-
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-
10155
/**
10256
* @param array $parameters
10357
* @return array
@@ -127,19 +81,6 @@ protected function prepareParameters($parameters)
12781
return $data;
12882
}
12983

130-
/**
131-
* @return void
132-
*/
133-
protected function checkQueryCache()
134-
{
135-
$this->qhash = sha1($this->session->id() . '~' . trim($this->yql));
136-
$this->query_id = static::$qcache[$this->qhash] ?? null;
137-
if ($this->query_id)
138-
{
139-
$this->cached = true;
140-
}
141-
}
142-
14384
/**
14485
* @return void
14586
*/

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->session();
33+
34+
$prepared_query = $ydb->table()->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+
}

0 commit comments

Comments
 (0)