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;
16 require_once 'Console/CommandLine.php';
19 * Command line interface
22 * @package PHP-SQLlint
24 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25 * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
31 protected $format = false;
34 * What syntax highlighting mode should be used
38 protected $highlight = 'none';
49 $parser = $this->loadOptionParser();
50 $files = $this->parseParameters($parser);
53 foreach ($files as $filename) {
55 $allfine &= $this->formatFile($filename);
57 $allfine &= $this->checkFile($filename);
61 if ($allfine == true) {
66 } catch (\Exception $e) {
67 echo 'Error: ' . $e->getMessage() . "\n";
73 * Check a .sql file for syntax errors
75 * @param string $filename File path
77 * @return boolean True if there were no errors, false if there were some
79 public function checkFile($filename)
81 $this->renderer->startRendering($filename);
82 $sql = $this->loadSql($filename);
87 $parser = new \PhpMyAdmin\SqlParser\Parser($sql);
88 if (count($parser->errors) == 0) {
89 $this->renderer->finishOk();
93 $lines = array(1 => 0);
96 while (false !== $pos = strpos($sql, "\n", ++$pos)) {
97 $lines[++$line] = $pos;
100 foreach ($parser->errors as $error) {
101 /* @var PhpMyAdmin\SqlParser\Exceptions\ParserException $error) */
104 while (next($lines) && $error->token->position >= current($lines)) {
107 $col = $error->token->position - $lines[$line];
109 $this->renderer->displayError(
110 $error->getMessage(),
111 //FIXME: ->token or ->value?
112 $error->token->token,
122 * Reformat the given file
124 protected function formatFile($filename)
126 $this->renderer->startRendering($filename);
127 $sql = $this->loadSql($filename);
128 if ($sql === false) {
138 'type' => $typeMap[$this->highlight],
140 echo \PhpMyAdmin\SqlParser\Utils\Formatter::format($sql, $options) . "\n";
143 protected function loadSql($filename)
145 if ($filename == '-') {
146 $sql = file_get_contents('php://stdin');
148 $sql = file_get_contents($filename);
150 if (trim($sql) == '') {
151 $this->renderer->displayError('SQL file empty', '', 0, 0);
158 * Load parameters for the CLI option parser.
160 * @return \Console_CommandLine CLI option parser
162 protected function loadOptionParser()
164 $parser = new \Console_CommandLine();
165 $parser->description = 'php-sqllint';
166 $parser->version = 'dev';
167 $parser->avoid_reading_stdin = true;
169 $versionFile = __DIR__ . '/../../VERSION';
170 if (file_exists($versionFile)) {
171 $parser->version = trim(file_get_contents($versionFile));
177 'short_name' => '-f',
178 'long_name' => '--format',
179 'description' => 'Reformat SQL instead of checking',
180 'action' => 'StoreTrue',
187 'short_name' => '-h',
188 'long_name' => '--highlight',
189 'description' => 'Highlighting mode (when using --format)',
190 'action' => 'StoreString',
198 'add_list_option' => true,
204 'short_name' => '-r',
205 'long_name' => '--renderer',
206 'description' => 'Output mode',
207 'action' => 'StoreString',
213 'add_list_option' => true,
217 $parser->addArgument(
220 'description' => 'SQL files, "-" for stdin',
229 * Let the CLI option parser parse the options.
231 * @param object $parser Option parser
233 * @return array Array of file names
235 protected function parseParameters(\Console_CommandLine $parser)
238 $result = $parser->parse();
240 $rendClass = '\\phpsqllint\\Renderer_'
241 . ucfirst($result->options['renderer']);
242 $this->renderer = new $rendClass();
244 $this->format = $result->options['format'];
246 $this->highlight = $result->options['highlight'];
247 if ($this->highlight == 'auto') {
248 if (php_sapi_name() == 'cli') {
249 //default coloring to enabled, except
250 // when piping | to another tool
251 $this->highlight = 'ansi';
252 if (function_exists('posix_isatty')
253 && !posix_isatty(STDOUT)
255 $this->highlight = 'none';
258 //no idea where we are, so do not highlight
259 $this->highlight = 'none';
263 foreach ($result->args['sql_files'] as $filename) {
264 if ($filename == '-') {
267 if (!file_exists($filename)) {
268 throw new \Exception('File does not exist: ' . $filename);
270 if (!is_file($filename)) {
271 throw new \Exception('Not a file: ' . $filename);
275 return $result->args['sql_files'];
276 } catch (\Exception $exc) {
277 $parser->displayError($exc->getMessage());