summaryrefslogtreecommitdiff
path: root/tools/php-sqllint/src/phpsqllint
diff options
context:
space:
mode:
Diffstat (limited to 'tools/php-sqllint/src/phpsqllint')
-rw-r--r--tools/php-sqllint/src/phpsqllint/Autoloader.php57
-rw-r--r--tools/php-sqllint/src/phpsqllint/Cli.php280
-rw-r--r--tools/php-sqllint/src/phpsqllint/Renderer.php54
-rw-r--r--tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php70
-rw-r--r--tools/php-sqllint/src/phpsqllint/Renderer/Text.php102
5 files changed, 563 insertions, 0 deletions
diff --git a/tools/php-sqllint/src/phpsqllint/Autoloader.php b/tools/php-sqllint/src/phpsqllint/Autoloader.php
new file mode 100644
index 000000000..6811b82f1
--- /dev/null
+++ b/tools/php-sqllint/src/phpsqllint/Autoloader.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Part of php-sqllint
+ *
+ * PHP version 5
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://cweiske.de/php-sqllint.htm
+ */
+namespace phpsqllint;
+
+/**
+ * Class autoloader, PSR-0 compliant.
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @version Release: @package_version@
+ * @link http://cweiske.de/php-sqllint.htm
+ */
+class Autoloader
+{
+ /**
+ * Load the given class
+ *
+ * @param string $class Class name
+ *
+ * @return void
+ */
+ public function load($class)
+ {
+ $file = strtr($class, '_\\', '//') . '.php';
+ if (stream_resolve_include_path($file)) {
+ include $file;
+ }
+ }
+
+ /**
+ * Register this autoloader
+ *
+ * @return void
+ */
+ public static function register()
+ {
+ set_include_path(
+ get_include_path() . PATH_SEPARATOR . __DIR__ . '/../'
+ );
+ spl_autoload_register(array(new self(), 'load'));
+ }
+}
+?> \ No newline at end of file
diff --git a/tools/php-sqllint/src/phpsqllint/Cli.php b/tools/php-sqllint/src/phpsqllint/Cli.php
new file mode 100644
index 000000000..1501815eb
--- /dev/null
+++ b/tools/php-sqllint/src/phpsqllint/Cli.php
@@ -0,0 +1,280 @@
+<?php
+/**
+ * Part of php-sqllint
+ *
+ * PHP version 5
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://cweiske.de/php-sqllint.htm
+ */
+namespace phpsqllint;
+use PhpMyAdmin\SqlParser\Parser;
+
+/**
+ * Command line interface
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
+ */
+class Cli
+{
+ protected $renderer;
+
+ protected $format = false;
+
+ /**
+ * What syntax highlighting mode should be used
+ *
+ * none, ansi, html
+ */
+ protected $highlight = 'none';
+
+
+ /**
+ * Start processing.
+ *
+ * @return void
+ */
+ public function run()
+ {
+ try {
+ $parser = $this->loadOptionParser();
+ $files = $this->parseParameters($parser);
+
+ $allfine = true;
+ foreach ($files as $filename) {
+ if ($this->format) {
+ $allfine &= $this->formatFile($filename);
+ } else {
+ $allfine &= $this->checkFile($filename);
+ }
+ }
+
+ if ($allfine == true) {
+ exit(0);
+ } else {
+ exit(10);
+ }
+ } catch (\Exception $e) {
+ echo 'Error: ' . $e->getMessage() . "\n";
+ exit(1);
+ }
+ }
+
+ /**
+ * Check a .sql file for syntax errors
+ *
+ * @param string $filename File path
+ *
+ * @return boolean True if there were no errors, false if there were some
+ */
+ public function checkFile($filename)
+ {
+ $this->renderer->startRendering($filename);
+ $sql = $this->loadSql($filename);
+ if ($sql === false) {
+ return false;
+ }
+
+ $parser = new \PhpMyAdmin\SqlParser\Parser($sql);
+ if (count($parser->errors) == 0) {
+ $this->renderer->finishOk();
+ return true;
+ }
+
+ $lines = array(1 => 0);
+ $pos = -1;
+ $line = 1;
+ while (false !== $pos = strpos($sql, "\n", ++$pos)) {
+ $lines[++$line] = $pos;
+ }
+
+ foreach ($parser->errors as $error) {
+ /* @var PhpMyAdmin\SqlParser\Exceptions\ParserException $error) */
+ reset($lines);
+ $line = 1;
+ while (next($lines) && $error->token->position >= current($lines)) {
+ ++$line;
+ }
+ $col = $error->token->position - $lines[$line];
+
+ $this->renderer->displayError(
+ $error->getMessage(),
+ //FIXME: ->token or ->value?
+ $error->token->token,
+ $line,
+ $col
+ );
+ }
+
+ return false;
+ }
+
+ /**
+ * Reformat the given file
+ */
+ protected function formatFile($filename)
+ {
+ $this->renderer->startRendering($filename);
+ $sql = $this->loadSql($filename);
+ if ($sql === false) {
+ return false;
+ }
+
+ $typeMap = array(
+ 'none' => 'text',
+ 'ansi' => 'cli',
+ 'html' => 'html',
+ );
+ $options = array(
+ 'type' => $typeMap[$this->highlight],
+ );
+ echo \PhpMyAdmin\SqlParser\Utils\Formatter::format($sql, $options) . "\n";
+ }
+
+ protected function loadSql($filename)
+ {
+ if ($filename == '-') {
+ $sql = file_get_contents('php://stdin');
+ } else {
+ $sql = file_get_contents($filename);
+ }
+ if (trim($sql) == '') {
+ $this->renderer->displayError('SQL file empty', '', 0, 0);
+ return false;
+ }
+ return $sql;
+ }
+
+ /**
+ * Load parameters for the CLI option parser.
+ *
+ * @return \Console_CommandLine CLI option parser
+ */
+ protected function loadOptionParser()
+ {
+ $parser = new \Console_CommandLine();
+ $parser->description = 'php-sqllint';
+ $parser->version = 'dev';
+ $parser->avoid_reading_stdin = true;
+
+ $versionFile = __DIR__ . '/../../VERSION';
+ if (file_exists($versionFile)) {
+ $parser->version = trim(file_get_contents($versionFile));
+ }
+
+ $parser->addOption(
+ 'format',
+ array(
+ 'short_name' => '-f',
+ 'long_name' => '--format',
+ 'description' => 'Reformat SQL instead of checking',
+ 'action' => 'StoreTrue',
+ 'default' => false,
+ )
+ );
+ $parser->addOption(
+ 'highlight',
+ array(
+ 'short_name' => '-h',
+ 'long_name' => '--highlight',
+ 'description' => 'Highlighting mode (when using --format)',
+ 'action' => 'StoreString',
+ 'choices' => array(
+ 'none',
+ 'ansi',
+ 'html',
+ 'auto',
+ ),
+ 'default' => 'auto',
+ 'add_list_option' => true,
+ )
+ );
+ $parser->addOption(
+ 'renderer',
+ array(
+ 'short_name' => '-r',
+ 'long_name' => '--renderer',
+ 'description' => 'Output mode',
+ 'action' => 'StoreString',
+ 'choices' => array(
+ 'emacs',
+ 'text',
+ ),
+ 'default' => 'text',
+ 'add_list_option' => true,
+ )
+ );
+
+ $parser->addArgument(
+ 'sql_files',
+ array(
+ 'description' => 'SQL files, "-" for stdin',
+ 'multiple' => true
+ )
+ );
+
+ return $parser;
+ }
+
+ /**
+ * Let the CLI option parser parse the options.
+ *
+ * @param object $parser Option parser
+ *
+ * @return array Array of file names
+ */
+ protected function parseParameters(\Console_CommandLine $parser)
+ {
+ try {
+ $result = $parser->parse();
+
+ $rendClass = '\\phpsqllint\\Renderer_'
+ . ucfirst($result->options['renderer']);
+ $this->renderer = new $rendClass();
+
+ $this->format = $result->options['format'];
+
+ $this->highlight = $result->options['highlight'];
+ if ($this->highlight == 'auto') {
+ if (php_sapi_name() == 'cli') {
+ //default coloring to enabled, except
+ // when piping | to another tool
+ $this->highlight = 'ansi';
+ if (function_exists('posix_isatty')
+ && !posix_isatty(STDOUT)
+ ) {
+ $this->highlight = 'none';
+ }
+ } else {
+ //no idea where we are, so do not highlight
+ $this->highlight = 'none';
+ }
+ }
+
+ foreach ($result->args['sql_files'] as $filename) {
+ if ($filename == '-') {
+ continue;
+ }
+ if (!file_exists($filename)) {
+ throw new \Exception('File does not exist: ' . $filename);
+ }
+ if (!is_file($filename)) {
+ throw new \Exception('Not a file: ' . $filename);
+ }
+ }
+
+ return $result->args['sql_files'];
+ } catch (\Exception $exc) {
+ $parser->displayError($exc->getMessage());
+ }
+ }
+
+}
+?>
diff --git a/tools/php-sqllint/src/phpsqllint/Renderer.php b/tools/php-sqllint/src/phpsqllint/Renderer.php
new file mode 100644
index 000000000..5b68ee11a
--- /dev/null
+++ b/tools/php-sqllint/src/phpsqllint/Renderer.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Part of php-sqllint
+ *
+ * PHP version 5
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://cweiske.de/php-sqllint.htm
+ */
+namespace phpsqllint;
+
+/**
+ * What every renderer has to implement
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
+ */
+interface Renderer
+{
+ /**
+ * Begin syntax check output rendering
+ *
+ * @param string $filename Path to the SQL file
+ *
+ * @return void
+ */
+ public function startRendering($filename);
+
+ /**
+ * Output errors in GNU style; see emacs compilation.txt
+ *
+ * @param string $msg Error message
+ * @param string $token Character which caused the error
+ * @param integer $line Line at which the error occured
+ * @param integer $col Column at which the error occured
+ *
+ * @return void
+ */
+ public function displayError($msg, $token, $line, $col);
+
+ /**
+ * Finish syntax check output rendering; no syntax errors found
+ *
+ * @return void
+ */
+ public function finishOk();
+}
+?>
diff --git a/tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php b/tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php
new file mode 100644
index 000000000..3a667c7f6
--- /dev/null
+++ b/tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Part of php-sqllint
+ *
+ * PHP version 5
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://cweiske.de/php-sqllint.htm
+ */
+namespace phpsqllint;
+
+/**
+ * Output for emacs' compilation mode
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
+ */
+class Renderer_Emacs implements Renderer
+{
+ protected $filename;
+
+ /**
+ * Begin syntax check output rendering
+ *
+ * @param string $filename Path to the SQL file
+ *
+ * @return void
+ */
+ public function startRendering($filename)
+ {
+ $this->filename = $filename;
+ }
+
+ /**
+ * Output errors in GNU style; see emacs compilation.txt
+ *
+ * @param string $msg Error message
+ * @param string $token Character which caused the error
+ * @param integer $line Line at which the error occured
+ * @param integer $col Column at which the error occured
+ *
+ * @return void
+ */
+ public function displayError($msg, $token, $line, $col)
+ {
+ echo $this->filename
+ . ':' . $line
+ . '.' . $col
+ . ':Error:'
+ . ' '. $msg
+ . "\n";
+ }
+
+ /**
+ * Finish syntax check output rendering; no syntax errors found
+ *
+ * @return void
+ */
+ public function finishOk()
+ {
+ //do nothing
+ }
+}
+?>
diff --git a/tools/php-sqllint/src/phpsqllint/Renderer/Text.php b/tools/php-sqllint/src/phpsqllint/Renderer/Text.php
new file mode 100644
index 000000000..44e7ecbd4
--- /dev/null
+++ b/tools/php-sqllint/src/phpsqllint/Renderer/Text.php
@@ -0,0 +1,102 @@
+<?php
+/**
+ * Part of php-sqllint
+ *
+ * PHP version 5
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://cweiske.de/php-sqllint.htm
+ */
+namespace phpsqllint;
+
+/**
+ * Textual output, easily readable by humans.
+ *
+ * @category Tools
+ * @package PHP-SQLlint
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
+ */
+class Renderer_Text implements Renderer
+{
+ protected $fileshown = false;
+ protected $filename = null;
+
+ /**
+ * Begin syntax check output rendering
+ *
+ * @param string $filename Path to the SQL file
+ *
+ * @return void
+ */
+ public function startRendering($filename)
+ {
+ $this->filename = $filename;
+ $this->fileshown = false;
+ }
+
+
+ protected function showFile()
+ {
+ if ($this->fileshown) {
+ return;
+ }
+
+ echo "Checking SQL syntax of " . $this->filename . "\n";
+ $this->fileshown = true;
+ }
+
+ /**
+ * Show the error to the user.
+ *
+ * @param string $msg Error message
+ * @param string $token Character which caused the error
+ * @param integer $line Line at which the error occured
+ * @param integer $col Column at which the error occured
+ *
+ * @return void
+ */
+ public function displayError($msg, $token, $line, $col)
+ {
+ $this->showFile();
+ echo ' Line ' . $line
+ . ', col ' . $col
+ . ' at "' . $this->niceToken($token) . '":'
+ . ' ' . $msg
+ . "\n";
+ }
+
+ /**
+ * Finish syntax check output rendering; no syntax errors found
+ *
+ * @return void
+ */
+ public function finishOk()
+ {
+ if ($this->fileshown) {
+ echo " OK\n";
+ }
+ }
+
+ /**
+ * Convert the token string to a readable one, especially special
+ * characters like newline and tabs
+ *
+ * @param string $str String with possibly special characters
+ *
+ * @return string Escaped string
+ */
+ protected function niceToken($str)
+ {
+ return str_replace(
+ ["\n", "\r", "\t"],
+ ['\n', '\r', '\t'],
+ $str
+ );
+ }
+}
+?>