FromRenderer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Select;
  7. use Magento\Framework\DB\Select;
  8. use Magento\Framework\DB\Platform\Quote;
  9. class FromRenderer implements RendererInterface
  10. {
  11. /**
  12. * @var Quote
  13. */
  14. protected $quote;
  15. /**
  16. * @param Quote $quote
  17. */
  18. public function __construct(
  19. Quote $quote
  20. ) {
  21. $this->quote = $quote;
  22. }
  23. /**
  24. * Render FROM & JOIN's section
  25. *
  26. * @param Select $select
  27. * @param string $sql
  28. * @return string
  29. * @throws \Zend_Db_Select_Exception
  30. */
  31. public function render(Select $select, $sql = '')
  32. {
  33. /*
  34. * If no table specified, use RDBMS-dependent solution
  35. * for table-less query. e.g. DUAL in Oracle.
  36. */
  37. $source = $select->getPart(Select::FROM);
  38. if (empty($source)) {
  39. $source = [];
  40. }
  41. $from = [];
  42. foreach ($source as $correlationName => $table) {
  43. $tmp = '';
  44. $joinType = ($table['joinType'] == Select::FROM) ? Select::INNER_JOIN : $table['joinType'];
  45. // Add join clause (if applicable)
  46. if (!empty($from)) {
  47. $tmp .= ' ' . strtoupper($joinType) . ' ';
  48. }
  49. $tmp .= $this->getQuotedSchema($table['schema']);
  50. $tmp .= $this->getQuotedTable($table['tableName'], $correlationName);
  51. // Add join conditions (if applicable)
  52. if (!empty($from) && !empty($table['joinCondition'])) {
  53. $tmp .= ' ' . Select::SQL_ON . ' ' . $table['joinCondition'];
  54. }
  55. // Add the table name and condition add to the list
  56. $from[] = $tmp;
  57. }
  58. // Add the list of all joins
  59. if (!empty($from)) {
  60. $sql .= ' ' . Select::SQL_FROM . ' ' . implode("\n", $from);
  61. }
  62. return $sql;
  63. }
  64. /**
  65. * Return a quoted schema name
  66. *
  67. * @param string $schema The schema name OPTIONAL
  68. * @return string|null
  69. */
  70. protected function getQuotedSchema($schema = null)
  71. {
  72. if ($schema === null) {
  73. return null;
  74. }
  75. return $this->quote->quoteIdentifier($schema) . '.';
  76. }
  77. /**
  78. * Return a quoted table name
  79. *
  80. * @param string $tableName The table name
  81. * @param string $correlationName The correlation name OPTIONAL
  82. * @return string
  83. */
  84. protected function getQuotedTable($tableName, $correlationName = null)
  85. {
  86. return $this->quote->quoteTableAs($tableName, $correlationName);
  87. }
  88. }