Beispiel #1
0
 function Query($strSql, $bIgnoreErrors = false, $error_position = "", $arOptions = array())
 {
     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
     static $bSelectOnly = true;
     if ($this->bModuleConnection) {
         //In case of dedicated module database
         //were is nothing to do
     } elseif ($DB->bMasterOnly > 0) {
         //We requested to process all queries
         //by master connection
     } 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 {
         $bSelect = preg_match('/^\\s*(select|show)/i', $strSql) && !preg_match('/get_lock/i', $strSql);
         if (!$bSelect && !isset($arOptions["ignore_dml"])) {
             $bSelectOnly = false;
         }
         if ($bSelect && $bSelectOnly) {
             if (!isset($this->obSlave)) {
                 $this->StartUsingMasterOnly();
                 //This is bootstrap code
                 $this->obSlave = CDatabase::SlaveConnection();
                 $this->StopUsingMasterOnly();
             }
             if (is_object($this->obSlave)) {
                 return $this->obSlave->Query($strSql, $bIgnoreErrors, $error_position, $arOptions);
             }
         }
     }
     $result = @mysql_query($strSql, $this->db_Conn);
     if ($this->DebugToFile || $DB->ShowSqlStat) {
         $exec_time = round(microtime(true) - $start_time, 10);
         if ($DB->ShowSqlStat) {
             $DB->cntQuery++;
             $DB->timeQuery += $exec_time;
             $DB->arQueryDebug[] = array("QUERY" => $strSql, "TIME" => $exec_time, "TRACE" => function_exists("debug_backtrace") ? debug_backtrace() : false, "BX_STATE" => $GLOBALS["BX_STATE"]);
         }
         if ($this->DebugToFile) {
             $fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/mysql_debug.sql", "ab+");
             $str = "TIME: " . $exec_time . " SESSION: " . session_id() . "  CONN: " . $this->db_Conn . "\n";
             $str .= $strSql . "\n\n";
             $str .= "----------------------------------------------------\n\n";
             fputs($fp, $str);
             @fclose($fp);
         }
     }
     if (!$result) {
         $this->db_Error = mysql_error($this->db_Conn);
         $this->db_ErrorSQL = $strSql;
         if (!$bIgnoreErrors) {
             AddMessage2Log($error_position . " MySql Query Error: " . $strSql . " [" . $this->db_Error . "]", "main");
             if ($this->DebugToFile) {
                 $fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/mysql_debug.sql", "ab+");
                 fputs($fp, "SESSION: " . session_id() . " ERROR: " . $this->db_Error . "\n\n----------------------------------------------------\n\n");
                 @fclose($fp);
             }
             if ($this->debug || @session_start() && $_SESSION["SESS_AUTH"]["ADMIN"]) {
                 echo $error_position . "<br><font color=#ff0000>MySQL Query Error: " . htmlspecialcharsbx($strSql) . "</font>[" . htmlspecialcharsbx($this->db_Error) . "]<br>";
             }
             $error_position = preg_replace("#<br[^>]*>#i", "\n", $error_position);
             SendError($error_position . "\nMySQL Query Error:\n" . $strSql . " \n [" . $this->db_Error . "]\n---------------\n\n");
             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";
             } elseif (file_exists($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/dbquery_error.php")) {
                 include $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/dbquery_error.php";
             } else {
                 die("MySQL Query Error!");
             }
             die;
         }
         return false;
     }
     $res = new CDBResult($result);
     $res->DB = $this;
     if ($DB->ShowSqlStat) {
         $res->SqlTraceIndex = count($DB->arQueryDebug);
     }
     return $res;
 }