GridStructure.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\DB\Ddl\Table;
  10. use Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver;
  11. use Magento\Framework\Search\Request\Dimension;
  12. class GridStructure implements IndexStructureInterface
  13. {
  14. /**
  15. * @var Resource
  16. */
  17. private $resource;
  18. /**
  19. * @var FlatScopeResolver
  20. */
  21. private $flatScopeResolver;
  22. /**
  23. * @var array
  24. */
  25. protected $columnTypesMap = [
  26. 'varchar' => ['type' => Table::TYPE_TEXT, 'size' => 255],
  27. 'mediumtext' => ['type' => Table::TYPE_TEXT, 'size' => 16777216],
  28. 'text' => ['type' => Table::TYPE_TEXT, 'size' => 65536],
  29. 'int' => ['type' => Table::TYPE_INTEGER, 'size' => null],
  30. 'date' => ['type' => Table::TYPE_DATE, 'size' => null],
  31. 'datetime' => ['type' => Table::TYPE_DATETIME, 'size' => null],
  32. 'timestamp' => ['type' => Table::TYPE_TIMESTAMP, 'size' => null],
  33. ];
  34. /**
  35. * @param ResourceConnection $resource
  36. * @param FlatScopeResolver $flatScopeResolver
  37. * @param array $columnTypesMap
  38. */
  39. public function __construct(
  40. ResourceConnection $resource,
  41. FlatScopeResolver $flatScopeResolver,
  42. array $columnTypesMap = []
  43. ) {
  44. $this->resource = $resource;
  45. $this->flatScopeResolver = $flatScopeResolver;
  46. $this->columnTypesMap = array_merge($this->columnTypesMap, $columnTypesMap);
  47. }
  48. /**
  49. * @param string $index
  50. * @param Dimension[] $dimensions
  51. * @return void
  52. */
  53. public function delete($index, array $dimensions = [])
  54. {
  55. $adapter = $this->getAdapter();
  56. $tableName = $this->flatScopeResolver->resolve($index, $dimensions);
  57. if ($adapter->isTableExists($tableName)) {
  58. $adapter->dropTable($tableName);
  59. }
  60. }
  61. /**
  62. * @param string $index
  63. * @param array $fields
  64. * @param Dimension[] $dimensions
  65. * @return void
  66. */
  67. public function create($index, array $fields, array $dimensions = [])
  68. {
  69. $this->createFlatTable($this->flatScopeResolver->resolve($index, $dimensions), $fields);
  70. }
  71. /**
  72. * @param string $tableName
  73. * @param array $fields
  74. * @throws \Zend_Db_Exception
  75. * @return void
  76. */
  77. protected function createFlatTable($tableName, array $fields)
  78. {
  79. $adapter = $this->getAdapter();
  80. $table = $adapter->newTable($tableName);
  81. $table->addColumn(
  82. 'entity_id',
  83. Table::TYPE_INTEGER,
  84. 10,
  85. ['unsigned' => true, 'nullable' => false, 'primary' => true],
  86. 'Entity ID'
  87. );
  88. $searchableFields = [];
  89. foreach ($fields as $field) {
  90. if ($field['type'] === 'searchable') {
  91. $searchableFields[] = $field['name'];
  92. }
  93. $columnMap = isset($field['dataType']) && isset($this->columnTypesMap[$field['dataType']])
  94. ? $this->columnTypesMap[$field['dataType']]
  95. : ['type' => $field['dataType'], 'size' => isset($field['size']) ? $field['size'] : null];
  96. $name = $field['name'];
  97. $type = $columnMap['type'];
  98. $size = $columnMap['size'];
  99. if ($field['type'] === 'filterable') {
  100. $table->addIndex(
  101. $this->resource->getIdxName($tableName, $name, AdapterInterface::INDEX_TYPE_INDEX),
  102. $name,
  103. ['type' => AdapterInterface::INDEX_TYPE_INDEX]
  104. );
  105. }
  106. $table->addColumn($name, $type, $size);
  107. }
  108. $table->addIndex(
  109. $this->resource->getIdxName(
  110. $tableName,
  111. $searchableFields,
  112. AdapterInterface::INDEX_TYPE_FULLTEXT
  113. ),
  114. $searchableFields,
  115. ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT]
  116. );
  117. $adapter->createTable($table);
  118. }
  119. /**
  120. * @return false|AdapterInterface
  121. */
  122. private function getAdapter()
  123. {
  124. $adapter = $this->resource->getConnection('write');
  125. return $adapter;
  126. }
  127. }