Skip to content

Commit d9a80dd

Browse files
author
Mathieu Ferment
committed
Enhance script
1 parent 53aada4 commit d9a80dd

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

explore_sql_query.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function buildSpaceString($length)
1111
for ($i = 0; $i < $length; $i++) {
1212
$string .= ' ';
1313
}
14+
1415
return $string;
1516
}
1617

@@ -24,13 +25,14 @@ function echoArrayAsString(array $elements, $tabLength)
2425
{
2526
$tab = buildSpaceString($tabLength + 1);
2627

27-
$elementsAsString = implode(' ', $elements);
28+
$elementsAsString = implode(' ', $elements);
2829
$commaSeparatedStrings = explode(',', $elementsAsString);
2930

3031
$length = count($commaSeparatedStrings);
3132

3233
if ($length === 1) {
3334
echo ' ' . $commaSeparatedStrings[0] . PHP_EOL;
35+
3436
return;
3537
}
3638

@@ -94,41 +96,59 @@ function isSpecificDoubleKeyWord($word)
9496
}
9597

9698
/**
99+
* Attempt to gather words in 'SQL parts' (for ex. all SELECT all together)
100+
*
97101
* @param array $words
98102
*
99103
* @return array
100104
*/
101-
function sortWords(array $words)
105+
function sortWordsInParts(array $words)
102106
{
103-
$parts = array();
104-
105-
$keyWord = 'SELECT';
107+
// variables init
108+
$parts = array();
109+
$keyWord = '';
106110
$part = array();
107111

108112
$length = count($words);
109113

110114
for ($i = 0; $i < $length; $i++) {
111115

112116
$currentWord = $words[$i];
113-
$nextWord = $words[$i + 1];
117+
118+
if ($i == ($length - 1)) {
119+
$nextWord = '';
120+
} else {
121+
$nextWord = $words[$i + 1];
122+
}
123+
114124
$doubleWord = $currentWord . ' ' . $nextWord;
115125

116126
if (isSpecificSingleKeyWord($currentWord)) {
117127

118-
$parts[$keyWord] = $part;
119-
$keyWord = $currentWord;
120-
$part = array();
128+
// found key word: gather all previous words in a part
129+
130+
$parts[] = [$keyWord, $part];
131+
$keyWord = $currentWord;
132+
$part = array();
121133
} else if (isSpecificDoubleKeyWord($doubleWord)) {
122-
$parts[$keyWord] = $part;
123-
$keyWord = $doubleWord;
124-
$part = array();
134+
135+
// found key double word: gather all previous words in a part
136+
137+
$parts[] = [$keyWord, $part];
138+
$keyWord = $doubleWord;
139+
$part = array();
140+
141+
// skip next word as it is part of the double word
125142
$i++;
126143
} else {
144+
145+
// keep adding words in the current part
146+
127147
$part[] = $currentWord;
128148
}
129149
}
130150

131-
$parts[$keyWord] = $part;
151+
$parts[] = [$keyWord, $part];
132152

133153
return $parts;
134154
}
@@ -162,17 +182,21 @@ function validateInput($argv)
162182
$query = str_replace(array("\r\n", "\r", "\n"), " ", $query);
163183

164184
$words = explode(' ', $query);
165-
$queryParts = sortWords($words);
185+
$queryParts = sortWordsInParts($words);
166186

167187
echo TextColorWriter::textColor('QUERY ANALYSIS:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
168188

169-
foreach ($queryParts as $key => $part) {
189+
foreach ($queryParts as $part) {
190+
$key = $part[0];
191+
$content = $part[1];
192+
170193
$tabLength = strlen($key) + 1;
171194

172-
if (in_array($key, array('AND','OR'))) {
195+
if (in_array($key, array('AND', 'OR'))) {
196+
// additionnal space
173197
echo ' ';
174198
}
175199

176200
echo ' ' . TextColorWriter::textColor($key, TextColorWriter::BASH_PROMPT_YELLOW) . ' ';
177-
echoArrayAsString($part, $tabLength);
201+
echoArrayAsString($content, $tabLength);
178202
}

0 commit comments

Comments
 (0)