3
<html> <style> body { font-family: Arial; } .annotation { color: gray; } </style> <body> <?php /* SAC: SqlAutoCommannotator: Structured Query Language Automatic Comment Annotator Sharon Xiao Liu Assumes all SQL given to it is valid. For MySQL to natural language. Work in progress: 20150706-20150730. */ /* Use OOP. Loop through arrays. PHP split string by line break. Add a text box. Generate code by analysing each segment before ;. PCRE. Pattern matching PHP. Nest pattern matching statements. Match even if the spacing is inconsistent. Select multiple columns. Pattern matching database: AND,OR (always after SELECT) similar to WHERE. First column: regex, depending on how many parentheses there are (count number of open parentheses), extract this information, format the final result. Design UI. Add 1 when meet open bracket, or you find a closed bracket immediately after. Look through the rest of the SQL code. Make a table of all types of translations for sac. JQuery UI Tooltip. */ $sqlBlock = "SELECT * FROM Customers WHERE Country = 'USA';\nSELECT * FROM Suppliers;\nSELECT ID, MiddleName FROM Customers;\nSELECT * FROM Suppliers WHERE House = '87' OR Credit = '90';\nSELECT * FROM Customers WHERE Country = 'Britain' AND Street = 'Belvedere' AND (House = '13' OR (House = '789' AND Start = '20150706') AND (Contact = 'Email' OR Contact = 'Phone'));"; class sac { public function sacWorking ($sqlBlock) { $sqlBlockTrimEnd = rtrim($sqlBlock, ";"); $sqlBlockExploded = explode(";", $sqlBlockTrimEnd); $output = ""; function parseBrackets($str) { $length = strlen($str); $stack = array(); $result = array(); for($i=0; $i < $length; $i++) { if($str[$i] == '(') { $stack[] = $i; } if($str[$i] == ')') { $open = array_pop($stack); $result[] = substr($str,$open+1, $i-$open-1); } } return $result; }

20150706 Sql AutoCommannotator - sac

Embed Size (px)

Citation preview

Page 1: 20150706 Sql AutoCommannotator - sac

<html><style>body {

font-family: Arial;}.annotation {

color: gray;}</style><body><?php/* SAC: SqlAutoCommannotator: Structured Query Language Automatic Comment AnnotatorSharon Xiao LiuAssumes all SQL given to it is valid. For MySQL to natural language.Work in progress: 20150706-20150730.*//* Use OOP. Loop through arrays. PHP split string by line break.Add a text box.Generate code by analysing each segment before ;. PCRE. Pattern matching PHP. Nest pattern matchingstatements. Match even if the spacing is inconsistent. Select multiple columns.Pattern matching database: AND,OR (always after SELECT) similar to WHERE. First column: regex, depending on how many parentheses there are (count number of open parentheses), extract this information, format the final result.Design UI.Add 1 when meet open bracket, or you find a closed bracket immediately after. Look through the rest of the SQL code. Make a table of all types of translations for sac.JQuery UI Tooltip.*/$sqlBlock = "SELECT * FROM Customers WHERE Country = 'USA';\nSELECT * FROM Suppliers;\nSELECT ID, MiddleName FROM Customers;\nSELECT * FROM Suppliers WHERE House = '87' OR Credit = '90';\nSELECT * FROM Customers WHERE Country = 'Britain' AND Street = 'Belvedere' AND (House = '13' OR (House = '789' AND Start = '20150706') AND (Contact = 'Email' OR Contact = 'Phone'));";

class sac {public function sacWorking ($sqlBlock) {

$sqlBlockTrimEnd = rtrim($sqlBlock, ";");$sqlBlockExploded = explode(";", $sqlBlockTrimEnd);

$output = "";

function parseBrackets($str) {

$length = strlen($str); $stack = array(); $result = array();

for($i=0; $i < $length; $i++) { if($str[$i] == '(') {

$stack[] = $i; }

if($str[$i] == ')') {$open = array_pop($stack);$result[] = substr($str,$open+1, $i-$open-1);

} }

return $result;}

Page 2: 20150706 Sql AutoCommannotator - sac

function substitution ($afterNested) {array_unshift($afterNested, "");$counted = count($afterNested);foreach ($afterNested as $key => $value) {

for ($i = $key+1; $i<$counted; $i++) {if (str_replace ($value, $key, $afterNested[$i])

== true) {$afterNested[$i] = str_replace ($value,

$key, $afterNested[$i]);}

}}$arrayIntoString = "";foreach ($afterNested as $key => $value) {

if ($key >= 1) {$arrayIntoString .= $key. ". ". $value. " ";

}}

$replaceEquals = preg_replace ("/\b(\w+)\b\s?=\s?'(\w+)'/", "the column $1 is $2", $arrayIntoString);

$andOrPattern[0] = "/AND/";$andOrPattern[1] = "/OR/";$andOrReplacement[0] = "and";$andOrReplacement[1] = "or";$replaceAndOr = preg_replace ($andOrPattern,

$andOrReplacement, $replaceEquals);return $replaceAndOr;

}

foreach ($sqlBlockExploded as $singleLine) {$output .= htmlspecialchars($singleLine. ";", ENT_QUOTES);$output .= "<br><div class=\"annotation\">";

if (preg_match("/SELECT\s\*/", $singleLine) == true) {$output .= "Select all columns ";

} elseif (preg_match_all("/SELECT\s\b(\w+)\b\sFROM/", $singleLine, $oneAfterSelect, PREG_PATTERN_ORDER) == true) {

$output .= "Select the column ". $oneAfterSelect[1][0];} elseif (preg_match_all("/SELECT\s(.*?)\sFROM/", $singleLine,

$afterSelect, PREG_PATTERN_ORDER) == true) {$output .= "Select the columns ";$explodeAfterSelect = explode (",", $afterSelect[1][0]);foreach ($explodeAfterSelect as $singleColumns) {

if ($singleColumns != end($explodeAfterSelect)) {$output .= $singleColumns . ", ";

} else {$output .= $singleColumns. " ";

}}

}

if (preg_match_all("/FROM\s\b(\w+)\b/", $singleLine, $afterFrom, PREG_PATTERN_ORDER) == true) {

$output .= "from the table ". $afterFrom[1][0];}

if (preg_match_all("/WHERE\s\b(\w+)\b\s?=\s?'(\w+)'/", $singleLine, $afterWhere, PREG_PATTERN_ORDER) == true) {

$output .= " where the column ". $afterWhere[1][0]. " is equal to ". $afterWhere[2][0];

}

Page 3: 20150706 Sql AutoCommannotator - sac

if (preg_match_all("/(AND|OR)\s\b(\w+)\b\s?=\s?'(\w+)'/", $singleLine, $afterAndNoBrackets, PREG_PATTERN_ORDER) == true) {

if ($afterAndNoBrackets[1][0] == "AND") {$andOr = " and the column ";

} else {$andOr = " or the column ";

}$output .= $andOr. $afterAndNoBrackets[2][0]. " is equal

to ". $afterAndNoBrackets[3][0];}

if (preg_match_all("/(AND|OR)\s(\(\X+\))$/", $singleLine, $afterAnd, PREG_PATTERN_ORDER) == true) {

if ($afterAnd[1][0] == "AND") {$andOrBracket = ", and ";

} else {$andOrBracket = ", or ";

}$processBrackets = parseBrackets ($afterAnd[2][0]);$processAndOr = substitution($processBrackets);$output .= $andOrBracket. $processAndOr;

}

$output .= ".</div><br>";}

return $output;}

public function sacGenerating () {$regexAnd = "/AND\s\b(\w+)\b\s?=\s?&#039;(\w+)&#039;/";

}}

$obj = new sac;echo $obj->sacWorking($sqlBlock);

/*run on XAMPP*/?></body></html>