Helper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel;
  7. /**
  8. * Eav Mysql resource helper model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Helper extends \Magento\Framework\DB\Helper
  14. {
  15. /**
  16. * Construct
  17. *
  18. * @param \Magento\Framework\App\ResourceConnection $resource
  19. * @param string $modulePrefix
  20. * @codeCoverageIgnore
  21. */
  22. public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix = 'Magento_Eav')
  23. {
  24. parent::__construct($resource, $modulePrefix);
  25. }
  26. /**
  27. * Mysql column - Table DDL type pairs
  28. *
  29. * @var array
  30. */
  31. protected $_ddlColumnTypes = [
  32. \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN => 'bool',
  33. \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT => 'smallint',
  34. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER => 'int',
  35. \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT => 'bigint',
  36. \Magento\Framework\DB\Ddl\Table::TYPE_FLOAT => 'float',
  37. \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL => 'decimal',
  38. \Magento\Framework\DB\Ddl\Table::TYPE_NUMERIC => 'decimal',
  39. \Magento\Framework\DB\Ddl\Table::TYPE_DATE => 'date',
  40. \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP => 'timestamp',
  41. \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME => 'datetime',
  42. \Magento\Framework\DB\Ddl\Table::TYPE_TEXT => 'text',
  43. \Magento\Framework\DB\Ddl\Table::TYPE_BLOB => 'blob',
  44. \Magento\Framework\DB\Ddl\Table::TYPE_VARBINARY => 'blob',
  45. ];
  46. /**
  47. * Attribute types that can be united via UNION into one query
  48. * while selecting attribute`s data from tables like `catalog_product_entity_datatype`
  49. *
  50. * This helps to run one query with all data types instead of one per each data type
  51. *
  52. * This data types are determined as 'groupable' because their tables have the same structure
  53. * which means that they can be used in one UNION query
  54. *
  55. * @var array
  56. */
  57. private $_groupableTypes = ['varchar', 'text', 'decimal', 'datetime', 'int'];
  58. /**
  59. * Returns DDL type by column type in database
  60. *
  61. * @param string $columnType
  62. * @return string
  63. */
  64. public function getDdlTypeByColumnType($columnType)
  65. {
  66. switch ($columnType) {
  67. case 'char':
  68. case 'varchar':
  69. $columnType = 'text';
  70. break;
  71. case 'tinyint':
  72. $columnType = 'smallint';
  73. break;
  74. default:
  75. break;
  76. }
  77. return array_search($columnType, $this->_ddlColumnTypes);
  78. }
  79. /**
  80. * Groups selects to separate unions depend on type
  81. *
  82. * E.g. for input array:
  83. * [
  84. * varchar => [select1, select2],
  85. * text => [select3],
  86. * int => [select4],
  87. * bool => [select5]
  88. * ]
  89. *
  90. * The result array will be:
  91. * [
  92. * 0 => [select1, select2, select3, select4] // contains queries for varchar & text & int
  93. * 1 => [select5] // contains queries for bool
  94. * ]
  95. *
  96. * @param array $selects
  97. * @return array
  98. */
  99. public function getLoadAttributesSelectGroups($selects)
  100. {
  101. $mainGroup = [];
  102. foreach ($selects as $dataType => $selectGroup) {
  103. if (in_array($dataType, $this->_groupableTypes)) {
  104. $mainGroup['all'][] = $selectGroup;
  105. continue;
  106. }
  107. $mainGroup[$dataType] = $selectGroup;
  108. }
  109. if (array_key_exists('all', $mainGroup)) {
  110. // it is better to call array_merge once after loop instead of calling it on each loop
  111. $mainGroup['all'] = array_merge(...$mainGroup['all']);
  112. }
  113. return array_values($mainGroup);
  114. }
  115. }