ReadTableCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Exception;
  10. use YdbPlatform\Ydb\Ydb;
  11. class ReadTableCommand extends Command
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected static $defaultName = 'read-table';
  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('Read table example.');
  29. $this->addArgument('table', InputArgument::REQUIRED, 'The table name.');
  30. }
  31. /**
  32. * @param InputInterface $input
  33. * @param OutputInterface $output
  34. * @return int
  35. * @throws \Exception
  36. */
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $table_name = $input->getArgument('table') ?: '';
  40. $ydb = $this->appService->initYdb();
  41. $return = $ydb->retry(function (Ydb $ydb) use ($table_name, $output) {
  42. $table = $ydb->table();
  43. $description = $table->describeTable($table_name);
  44. $columns = [];
  45. foreach ($description['columns'] ?? [] as $column)
  46. {
  47. $columns[] = $column['name'];
  48. }
  49. if ( ! $columns)
  50. {
  51. throw new Exception('Failed to get columns for table ' . $table_name);
  52. }
  53. $options = [
  54. 'row_limit' => 10,
  55. 'ordered' => true,
  56. 'key_range' => [
  57. 'gte' => $table->tuple('uint64', 10),
  58. 'lte' => $table->tuple('uint64', 30),
  59. // other options:
  60. // multi-column primary key:
  61. // 'lte' => $table->tuple('uint64,uin64', [30, 10]), // two-column primary key
  62. // operators variant:
  63. // 'less' => $table->tuple('uint64', 30), // less than
  64. // 'lt' => $table->tuple('uint64', 30), // less than
  65. // '<' => $table->tuple('uint64', 30), // less than
  66. // 'less_or_equal' => $table->tuple('uint64', 30), // less than or equal
  67. // 'lte' => $table->tuple('uint64', 30), // less than or equal
  68. // '<=' => $table->tuple('uint64', 30), // less than or equal
  69. // 'greater' => $table->tuple('uint64', 30), // greater than
  70. // 'gt' => $table->tuple('uint64', 30), // greater than
  71. // '>' => $table->tuple('uint64', 30), // greater than
  72. // 'greater_or_equal' => $table->tuple('uint64', 30), // greater than or equal
  73. // 'gte' => $table->tuple('uint64', 30), // greater than or equal
  74. // '>=' => $table->tuple('uint64', 30), // greater than or equal
  75. ],
  76. ];
  77. foreach ($table->readTable($table_name, $columns, $options) as $i => $result)
  78. {
  79. $output->writeln('Portion #' . ($i + 1));
  80. $output->writeln('Column count: ' . $result->columnCount());
  81. $output->writeln('Row count: ' . $result->rowCount());
  82. $t = new Table($output);
  83. $t
  84. ->setHeaders(array_map(function($column) {
  85. return $column['name'];
  86. }, $result->columns()))
  87. ->setRows($result->rows())
  88. ;
  89. $t->render();
  90. }
  91. });
  92. return Command::SUCCESS;
  93. }
  94. }