• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/database.php
  • Класс: CAllDatabase
  • Вызов: CAllDatabase::Query
public function Query($strSql, $bIgnoreErrors = false, $error_position = "", $arOptions = [])
{
	global $DB;

	$this->DoConnect();
	$this->db_Error = "";

	if ($this->DebugToFile || $DB->ShowSqlStat)
	{
		$start_time = microtime(true);
	}

	//We track queries for DML statements
	//and when there is no one we can choose
	//to run query against master connection
	//or replicated one
	$connectionPool = MainApplication::getInstance()->getConnectionPool();

	if ($connectionPool->isMasterOnly())
	{
		//We requested to process all queries
		//by master connection
	}
	elseif ($this->bModuleConnection)
	{
		//In case of dedicated module database
		//were is nothing to do
	}
	elseif (isset($arOptions["fixed_connection"]))
	{
		//We requested to process this query
		//by current connection
	}
	elseif ($this->bNodeConnection)
	{
		//It is node so nothing to do
	}
	else
	{
		if (isset($arOptions["ignore_dml"]))
		{
			$connectionPool->ignoreDml(true);
		}

		$connection = $connectionPool->getSlaveConnection($strSql);

		if (isset($arOptions["ignore_dml"]))
		{
			$connectionPool->ignoreDml(false);
		}

		if ($connection !== null)
		{
			if (!isset($this->obSlave))
			{
				$nodeId = $connection->getNodeId();

				ob_start();
				$conn = CDatabase::GetDBNodeConnection($nodeId, true);
				ob_end_clean();

				if (is_object($conn))
				{
					$this->obSlave = $conn;
				}
				else
				{
					self::$arNodes[$nodeId]["ONHIT_ERROR"] = true;
					CClusterDBNode::SetOffline($nodeId);
				}
			}

			if (is_object($this->obSlave))
			{
				return $this->obSlave->Query($strSql, $bIgnoreErrors, $error_position, $arOptions);
			}
		}
	}

	$result = $this->QueryInternal($strSql);

	if ($this->DebugToFile || $DB->ShowSqlStat)
	{
		/** @noinspection PhpUndefinedVariableInspection */
		$exec_time = round(microtime(true) - $start_time, 10);

		if ($DB->ShowSqlStat)
		{
			$DB->addDebugQuery($strSql, $exec_time, $connectionPool->isSlavePossible() ? $this->node_id : -1);
		}

		if ($this->DebugToFile)
		{
			$this->startSqlTracker()->writeFileLog($strSql, $exec_time, "CONN: " . $this->getThreadId());
		}
	}

	if (!$result)
	{
		$this->db_Error = $this->GetError();
		$this->db_ErrorSQL = $strSql;
		if (!$bIgnoreErrors)
		{
			$application = MainApplication::getInstance();

			$ex = new MainDBSqlQueryException('Query error', $this->db_Error, $strSql);
			$application->getExceptionHandler()->writeToLog($ex);

			(new MainHttpResponse())
				->setStatus('500 Internal Server Error')
				->writeHeaders()
			;

			if ($this->DebugToFile)
			{
				$this->startSqlTracker()->writeFileLog("ERROR: " . $this->db_Error, 0, "CONN: " . $this->getThreadId());
			}

			if ($this->debug)
			{
				echo $error_position . "
Query Error: " . htmlspecialcharsbx($strSql) . "[" . htmlspecialcharsbx($this->db_Error) . "]
"; } $error_position = preg_replace("#]*>#i", "n", $error_position); SendError($error_position . "nQuery Error:n" . $strSql . " n [" . $this->db_Error . "]n---------------nn"); if (file_exists($_SERVER["DOCUMENT_ROOT"] . BX_PERSONAL_ROOT . "/php_interface/dbquery_error.php")) { include($_SERVER["DOCUMENT_ROOT"] . BX_PERSONAL_ROOT . "/php_interface/dbquery_error.php"); die(); } else { die("Query Error!"); } } return false; } $res = new CDBResult($result); $res->DB = $this; if ($DB->ShowSqlStat) { $res->SqlTraceIndex = count($DB->arQueryDebug) - 1; } return $res; }