1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CLI interface. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PhpMyAdmin\SqlParser\Utils; |
8
|
|
|
|
9
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
10
|
|
|
use PhpMyAdmin\SqlParser\Lexer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* CLI interface. |
14
|
|
|
* |
15
|
|
|
* @category Exceptions |
16
|
|
|
* |
17
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ |
18
|
|
|
*/ |
19
|
|
|
class CLI |
20
|
|
|
{ |
21
|
13 |
|
public function mergeLongOpts(&$params, &$longopts) |
22
|
|
|
{ |
23
|
13 |
|
foreach ($longopts as $value) { |
24
|
13 |
|
$value = rtrim($value, ':'); |
25
|
13 |
|
if (isset($params[$value])) { |
26
|
3 |
|
$params[$value[0]] = $params[$value]; |
27
|
2 |
|
} |
28
|
13 |
|
} |
29
|
13 |
|
} |
30
|
|
|
|
31
|
2 |
|
public function usageHighlight() |
32
|
|
|
{ |
33
|
2 |
|
echo "Usage: highlight-query --query SQL [--format html|cli|text]\n"; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
public function getopt($opt, $long) |
37
|
|
|
{ |
38
|
|
|
return getopt($opt, $long); |
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
public function parseHighlight() |
42
|
|
|
{ |
43
|
8 |
|
$longopts = array('help', 'query:', 'format:'); |
44
|
8 |
|
$params = $this->getopt( |
45
|
8 |
|
'hq:f:', $longopts |
46
|
8 |
|
); |
47
|
8 |
|
if ($params === false) { |
48
|
1 |
|
return false; |
49
|
|
|
} |
50
|
7 |
|
$this->mergeLongOpts($params, $longopts); |
51
|
7 |
|
if (!isset($params['f'])) { |
52
|
4 |
|
$params['f'] = 'cli'; |
53
|
4 |
|
} |
54
|
7 |
|
if (!in_array($params['f'], array('html', 'cli', 'text'))) { |
55
|
1 |
|
echo "ERROR: Invalid value for format!\n"; |
56
|
|
|
|
57
|
1 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
return $params; |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
public function runHighlight() |
64
|
|
|
{ |
65
|
8 |
|
$params = $this->parseHighlight(); |
66
|
8 |
|
if ($params === false) { |
67
|
2 |
|
return 1; |
68
|
|
|
} |
69
|
6 |
|
if (isset($params['h'])) { |
70
|
1 |
|
$this->usageHighlight(); |
71
|
|
|
|
72
|
1 |
|
return 0; |
73
|
|
|
} |
74
|
5 |
|
if (isset($params['q'])) { |
75
|
4 |
|
echo Formatter::format( |
76
|
4 |
|
$params['q'], array('type' => $params['f']) |
77
|
4 |
|
); |
78
|
4 |
|
echo "\n"; |
79
|
|
|
|
80
|
4 |
|
return 0; |
81
|
|
|
} |
82
|
1 |
|
echo "ERROR: Missing parameters!\n"; |
83
|
1 |
|
$this->usageHighlight(); |
84
|
|
|
|
85
|
1 |
|
return 1; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
public function usageLint() |
89
|
|
|
{ |
90
|
2 |
|
echo "Usage: lint-query --query SQL\n"; |
91
|
2 |
|
} |
92
|
|
|
|
93
|
6 |
|
public function parseLint() |
94
|
|
|
{ |
95
|
6 |
|
$longopts = array('help', 'query:'); |
96
|
6 |
|
$params = $this->getopt( |
97
|
6 |
|
'hq:', $longopts |
98
|
6 |
|
); |
99
|
6 |
|
$this->mergeLongOpts($params, $longopts); |
100
|
|
|
|
101
|
6 |
|
return $params; |
102
|
|
|
} |
103
|
|
|
|
104
|
6 |
|
public function runLint() |
105
|
|
|
{ |
106
|
6 |
|
$params = $this->parseLint(); |
107
|
6 |
|
if ($params === false) { |
108
|
1 |
|
return 1; |
109
|
|
|
} |
110
|
5 |
|
if (isset($params['h'])) { |
111
|
1 |
|
$this->usageLint(); |
112
|
|
|
|
113
|
1 |
|
return 0; |
114
|
|
|
} |
115
|
4 |
|
if (isset($params['q'])) { |
116
|
3 |
|
$lexer = new Lexer($params['q'], false); |
117
|
3 |
|
$parser = new Parser($lexer->list); |
118
|
3 |
|
$errors = Error::get(array($lexer, $parser)); |
119
|
3 |
|
if (count($errors) == 0) { |
120
|
2 |
|
return 0; |
121
|
|
|
} |
122
|
1 |
|
$output = Error::format($errors); |
123
|
1 |
|
echo implode("\n", $output); |
124
|
1 |
|
echo "\n"; |
125
|
|
|
|
126
|
1 |
|
return 10; |
127
|
|
|
} |
128
|
1 |
|
echo "ERROR: Missing parameters!\n"; |
129
|
1 |
|
$this->usageLint(); |
130
|
|
|
|
131
|
1 |
|
return 1; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|