forked from MattKetmo/sqlformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlformat.php
55 lines (44 loc) · 1.23 KB
/
sqlformat.php
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
$version = "@package_version@";
$basename = basename($argv[0]);
$doc = <<<DOC
Usage:
$basename [--no-format] [--no-highlight] [<sql>]
$basename [--no-comment] [--no-highlight] [<sql>]
$basename [-c|--compress] [<sql>]
$basename [-h | --help]
$basename [--version]
Options:
--no-highlight Disable highlight.
--no-format Disable auto formatting.
--no-comment Remove comment.
-c --compress Compress SQL request.
-h --help Display this help.
-V --version Show the version
DOC;
$handler = new Docopt\Handler();
$args = $handler->handle($doc);
// Show version
if ($args['--version']) {
echo "$basename $version\n";
return;
}
// Get SQL request for arguments or from stdin
$sql = $args['<sql>'] ?: stream_get_contents(fopen("php://stdin", "r"));
// Compress
if ($args['--compress']) {
echo SqlFormatter::compress($sql);
return;
}
// No formatting
if ($args['--no-format']) {
echo $args['--no-highlight'] ? $sql : SqlFormatter::highlight($sql);
return;
}
// No comment (implies format is enabled)
if ($args['--no-comment']) {
$sql = SqlFormatter::removeComments($sql);
}
echo SqlFormatter::format($sql, !$args['--no-highlight']);