TableHelpersTrait.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace YdbPlatform\Ydb\Traits;
  3. use Ydb\Table\ColumnMeta;
  4. use Ydb\Table\TableIndex;
  5. use Ydb\Table\CopyTableItem;
  6. trait TableHelpersTrait
  7. {
  8. /**
  9. * Prefix the table name.
  10. *
  11. * @param string $table
  12. * @return mixed|string
  13. */
  14. protected function pathPrefix($table)
  15. {
  16. if (substr($table, 0, 1) !== '/')
  17. {
  18. $table = rtrim($this->path . '/' . $table, '/');
  19. }
  20. return $table;
  21. }
  22. /**
  23. * @param array $columns
  24. * @return array
  25. */
  26. protected function convertColumns(array $columns)
  27. {
  28. $_columns = [];
  29. foreach ($columns as $name => $column)
  30. {
  31. if (!is_a($column, ColumnMeta::class))
  32. {
  33. $column = $this->column($name, $column);
  34. }
  35. $_columns[] = $column;
  36. }
  37. return $_columns;
  38. }
  39. /**
  40. * @param array $indexes
  41. * @return array
  42. */
  43. protected function convertIndexes(array $indexes)
  44. {
  45. $_indexes = [];
  46. foreach ($indexes as $name => $index)
  47. {
  48. if (!is_a($index, TableIndex::class))
  49. {
  50. $index = $this->tableIndex($name, (array)$index);
  51. }
  52. $_indexes[] = $index;
  53. }
  54. return $_indexes;
  55. }
  56. /**
  57. * @param array $tables
  58. * @return array
  59. */
  60. protected function convertTableItems(array $tables)
  61. {
  62. $_tables = [];
  63. foreach ($tables as $source_table => $destination_table)
  64. {
  65. $_table = $destination_table;
  66. if (!is_a($destination_table, CopyTableItem::class))
  67. {
  68. $_table = new CopyTableItem([
  69. 'source_path' => $this->pathPrefix($source_table),
  70. 'destination_path' => $this->pathPrefix($destination_table),
  71. ]);
  72. }
  73. $_tables[] = $_table;
  74. }
  75. return $_tables;
  76. }
  77. }