• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/orm/query/chainelement.php
  • Класс: BitrixMainORMQueryChainElement
  • Вызов: ChainElement::getSqlDefinition
public function getSqlDefinition()
{
	if (is_array($this->value) || $this->value instanceof Reference || $this->value instanceof Entity)
	{
		throw new SystemException(sprintf(
			'There is no SQL definition for Entity `%s`, please use a scalar field',
			$this->getAliasFragment()
		));
	}

	$helper = $this->value->getEntity()->getConnection()->getSqlHelper();

	if ($this->value instanceof ExpressionField)
	{
		$SQLBuildFrom = [];
		$buildFromChains = $this->value->getBuildFromChains();

		foreach ($this->value->getBuildFrom() as $element)
		{
			if ($element instanceof Closure)
			{
				/** @var SqlExpression $sqlExpression */
				$sqlExpression = $element();

				if (!($sqlExpression instanceof SqlExpression))
				{
					throw new ArgumentException(sprintf(
						'Expected instance of %s, got %s instead.',
						SqlExpression::class, gettype($sqlExpression)
					));
				}

				$SQLBuildFrom[] = $sqlExpression->compile();
			}
			else
			{
				$chain = array_shift($buildFromChains);
				$SQLBuildFrom[] = $chain->getSQLDefinition();
			}
		}

		$expr = $this->value->getExpression();

		// insert talias
		if (strpos($expr, '%%TABLE_ALIAS') !== false)
		{
			$expr = str_replace('%%TABLE_ALIAS', $helper->quote($this->getParameter('talias')), $expr);
		}

		// join
		$sql = call_user_func_array('sprintf', array_merge([$expr], $SQLBuildFrom));
	}
	else
	{
		$sql = $helper->quote($this->getParameter('talias')) . '.';
		$sql .= $helper->quote($this->value->getColumnName());
	}

	return $sql;
}