LookupExpression.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Sql;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\DB\Select;
  10. /**
  11. * Class LookupExpression
  12. */
  13. class LookupExpression extends Expression
  14. {
  15. /**
  16. * @var Resource
  17. */
  18. protected $resource;
  19. /**
  20. * @var AdapterInterface
  21. */
  22. protected $adapter;
  23. /**
  24. * @var string
  25. */
  26. protected $targetColumn;
  27. /**
  28. * @var string
  29. */
  30. protected $targetTable;
  31. /**
  32. * @var array
  33. */
  34. protected $referenceColumns;
  35. /**
  36. * @var array
  37. */
  38. protected $sortOrder;
  39. /**
  40. * @param ResourceConnection $resource
  41. * @param string $targetColumn
  42. * @param string $targetTable
  43. * @param array $referenceColumns
  44. * @param array $sortOrder
  45. */
  46. public function __construct(
  47. ResourceConnection $resource,
  48. $targetColumn,
  49. $targetTable,
  50. array $referenceColumns = [],
  51. array $sortOrder = []
  52. ) {
  53. $this->targetTable = $targetTable;
  54. $this->targetColumn = $targetColumn;
  55. $this->referenceColumns = $referenceColumns;
  56. $this->sortOrder = $sortOrder;
  57. $this->resource = $resource;
  58. $this->adapter = $this->resource->getConnection();
  59. }
  60. /**
  61. * Process WHERE clause
  62. *
  63. * @param Select $select
  64. * @return void
  65. */
  66. protected function processWhereCondition(Select $select)
  67. {
  68. foreach ($this->referenceColumns as $column => $referenceColumn) {
  69. $identifier = '';
  70. if (isset($referenceColumn['tableAlias'])) {
  71. $identifier = $referenceColumn['tableAlias'] . '.';
  72. }
  73. $columnName = $column;
  74. if (isset($referenceColumn['columnName'])) {
  75. $columnName = $referenceColumn['columnName'];
  76. }
  77. $select->where(
  78. sprintf(
  79. '%s = %s',
  80. $this->adapter->quoteIdentifier('lookup.' . $column),
  81. $this->adapter->quoteIdentifier($identifier . $columnName)
  82. )
  83. );
  84. }
  85. }
  86. /**
  87. * Process ORDER BY clause
  88. *
  89. * @param Select $select
  90. * @return void
  91. */
  92. protected function processSortOrder(Select $select)
  93. {
  94. foreach ($this->sortOrder as $direction => $column) {
  95. if (!in_array($direction, [Select::SQL_ASC, Select::SQL_DESC])) {
  96. $direction = '';
  97. }
  98. $expr = new \Zend_Db_Expr(
  99. sprintf(
  100. '%s %s',
  101. $this->adapter->quoteIdentifier('lookup.' . $column),
  102. $direction
  103. )
  104. );
  105. $select->order($expr);
  106. }
  107. }
  108. /**
  109. * Returns lookup SQL
  110. *
  111. * @return string
  112. */
  113. public function __toString()
  114. {
  115. $select = $this->adapter->select()
  116. ->from(['lookup' => $this->resource->getTableName($this->targetTable)], [$this->targetColumn])
  117. ->limit(1);
  118. $this->processWhereCondition($select);
  119. $this->processSortOrder($select);
  120. return sprintf('(%s)', $select->assemble());
  121. }
  122. }