public static function init(&$report) { $environments = PhpReports::$config['environments']; if (!isset($environments[$report->options['Environment']][$report->options['Database']])) { throw new Exception("No " . $report->options['Database'] . " database defined for environment '" . $report->options['Environment'] . "'"); } //make sure the syntax highlighting is using the proper class SqlFormatter::$pre_attributes = "class='prettyprint linenums lang-sql'"; $object = spyc_load($report->raw_query); $report->raw_query = array(); //if there are any included reports, add the report sql to the top if (isset($report->options['Includes'])) { $included_sql = ''; foreach ($report->options['Includes'] as &$included_report) { $included_sql .= trim($included_report->raw_query) . "\n"; } if (strlen($included_sql) > 0) { $report->raw_query[] = $included_sql; } } $report->raw_query[] = $object; //set a formatted query here for debugging. It will be overwritten below after macros are substituted. //We can not set the query here - it's not a query just yet... //$report->options['Query_Formatted'] = SqlFormatter::format($report->raw_query); }
public static function init(&$report) { $environments = PhpReports::$config['environments']; if (!isset($environments[$report->options['Environment']][$report->options['Database']])) { throw new Exception("No " . $report->options['Database'] . " info defined for environment '" . $report->options['Environment'] . "'"); } //make sure the syntax highlighting is using the proper class SqlFormatter::$pre_attributes = "class='prettyprint linenums lang-sql'"; $mysql = $environments[$report->options['Environment']][$report->options['Database']]; //default host macro to mysql's host if it isn't defined elsewhere if (!isset($report->macros['host'])) { $report->macros['host'] = $mysql['host']; } //replace legacy shorthand macro format foreach ($report->macros as $key => $value) { if (isset($report->options['Variables'][$key])) { $params = $report->options['Variables'][$key]; } else { $params = array(); } //macros shortcuts for arrays if (isset($params['multiple']) && $params['multiple']) { //allow {macro} instead of {% for item in macro %}{% if not item.first %},{% endif %}{{ item.value }}{% endfor %} //this is shorthand for comma separated list $report->raw_query = preg_replace('/([^\\{])\\{' . $key . '\\}([^\\}])/', '$1{% for item in ' . $key . ' %}{% if not loop.first %},{% endif %}\'{{ item }}\'{% endfor %}$2', $report->raw_query); //allow {(macro)} instead of {% for item in macro %}{% if not item.first %},{% endif %}{{ item.value }}{% endfor %} //this is shorthand for quoted, comma separated list $report->raw_query = preg_replace('/([^\\{])\\{\\(' . $key . '\\)\\}([^\\}])/', '$1{% for item in ' . $key . ' %}{% if not loop.first %},{% endif %}(\'{{ item }}\'){% endfor %}$2', $report->raw_query); } else { //allow {macro} instead of {{macro}} for legacy support $report->raw_query = preg_replace('/([^\\{])(\\{' . $key . '+\\})([^\\}])/', '$1{$2}$3', $report->raw_query); } } //if there are any included reports, add the report sql to the top if (isset($report->options['Includes'])) { $included_sql = ''; foreach ($report->options['Includes'] as &$included_report) { $included_sql .= trim($included_report->raw_query) . "\n"; } $report->raw_query = $included_sql . $report->raw_query; } //set a formatted query here for debugging. It will be overwritten below after macros are substituted. $report->options['Query_Formatted'] = SqlFormatter::format($report->raw_query); }
/** * Formats and/or highlights the given SQL statement. * * @param string $sql * @param bool $highlightOnly If true the query is not formatted, just highlighted * * @return string */ public function formatQuery($sql, $highlightOnly = false) { \SqlFormatter::$pre_attributes = 'class="highlight highlight-sql"'; \SqlFormatter::$quote_attributes = 'class="string"'; \SqlFormatter::$backtick_quote_attributes = 'class="string"'; \SqlFormatter::$reserved_attributes = 'class="keyword"'; \SqlFormatter::$boundary_attributes = 'class="symbol"'; \SqlFormatter::$number_attributes = 'class="number"'; \SqlFormatter::$word_attributes = 'class="word"'; \SqlFormatter::$error_attributes = 'class="error"'; \SqlFormatter::$comment_attributes = 'class="comment"'; \SqlFormatter::$variable_attributes = 'class="variable"'; if ($highlightOnly) { $html = \SqlFormatter::highlight($sql); $html = preg_replace('/<pre class=".*">([^"]*+)<\\/pre>/Us', '\\1', $html); } else { $html = \SqlFormatter::format($sql); $html = preg_replace('/<pre class="(.*)">([^"]*+)<\\/pre>/Us', '<div class="\\1"><pre>\\2</pre></div>', $html); } return $html; }
/** * Highlight SQL syntax. * * @param string $sql * @return string */ public function highlightSQL($sql) { \SqlFormatter::$pre_attributes = ''; return trim(substr(\SqlFormatter::highlight($sql), 6, -6)); }
/** * Renders HTML code for custom panel. * @return string */ public function getPanel() { $queries = $this->queries; $html = '<h1>' . self::$title . '</h1>'; $html .= '<div class="tracy-inner tracy-InfoPanel"><table width="300">'; \SqlFormatter::$pre_attributes = 'style="color: black;"'; foreach ($queries as $query) { $html .= '<tr><td>' . \SqlFormatter::highlight($query) . '</td></tr>'; } $html .= '</table></div>'; return $html; }