Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c16301

Browse files
author
Mathieu Ferment
committed
Add explore_sql_query.php script
1 parent fa460e2 commit 5c16301

File tree

2 files changed

+183
-5
lines changed

2 files changed

+183
-5
lines changed

‎README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ explore_url
77
-----------
88

99
```bash
10-
php explore_url <url> [--urldecode]
10+
php explore_url.php <url> [--urldecode]
1111
```
1212
Explode an URL in different parts to ease reading
1313

14-
TODO
15-
----
16-
17-
Script 'explore_sql_query' to ease SQL queries reading
14+
```bash
15+
php explore_sql_query.php <query>
16+
```
17+
Display a SQL query formatted in a more readable way

‎explore_sql_query.php‎

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
/**
4+
* @param $length
5+
*
6+
* @return string
7+
*/
8+
function buildSpaceString($length)
9+
{
10+
$string = '';
11+
for ($i = 0; $i < $length; $i++) {
12+
$string .= '';
13+
}
14+
return $string;
15+
}
16+
17+
/**
18+
* Echo array of strings in output
19+
*
20+
* @param array $elements
21+
* @param int $tabLength
22+
*/
23+
function echoArrayAsString(array $elements, $tabLength)
24+
{
25+
$tab = buildSpaceString($tabLength + 1);
26+
27+
$elementsAsString = implode('', $elements);
28+
$commaSeparatedStrings = explode(',', $elementsAsString);
29+
30+
$length = count($commaSeparatedStrings);
31+
32+
if ($length === 1) {
33+
echo '' . $commaSeparatedStrings[0] . PHP_EOL;
34+
return;
35+
}
36+
37+
for ($i = 0; $i < $length; $i++) {
38+
39+
if ($i === 0) {
40+
echo '' . $commaSeparatedStrings[$i] . ',' . PHP_EOL;
41+
} else if ($i === ($length - 1)) {
42+
echo $tab . $commaSeparatedStrings[$i] . PHP_EOL;
43+
} else {
44+
echo $tab . $commaSeparatedStrings[$i] . ',' . PHP_EOL;
45+
}
46+
}
47+
}
48+
49+
/**
50+
* @param string $word
51+
*
52+
* @return bool
53+
*/
54+
function isSpecificSingleKeyWord($word)
55+
{
56+
$keyWords = array(
57+
'SELECT',
58+
'FROM',
59+
'JOIN',
60+
'WHERE',
61+
'AND',
62+
'OR',
63+
'HAVING',
64+
'LIKE',
65+
);
66+
67+
if (in_array($word, $keyWords)) {
68+
return true;
69+
} else {
70+
return false;
71+
}
72+
}
73+
74+
/**
75+
* @param string $word
76+
*
77+
* @return bool
78+
*/
79+
function isSpecificDoubleKeyWord($word)
80+
{
81+
$keyWords = array(
82+
'INNER JOIN',
83+
'LEFT JOIN',
84+
'RIGHT JOIN',
85+
'ORDER BY',
86+
'GROUP BY',
87+
);
88+
89+
if (in_array($word, $keyWords)) {
90+
return true;
91+
} else {
92+
return false;
93+
}
94+
}
95+
96+
/**
97+
* @param array $words
98+
*
99+
* @return array
100+
*/
101+
function sortWords(array $words)
102+
{
103+
$parts = array();
104+
105+
$keyWord = 'SELECT';
106+
$part = array();
107+
108+
$length = count($words);
109+
110+
for ($i = 0; $i < $length; $i++) {
111+
112+
$currentWord = $words[$i];
113+
$nextWord = $words[$i + 1];
114+
$doubleWord = $currentWord . '' . $nextWord;
115+
116+
if (isSpecificSingleKeyWord($currentWord)) {
117+
118+
$parts[$keyWord] = $part;
119+
$keyWord = $currentWord;
120+
$part = array();
121+
} else if (isSpecificDoubleKeyWord($doubleWord)) {
122+
$parts[$keyWord] = $part;
123+
$keyWord = $doubleWord;
124+
$part = array();
125+
$i++;
126+
} else {
127+
$part[] = $currentWord;
128+
}
129+
}
130+
131+
$parts[$keyWord] = $part;
132+
133+
return $parts;
134+
}
135+
136+
/**
137+
* Validate console input
138+
*
139+
* Die if input is not valid
140+
*
141+
* @param array $argv user inputs
142+
*/
143+
function validateInput($argv)
144+
{
145+
$isValid = false;
146+
147+
if (count($argv) === 2) {
148+
$isValid = true;
149+
}
150+
151+
if (!$isValid) {
152+
echo 'php explore_sql_query <query>' . PHP_EOL;
153+
die(1);
154+
}
155+
}
156+
157+
validateInput($argv);
158+
159+
require 'Util/TextColorWriter.php';
160+
161+
$query = $argv[1];
162+
$query = str_replace(array("\r\n", "\r", "\n"), "", $query);
163+
164+
$words = explode('', $query);
165+
$queryParts = sortWords($words);
166+
167+
echo TextColorWriter::textColor('QUERY ANALYSIS:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
168+
169+
foreach ($queryParts as $key => $part) {
170+
$tabLength = strlen($key) + 1;
171+
172+
if (in_array($key, array('AND','OR'))) {
173+
echo '';
174+
}
175+
176+
echo '' . TextColorWriter::textColor($key, TextColorWriter::BASH_PROMPT_YELLOW) . '';
177+
echoArrayAsString($part, $tabLength);
178+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /