9 * @author Christian Weiske <cweiske@cweiske.de>
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
23 * @author Christian Weiske <cweiske@cweiske.de>
24 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25 * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp
31 protected $format = false;
32 protected $colors = false;
42 $parser = $this->loadOptionParser();
43 $files = $this->parseParameters($parser);
46 foreach ($files as $filename) {
48 $allfine &= $this->formatFile($filename);
50 $allfine &= $this->checkFile($filename);
54 if ($allfine == true) {
59 } catch (\Exception $e) {
60 echo 'Error: ' . $e->getMessage() . "\n";
66 * Check a .sql file for syntax errors
68 * @param string $filename File path
70 * @return boolean True if there were no errors, false if there were some
72 public function checkFile($filename)
74 $this->renderer->startRendering($filename);
75 $sql = $this->loadSql($filename);
80 $parser = new \SqlParser\Parser($sql);
81 if (count($parser->errors) == 0) {
82 $this->renderer->finishOk();
86 $lines = array(1 => 0);
89 while (false !== $pos = strpos($sql, "\n", ++$pos)) {
90 $lines[++$line] = $pos;
93 foreach ($parser->errors as $error) {
94 /* @var SqlParser\Exceptions\ParserException $error) */
97 while (next($lines) && $error->token->position >= current($lines)) {
100 $col = $error->token->position - $lines[$line];
102 $this->renderer->displayError(
103 $error->getMessage(),
104 //FIXME: ->token or ->value?
105 $error->token->token,
115 * Reformat the given file
117 protected function formatFile($filename)
119 $this->renderer->startRendering($filename);
120 $sql = $this->loadSql($filename);
121 if ($sql === false) {
126 //FIXME: automatically detect if the shell/tool supports colors
127 'type' => $this->colors ? 'cli' : 'text'
129 echo \SqlParser\Utils\Formatter::format($sql, $options) . "\n";
132 protected function loadSql($filename)
134 if ($filename == '-') {
135 $sql = file_get_contents('php://stdin');
137 $sql = file_get_contents($filename);
139 if (trim($sql) == '') {
140 $this->renderer->displayError('SQL file empty', '', 0, 0);
147 * Load parameters for the CLI option parser.
149 * @return \Console_CommandLine CLI option parser
151 protected function loadOptionParser()
153 $parser = new \Console_CommandLine();
154 $parser->description = 'php-sqllint';
155 $parser->version = '0.0.2';
156 $parser->avoid_reading_stdin = true;
161 'short_name' => '-f',
162 'long_name' => '--format',
163 'description' => 'Reformat SQL instead of checking',
164 'action' => 'StoreTrue',
171 'short_name' => '-c',
172 'long_name' => '--colors',
173 'description' => 'Use colors in formatting output',
174 'action' => 'StoreTrue',
181 'short_name' => '-r',
182 'long_name' => '--renderer',
183 'description' => 'Output mode',
184 'action' => 'StoreString',
190 'add_list_option' => true,
194 $parser->addArgument(
197 'description' => 'SQL files, "-" for stdin',
206 * Let the CLI option parser parse the options.
208 * @param object $parser Option parser
210 * @return array Array of file names
212 protected function parseParameters(\Console_CommandLine $parser)
215 $result = $parser->parse();
217 $rendClass = '\\phpsqllint\\Renderer_'
218 . ucfirst($result->options['renderer']);
219 $this->renderer = new $rendClass();
221 $this->format = $result->options['format'];
222 $this->colors = $result->options['colors'];
224 foreach ($result->args['sql_files'] as $filename) {
225 if ($filename == '-') {
228 if (!file_exists($filename)) {
229 throw new \Exception('File does not exist: ' . $filename);
231 if (!is_file($filename)) {
232 throw new \Exception('Not a file: ' . $filename);
236 return $result->args['sql_files'];
237 } catch (\Exception $exc) {
238 $parser->displayError($exc->getMessage());