summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIsaac Bennetch <bennetch@gmail.com>2019-04-06 22:05:26 -0400
committerGitHub <noreply@github.com>2019-04-06 22:05:26 -0400
commit975a0b84ed3a037047aa3dc1b99050915dc232ae (patch)
tree7fc0d9b02a7d7731a02cb2773430ad2cd6b388e1
parentd500a211b54c64e620420c176d5269a59d64979d (diff)
parent67ca444cab993e3bc6ce5aa9f715ed9c759a22ba (diff)
downloadsql-parser-975a0b84ed3a037047aa3dc1b99050915dc232ae.zip
sql-parser-975a0b84ed3a037047aa3dc1b99050915dc232ae.tar.gz
sql-parser-975a0b84ed3a037047aa3dc1b99050915dc232ae.tar.bz2
Merge pull request #239 from staabm/stdin
Support reading from stdin
-rw-r--r--src/Utils/CLI.php23
-rw-r--r--tests/Utils/CLITest.php26
2 files changed, 49 insertions, 0 deletions
diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php
index 31d09ce..79e88dc 100644
--- a/src/Utils/CLI.php
+++ b/src/Utils/CLI.php
@@ -77,6 +77,11 @@ class CLI
return 0;
}
+ if (!isset($params['q'])) {
+ if ($stdIn = $this->readStdin()) {
+ $params['q'] = $stdIn;
+ }
+ }
if (isset($params['q'])) {
echo Formatter::format(
$params['q'],
@@ -127,6 +132,11 @@ class CLI
if (isset($params['c'])) {
Context::load($params['c']);
}
+ if (!isset($params['q'])) {
+ if ($stdIn = $this->readStdin()) {
+ $params['q'] = $stdIn;
+ }
+ }
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
$parser = new Parser($lexer->list);
@@ -177,6 +187,11 @@ class CLI
return 0;
}
+ if (!isset($params['q'])) {
+ if ($stdIn = $this->readStdin()) {
+ $params['q'] = $stdIn;
+ }
+ }
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
foreach ($lexer->list->tokens as $idx => $token) {
@@ -199,4 +214,12 @@ class CLI
return 1;
}
+
+ private function readStdin() {
+ stream_set_blocking(STDIN, false);
+ $stdin = stream_get_contents(STDIN);
+ // restore-default block-mode setting
+ stream_set_blocking(STDIN, true);
+ return $stdin;
+ }
}
diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php
index bacf5fb..d9eadae 100644
--- a/tests/Utils/CLITest.php
+++ b/tests/Utils/CLITest.php
@@ -205,4 +205,30 @@ class CLITest extends TestCase
],
];
}
+
+ /**
+ * @dataProvider stdinParams
+ *
+ * @param string $cmd
+ * @param int $result
+ */
+ public function testStdinPipe($cmd, $result)
+ {
+ exec ($cmd, $out, $ret);
+ $this->assertSame($result, $ret);
+ }
+
+ public function stdinParams()
+ {
+ $binPath = PHP_BINARY .' '. dirname(__DIR__,2 ). '/bin/';
+
+ return [
+ ['echo "SELECT 1" | '. $binPath .'highlight-query', 0],
+ ['echo "invalid query" | '. $binPath .'highlight-query', 0],
+ ['echo "SELECT 1" | '. $binPath .'lint-query', 0],
+ ['echo "invalid query" | '. $binPath .'lint-query', 10],
+ ['echo "SELECT 1" | '. $binPath .'tokenize-query', 0],
+ ['echo "invalid query" | '. $binPath .'tokenize-query', 0],
+ ];
+ }
}