YdbQuery.php 4.8 KB

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