89 if (!array_key_exists(
$node_id, self::$arNodes))
91 if (CModule::IncludeModule(
'cluster'))
107 $node[
"ACTIVE"] ==
"Y"
108 && ($node[
"STATUS"] ==
"ONLINE" || $node[
"STATUS"] ==
"READY")
111 && !isset($node[
"ONHIT_ERROR"])
114 if (!array_key_exists(
"DB", $node))
117 $node_DB->type =
$DB->type;
118 $node_DB->debug =
$DB->debug;
119 $node_DB->DebugToFile =
$DB->DebugToFile;
120 $node_DB->bNodeConnection =
true;
123 if ($node_DB->Connect($node[
"DB_HOST"], $node[
"DB_NAME"], $node[
"DB_LOGIN"], $node[
"DB_PASSWORD"],
"node" .
$node_id))
127 if ($node_DB->DoConnect(
"node" .
$node_id))
129 $node[
"DB"] = $node_DB;
134 $node[
"DB"] = $node_DB;
139 if (array_key_exists(
"DB", $node))
151 static::showConnectionError();
159 $response->setStatus(
'500 Internal Server Error');
162 if (file_exists(
$_SERVER[
"DOCUMENT_ROOT"] . BX_PERSONAL_ROOT .
"/php_interface/dbconn_error.php"))
164 include(
$_SERVER[
"DOCUMENT_ROOT"] . BX_PERSONAL_ROOT .
"/php_interface/dbconn_error.php");
168 echo
"Error connecting to database. Please try again later.";
196 if (is_object($moduleDB))
198 $moduleDB->bModuleConnection =
true;
203 if ($bModuleInclude && CModule::IncludeModule(
'cluster'))
241 if ($this->connection && $this->connection->isConnected())
244 $this->db_Conn = $this->connection->getResource();
258 if ((
string)$this->DBHost ===
'')
295 ConnectionPool::DEFAULT_CONNECTION_NAME,
314 $this->sqlTracker =
null;
316 $this->timeQuery = 0;
317 $this->arQueryDebug = [];
327 if (!$this->sqlTracker)
330 $this->sqlTracker =
$app->getConnection()->startTracker();
339 $this->
version = $this->connection->getVersion()[0];
347 return $this->CurrentTimeFunction();
352 return $this->CurrentDateFunction();
359 $id = $strType .
',' .
$lang .
',' . $bSearchInSitesOnly;
360 if (!isset($CACHE[$id]))
364 $format = ($strType ==
"FULL" ?
$culture->getFormatDatetime() :
$culture->getFormatDate());
368 $format = CLang::GetDateFormat($strType,
$lang, $bSearchInSitesOnly);
370 $CACHE[$id] = $this->DateFormatToDB($format);
373 $sFieldExpr = $strFieldName;
376 if ($strType ==
"FULL" && CTimeZone::Enabled())
378 $diff = CTimeZone::GetOffset();
382 $sFieldExpr = $this->connection->getSqlHelper()->addSecondsToDateTime($diff, $strFieldName);
386 return str_replace(
"#FIELD#", $sFieldExpr, $CACHE[$id]);
394 $strValue = clone $strValue;
395 $strValue->toUserTime();
401 $format = ($strType ==
"FULL" ?
$culture->getFormatDatetime() :
$culture->getFormatDate());
405 $format = CLang::GetDateFormat($strType,
$lang);
408 $sFieldExpr =
"'" .
CDatabase::FormatDate($strValue, $format, ($strType ==
"SHORT" ?
"YYYY-MM-DD" :
"YYYY-MM-DD HH:MI:SS")) .
"'";
411 if ($strType ==
"FULL" && CTimeZone::Enabled())
413 $diff = CTimeZone::GetOffset();
418 $sFieldExpr = $this->connection->getSqlHelper()->addSecondsToDateTime(-$diff, $sFieldExpr);
428 return call_user_func_array([$this->connection->getSqlHelper(),
'getConcatFunction'], func_get_args());
434 $sql =
'SUBSTR(' .
$str .
', ' . $from;
436 if (!is_null($length))
438 $sql .=
', ' . $length;
447 return $this->connection->getSqlHelper()->getIsNullFunction($expression,
$result);
453 return $this->connection->getSqlHelper()->getLengthFunction($field);
458 return "CAST(" . $expr .
" AS CHAR" . ($len > 0 ?
"(" . $len .
")" :
"") .
")";
463 return "CAST(" . $expr .
" AS SIGNED)";
469 if (!isset($cache[$format]))
473 return $cache[$format];
476 public static function FormatDate($strDate, $format =
"DD.MM.YYYY HH:MI:SS", $new_format =
"DD.MM.YYYY HH:MI:SS")
483 if ($format ===
false && defined(
"FORMAT_DATETIME"))
502 $fixed_format = preg_replace(
505 "/(?<!M)M(?![MI])/i",
515 mb_strtoupper($new_format)
519 return $time->format($toPhpFormat);
525 public function TopSql($strSql, $nTopCount)
527 $nTopCount = intval($nTopCount);
530 return $strSql .
"\nLIMIT " . $nTopCount;
541 return $this->connection->getInsertedId();
546 return array_keys($this->GetTableFields(
$table));
556 public function Query($strSql, $bIgnoreErrors =
false, $error_position =
"",
$arOptions = [])
561 $this->db_Error =
"";
563 if ($this->DebugToFile ||
$DB->ShowSqlStat)
574 if ($connectionPool->isMasterOnly())
579 elseif ($this->bModuleConnection)
589 elseif ($this->bNodeConnection)
597 $connectionPool->ignoreDml(
true);
600 $connection = $connectionPool->getSlaveConnection($strSql);
604 $connectionPool->ignoreDml(
false);
609 if (!isset($this->obSlave))
617 if (is_object($conn))
619 $this->obSlave = $conn;
623 self::$arNodes[$nodeId][
"ONHIT_ERROR"] =
true;
628 if (is_object($this->obSlave))
630 return $this->obSlave->Query($strSql, $bIgnoreErrors, $error_position,
$arOptions);
635 $result = $this->QueryInternal($strSql);
637 if ($this->DebugToFile ||
$DB->ShowSqlStat)
640 $exec_time = round(microtime(
true) -
$start_time, 10);
642 if (
$DB->ShowSqlStat)
644 $DB->addDebugQuery($strSql, $exec_time, $connectionPool->isSlavePossible() ? $this->node_id : -1);
647 if ($this->DebugToFile)
649 $this->
startSqlTracker()->writeFileLog($strSql, $exec_time,
"CONN: " . $this->getThreadId());
655 $this->db_Error = $this->GetError();
656 $this->db_ErrorSQL = $strSql;
659 if ($this->DebugToFile)
661 $this->
startSqlTracker()->writeFileLog(
"ERROR: " . $this->db_Error, 0,
"CONN: " . $this->getThreadId());
664 if (defined(
'ERROR_EMAIL') && ERROR_EMAIL !=
'')
666 $error_position = preg_replace(
"#<br[^>]*>#i",
"\n", $error_position);
667 SendError($error_position .
"\nQuery Error:\n" . $strSql .
" \n [" . $this->db_Error .
"]\n---------------\n\n");
670 throw $this->connection->createQueryException($this->GetErrorCode(), $this->db_Error, $strSql);
677 if (
$DB->ShowSqlStat)
684 public function QueryBind($strSql, $arBinds, $bIgnoreErrors =
false)
686 return $this->
Query($strSql, $bIgnoreErrors);
692 public function QueryLong($strSql, $bIgnoreErrors =
false)
694 return $this->
Query($strSql, $bIgnoreErrors);
697 public function ForSql($strValue, $iMaxLength = 0)
700 return $this->connection->getSqlHelper()->forSql($strValue, $iMaxLength);
706 return $this->connection->isTableExists($tableName);
712 return $this->connection->getSqlHelper()->quote($identifier);
724 public function Update(
$table,
$arFields, $WHERE =
"", $error_position =
"", $DEBUG =
false, $ignore_errors =
false, $additional_check =
true)
732 if ((
string)$value ==
'')
734 $ar[] = $this->
quote($field) .
" = ''";
738 $ar[] = $this->
quote($field) .
" = " . $value;
744 $strSql =
"UPDATE " .
$table .
" SET " . implode(
", ",
$ar) .
" " . $WHERE;
749 $w = $this->
Query($strSql, $ignore_errors, $error_position);
752 $rows = $w->AffectedRowsCount();
755 echo
"affected_rows = " .
$rows .
"<br>";
758 if (
$rows <= 0 && $additional_check)
760 $w = $this->
Query(
"SELECT 'x' FROM " .
$table .
" " . $WHERE, $ignore_errors, $error_position);
765 $rows = $w->SelectedRowsCount();
769 echo
"num_rows = " .
$rows .
"<br>";
779 public function InitTableVarsForEdit($tablename, $strIdentFrom =
"str_", $strIdentTo =
"str_", $strSuffixFrom =
"", $bAlways =
false)
781 $fields = $this->GetTableFields($tablename);
782 foreach (
$fields as $strColumnName => $field)
784 $varnameFrom = $strIdentFrom . $strColumnName . $strSuffixFrom;
785 $varnameTo = $strIdentTo . $strColumnName;
786 global ${$varnameFrom}, ${$varnameTo};
787 if ((isset(${$varnameFrom}) || $bAlways))
789 if (is_array(${$varnameFrom}))
792 foreach (${$varnameFrom} as
$k => $v)
813 return $this->connection->parseSqlBatch($strSql);
818 if (!file_exists($filepath) || !is_file($filepath))
820 return [
"File $filepath is not found."];
824 $contents = file_get_contents($filepath);
827 foreach ($this->connection->parseSqlBatch(
$contents) as $strSql)
829 if (!$this->
Query($strSql,
true))
831 $arErr[] =
"<hr><pre>Query:\n" . $strSql .
"\n\nError:\n<font color=red>" . $this->
GetErrorMessage() .
"</font></pre>";
843 public function IsDate($value, $format =
false,
$lang =
false, $format_type =
"SHORT")
845 if ($format ===
false)
847 $format = CLang::GetDateFormat($format_type,
$lang);
854 if (is_object($this->obSlave) && $this->obSlave->db_Error <>
'')
856 return $this->obSlave->db_Error;
858 elseif ($this->db_Error <>
'')
860 return $this->db_Error .
"!";
870 if (is_object($this->obSlave) && $this->obSlave->db_ErrorSQL <>
'')
872 return $this->obSlave->db_ErrorSQL;
874 elseif ($this->db_ErrorSQL <>
'')
887 $this->connection->startTransaction();
893 $this->connection->commitTransaction();
899 $this->connection->rollbackTransaction();
902 public function DDL($strSql, $bIgnoreErrors =
false, $error_position =
"",
$arOptions = [])
907 $this->column_cache = [];
915 $this->timeQuery += $exec_time;
918 ->setTime($exec_time)
919 ->setTrace(defined(
"BX_NO_SQL_BACKTRACE") ?
null :
Main\
Diag\Helper::getBackTrace(8,
null, 2))
927 $this->arQueryDebug[$index]?->addTime($exec_time);
933 return $this->connection->getIndexName($tableName, $arColumns, $bStrict) ??
'';
936 public function IndexExists($tableName, $arColumns, $bStrict =
false)
938 return $this->
GetIndexName($tableName, $arColumns, $bStrict) !==
"";
941 public function CreateIndex($indexName, $tableName, $columns, $unique =
false, $fulltext =
false)
954 if ($connectionType ===
null)
957 $connectionType =
$application->getConnection()->getType();
963 'CDatabase' =>
'classes/' . $connectionType .
'/database.php',
964 'CDBResult' =>
'classes/' . $connectionType .
'/dbresult.php',
static getConnection($name="")
static registerAutoLoadClasses($moduleName, array $classes)
static convertFormatToPhp($format)
static SetOffline($node_id)
static GetByID($node_id, $arVirtNode=false)
IndexExists($tableName, $arColumns, $bStrict=false)
DoConnect($connectionName='')
GetIndexName($tableName, $arColumns, $bStrict=false)
InitTableVarsForEdit($tablename, $strIdentFrom="str_", $strIdentTo="str_", $strSuffixFrom="", $bAlways=false)
IsDate($value, $format=false, $lang=false, $format_type="SHORT")
GetTableFieldsList($table)
DDL($strSql, $bIgnoreErrors=false, $error_position="", $arOptions=[])
IsNull($expression, $result)
PrepareInsert($strTableName, $arFields)
addDebugTime($index, $exec_time)
static showConnectionError()
Query($strSql, $bIgnoreErrors=false, $error_position="", $arOptions=[])
static registerAutoload(?string $connectionType=null)
CreateIndex($indexName, $tableName, $columns, $unique=false, $fulltext=false)
Update($table, $arFields, $WHERE="", $error_position="", $DEBUG=false, $ignore_errors=false, $additional_check=true)
static GetModuleConnection($module_id, $bModuleInclude=false)
PrepareUpdate($strTableName, $arFields)
CharToDateFunction($strValue, $strType="FULL", $lang=false)
Connect($DBHost, $DBName, $DBLogin, $DBPassword, $connectionName="")
ForSql($strValue, $iMaxLength=0)
static FormatDate($strDate, $format="DD.MM.YYYY HH:MI:SS", $new_format="DD.MM.YYYY HH:MI:SS")
PrepareUpdateJoin($strTableName, $arFields, $from, $where)
DateToCharFunction($strFieldName, $strType="FULL", $lang=false, $bSearchInSitesOnly=false)
QueryLong($strSql, $bIgnoreErrors=false)
TopSql($strSql, $nTopCount)
static DateFormatToPHP($format)
static GetDBNodeConnection($node_id, $bIgnoreErrors=false, $bCheckStatus=true)
Substr($str, $from, $length=null)
QueryBind($strSql, $arBinds, $bIgnoreErrors=false)
addDebugQuery($strSql, $exec_time, $node_id=0)
$_SERVER["DOCUMENT_ROOT"]
if(!defined('SITE_ID')) $lang
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
CheckDateTime($datetime, $format=false)
$GLOBALS['____1690880296']
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"