10 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
11 * @link http://cweiske.de/php-sqllint.htm
14 use PhpMyAdmin\SqlParser\Parser;
17 * Command line interface
20 * @package PHP-SQLlint
22 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
23 * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
29 protected $format = false;
32 * What syntax highlighting mode should be used
36 protected $highlight = 'none';
47 $parser = $this->loadOptionParser();
48 $files = $this->parseParameters($parser);
51 foreach ($files as $filename) {
53 $allfine &= $this->formatFile($filename);
55 $allfine &= $this->checkFile($filename);
59 if ($allfine == true) {
64 } catch (\Exception $e) {
65 echo 'Error: ' . $e->getMessage() . "\n";
71 * Check a .sql file for syntax errors
73 * @param string $filename File path
75 * @return boolean True if there were no errors, false if there were some
77 public function checkFile($filename)
79 $this->renderer->startRendering($filename);
80 $sql = $this->loadSql($filename);
85 $parser = new \PhpMyAdmin\SqlParser\Parser($sql);
86 if (count($parser->errors) == 0) {
87 $this->renderer->finishOk();
91 $lines = array(1 => 0);
94 while (false !== $pos = mb_strpos($sql, "\n", ++$pos)) {
95 $lines[++$line] = $pos;
98 foreach ($parser->errors as $error) {
99 /* @var PhpMyAdmin\SqlParser\Exceptions\ParserException $error) */
102 while (next($lines) && $error->token->position >= current($lines)) {
105 $col = $error->token->position - $lines[$line];
107 $this->renderer->displayError(
108 $error->getMessage(),
109 //FIXME: ->token or ->value?
110 $error->token->token,
120 * Reformat the given file
122 protected function formatFile($filename)
124 $this->renderer->startRendering($filename);
125 $sql = $this->loadSql($filename);
126 if ($sql === false) {
136 'type' => $typeMap[$this->highlight],
138 echo \PhpMyAdmin\SqlParser\Utils\Formatter::format($sql, $options) . "\n";
141 protected function loadSql($filename)
143 if ($filename == '-') {
144 $sql = file_get_contents('php://stdin');
146 $sql = file_get_contents($filename);
148 if (trim($sql) == '') {
149 $this->renderer->displayError('SQL file empty', '', 0, 0);
156 * Load parameters for the CLI option parser.
158 * @return \Console_CommandLine CLI option parser
160 protected function loadOptionParser()
162 $parser = new \Console_CommandLine();
163 $parser->description = 'php-sqllint';
164 $parser->version = 'dev';
165 $parser->avoid_reading_stdin = true;
167 $versionFile = __DIR__ . '/../../VERSION';
168 if (file_exists($versionFile)) {
169 $parser->version = trim(file_get_contents($versionFile));
175 'short_name' => '-f',
176 'long_name' => '--format',
177 'description' => 'Reformat SQL instead of checking',
178 'action' => 'StoreTrue',
185 'short_name' => '-h',
186 'long_name' => '--highlight',
187 'description' => 'Highlighting mode (when using --format)',
188 'action' => 'StoreString',
196 'add_list_option' => true,
202 'short_name' => '-r',
203 'long_name' => '--renderer',
204 'description' => 'Output mode',
205 'action' => 'StoreString',
211 'add_list_option' => true,
215 $parser->addArgument(
218 'description' => 'SQL files, "-" for stdin',
227 * Let the CLI option parser parse the options.
229 * @param object $parser Option parser
231 * @return array Array of file names
233 protected function parseParameters(\Console_CommandLine $parser)
236 $result = $parser->parse();
238 $rendClass = '\\phpsqllint\\Renderer_'
239 . ucfirst($result->options['renderer']);
240 $this->renderer = new $rendClass();
242 $this->format = $result->options['format'];
244 $this->highlight = $result->options['highlight'];
245 if ($this->highlight == 'auto') {
246 if (php_sapi_name() == 'cli') {
247 //default coloring to enabled, except
248 // when piping | to another tool
249 $this->highlight = 'ansi';
250 if (function_exists('posix_isatty')
251 && !posix_isatty(STDOUT)
253 $this->highlight = 'none';
256 //no idea where we are, so do not highlight
257 $this->highlight = 'none';
261 foreach ($result->args['sql_files'] as $filename) {
262 if ($filename == '-') {
265 if (!file_exists($filename)) {
266 throw new \Exception('File does not exist: ' . $filename);
268 if (!is_file($filename)) {
269 throw new \Exception('Not a file: ' . $filename);
273 return $result->args['sql_files'];
274 } catch (\Exception $exc) {
275 $parser->displayError($exc->getMessage());