-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathsql-formatter
More file actions
executable file
·40 lines (34 loc) · 1.33 KB
/
sql-formatter
File metadata and controls
executable file
·40 lines (34 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env php
<?php
if("cli" !== php_sapi_name()) {
echo "<p>Run this PHP script from the command line to see CLI syntax highlighting and formatting. It supports Unix pipes or command line argument style.</p>";
echo "<pre><code>php bin/sql-formatter \"SELECT * FROM MyTable WHERE (id>5 AND \\`name\\` LIKE \\"testing\\");\"</code></pre>";
echo "<pre><code>echo \"SELECT * FROM MyTable WHERE (id>5 AND \\`name\\` LIKE \\"testing\\");\" | php bin/sql-formatter</code></pre>";
exit;
}
if(isset($argv[1])) {
$sql = $argv[1];
}
else {
$sql = stream_get_contents(fopen("php://stdin", "r"));
}
require_once(__DIR__.'/../lib/SqlFormatter.php');
/**
* Returns true if the stream supports colorization.
*
* Colorization is disabled if not supported by the stream:
*
* - Windows without Ansicon and ConEmu
* - non tty consoles
*
* @return bool true if the stream supports colorization, false otherwise
* @link https://github.com/symfony/symfony/blob/v2.4.6/src/Symfony/Component/Console/Output/StreamOutput.php#L97
*/
function hasColorSupport() {
if (DIRECTORY_SEPARATOR == '\\') {
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
}
return function_exists('posix_isatty') && @posix_isatty(STDOUT);
}
$highlight = hasColorSupport();
echo SqlFormatter::format($sql, $highlight);