Select1Command.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Commands;
  3. use App\AppService;
  4. use Symfony\Component\Console\Helper\Table;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use YdbPlatform\Ydb\Session;
  10. use YdbPlatform\Ydb\Ydb;
  11. class Select1Command extends Command
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected static $defaultName = 'select1';
  17. /**
  18. * @var AppService
  19. */
  20. protected $appService;
  21. public function __construct()
  22. {
  23. $this->appService = new AppService;
  24. parent::__construct();
  25. }
  26. protected function configure()
  27. {
  28. $this->setDescription('Select 1.');
  29. }
  30. /**
  31. * @param InputInterface $input
  32. * @param OutputInterface $output
  33. * @return int
  34. * @throws \YandexCloud\Ydb\Exception
  35. */
  36. protected function execute(InputInterface $input, OutputInterface $output)
  37. {
  38. $ydb = $this->appService->initYdb();
  39. $result = $ydb->table()->retrySession(function (Session $session) use ($output) {
  40. return $session->query('select 1;');
  41. }, true);
  42. $output->writeln('Column count: ' . $result->columnCount());
  43. $output->writeln('Row count: ' . $result->rowCount());
  44. $t = new Table($output);
  45. $t
  46. ->setHeaders(array_map(function($column) {
  47. return $column['name'];
  48. }, $result->columns()))
  49. ->setRows($result->rows())
  50. ;
  51. $t->render();
  52. return Command::SUCCESS;
  53. }
  54. }