/** * @param Join[] $joins * @param PlatformInterface $platform * @param null|DriverInterface $driver * @param null|ParameterContainer $parameterContainer * @return null|string[] Null if no joins present, array of JOIN statements * otherwise * @throws Exception\InvalidArgumentException for invalid JOIN table names. */ protected function processJoin(Join $joins, PlatformInterface $platform, DriverInterface $driver = null, ParameterContainer $parameterContainer = null) { if (!$joins->count()) { return; } // process joins $joinSpecArgArray = []; foreach ($joins->getJoins() as $j => $join) { $joinName = null; $joinAs = null; // table name if (is_array($join['name'])) { $joinName = current($join['name']); $joinAs = $platform->quoteIdentifier(key($join['name'])); } else { $joinName = $join['name']; } if ($joinName instanceof Expression) { $joinName = $joinName->getExpression(); } elseif ($joinName instanceof TableIdentifier) { $joinName = $joinName->getTableAndSchema(); $joinName = ($joinName[1] ? $platform->quoteIdentifier($joinName[1]) . $platform->getIdentifierSeparator() : '') . $platform->quoteIdentifier($joinName[0]); } elseif ($joinName instanceof Select) { $joinName = '(' . $this->processSubSelect($joinName, $platform, $driver, $parameterContainer) . ')'; } elseif (is_string($joinName) || is_object($joinName) && is_callable([$joinName, '__toString'])) { $joinName = $platform->quoteIdentifier($joinName); } else { throw new Exception\InvalidArgumentException(sprintf('Join name expected to be Expression|TableIdentifier|Select|string, "%s" given', gettype($joinName))); } $joinSpecArgArray[$j] = [strtoupper($join['type']), $this->renderTable($joinName, $joinAs)]; // on expression // note: for Expression objects, pass them to processExpression with a prefix specific to each join (used for named parameters) $joinSpecArgArray[$j][] = $join['on'] instanceof ExpressionInterface ? $this->processExpression($join['on'], $platform, $driver, $parameterContainer, 'join' . ($j + 1) . 'part') : $platform->quoteIdentifierInFragment($join['on'], ['=', 'AND', 'OR', '(', ')', 'BETWEEN', '<', '>']); // on } return [$joinSpecArgArray]; }
/** * Process the select part * * @param PlatformInterface $platform * @param DriverInterface $driver * @param ParameterContainer $parameterContainer * @return null|array */ protected function processSelect(PlatformInterface $platform, DriverInterface $driver = null, ParameterContainer $parameterContainer = null) { $expr = 1; list($table, $fromTable) = $this->resolveTable($this->table, $platform, $driver, $parameterContainer); // process table columns $columns = []; foreach ($this->columns as $columnIndexOrAs => $column) { if ($column === self::SQL_STAR) { $columns[] = [$fromTable . self::SQL_STAR]; continue; } $columnName = $this->resolveColumnValue(['column' => $column, 'fromTable' => $fromTable, 'isIdentifier' => true], $platform, $driver, $parameterContainer, is_string($columnIndexOrAs) ? $columnIndexOrAs : 'column'); // process As portion if (is_string($columnIndexOrAs)) { $columnAs = $platform->quoteIdentifier($columnIndexOrAs); } elseif (stripos($columnName, ' as ') === false) { $columnAs = is_string($column) ? $platform->quoteIdentifier($column) : 'Expression' . $expr++; } $columns[] = isset($columnAs) ? [$columnName, $columnAs] : [$columnName]; } // process join columns foreach ($this->joins->getJoins() as $join) { $joinName = is_array($join['name']) ? key($join['name']) : $join['name']; $joinName = parent::resolveTable($joinName, $platform, $driver, $parameterContainer); foreach ($join['columns'] as $jKey => $jColumn) { $jColumns = []; $jFromTable = is_scalar($jColumn) ? $joinName . $platform->getIdentifierSeparator() : ''; $jColumns[] = $this->resolveColumnValue(['column' => $jColumn, 'fromTable' => $jFromTable, 'isIdentifier' => true], $platform, $driver, $parameterContainer, is_string($jKey) ? $jKey : 'column'); if (is_string($jKey)) { $jColumns[] = $platform->quoteIdentifier($jKey); } elseif ($jColumn !== self::SQL_STAR) { $jColumns[] = $platform->quoteIdentifier($jColumn); } $columns[] = $jColumns; } } if ($this->quantifier) { $quantifier = $this->quantifier instanceof ExpressionInterface ? $this->processExpression($this->quantifier, $platform, $driver, $parameterContainer, 'quantifier') : $this->quantifier; } if (!isset($table)) { return [$columns]; } elseif (isset($quantifier)) { return [$quantifier, $columns, $table]; } else { return [$columns, $table]; } }