/
githubmirror
/
ydb-php-sdk
Обзор
Документация
Войти
/
githubmirror
/
ydb-php-sdk
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
examples/app/Commands/SelectCommand.php
73 строки
2 KB
Ilya Kharev
Update exceptions, examples
23 июн 2023, 13:37
23 июн 2023, 13:37
78af75c
Код
Авторство
О чём код?
<?php namespace App\Commands; use App\AppService; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use YdbPlatform\Ydb\Session; use YdbPlatform\Ydb\Ydb; class SelectCommand extends Command { /** * @var string */ protected static $defaultName = 'select'; /** * @var AppService */ protected $appService; public function __construct() { $this->appService = new AppService; parent::__construct(); } protected function configure() { $this->setDescription('Select rows form a table.'); $this->addArgument('table', InputArgument::REQUIRED, 'The table name.'); } /** * @param InputInterface $input * @param OutputInterface $output * @return int * @throws \YandexCloud\Ydb\Exception */ protected function execute(InputInterface $input, OutputInterface $output) { $table_name = $input->getArgument('table') ?: ''; $ydb = $this->appService->initYdb(); $result = $ydb->table()->retrySession(function (Session $session) use ($table_name, $output) { return $session->query('select * from `' . $table_name . '` limit 10;'); }, true); $output->writeln('Column count: ' . $result->columnCount()); $output->writeln('Row count: ' . $result->rowCount()); $t = new Table($output); $t ->setHeaders(array_map(function($column) { return $column['name']; }, $result->columns())) ->setRows($result->rows()) ; $t->render(); return Command::SUCCESS; } }