<commit_before><?php
/**
 * Shows SQL query profiling information
 */

//STATUS: wip

function print_query($q)
{
    if (! ($q instanceof SqlQuery))
        throw new Exception ('odd input: '.$q);

    $query = htmlentities(nl2br($q->query), ENT_COMPAT, 'UTF-8');

    $keywords = array(
    'SELECT ', 'UPDATE ', 'INSERT ', 'DELETE ', 'SHOW ',
    ' FROM ', ' SET ', ' WHERE ',
    ' LEFT JOIN ', ' LEFT OUTER JOIN ', ' INNER JOIN ',
    ' GROUP BY ', ' ORDER BY ',
    ' ON ', ' AS ', ' AND ', ' OR ', ' LIMIT ', ' BETWEEN ',
    ' STATUS',
    ' IS NULL', ' IS NOT NULL', ' DESC', ' ASC',
    ' != ',
    'NOW()', ' DATE(',
    ' COUNT(', ' DISTINCT(', ' SUM(', ' LENGTH(', ' CHAR_LENGTH(',
    ' MIN(', ' MAX(',
    );

    $decorated = array(
    '<b>SELECT</b> ', '<b>UPDATE</b> ', '<b>INSERT</b> ', '<b>DELETE</b> ', '<b>SHOW</b> ',
    ' <b>FROM</b> ', '<br/><b>SET</b> ', '<br/><b>WHERE</b> ',
    '<br/><b>LEFT JOIN</b> ', '<br/><b>LEFT OUTER JOIN</b> ', '<br/><b>INNER JOIN</b> ',
    '<br/><b>GROUP BY</b> ', '<br/><b>ORDER BY</b> ',
    ' <b>ON</b> ', ' <b>AS</b> ', ' <b>AND</b> ', ' <b>OR</b> ', ' <b>LIMIT</b> ', ' <b>BETWEEN</b> ',
    ' <b>STATUS</b>',
    ' <b>IS NULL</b>', ' <b>IS NOT NULL</b>', ' <b>DESC</b>', ' <b>ASC</b>',
    ' <b>!=</b> ',
    '<b>NOW()</b>', ' <b>DATE</b>(',
    ' <b>COUNT</b>( ', ' <b>DISTINCT</b>( ',' <b>SUM</b>( ', ' <b>LENGTH</b>( ', ' <b>CHAR_LENGTH</b>( ',
    ' <b>MIN</b>( ',' <b>MAX</b>( ',
    );

    $query = str_replace($keywords, $decorated, $query);

    $header = XhtmlHeader::getInstance();
    $header->registerCss(
    '.cd_sql_row'.
    '{'.
        'overflow:auto;'.
        'max-width:400px;'.
    '}'
    );

    $header->registerCss(
    '.cd_sql_row:hover'.
    '{'.
        'background-color:#ccc;'.
    '}'
    );

    $header->registerCss(
    '.cd_sql_box,.cd_sql_box_p'.
    '{'.
        'width:30px;'.
        'float:left;'.
    '}'
    );

    $header->registerCss(
    '.cd_sql_box_p'.
    '{'.
        'background-color:#B1F9AA;'.
    '}'
    );

    $header->registerCss(
    '.cd_sql_text'.
    '{'.
        'width:300px;'.
        'float:left;'.
    '}'
    );

    echo
    '<div class="cd_sql_row">'.
    '<div class="'.($q->prepared ? 'cd_sql_box_p' : 'cd_sql_box').'">';

    if ($q->error)
        echo coreButton('Error', '', 'SQL Error');
    else
        echo round($q->time, 2).'s';

    echo '</div><div class="cd_sql_text">';

    if ($q->error)
        echo 'Error: <b>'.$q->error.'</b><br/><br/>';
<commit_msg>call registerCss() once per page load rather than for every sql query<commit_after><?php
/**
 * Shows SQL query profiling information
 */

//STATUS: wip

$header->registerCss(
'.cd_sql_row'.
'{'.
    'overflow:auto;'.
    'max-width:400px;'.
'}'
);

$header->registerCss(
'.cd_sql_row:hover'.
'{'.
    'background-color:#ccc;'.
'}'
);

$header->registerCss(
'.cd_sql_box,.cd_sql_box_p'.
'{'.
    'width:30px;'.
    'float:left;'.
'}'
);

$header->registerCss(
'.cd_sql_box_p'.
'{'.
    'background-color:#B1F9AA;'.
'}'
);

$header->registerCss(
'.cd_sql_text'.
'{'.
    'width:300px;'.
    'float:left;'.
'}'
);

function print_query($q)
{
    if (! ($q instanceof SqlQuery))
        throw new Exception ('odd input: '.$q);

    $query = htmlentities(nl2br($q->query), ENT_COMPAT, 'UTF-8');

    $keywords = array(
    'SELECT ', 'UPDATE ', 'INSERT ', 'DELETE ', 'SHOW ',
    ' FROM ', ' SET ', ' WHERE ',
    ' LEFT JOIN ', ' LEFT OUTER JOIN ', ' INNER JOIN ',
    ' GROUP BY ', ' ORDER BY ',
    ' ON ', ' AS ', ' AND ', ' OR ', ' LIMIT ', ' BETWEEN ',
    ' STATUS',
    ' IS NULL', ' IS NOT NULL', ' DESC', ' ASC',
    ' != ',
    'NOW()', ' DATE(',
    ' COUNT(', ' DISTINCT(', ' SUM(', ' LENGTH(', ' CHAR_LENGTH(',
    ' MIN(', ' MAX(',
    );

    $decorated = array(
    '<b>SELECT</b> ', '<b>UPDATE</b> ', '<b>INSERT</b> ', '<b>DELETE</b> ', '<b>SHOW</b> ',
    ' <b>FROM</b> ', '<br/><b>SET</b> ', '<br/><b>WHERE</b> ',
    '<br/><b>LEFT JOIN</b> ', '<br/><b>LEFT OUTER JOIN</b> ', '<br/><b>INNER JOIN</b> ',
    '<br/><b>GROUP BY</b> ', '<br/><b>ORDER BY</b> ',
    ' <b>ON</b> ', ' <b>AS</b> ', ' <b>AND</b> ', ' <b>OR</b> ', ' <b>LIMIT</b> ', ' <b>BETWEEN</b> ',
    ' <b>STATUS</b>',
    ' <b>IS NULL</b>', ' <b>IS NOT NULL</b>', ' <b>DESC</b>', ' <b>ASC</b>',
    ' <b>!=</b> ',
    '<b>NOW()</b>', ' <b>DATE</b>(',
    ' <b>COUNT</b>( ', ' <b>DISTINCT</b>( ',' <b>SUM</b>( ', ' <b>LENGTH</b>( ', ' <b>CHAR_LENGTH</b>( ',
    ' <b>MIN</b>( ',' <b>MAX</b>( ',
    );

    $query = str_replace($keywords, $decorated, $query);

    echo
    '<div class="cd_sql_row">'.
    '<div class="'.($q->prepared ? 'cd_sql_box_p' : 'cd_sql_box').'">';

    if ($q->error)
        echo coreButton('Error', '', 'SQL Error');
    else
        echo round($q->time, 2).'s';

    echo '</div><div class="cd_sql_text">';

    if ($q->error)
        echo 'Error: <b>'.$q->error.'</b><br/><br/>';

{'fraction_non_alphanumeric': 0.2935049019607843, 'fraction_numerical': 0.0053104575163398695, 'mean_word_length': 2.603384841795438, 'pattern_counts': {'":': 0, '<': 171, '<?xml version=': 0, '>': 191, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3210411', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2045, 'n_words': 333}