10 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
11 * @link http://cweiske.de/php-sqllint.htm
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 \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 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 \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 = '0.0.2';
167 $parser->avoid_reading_stdin = true;
172 'short_name' => '-f',
173 'long_name' => '--format',
174 'description' => 'Reformat SQL instead of checking',
175 'action' => 'StoreTrue',
182 'short_name' => '-h',
183 'long_name' => '--highlight',
184 'description' => 'Highlighting mode (when using --format)',
185 'action' => 'StoreString',
193 'add_list_option' => true,
199 'short_name' => '-r',
200 'long_name' => '--renderer',
201 'description' => 'Output mode',
202 'action' => 'StoreString',
208 'add_list_option' => true,
212 $parser->addArgument(
215 'description' => 'SQL files, "-" for stdin',
224 * Let the CLI option parser parse the options.
226 * @param object $parser Option parser
228 * @return array Array of file names
230 protected function parseParameters(\Console_CommandLine $parser)
233 $result = $parser->parse();
235 $rendClass = '\\phpsqllint\\Renderer_'
236 . ucfirst($result->options['renderer']);
237 $this->renderer = new $rendClass();
239 $this->format = $result->options['format'];
241 $this->highlight = $result->options['highlight'];
242 if ($this->highlight == 'auto') {
243 if (php_sapi_name() == 'cli') {
244 //default coloring to enabled, except
245 // when piping | to another tool
246 $this->highlight = 'ansi';
247 if (function_exists('posix_isatty')
248 && !posix_isatty(STDOUT)
250 $this->highlight = 'none';
253 //no idea where we are, so do not highlight
254 $this->highlight = 'none';
258 foreach ($result->args['sql_files'] as $filename) {
259 if ($filename == '-') {
262 if (!file_exists($filename)) {
263 throw new \Exception('File does not exist: ' . $filename);
265 if (!is_file($filename)) {
266 throw new \Exception('Not a file: ' . $filename);
270 return $result->args['sql_files'];
271 } catch (\Exception $exc) {
272 $parser->displayError($exc->getMessage());