/
githubmirror
/
ydb-php-sdk
Обзор
Документация
Войти
/
githubmirror
/
ydb-php-sdk
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Statement.php
139 строк
3 KB
gfh31fgh2
Update Statement.php
05 дек 2025, 13:22
Не верифицирован
05 дек 2025, 13:22
4b811cc
Код
Авторство
О чём код?
<?php namespace YdbPlatform\Ydb; use Exception; use Ydb\TypedValue; use Ydb\Table\Query; class Statement { use Traits\TypeHelpersTrait; /** * @var Session */ protected $session; /** * @var string */ protected $yql; /** * @var string */ protected $qhash; /** * @var bool */ protected $cached = false; /** * @var array */ protected $params = []; /** * @var array */ protected static $qcache = []; /** * @param Session $session * @param string $yql */ public function __construct(Session $session, $yql) { $this->session = $session; $this->yql = $yql; $this->checkQueryCache(); $this->detectParams(); } /** * @param array $parameters * @param array $options * @return bool|QueryResult * @throws \YdbPlatform\Ydb\Exception */ public function execute(array $parameters = [], array $options = []) { $q = new Query([ 'yql_text' => $this->yql, ]); return $this->session->query($q, $this->prepareParameters($parameters), $options); } /** * @return bool */ public function isCached() { return $this->cached; } /** * @param array $parameters * @return array * @throws Exception */ protected function prepareParameters($parameters) { $data = []; foreach ($parameters as $key => $value) { if (substr($key, 0, 1) !== '$') { $key = '$' . $key; } if (!isset($this->params[$key])) { throw new Exception('YDB: Statement parameter [' . $key . '] not declared.'); } if (!is_a($value, TypedValue::class)) { $value = $this->typeValue($value, $this->params[$key])->toTypedValue(); } $data[$key] = $value; } return $data; } /** * @return void */ protected function checkQueryCache() { $this->qhash = sha1(trim($this->yql)); if (in_array($this->qhash, static::$qcache)) { $this->cached = true; } } /** * @return void */ protected function detectParams() { if (preg_match_all('/declare\s+(\$\w+)\s+as\s+(.+?);/is', $this->yql, $matches)) { foreach ($matches[1] as $i => $param) { $type = trim($matches[2][$i]); $this->params[$param] = $type; } } } public function saveInCache(){ static::$qcache[$this->qhash] = $this->qhash; } }