Skip to content

Instantly share code, notes, and snippets.

@codezixo
Forked from di7spider/_dumpSQL.php
Created May 24, 2023 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codezixo/b8ad3cd8750a7755d62a1d054341774f to your computer and use it in GitHub Desktop.
Save codezixo/b8ad3cd8750a7755d62a1d054341774f to your computer and use it in GitHub Desktop.
1C Bitrix :: D7 SQL Dump
<?php
/** Выводит / возвращает Dump SQL запроса Bitrix D7 и старого ядра */
if( !function_exists('_dumpSQL') ){
function _dumpSQL(callable $callback, array $params = [])
{
$result = [];
if($params['user'] == 'all' || $GLOBALS['USER']-> isAdmin() ){
$backtrace = array_shift( debug_backtrace() );
$result['FILE'] = $backtrace['line'].": ".$backtrace['file'];
$con = \Bitrix\Main\Application::getConnection();
$GLOBALS['DB']-> sqlTracker = $con-> startTracker();
$res = call_user_func($callback);
$con-> stopTracker();
foreach(is_array($res) ? $res : [$res] as $key => $obj){
$sql = null;
if( is_a($obj, '\CDBResult') ){
$sql = $obj-> DB-> sqlTracker-> current()-> getSql();
}else if( is_a($obj, '\Bitrix\Main\DB\Result') ){
$sql = $obj -> getTrackerQuery()-> getSql();
}
if( !empty($sql) ){
$result['SQL'][$key] = $sql;
$dump[] = $key." - ".get_class($obj)." -> \n".$sql;
}
}
if($params['show'] !== false){
$separator = "\n".str_pad("", strlen($result['FILE']), "=")."\n";
die(
'<pre>'
.$result['FILE']
.$separator
.implode($separator, $dump)
.'</pre>'
);
}
}
return $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment