Skip to content

Commit 1ac158e

Browse files
committed
ydb query builder introduced
1 parent 1abb82b commit 1ac158e

File tree

4 files changed

+289
-36
lines changed

4 files changed

+289
-36
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,39 @@ $result = $table->query('select * from `users` limit 10;');
205205
```
206206

207207
As soon as your script is finished, the session will be destroyed.
208+
209+
## Customizing queries
210+
211+
Normally, a regular query through the `query()` method is sufficient, but in exceptional cases, you may need to fine-tune the query settings. You could do it using the query builder:
212+
213+
```php
214+
<?php
215+
216+
$session = $table->session();
217+
218+
// creating a new query builder instance
219+
$query = $session->newQuery('select * from `users` limit 10;');
220+
221+
// a setting to keep in cache
222+
$query->keepInCache();
223+
224+
// a setting to begin a transaction with the given mode
225+
$query->beginTx('stale');
226+
227+
$result = $query->execute();
228+
```
229+
230+
Methods of the query builder:
231+
232+
- `keepInCache(bool $value)` - keep in cache (default: `true`)
233+
- `collectStats(int $value)` - collect stats (default: 1)
234+
- `parameters(array $parameters)` - parameters
235+
- `operationParams(\Ydb\Operations\OperationParams $operation_params)` - operation params
236+
- `beginTx(string $mode)` - begin a transaction with the given [mode](https://cloud.yandex.ru/docs/ydb/concepts/transactions):
237+
- stale
238+
- online
239+
- online_inconsistent
240+
- serializable
241+
- `txControl(\Ydb\Table\TransactionControl $tx_control)` - transaction control with custom settings
242+
243+
You can chain these methods for convenience.

src/Session.php

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,32 @@ public function keepInCache($value = true)
312312
}
313313

314314
/**
315-
* @param string $yql
315+
* @param string|\Ydb\Table\Query $yql
316+
* @return YdbQuery
317+
* @throws \YandexCloud\Ydb\Exception
318+
*/
319+
public function newQuery($yql)
320+
{
321+
return new YdbQuery($this, $yql);
322+
}
323+
324+
/**
325+
* @param YdbQuery $query
326+
* @return bool|QueryResult
327+
* @throws \YandexCloud\Ydb\Exception
328+
*/
329+
public function executeQuery(YdbQuery $query)
330+
{
331+
$data = $query->getRequestData();
332+
$data['session_id'] = $this->session_id;
333+
334+
$result = $this->request('ExecuteDataQuery', $data);
335+
336+
return $result ? new QueryResult($result) : true;
337+
}
338+
339+
/**
340+
* @param string|\Ydb\Table\Query $yql
316341
* @param array|null $parameters
317342
* @return bool|QueryResult
318343
* @throws \YandexCloud\Ydb\Exception
@@ -326,48 +351,22 @@ public function query($yql, array $parameters = null)
326351
$tx_id = $this->beginTransaction();
327352
}
328353

329-
if (is_a($yql, Query::class))
330-
{
331-
$q = $yql;
332-
}
333-
else
334-
{
335-
$q = new Query([
336-
'yql_text' => $yql,
337-
]);
338-
}
339-
340-
$transaction_control = new TransactionControl([
354+
$tx_control = new TransactionControl([
341355
'tx_id' => $tx_id,
342-
// 'commit_tx' => true,
343-
]);
344-
345-
$query_cache_policy = new QueryCachePolicy([
346-
'keep_in_cache' => $this->keep_query_in_cache,
347356
]);
348357

349-
$data = [
350-
'session_id' => $this->session_id,
351-
'query' => $q,
352-
'tx_control' => $transaction_control,
353-
'query_cache_policy' => $query_cache_policy,
354-
'collect_stats' => 1,
355-
];
356-
357-
if ($parameters !== null)
358-
{
359-
$data['parameters'] = $parameters;
360-
}
358+
$query = $this->newQuery($yql)
359+
->parameters($parameters)
360+
->txControl($tx_control)
361+
->keepInCache($this->keep_query_in_cache);
361362

362-
$result = $this->request('ExecuteDataQuery', $data);
363-
364-
return $result ? new QueryResult($result) : true;
363+
return $this->executeQuery($query);
365364
}
366365

367366
/**
368367
* An alias to query with no result.
369368
*
370-
* @param string $yql
369+
* @param string|\Ydb\Table\Query $yql
371370
* @param array|null $parameters
372371
* @return bool
373372
* @throws \YandexCloud\Ydb\Exception

src/Table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function transaction(Closure $closure)
197197
/**
198198
* Proxy to Session::query.
199199
*
200-
* @param string $yql
200+
* @param string|\Ydb\Table\Query $yql
201201
* @return bool|QueryResult
202202
* @throws \YandexCloud\Ydb\Exception
203203
*/
@@ -209,7 +209,7 @@ public function query($yql)
209209
/**
210210
* Proxy to Session::exec.
211211
*
212-
* @param string $yql
212+
* @param string|\Ydb\Table\Query $yql
213213
* @return bool
214214
* @throws \YandexCloud\Ydb\Exception
215215
*/

src/YdbQuery.php

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
namespace YandexCloud\Ydb;
4+
5+
use Ydb\Table\Query;
6+
use Ydb\Table\QueryCachePolicy;
7+
use Ydb\Table\StaleModeSettings;
8+
use Ydb\Table\OnlineModeSettings;
9+
use Ydb\Table\TransactionControl;
10+
use Ydb\Table\TransactionSettings;
11+
use Ydb\Table\SerializableModeSettings;
12+
use Ydb\Operations\OperationParams;
13+
14+
class YdbQuery
15+
{
16+
/**
17+
* @var Session
18+
*/
19+
protected $session = null;
20+
21+
/**
22+
* @var \Ydb\Table\Query
23+
*/
24+
protected $yql;
25+
26+
/**
27+
* @var bool
28+
*/
29+
protected $keep_query_in_cache = false;
30+
31+
/**
32+
* @var int
33+
*/
34+
protected $collect_stats = 1;
35+
36+
/**
37+
* @var \Ydb\Table\TransactionControl
38+
*/
39+
protected $tx_control;
40+
41+
/**
42+
* @var \Ydb\Operations\OperationParams
43+
*/
44+
protected $operation_params;
45+
46+
/**
47+
* @var array
48+
*/
49+
protected $parameters = null;
50+
51+
/**
52+
* @param Session $session
53+
* @param string|\Ydb\Table\Query $yql
54+
*/
55+
public function __construct(Session $session, $yql)
56+
{
57+
$this->session = $session;
58+
$this->yql($yql);
59+
}
60+
61+
/**
62+
* @param string|\Ydb\Table\Query $yql
63+
* @return $this
64+
*/
65+
public function yql($yql)
66+
{
67+
if (!is_a($yql, Query::class))
68+
{
69+
$yql = new Query([
70+
'yql_text' => $yql,
71+
]);
72+
}
73+
$this->yql = $yql;
74+
return $this;
75+
}
76+
77+
/**
78+
* Set whether to keep query in cache.
79+
*
80+
* @param bool $value
81+
* @return $this
82+
*/
83+
public function keepInCache($value = true)
84+
{
85+
$this->keep_query_in_cache = (bool)$value;
86+
return $this;
87+
}
88+
89+
/**
90+
* Set whether to collect stats.
91+
*
92+
* @param int $value
93+
* @return $this
94+
*/
95+
public function collectStats($value = 1)
96+
{
97+
$this->collect_stats = $value;
98+
return $this;
99+
}
100+
101+
/**
102+
* Set parameters.
103+
*
104+
* @param array|null $parameters
105+
* @return $this
106+
*/
107+
public function parameters(array $parameters = null)
108+
{
109+
$this->parameters = $parameters;
110+
return $this;
111+
}
112+
113+
/**
114+
* Set operation params.
115+
*
116+
* @param array|\Ydb\Operations\OperationParams $params
117+
* @return $this
118+
*/
119+
public function operationParams($operation_params)
120+
{
121+
if (!is_a($operation_params, OperationParams::class))
122+
{
123+
$operation_params = new OperationParams($operation_params);
124+
}
125+
$this->operation_params = $operation_params;
126+
return $this;
127+
}
128+
129+
/**
130+
* Set transaction control.
131+
*
132+
* @param \Ydb\Table\TransactionControl $tx_control
133+
* @return $this
134+
*/
135+
public function txControl(TransactionControl $tx_control)
136+
{
137+
$this->tx_control = $tx_control;
138+
return $this;
139+
}
140+
141+
/**
142+
* Begin a transaction with the given mode (stale, online, serializable).
143+
*
144+
* @param string $mode
145+
* @return $this
146+
*/
147+
public function beginTx($mode)
148+
{
149+
$tx_settings = [];
150+
151+
switch ($mode)
152+
{
153+
case 'stale':
154+
case 'stale_read_only':
155+
$tx_settings['stale_read_only'] = new StaleModeSettings;
156+
break;
157+
158+
case 'online':
159+
case 'online_read_only':
160+
$tx_settings['online_read_only'] = new OnlineModeSettings([
161+
'allow_inconsistent_reads' => false,
162+
]);
163+
break;
164+
165+
case 'inconsistent_reads':
166+
case 'online_inconsistent':
167+
case 'online_inconsistent_reads':
168+
$tx_settings['online_read_only'] = new OnlineModeSettings([
169+
'allow_inconsistent_reads' => true,
170+
]);
171+
break;
172+
173+
case 'serializable':
174+
case 'serializable_read_write':
175+
default:
176+
$tx_settings['serializable_read_write'] = new SerializableModeSettings;
177+
break;
178+
}
179+
180+
$this->tx_control = new TransactionControl([
181+
'begin_tx' => new TransactionSettings($tx_settings),
182+
'commit_tx' => true,
183+
]);
184+
return $this;
185+
}
186+
187+
/**
188+
* @return array
189+
*/
190+
public function getRequestData()
191+
{
192+
$data = [];
193+
$data['query'] = $this->yql;
194+
$data['tx_control'] = $this->tx_control;
195+
$data['collect_stats'] = $this->collect_stats;
196+
if ($this->parameters)
197+
{
198+
$data['parameters'] = $this->parameters;
199+
}
200+
if ($this->operation_params)
201+
{
202+
$data['operation_params'] = $this->operation_params;
203+
}
204+
$data['query_cache_policy'] = new QueryCachePolicy([
205+
'keep_in_cache' => $this->keep_query_in_cache,
206+
]);
207+
return $data;
208+
}
209+
210+
/**
211+
* @return bool|QueryResult
212+
* @throws \YandexCloud\Ydb\Exception
213+
*/
214+
public function execute()
215+
{
216+
return $this->session->executeQuery($this);
217+
}
218+
}

0 commit comments

Comments
 (0)