Helper.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Model\ResourceModel;
  7. /**
  8. * ImportExport MySQL resource helper model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Helper extends \Magento\Framework\DB\Helper
  14. {
  15. /**
  16. * Constants to be used for DB
  17. */
  18. const DB_MAX_PACKET_SIZE = 1048576;
  19. // Maximal packet length by default in MySQL
  20. const DB_MAX_PACKET_COEFFICIENT = 0.85;
  21. // The coefficient of useful data from maximum packet length
  22. /**
  23. * @param \Magento\Framework\App\ResourceConnection $resource
  24. * @param string $modulePrefix
  25. */
  26. public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix = 'importexport')
  27. {
  28. parent::__construct($resource, $modulePrefix);
  29. }
  30. /**
  31. * Returns maximum size of packet, that we can send to DB
  32. *
  33. * @return int
  34. */
  35. public function getMaxDataSize()
  36. {
  37. $maxPacketData = $this->getConnection()->fetchRow('SHOW VARIABLES LIKE "max_allowed_packet"');
  38. $maxPacket = empty($maxPacketData['Value']) ? self::DB_MAX_PACKET_SIZE : $maxPacketData['Value'];
  39. return floor($maxPacket * self::DB_MAX_PACKET_COEFFICIENT);
  40. }
  41. /**
  42. * Returns next autoincrement value for a table
  43. *
  44. * @param string $tableName Real table name in DB
  45. * @return int
  46. */
  47. public function getNextAutoincrement($tableName)
  48. {
  49. $connection = $this->getConnection();
  50. $entityStatus = $connection->showTableStatus($tableName);
  51. if (empty($entityStatus['Auto_increment'])) {
  52. throw new \Magento\Framework\Exception\LocalizedException(__('Cannot get autoincrement value'));
  53. }
  54. return $entityStatus['Auto_increment'];
  55. }
  56. }